diff --git a/docs/migration_1.0/c++/index.html b/docs/migration_1.0/c++/index.html index 17b0f42a..0d563106 100644 --- a/docs/migration_1.0/c++/index.html +++ b/docs/migration_1.0/c++/index.html @@ -1,6 +1,6 @@ C++ · Zenoh - pub/sub, geo distributed storage, query

C++

Zenoh 1.0.0 brings a number of changes to the API, with a concentrated effort to bring the C++ API to more closely resemble the Rust API in usage.

The improvements we bring in this update include:

  • A simpler organization of the Zenoh classes, abstracting away the notion of View and Closure.
  • Improved and more flexible Error Handling through error codes and exceptions.
  • Support for serialization of common types like strings, numbers, vectors through Codecs.
  • Ability for users to define their own Codecs (for their own types or to overwrite the default one)!
  • Improved stream handlers and callback support.
  • Simpler attachment API.

Now that the amuse bouche is served, let’s get into the main course!

Error Handling

In version 0.11.0 all Zenoh call failures were handled by either returning a bool value indicating success or failure (and probably returning an error code) or returning an std::variant<ReturnType, ErrorMessage>. For instance:

std::variant<z::Config, ErrorMessage> config_client(const z::StrArrayView& peers);
+

C++

Zenoh 1.0.0 brings a number of changes to the API, with a concentrated effort to bring the C++ API to more closely resemble the Rust API in usage.

The improvements we bring in this update include:

  • A simpler organization of the Zenoh classes, abstracting away the notion of View and Closure.
  • Improved and more flexible Error Handling through error codes and exceptions.
  • Support for serialization of common types like strings, numbers, vectors through Codecs.
  • Ability for users to define their own Codecs (for their own types or to overwrite the default one)!
  • Improved stream handlers and callback support.
  • Simpler attachment API.

Now that the amuse bouche is served, let’s get into the main course!

Error Handling

In version 0.11.0 all Zenoh call failures were handled by either returning a bool value indicating success or failure (and probably returning an error code) or returning an std::variant<ReturnType, ErrorMessage>. For instance:

std::variant<z::Config, ErrorMessage> config_client(const z::StrArrayView& peers);
 
 bool put(const z::BytesView& payload, const z::PublisherPutOptions& options, ErrNo& error);
 

In 1.0.0, all functions that can fail on the Zenoh side now offer 2 options for error handling:

A. Exceptions

B. Error Codes

Any function that can fail now accepts an optional parameter ZError* err pointer to the error code. If it is not provided (or set to nullptr), an instance of ZException will be thrown, otherwise the error code will be written into the err pointer.

static Config client(const std::vector<std::string>& peers, ZError* err = nullptr);
@@ -46,35 +46,44 @@
 void receive_bytes(const Sample &sample) {
   std::vector<uint8_t> = sample.get_payload().as_vector();
 };
-

Additionally zenoh::ext namespace provides support for serialization/deserialziation of typed data to/into Bytes:

  // arithmetic types
-  double pi = 3.1415926;
-  Bytes b = ext::serialize(pi);
-  assert(ext::deserialize<doulbe>(b) == pi);
-  
-  // Composite types
-  std::vector<float> v = {0.1f, 0.2f, 0.3f};
-  b = ext::serialize(v);
-  assert(ext::deserialize<std::vector<float>>(b) == v);
-  
-  std::unordered_map<std::string, std::deque<double>> m = {
-    {"a", {0.5, 0.2}},
-    {"b", {-123.45, 0.4}},
-    {"abc", {3.1415926, -1.0} }
-  };
+

Additionally zenoh::ext namespace provides support for serialization/deserialziation of typed data to/into Bytes:

// arithmetic types
+double pi = 3.1415926;
+Bytes b = ext::serialize(pi);
+assert(ext::deserialize<doulbe>(b) == pi);
 
-  b = ext::serialize(m);
-  assert(ext::deserialize<std::unordered_map<std::string, std::deque<double>>>(b) == m); 
+// Composite types
+std::vector<float> v = {0.1f, 0.2f, 0.3f};
+b = ext::serialize(v);
+assert(ext::deserialize<std::vector<float>>(b) == v);
+
+std::unordered_map<std::string, std::deque<double>> m = {
+  {"a", {0.5, 0.2}},
+  {"b", {-123.45, 0.4}},
+  {"abc", {3.1415926, -1.0} }
+};
+
+b = ext::serialize(m);
+assert(ext::deserialize<std::unordered_map<std::string, std::deque<double>>>(b) == m); 
 

Users can easily define serialization/deserialization for their own custom types by using ext::Serializer and ext::Deserializer classes:

struct CustomStruct {
   std::vector<double> vd;
   int32_t i;
   std::string s;
 };
 
-// One needs to implement __zenoh_serialize_with_serializer in the same namespace as CustomStruct
-void __zenoh_serialize_with_serializer(ext::Serializer& serializer, const CustomStruct& s) {
-  serializer.serialize(s.vd);
-  serializer.serialize(s.i);
-  serializer.serialize(s.s);
+// One needs to implement __zenoh_serialize_with_serializer and __zenoh_deserialize_with_deserializer
+// in the same namespace, where CustomStruct is defined.
+// To simplify implementation users are allowed to use
+// serialize_with_serializer and deserialize_with_deserializer functions defined in zenoh::ext::detail namespace.
+bool __zenoh_serialize_with_serializer(zenoh::ext::Serializer& serializer, const CustomStruct& s, ZResult* err) {
+  return zenoh::ext::detail::serialize_with_serializer(serializer, s.vd, err) &&
+         zenoh::ext::detail::serialize_with_serializer(serializer, s.i, err) &&
+         zenoh::ext::detail::serialize_with_serializer(serializer, s.s, err);
+}
+
+bool __zenoh_deserialize_with_deserializer(zenoh::ext::Deserializer& deserializer, CustomStruct& s, ZResult* err) {
+  return zenoh::ext::detail::deserialize_with_deserializer(deserializer, s.vd, err) &&
+         zenoh::ext::detail::deserialize_with_deserializer(deserializer, s.i, err) &&
+         zenoh::ext::detail::deserialize_with_deserializer(deserializer, s.s, err);
 }
 
 void serialize_custom() {