Skip to content

Commit 3e47d0a

Browse files
committed
Remove obsolete methods from nanopb::Writer
1 parent 45ef270 commit 3e47d0a

File tree

2 files changed

+1
-157
lines changed

2 files changed

+1
-157
lines changed

Firestore/core/src/firebase/firestore/nanopb/writer.cc

Lines changed: 1 addition & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,12 @@
1616

1717
#include "Firestore/core/src/firebase/firestore/nanopb/writer.h"
1818

19-
#include "Firestore/Protos/nanopb/google/firestore/v1beta1/document.nanopb.h"
2019
#include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
2120

2221
namespace firebase {
2322
namespace firestore {
2423
namespace nanopb {
2524

26-
using std::int64_t;
27-
using std::int8_t;
28-
using std::uint64_t;
29-
3025
namespace {
3126

3227
// TODO(rsgowman): find a better home for this constant.
@@ -66,111 +61,21 @@ pb_ostream_t WrapContainer(Container* out_container) {
6661

6762
} // namespace
6863

69-
Writer Writer::Wrap(std::vector<uint8_t>* out_bytes) {
64+
Writer Writer::Wrap(std::vector<std::uint8_t>* out_bytes) {
7065
return Writer{WrapContainer(out_bytes)};
7166
}
7267

7368
Writer Writer::Wrap(std::string* out_string) {
7469
return Writer{WrapContainer(out_string)};
7570
}
7671

77-
void Writer::WriteTag(Tag tag) {
78-
if (!pb_encode_tag(&stream_, tag.wire_type, tag.field_number)) {
79-
HARD_FAIL(PB_GET_ERROR(&stream_));
80-
}
81-
}
82-
8372
void Writer::WriteNanopbMessage(const pb_field_t fields[],
8473
const void* src_struct) {
8574
if (!pb_encode(&stream_, fields, src_struct)) {
8675
HARD_FAIL(PB_GET_ERROR(&stream_));
8776
}
8877
}
8978

90-
void Writer::WriteSize(size_t size) {
91-
return WriteVarint(size);
92-
}
93-
94-
void Writer::WriteVarint(uint64_t value) {
95-
if (!pb_encode_varint(&stream_, value)) {
96-
HARD_FAIL(PB_GET_ERROR(&stream_));
97-
}
98-
}
99-
100-
void Writer::WriteNull() {
101-
return WriteVarint(google_protobuf_NullValue_NULL_VALUE);
102-
}
103-
104-
void Writer::WriteBool(bool bool_value) {
105-
return WriteVarint(bool_value);
106-
}
107-
108-
void Writer::WriteInteger(int64_t integer_value) {
109-
return WriteVarint(integer_value);
110-
}
111-
112-
void Writer::WriteString(const std::string& string_value) {
113-
if (!pb_encode_string(
114-
&stream_, reinterpret_cast<const pb_byte_t*>(string_value.c_str()),
115-
string_value.length())) {
116-
HARD_FAIL(PB_GET_ERROR(&stream_));
117-
}
118-
}
119-
120-
void Writer::WriteBytes(const std::vector<uint8_t>& bytes) {
121-
if (!pb_encode_string(&stream_,
122-
reinterpret_cast<const pb_byte_t*>(bytes.data()),
123-
bytes.size())) {
124-
HARD_FAIL(PB_GET_ERROR(&stream_));
125-
}
126-
}
127-
128-
void Writer::WriteNestedMessage(
129-
const std::function<void(Writer*)>& write_message_fn) {
130-
// First calculate the message size using a non-writing substream.
131-
Writer sizer = Writer::Sizing();
132-
write_message_fn(&sizer);
133-
size_t size = sizer.bytes_written();
134-
135-
// Write out the size to the output writer.
136-
WriteSize(size);
137-
138-
// If this stream is itself a sizing stream, then we don't need to actually
139-
// parse field_value a second time; just update the bytes_written via a call
140-
// to pb_write. (If we try to write the contents into a sizing stream, it'll
141-
// fail since sizing streams don't actually have any buffer space.)
142-
if (stream_.callback == nullptr) {
143-
if (!pb_write(&stream_, nullptr, size)) {
144-
HARD_FAIL(PB_GET_ERROR(&stream_));
145-
}
146-
return;
147-
}
148-
149-
// Ensure the output stream has enough space
150-
if (stream_.bytes_written + size > stream_.max_size) {
151-
HARD_FAIL(
152-
"Insufficient space in the output stream to write the given message");
153-
}
154-
155-
// Use a substream to verify that a callback doesn't write more than what it
156-
// did the first time. (Use an initializer rather than setting fields
157-
// individually like nanopb does. This gives us a *chance* of noticing if
158-
// nanopb adds new fields.)
159-
Writer writer({stream_.callback, stream_.state,
160-
/*max_size=*/size, /*bytes_written=*/0,
161-
/*errmsg=*/nullptr});
162-
write_message_fn(&writer);
163-
164-
stream_.bytes_written += writer.stream_.bytes_written;
165-
stream_.state = writer.stream_.state;
166-
stream_.errmsg = writer.stream_.errmsg;
167-
168-
if (writer.bytes_written() != size) {
169-
// submsg size changed
170-
HARD_FAIL("Parsing the nested message twice yielded different sizes");
171-
}
172-
}
173-
17479
} // namespace nanopb
17580
} // namespace firestore
17681
} // namespace firebase

Firestore/core/src/firebase/firestore/nanopb/writer.h

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,9 @@
2121
#include <pb_encode.h>
2222

2323
#include <cstdint>
24-
#include <functional>
2524
#include <string>
2625
#include <vector>
2726

28-
#include "Firestore/core/src/firebase/firestore/nanopb/tag.h"
29-
3027
namespace firebase {
3128
namespace firestore {
3229
namespace nanopb {
@@ -59,21 +56,6 @@ class Writer {
5956
*/
6057
static Writer Wrap(std::string* out_string);
6158

62-
/**
63-
* Creates a non-writing output stream used to calculate the size of
64-
* the serialized output.
65-
*/
66-
static Writer Sizing() {
67-
return Writer(PB_OSTREAM_SIZING);
68-
}
69-
70-
/**
71-
* Writes a message type to the output stream.
72-
*
73-
* This essentially wraps calls to nanopb's pb_encode_tag() method.
74-
*/
75-
void WriteTag(Tag tag);
76-
7759
/**
7860
* Writes a nanopb message to the output stream.
7961
*
@@ -83,34 +65,6 @@ class Writer {
8365
*/
8466
void WriteNanopbMessage(const pb_field_t fields[], const void* src_struct);
8567

86-
void WriteSize(size_t size);
87-
void WriteNull();
88-
void WriteBool(bool bool_value);
89-
void WriteInteger(std::int64_t integer_value);
90-
91-
void WriteString(const std::string& string_value);
92-
void WriteBytes(const std::vector<uint8_t>& bytes);
93-
94-
/**
95-
* Writes a message and its length.
96-
*
97-
* When writing a top level message, protobuf doesn't include the length
98-
* (since you can get that already from the length of the binary output.) But
99-
* when writing a sub/nested message, you must include the length in the
100-
* serialization.
101-
*
102-
* Call this method when writing a nested message. Provide a function to
103-
* write the message itself. This method will calculate the size of the
104-
* written message (using the provided function with a non-writing sizing
105-
* stream), write out the size (and perform sanity checks), and then serialize
106-
* the message by calling the provided function a second time.
107-
*/
108-
void WriteNestedMessage(const std::function<void(Writer*)>& write_message_fn);
109-
110-
size_t bytes_written() const {
111-
return stream_.bytes_written;
112-
}
113-
11468
private:
11569
/**
11670
* Creates a new Writer, based on the given nanopb pb_ostream_t. Note that
@@ -120,21 +74,6 @@ class Writer {
12074
explicit Writer(const pb_ostream_t& stream) : stream_(stream) {
12175
}
12276

123-
/**
124-
* Writes a "varint" to the output stream.
125-
*
126-
* This essentially wraps calls to nanopb's pb_encode_varint() method.
127-
*
128-
* Note that (despite the value parameter type) this works for bool, enum,
129-
* int32, int64, uint32 and uint64 proto field types.
130-
*
131-
* Note: This is not expected to be called directly, but rather only
132-
* via the other Write* methods (i.e. WriteBool, WriteLong, etc)
133-
*
134-
* @param value The value to write, represented as a uint64_t.
135-
*/
136-
void WriteVarint(std::uint64_t value);
137-
13877
pb_ostream_t stream_;
13978
};
14079

0 commit comments

Comments
 (0)