pw_protobuf: Move classes to the internal namespace
Codegen classes like MessageField should not be used directly, so move
them to the internal namespace.
Change-Id: I327603b74126a1998daefc35f7d196dca47419a4
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/110230
Reviewed-by: Armando Montanez <amontanez@google.com>
Pigweed-Auto-Submit: Wyatt Hepler <hepler@google.com>
Commit-Queue: Wyatt Hepler <hepler@google.com>
diff --git a/pw_protobuf/codegen_message_test.cc b/pw_protobuf/codegen_message_test.cc
index 55ebd0f..f23471d 100644
--- a/pw_protobuf/codegen_message_test.cc
+++ b/pw_protobuf/codegen_message_test.cc
@@ -11,6 +11,7 @@
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
+
#include <array>
#include <string_view>
#include <tuple>
@@ -1244,7 +1245,8 @@
public:
constexpr BreakableDecoder(stream::Reader& reader) : StreamDecoder(reader) {}
- Status Read(KeyValuePair::Message& message, span<const MessageField> table) {
+ Status Read(KeyValuePair::Message& message,
+ span<const internal::MessageField> table) {
return ::pw::protobuf::StreamDecoder::Read(
as_writable_bytes(span(&message, 1)), table);
}
@@ -1254,11 +1256,11 @@
// Deliberately construct a message table that attempts to violate the bounds
// of the structure. We're not testing that a developer can't do this, rather
// that the protobuf decoder can't be exploited in this way.
- constexpr MessageField kMessageFields[] = {
+ constexpr internal::MessageField kMessageFields[] = {
{1,
WireType::kDelimited,
sizeof(std::byte),
- static_cast<VarintType>(0),
+ static_cast<internal::VarintType>(0),
false,
false,
false,
@@ -1845,7 +1847,7 @@
: KeyValuePair::MemoryEncoder(buffer) {}
Status Write(const KeyValuePair::Message& message,
- span<const MessageField> table) {
+ span<const internal::MessageField> table) {
return ::pw::protobuf::StreamEncoder::Write(as_bytes(span(&message, 1)),
table);
}
@@ -1855,11 +1857,11 @@
// Deliberately construct a message table that attempts to violate the bounds
// of the structure. We're not testing that a developer can't do this, rather
// that the protobuf encoder can't be exploited in this way.
- constexpr MessageField kMessageFields[] = {
+ constexpr internal::MessageField kMessageFields[] = {
{1,
WireType::kDelimited,
sizeof(std::byte),
- static_cast<VarintType>(0),
+ static_cast<internal::VarintType>(0),
false,
false,
false,
diff --git a/pw_protobuf/encoder.cc b/pw_protobuf/encoder.cc
index 627baef..54a4677 100644
--- a/pw_protobuf/encoder.cc
+++ b/pw_protobuf/encoder.cc
@@ -34,6 +34,8 @@
namespace pw::protobuf {
+using internal::VarintType;
+
StreamEncoder StreamEncoder::GetNestedEncoder(uint32_t field_number,
bool write_when_empty) {
PW_CHECK(!nested_encoder_open());
@@ -239,7 +241,7 @@
}
Status StreamEncoder::Write(span<const std::byte> message,
- span<const MessageField> table) {
+ span<const internal::MessageField> table) {
PW_CHECK(!nested_encoder_open());
PW_TRY(status_);
diff --git a/pw_protobuf/public/pw_protobuf/encoder.h b/pw_protobuf/public/pw_protobuf/encoder.h
index 79195b8..7e8becb 100644
--- a/pw_protobuf/public/pw_protobuf/encoder.h
+++ b/pw_protobuf/public/pw_protobuf/encoder.h
@@ -191,7 +191,8 @@
//
// Precondition: Encoder has no active child encoder.
Status WritePackedUint32(uint32_t field_number, span<const uint32_t> values) {
- return WritePackedVarints(field_number, values, VarintType::kNormal);
+ return WritePackedVarints(
+ field_number, values, internal::VarintType::kNormal);
}
// Writes a repeated uint32 using packed encoding.
@@ -199,8 +200,9 @@
// Precondition: Encoder has no active child encoder.
Status WriteRepeatedUint32(uint32_t field_number,
const pw::Vector<uint32_t>& values) {
- return WritePackedVarints(
- field_number, span(values.data(), values.size()), VarintType::kNormal);
+ return WritePackedVarints(field_number,
+ span(values.data(), values.size()),
+ internal::VarintType::kNormal);
}
// Writes a proto uint64 key-value pair.
@@ -214,7 +216,8 @@
//
// Precondition: Encoder has no active child encoder.
Status WritePackedUint64(uint64_t field_number, span<const uint64_t> values) {
- return WritePackedVarints(field_number, values, VarintType::kNormal);
+ return WritePackedVarints(
+ field_number, values, internal::VarintType::kNormal);
}
// Writes a repeated uint64 using packed encoding.
@@ -222,8 +225,9 @@
// Precondition: Encoder has no active child encoder.
Status WriteRepeatedUint64(uint32_t field_number,
const pw::Vector<uint64_t>& values) {
- return WritePackedVarints(
- field_number, span(values.data(), values.size()), VarintType::kNormal);
+ return WritePackedVarints(field_number,
+ span(values.data(), values.size()),
+ internal::VarintType::kNormal);
}
// Writes a proto int32 key-value pair.
@@ -240,7 +244,7 @@
return WritePackedVarints(
field_number,
span(reinterpret_cast<const uint32_t*>(values.data()), values.size()),
- VarintType::kNormal);
+ internal::VarintType::kNormal);
}
// Writes a repeated int32 using packed encoding.
@@ -251,7 +255,7 @@
return WritePackedVarints(
field_number,
span(reinterpret_cast<const uint32_t*>(values.data()), values.size()),
- VarintType::kNormal);
+ internal::VarintType::kNormal);
}
// Writes a proto int64 key-value pair.
@@ -268,7 +272,7 @@
return WritePackedVarints(
field_number,
span(reinterpret_cast<const uint64_t*>(values.data()), values.size()),
- VarintType::kNormal);
+ internal::VarintType::kNormal);
}
// Writes a repeated int64 using packed encoding.
@@ -279,7 +283,7 @@
return WritePackedVarints(
field_number,
span(reinterpret_cast<const uint64_t*>(values.data()), values.size()),
- VarintType::kNormal);
+ internal::VarintType::kNormal);
}
// Writes a proto sint32 key-value pair.
@@ -296,7 +300,7 @@
return WritePackedVarints(
field_number,
span(reinterpret_cast<const uint32_t*>(values.data()), values.size()),
- VarintType::kZigZag);
+ internal::VarintType::kZigZag);
}
// Writes a repeated sint32 using packed encoding.
@@ -307,7 +311,7 @@
return WritePackedVarints(
field_number,
span(reinterpret_cast<const uint32_t*>(values.data()), values.size()),
- VarintType::kZigZag);
+ internal::VarintType::kZigZag);
}
// Writes a proto sint64 key-value pair.
@@ -324,7 +328,7 @@
return WritePackedVarints(
field_number,
span(reinterpret_cast<const uint64_t*>(values.data()), values.size()),
- VarintType::kZigZag);
+ internal::VarintType::kZigZag);
}
// Writes a repeated sint64 using packed encoding.
@@ -335,7 +339,7 @@
return WritePackedVarints(
field_number,
span(reinterpret_cast<const uint64_t*>(values.data()), values.size()),
- VarintType::kZigZag);
+ internal::VarintType::kZigZag);
}
// Writes a proto bool key-value pair.
@@ -354,7 +358,7 @@
return WritePackedVarints(
field_number,
span(reinterpret_cast<const uint8_t*>(values.data()), values.size()),
- VarintType::kNormal);
+ internal::VarintType::kNormal);
}
// Writes a repeated bool using packed encoding.
@@ -368,7 +372,7 @@
return WritePackedVarints(
field_number,
span(reinterpret_cast<const uint8_t*>(values.data()), values.size()),
- VarintType::kNormal);
+ internal::VarintType::kNormal);
}
// Writes a proto fixed32 key-value pair.
@@ -632,7 +636,8 @@
// This is called by codegen subclass Write() functions that accept a typed
// struct Message reference, using the appropriate codegen MessageField table
// corresponding to that type.
- Status Write(span<const std::byte> message, span<const MessageField> table);
+ Status Write(span<const std::byte> message,
+ span<const internal::MessageField> table);
// Protected method to create a nested encoder, specifying whether the field
// should be written when no fields were added to the nested encoder. Not
@@ -692,7 +697,7 @@
template <typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
Status WritePackedVarints(uint32_t field_number,
span<T> values,
- VarintType encode_type) {
+ internal::VarintType encode_type) {
static_assert(std::is_same<T, const uint8_t>::value ||
std::is_same<T, const uint32_t>::value ||
std::is_same<T, const int32_t>::value ||
@@ -703,7 +708,7 @@
size_t payload_size = 0;
for (T val : values) {
- if (encode_type == VarintType::kZigZag) {
+ if (encode_type == internal::VarintType::kZigZag) {
int64_t integer =
static_cast<int64_t>(static_cast<std::make_signed_t<T>>(val));
payload_size += varint::EncodedSize(varint::ZigZagEncode(integer));
@@ -723,7 +728,7 @@
WriteVarint(payload_size)
.IgnoreError(); // TODO(b/242598609): Handle Status properly
for (T value : values) {
- if (encode_type == VarintType::kZigZag) {
+ if (encode_type == internal::VarintType::kZigZag) {
WriteZigzagVarint(static_cast<std::make_signed_t<T>>(value))
.IgnoreError(); // TODO(b/242598609): Handle Status properly
} else {
diff --git a/pw_protobuf/public/pw_protobuf/internal/codegen.h b/pw_protobuf/public/pw_protobuf/internal/codegen.h
index 7631229..34f4478 100644
--- a/pw_protobuf/public/pw_protobuf/internal/codegen.h
+++ b/pw_protobuf/public/pw_protobuf/internal/codegen.h
@@ -21,6 +21,7 @@
#include "pw_status/status.h"
namespace pw::protobuf {
+namespace internal {
// Varints can be encoded as an unsigned type, a signed type with normal
// encoding, or a signed type with zigzag encoding.
@@ -149,6 +150,8 @@
static_assert(sizeof(MessageField) <= sizeof(size_t) * 4,
"MessageField should be four words or less");
+} // namespace internal
+
// Callback for a structure member that cannot be represented by a data type.
// Holds either a callback for encoding a field, or a callback for decoding
// a field.
diff --git a/pw_protobuf/public/pw_protobuf/stream_decoder.h b/pw_protobuf/public/pw_protobuf/stream_decoder.h
index 20625c5..6ca8803 100644
--- a/pw_protobuf/public/pw_protobuf/stream_decoder.h
+++ b/pw_protobuf/public/pw_protobuf/stream_decoder.h
@@ -159,7 +159,7 @@
// Reads a proto int32 value from the current position.
Result<int32_t> ReadInt32() {
- return ReadVarintField<int32_t>(VarintType::kNormal);
+ return ReadVarintField<int32_t>(internal::VarintType::kNormal);
}
// Reads repeated int32 values from the current position using packed
@@ -169,18 +169,18 @@
// indicates the number of values successfully read, in addition to the error.
StatusWithSize ReadPackedInt32(span<int32_t> out) {
return ReadPackedVarintField(
- as_writable_bytes(out), sizeof(int32_t), VarintType::kNormal);
+ as_writable_bytes(out), sizeof(int32_t), internal::VarintType::kNormal);
}
// Reads repeated int32 values from the current position into the vector,
// supporting either repeated single field elements or packed encoding.
Status ReadRepeatedInt32(pw::Vector<int32_t>& out) {
- return ReadRepeatedVarintField<int32_t>(out, VarintType::kNormal);
+ return ReadRepeatedVarintField<int32_t>(out, internal::VarintType::kNormal);
}
// Reads a proto uint32 value from the current position.
Result<uint32_t> ReadUint32() {
- return ReadVarintField<uint32_t>(VarintType::kUnsigned);
+ return ReadVarintField<uint32_t>(internal::VarintType::kUnsigned);
}
// Reads repeated uint32 values from the current position using packed
@@ -189,19 +189,21 @@
// Returns the number of values read. In the case of error, the return value
// indicates the number of values successfully read, in addition to the error.
StatusWithSize ReadPackedUint32(span<uint32_t> out) {
- return ReadPackedVarintField(
- as_writable_bytes(out), sizeof(uint32_t), VarintType::kUnsigned);
+ return ReadPackedVarintField(as_writable_bytes(out),
+ sizeof(uint32_t),
+ internal::VarintType::kUnsigned);
}
// Reads repeated uint32 values from the current position into the vector,
// supporting either repeated single field elements or packed encoding.
Status ReadRepeatedUint32(pw::Vector<uint32_t>& out) {
- return ReadRepeatedVarintField<uint32_t>(out, VarintType::kUnsigned);
+ return ReadRepeatedVarintField<uint32_t>(out,
+ internal::VarintType::kUnsigned);
}
// Reads a proto int64 value from the current position.
Result<int64_t> ReadInt64() {
- return ReadVarintField<int64_t>(VarintType::kNormal);
+ return ReadVarintField<int64_t>(internal::VarintType::kNormal);
}
// Reads repeated int64 values from the current position using packed
@@ -212,18 +214,18 @@
// error.
StatusWithSize ReadPackedInt64(span<int64_t> out) {
return ReadPackedVarintField(
- as_writable_bytes(out), sizeof(int64_t), VarintType::kNormal);
+ as_writable_bytes(out), sizeof(int64_t), internal::VarintType::kNormal);
}
// Reads repeated int64 values from the current position into the vector,
// supporting either repeated single field elements or packed encoding.
Status ReadRepeatedInt64(pw::Vector<int64_t>& out) {
- return ReadRepeatedVarintField<int64_t>(out, VarintType::kNormal);
+ return ReadRepeatedVarintField<int64_t>(out, internal::VarintType::kNormal);
}
// Reads a proto uint64 value from the current position.
Result<uint64_t> ReadUint64() {
- return ReadVarintField<uint64_t>(VarintType::kUnsigned);
+ return ReadVarintField<uint64_t>(internal::VarintType::kUnsigned);
}
// Reads repeated uint64 values from the current position using packed
@@ -233,19 +235,21 @@
// indicates the number of values successfully read, in addition to the
// error.
StatusWithSize ReadPackedUint64(span<uint64_t> out) {
- return ReadPackedVarintField(
- as_writable_bytes(out), sizeof(uint64_t), VarintType::kUnsigned);
+ return ReadPackedVarintField(as_writable_bytes(out),
+ sizeof(uint64_t),
+ internal::VarintType::kUnsigned);
}
// Reads repeated uint64 values from the current position into the vector,
// supporting either repeated single field elements or packed encoding.
Status ReadRepeatedUint64(pw::Vector<uint64_t>& out) {
- return ReadRepeatedVarintField<uint64_t>(out, VarintType::kUnsigned);
+ return ReadRepeatedVarintField<uint64_t>(out,
+ internal::VarintType::kUnsigned);
}
// Reads a proto sint32 value from the current position.
Result<int32_t> ReadSint32() {
- return ReadVarintField<int32_t>(VarintType::kZigZag);
+ return ReadVarintField<int32_t>(internal::VarintType::kZigZag);
}
// Reads repeated sint32 values from the current position using packed
@@ -256,18 +260,18 @@
// error.
StatusWithSize ReadPackedSint32(span<int32_t> out) {
return ReadPackedVarintField(
- as_writable_bytes(out), sizeof(int32_t), VarintType::kZigZag);
+ as_writable_bytes(out), sizeof(int32_t), internal::VarintType::kZigZag);
}
// Reads repeated sint32 values from the current position into the vector,
// supporting either repeated single field elements or packed encoding.
Status ReadRepeatedSint32(pw::Vector<int32_t>& out) {
- return ReadRepeatedVarintField<int32_t>(out, VarintType::kZigZag);
+ return ReadRepeatedVarintField<int32_t>(out, internal::VarintType::kZigZag);
}
// Reads a proto sint64 value from the current position.
Result<int64_t> ReadSint64() {
- return ReadVarintField<int64_t>(VarintType::kZigZag);
+ return ReadVarintField<int64_t>(internal::VarintType::kZigZag);
}
// Reads repeated int64 values from the current position using packed
@@ -278,18 +282,18 @@
// error.
StatusWithSize ReadPackedSint64(span<int64_t> out) {
return ReadPackedVarintField(
- as_writable_bytes(out), sizeof(int64_t), VarintType::kZigZag);
+ as_writable_bytes(out), sizeof(int64_t), internal::VarintType::kZigZag);
}
// Reads repeated sint64 values from the current position into the vector,
// supporting either repeated single field elements or packed encoding.
Status ReadRepeatedSint64(pw::Vector<int64_t>& out) {
- return ReadRepeatedVarintField<int64_t>(out, VarintType::kZigZag);
+ return ReadRepeatedVarintField<int64_t>(out, internal::VarintType::kZigZag);
}
// Reads a proto bool value from the current position.
Result<bool> ReadBool() {
- return ReadVarintField<bool>(VarintType::kUnsigned);
+ return ReadVarintField<bool>(internal::VarintType::kUnsigned);
}
// Reads repeated bool values from the current position using packed
@@ -300,13 +304,13 @@
// error.
StatusWithSize ReadPackedBool(span<bool> out) {
return ReadPackedVarintField(
- as_writable_bytes(out), sizeof(bool), VarintType::kUnsigned);
+ as_writable_bytes(out), sizeof(bool), internal::VarintType::kUnsigned);
}
// Reads repeated bool values from the current position into the vector,
// supporting either repeated single field elements or packed encoding.
Status ReadRepeatedBool(pw::Vector<bool>& out) {
- return ReadRepeatedVarintField<bool>(out, VarintType::kUnsigned);
+ return ReadRepeatedVarintField<bool>(out, internal::VarintType::kUnsigned);
}
// Reads a proto fixed32 value from the current position.
@@ -530,7 +534,8 @@
// This is called by codegen subclass Read() functions that accept a typed
// struct Message reference, using the appropriate codegen MessageField table
// corresponding to that type.
- Status Read(span<std::byte> message, span<const MessageField> table);
+ Status Read(span<std::byte> message,
+ span<const internal::MessageField> table);
private:
friend class BytesReader;
@@ -589,12 +594,13 @@
Status ReadFieldKey();
Status SkipField();
- Status ReadVarintField(span<std::byte> out, VarintType decode_type);
+ Status ReadVarintField(span<std::byte> out, internal::VarintType decode_type);
- StatusWithSize ReadOneVarint(span<std::byte> out, VarintType decode_type);
+ StatusWithSize ReadOneVarint(span<std::byte> out,
+ internal::VarintType decode_type);
template <typename T>
- Result<T> ReadVarintField(VarintType decode_type) {
+ Result<T> ReadVarintField(internal::VarintType decode_type) {
static_assert(
std::is_same_v<T, bool> || std::is_same_v<T, uint32_t> ||
std::is_same_v<T, int32_t> || std::is_same_v<T, uint64_t> ||
@@ -635,7 +641,7 @@
StatusWithSize ReadPackedVarintField(span<std::byte> out,
size_t elem_size,
- VarintType decode_type);
+ internal::VarintType decode_type);
template <typename T>
Status ReadRepeatedFixedField(pw::Vector<T>& out) {
@@ -662,7 +668,8 @@
}
template <typename T>
- Status ReadRepeatedVarintField(pw::Vector<T>& out, VarintType decode_type) {
+ Status ReadRepeatedVarintField(pw::Vector<T>& out,
+ internal::VarintType decode_type) {
if (out.full()) {
return Status::ResourceExhausted();
}
diff --git a/pw_protobuf/py/pw_protobuf/codegen_pwpb.py b/pw_protobuf/py/pw_protobuf/codegen_pwpb.py
index 883c3e4..bc815bf 100644
--- a/pw_protobuf/py/pw_protobuf/codegen_pwpb.py
+++ b/pw_protobuf/py/pw_protobuf/codegen_pwpb.py
@@ -37,6 +37,7 @@
PROTO_CC_EXTENSION = '.pwpb.cc'
PROTOBUF_NAMESPACE = '::pw::protobuf'
+_INTERNAL_NAMESPACE = '::pw::protobuf::internal'
class ClassType(enum.Enum):
@@ -408,10 +409,10 @@
def _varint_type_table_entry(self) -> str:
if self.wire_type() == 'kVarint':
- return '{}::VarintType::{}'.format(PROTOBUF_NAMESPACE,
+ return '{}::VarintType::{}'.format(_INTERNAL_NAMESPACE,
self.varint_decode_type())
- return f'static_cast<{PROTOBUF_NAMESPACE}::VarintType>(0)'
+ return f'static_cast<{_INTERNAL_NAMESPACE}::VarintType>(0)'
def _wire_type_table_entry(self) -> str:
return '{}::WireType::{}'.format(PROTOBUF_NAMESPACE, self.wire_type())
@@ -2110,7 +2111,7 @@
prop.name()))
output.write_line('static_assert(sizeof(Message::{}) <= '
'{}::MessageField::kMaxFieldSize);'.format(
- prop.name(), PROTOBUF_NAMESPACE))
+ prop.name(), _INTERNAL_NAMESPACE))
# Zero-length C arrays are not permitted by the C++ standard, so only
# generate the message fields array if it is non-empty. Zero-length
@@ -2121,7 +2122,7 @@
# not. Only the span is referenced elsewhere.
if properties:
output.write_line(
- f'inline constexpr {PROTOBUF_NAMESPACE}::MessageField '
+ f'inline constexpr {_INTERNAL_NAMESPACE}::MessageField '
' _kMessageFields[] = {')
# Generate members for each of the message's fields.
@@ -2134,11 +2135,11 @@
output.write_line('PW_MODIFY_DIAGNOSTICS_POP();')
output.write_line(
- f'inline constexpr pw::span<const {PROTOBUF_NAMESPACE}::'
+ f'inline constexpr pw::span<const {_INTERNAL_NAMESPACE}::'
'MessageField> kMessageFields = _kMessageFields;')
else:
output.write_line(
- f'inline constexpr pw::span<const {PROTOBUF_NAMESPACE}::'
+ f'inline constexpr pw::span<const {_INTERNAL_NAMESPACE}::'
'MessageField> kMessageFields;')
output.write_line(f'}} // namespace {namespace}')
diff --git a/pw_protobuf/stream_decoder.cc b/pw_protobuf/stream_decoder.cc
index 2a93ec6..a781cf6 100644
--- a/pw_protobuf/stream_decoder.cc
+++ b/pw_protobuf/stream_decoder.cc
@@ -37,6 +37,8 @@
namespace pw::protobuf {
+using internal::VarintType;
+
Status StreamDecoder::BytesReader::DoSeek(ptrdiff_t offset, Whence origin) {
PW_TRY(status_);
if (!decoder_.reader_.seekable()) {
@@ -513,7 +515,7 @@
}
Status StreamDecoder::Read(span<std::byte> message,
- span<const MessageField> table) {
+ span<const internal::MessageField> table) {
PW_TRY(status_);
while (Next().ok()) {
diff --git a/pw_rpc/pwpb/public/pw_rpc/pwpb/internal/common.h b/pw_rpc/pwpb/public/pw_rpc/pwpb/internal/common.h
index 8bcec6e..16f2847 100644
--- a/pw_rpc/pwpb/public/pw_rpc/pwpb/internal/common.h
+++ b/pw_rpc/pwpb/public/pw_rpc/pwpb/internal/common.h
@@ -26,7 +26,8 @@
namespace pw::rpc::internal {
-using PwpbMessageDescriptor = const span<const protobuf::MessageField>*;
+using PwpbMessageDescriptor =
+ const span<const protobuf::internal::MessageField>*;
// Serializer/deserializer for a pw_protobuf message.
class PwpbSerde {