• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 #pragma once
15 
16 #include "pw_protobuf/encoder.h"
17 
18 namespace pw::protobuf {
19 
20 // Base class for generated encoders. Stores a reference to a low-level proto
21 // encoder. If representing a nested message, knows the field number of the
22 // message in its parent and and automatically calls Push and Pop when on the
23 // encoder when created/destroyed.
24 class ProtoMessageEncoder {
25  public:
26   ProtoMessageEncoder(Encoder* encoder, uint32_t parent_field = 0)
encoder_(encoder)27       : encoder_(encoder), parent_field_(parent_field) {
28     if (parent_field_ != 0) {
29       encoder_->Push(parent_field_);
30     }
31   }
32 
~ProtoMessageEncoder()33   ~ProtoMessageEncoder() {
34     if (parent_field_ != 0) {
35       encoder_->Pop();
36     }
37   }
38 
39  protected:
40   Encoder* encoder_;
41   uint32_t parent_field_;
42 };
43 
44 }  // namespace pw::protobuf
45