DCCL v5
Loading...
Searching...
No Matches
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
29namespace dccl
30{
31class FieldCodecBase;
32enum MessagePart
33{
34 HEAD,
35 BODY,
36 UNKNOWN
37};
38
40namespace 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
75bool has_head_field(const google::protobuf::Descriptor* desc);
76
77//RAII handler for the current Message recursion stack
79{
80 public:
81 MessageStack(const google::protobuf::Message* root_message, MessageStackData& data,
82 const google::protobuf::FieldDescriptor* field = nullptr);
83
85
86 bool first() const { return data_.desc.empty(); }
87 int count() const { return data_.desc.size(); }
88
89 void push(const google::protobuf::Descriptor* desc);
90 void push(const google::protobuf::FieldDescriptor* field);
91 void push(MessagePart part);
92
93 void update_index(const google::protobuf::Message* root_message,
94 const google::protobuf::FieldDescriptor* field, int index);
95 void push_message(const google::protobuf::Message* root_message,
96 const google::protobuf::FieldDescriptor* field, int index = -1);
97
98 std::size_t field_size() const { return data_.field.size(); }
99 MessagePart current_part() const { return data_.current_part(); }
100
101 private:
102 void __pop_desc();
103 void __pop_field();
104 void __pop_parts();
105 void __pop_messages();
106
107 MessageStackData& data_;
108
109 int descriptors_pushed_;
110 int fields_pushed_;
111 int parts_pushed_;
112 int messages_pushed_;
113};
114} // namespace internal
115} // namespace dccl
116
117#endif
bool has_head_field(const google::protobuf::Descriptor *desc)
Recursively checks if a message descriptor has any fields with in_head = true.
Dynamic Compact Control Language namespace.
Definition any.h:28