message_parser.h
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement
4  *
5  * Copyright (c) 2020,
6  * TU Dortmund - Institute of Control Theory and Systems Engineering.
7  * All rights reserved.
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <https://www.gnu.org/licenses/>.
21  *
22  * Authors: Christoph Rösmann
23  *********************************************************************/
24 
25 #ifndef SRC_COMMUNICATION_INCLUDE_CORBO_COMMUNICATION_MESSAGE_PARSER_H_
26 #define SRC_COMMUNICATION_INCLUDE_CORBO_COMMUNICATION_MESSAGE_PARSER_H_
27 
28 #ifdef MESSAGE_SUPPORT
29 
30 #include <corbo-communication/messages/descriptor_extensions.pb.h>
31 #include <google/protobuf/message.h>
32 #include <functional>
33 #include <list>
34 #include <string>
35 #include <utility>
36 #include <vector>
37 
38 namespace corbo {
39 
56 class MessageParser
57 {
58  public:
60  struct FieldInformation
61  {
62  std::string msg_type;
63  std::string field_name;
64  std::vector<std::string> enum_list;
65  std::list<std::string> nested_names;
66 
67  // support manipulation of the field from outside
68  // (using reflection interface):
69  const google::protobuf::FieldDescriptor* field_raw = nullptr;
70  google::protobuf::Message* message_raw = nullptr;
71  const google::protobuf::OneofDescriptor* oneof_descriptor = nullptr;
72  const google::protobuf::FieldDescriptor* oneof_selected_field = nullptr;
73 
74  bool new_message = true;
75 
76  // The following list contains extra options that are defined in 'messages/descriptor_extensions.proto'
77  // better would be std::optional (C++17)
78  std::pair<bool, std::string> label;
79  std::pair<bool, std::string> description;
80  std::pair<bool, std::string> msg_description;
81  std::pair<bool, std::string> default_value;
82  std::pair<bool, double> min_value;
83  std::pair<bool, double> max_value;
84  bool dynamic_size = false;
85  std::pair<bool, int> min_size;
86  std::pair<bool, int> max_size;
87  std::pair<bool, bool> info_text;
88 
89  std::pair<bool, messages::GuiType> gui_type;
90  bool collapsed = false;
91  bool update_signals = false;
92  bool print_description = false;
93  };
94 
96  enum class MessageEvent { MessageStart, MessageEnd, OneOf };
97 
98  template <typename T>
99  using CbValue = std::function<void(const T&, const FieldInformation&)>;
100 
101  using CbMessageEvent = std::function<void(const MessageEvent&, const FieldInformation&)>;
102 
104  void setCallbackValueInt32(CbValue<std::int32_t> fun_value_int32) { _fun_value_int32 = fun_value_int32; }
106  void setCallbackValueInt32Array(CbValue<std::vector<std::int32_t>> fun_value_int32_array) { _fun_value_int32_array = fun_value_int32_array; }
107 
109  void setCallbackValueUInt32(CbValue<std::uint32_t> fun_value_uint32) { _fun_value_uint32 = fun_value_uint32; }
111  void setCallbackValueUInt32Array(CbValue<std::vector<std::uint32_t>> fun_value_uint32_array) { _fun_value_uint32_array = fun_value_uint32_array; }
112 
114  void setCallbackValueInt64(CbValue<std::int64_t> fun_value_int64) { _fun_value_int64 = fun_value_int64; }
116  void setCallbackValueInt64Array(CbValue<std::vector<std::int64_t>> fun_value_int64_array) { _fun_value_int64_array = fun_value_int64_array; }
117 
119  void setCallbackValueUInt64(CbValue<std::uint64_t> fun_value_uint64) { _fun_value_uint64 = fun_value_uint64; }
121  void setCallbackValueUInt64Array(CbValue<std::vector<std::uint64_t>> fun_value_uint64_array) { _fun_value_uint64_array = fun_value_uint64_array; }
122 
124  void setCallbackValueFloat(CbValue<float> fun_value_float) { _fun_value_float = fun_value_float; }
126  void setCallbackValueFloatArray(CbValue<std::vector<float>> fun_value_float_array) { _fun_value_float_array = fun_value_float_array; }
127 
129  void setCallbackValueDouble(CbValue<double> fun_value_double) { _fun_value_double = fun_value_double; }
131  void setCallbackValueDoubleArray(CbValue<std::vector<double>> fun_value_double_array) { _fun_value_double_array = fun_value_double_array; }
132 
134  void setCallbackValueString(CbValue<std::string> fun_value_string) { _fun_value_string = fun_value_string; }
136  void setCallbackValueStringArray(CbValue<std::vector<std::string>> fun_value_string_array) { _fun_value_string_array = fun_value_string_array; }
137 
139  void setCallbackValueStringInfo(CbValue<std::string> fun_value_string_info) { _fun_value_string_info = fun_value_string_info; }
140 
142  void setCallbackValueBool(CbValue<bool> fun_value_bool) { _fun_value_bool = fun_value_bool; }
144  void setCallbackValueBoolArray(CbValue<std::vector<bool>> fun_value_bool_array) { _fun_value_bool_array = fun_value_bool_array; }
145 
147  void setCallbackValueEnum(CbValue<std::string> fun_value_enum) { _fun_value_enum = fun_value_enum; }
149  void setCallbackValueEnumArray(CbValue<std::vector<std::string>> fun_value_enum_array) { _fun_value_enum_array = fun_value_enum_array; }
150 
152  void setCallbackMessageEvent(CbMessageEvent fun_msg_event) { _fun_msg_event = fun_msg_event; }
153 
162  void parse(google::protobuf::Message* message, bool expand_oneof = true, bool skip_inactive_oneof = true,
163  std::list<std::string>* nested_names = nullptr);
164 
176  void parseField(google::protobuf::Message* message, const google::protobuf::FieldDescriptor* field, bool expand_oneof = true,
177  bool expand_first_oneof = true, bool skip_inactive_oneof = true, std::list<std::string>* nested_names = nullptr);
178 
180  void setVerbose(bool active) { _verbose = active; }
181 
183  static std::string nestedNamesToString(const std::list<std::string>& namespaces);
184 
185  protected:
187  void consumeFieldMessage(google::protobuf::Message* message, const google::protobuf::Reflection* reflection,
188  const google::protobuf::FieldDescriptor* field, bool expand_oneof = true, bool skip_inactive_oneof = true,
189  std::list<std::string>* nested_names = nullptr);
191  void consumeFieldValue(google::protobuf::Message* message, const google::protobuf::Reflection* reflection,
192  const google::protobuf::FieldDescriptor* field, std::list<std::string>* nested_names = nullptr);
193 
195  void getFieldInformation(google::protobuf::Message* message, const google::protobuf::FieldDescriptor* field, FieldInformation& info,
196  std::list<std::string>* nested_names = nullptr, bool is_message = false);
197 
198  CbValue<std::int32_t> _fun_value_int32;
199  CbValue<std::vector<std::int32_t>> _fun_value_int32_array;
200  CbValue<std::uint32_t> _fun_value_uint32;
201  CbValue<std::vector<std::uint32_t>> _fun_value_uint32_array;
202  CbValue<std::int64_t> _fun_value_int64;
203  CbValue<std::vector<std::int64_t>> _fun_value_int64_array;
204  CbValue<std::uint64_t> _fun_value_uint64;
205  CbValue<std::vector<std::uint64_t>> _fun_value_uint64_array;
206  CbValue<float> _fun_value_float;
207  CbValue<std::vector<float>> _fun_value_float_array;
208  CbValue<double> _fun_value_double;
209  CbValue<std::vector<double>> _fun_value_double_array;
210  CbValue<std::string> _fun_value_string;
211  CbValue<std::vector<std::string>> _fun_value_string_array;
212  CbValue<std::string> _fun_value_string_info;
213  CbValue<bool> _fun_value_bool;
214  CbValue<std::vector<bool>> _fun_value_bool_array;
215  CbValue<std::string> _fun_value_enum;
216  CbValue<std::vector<std::string>> _fun_value_enum_array;
217 
218  CbMessageEvent _fun_msg_event;
219 
220  bool _verbose = true;
221 };
222 
223 } // namespace corbo
224 
225 #endif // MESSAGE_SUPPORT
226 
227 #endif // SRC_COMMUNICATION_INCLUDE_CORBO_COMMUNICATION_MESSAGE_PARSER_H_
else if n * info
Definition: cholesky.cpp:18


control_box_rst
Author(s): Christoph Rösmann
autogenerated on Mon Feb 28 2022 22:07:05