1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- #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;
- }
|