DCCL v4
field_codec_message_stack.h
1 // Copyright 2014-2023:
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 // Community contributors (see AUTHORS file)
5 // File authors:
6 // Toby Schneider <toby@gobysoft.org>
7 //
8 //
9 // This file is part of the Dynamic Compact Control Language Library
10 // ("DCCL").
11 //
12 // DCCL is free software: you can redistribute it and/or modify
13 // it under the terms of the GNU Lesser General Public License as published by
14 // the Free Software Foundation, either version 2.1 of the License, or
15 // (at your option) any later version.
16 //
17 // DCCL is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU Lesser General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public License
23 // along with DCCL. If not, see <http://www.gnu.org/licenses/>.
24 #ifndef DCCLFIELDCODECHELPERS20110825H
25 #define DCCLFIELDCODECHELPERS20110825H
26 
27 #include "../common.h"
28 
29 namespace dccl
30 {
31 class FieldCodecBase;
32 enum MessagePart
33 {
34  HEAD,
35  BODY,
36  UNKNOWN
37 };
38 
40 namespace internal
41 {
43 {
44  const google::protobuf::Descriptor* top_descriptor() const
45  {
46  return !desc.empty() ? desc.back() : nullptr;
47  }
48  const google::protobuf::Message* top_message() const
49  {
50  return !messages.empty() ? messages.back().msg : nullptr;
51  }
52  const google::protobuf::FieldDescriptor* top_field() const
53  {
54  return !field.empty() ? field.back() : nullptr;
55  }
56  MessagePart current_part() const { return parts.empty() ? UNKNOWN : parts.back(); }
57 
58  std::vector<const google::protobuf::Descriptor*> desc;
59  std::vector<const google::protobuf::FieldDescriptor*> field;
60  std::vector<MessagePart> parts;
62  {
63  // latest depth of message
64  const google::protobuf::Message* msg;
65  // field corresponding to this message (or nullptr for the first)
66  const google::protobuf::FieldDescriptor* field{nullptr};
67  };
68  std::vector<MessageAndField> messages;
69 };
70 
71 //RAII handler for the current Message recursion stack
73 {
74  public:
75  MessageStack(const google::protobuf::Message* root_message, MessageStackData& data,
76  const google::protobuf::FieldDescriptor* field = nullptr);
77 
78  ~MessageStack();
79 
80  bool first() const { return data_.desc.empty(); }
81  int count() const { return data_.desc.size(); }
82 
83  void push(const google::protobuf::Descriptor* desc);
84  void push(const google::protobuf::FieldDescriptor* field);
85  void push(MessagePart part);
86 
87  void update_index(const google::protobuf::Message* root_message,
88  const google::protobuf::FieldDescriptor* field, int index);
89  void push_message(const google::protobuf::Message* root_message,
90  const google::protobuf::FieldDescriptor* field, int index = -1);
91 
92  std::size_t field_size() const { return data_.field.size(); }
93  MessagePart current_part() const { return data_.current_part(); }
94 
95  private:
96  void __pop_desc();
97  void __pop_field();
98  void __pop_parts();
99  void __pop_messages();
100 
101  MessageStackData& data_;
102 
103  int descriptors_pushed_;
104  int fields_pushed_;
105  int parts_pushed_;
106  int messages_pushed_;
107 };
108 } // namespace internal
109 } // namespace dccl
110 
111 #endif
dccl
Dynamic Compact Control Language namespace.
Definition: any.h:49
dccl::internal::MessageStack
Definition: field_codec_message_stack.h:72
dccl::internal::MessageStackData::MessageAndField
Definition: field_codec_message_stack.h:61
Message
dccl::internal::MessageStackData
Definition: field_codec_message_stack.h:42