2
0
philippe44 1 жил өмнө
parent
commit
fa91879535

+ 59 - 0
components/spotify/cspot/bell/main/utilities/NanoPBExtensions.cpp

@@ -0,0 +1,59 @@
+#include "NanoPBExtensions.h"
+
+#include <optional>  // for optional
+#include <string>    // for string
+#include <vector>    // for vector
+
+#include <pb_common.h>
+#include <pb_encode.h>
+
+bool bell::nanopb::encodeString(pb_ostream_t* stream, const pb_field_t* field,
+                                void* const* arg) {
+  auto& str = *static_cast<std::string*>(*arg);
+
+  if (str.size() > 0) {
+    if (!pb_encode_tag_for_field(stream, field)) {
+      return false;
+    }
+
+    if (!pb_encode_string(stream, (uint8_t*)str.c_str(), str.size())) {
+      return false;
+    }
+  }
+
+  return true;
+}
+
+bool bell::nanopb::encodeBoolean(pb_ostream_t* stream, const pb_field_t* field,
+                                 void* const* arg) {
+  auto& boolean = *static_cast<std::optional<bool>*>(*arg);
+
+  if (boolean.has_value()) {
+    if (!pb_encode_tag_for_field(stream, field)) {
+      return false;
+    }
+
+    if (!pb_encode_varint(stream, boolean.value())) {
+      return false;
+    }
+  }
+
+  return true;
+}
+
+bool bell::nanopb::encodeVector(pb_ostream_t* stream, const pb_field_t* field,
+                                void* const* arg) {
+  auto& vector = *static_cast<std::vector<uint8_t>*>(*arg);
+
+  if (vector.size() > 0) {
+    if (!pb_encode_tag_for_field(stream, field)) {
+      return false;
+    }
+
+    if (!pb_encode_string(stream, vector.data(), vector.size())) {
+      return false;
+    }
+  }
+
+  return true;
+}

+ 15 - 0
components/spotify/cspot/bell/main/utilities/include/NanoPBExtensions.h

@@ -0,0 +1,15 @@
+#pragma once
+
+#include <pb.h>
+
+/// Set of helper methods that simplify nanopb usage in C++.
+namespace bell::nanopb {
+bool encodeString(pb_ostream_t* stream, const pb_field_t* field,
+                  void* const* arg);
+
+bool encodeVector(pb_ostream_t* stream, const pb_field_t* field,
+                  void* const* arg);
+
+bool encodeBoolean(pb_ostream_t* stream, const pb_field_t* field,
+                   void* const* arg);
+}  // namespace bell::nanopb