mirror of
https://github.com/sle118/squeezelite-esp32.git
synced 2025-12-09 21:17:18 +03:00
more missing stuff
This commit is contained in:
@@ -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;
|
||||
}
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user