OpenDDS  Snapshot(2023/04/28-20:55)
annotations.cpp
Go to the documentation of this file.
1 #include "annotations.h"
2 
3 #include "be_extern.h"
4 #include "be_util.h"
5 
6 #include <ast_annotation_appl.h>
7 #include <ast_annotation_decl.h>
8 #include <ast_annotation_member.h>
9 #include <ast_sequence.h>
10 #include <ast_array.h>
11 #include <ast_union.h>
12 #include <utl_string.h>
13 
15 {
16  register_one<KeyAnnotation>();
17  register_one<TopicAnnotation>();
18  register_one<NestedAnnotation>();
19  register_one<DefaultNestedAnnotation>();
20  register_one<IdAnnotation>();
21  register_one<AutoidAnnotation>();
22  register_one<HashidAnnotation>();
23  register_one<OptionalAnnotation>();
24  register_one<MustUnderstandAnnotation>();
25  register_one<ExternalAnnotation>();
26  register_one<ExtensibilityAnnotation>();
27  register_one<FinalAnnotation>();
28  register_one<AppendableAnnotation>();
29  register_one<MutableAnnotation>();
30  register_one<TryConstructAnnotation>();
31  register_one<OpenDDS::DataRepresentationAnnotation>();
32  register_one<OpenDDS::internal::NoDynamicDataAdapterAnnotation>();
33  register_one<OpenDDS::internal::SpecialSerializationAnnotation>();
34 }
35 
37 {
38 }
39 
41 {
42  for (MapType::iterator i = map_.begin(); i != map_.end(); ++i) {
43  delete i->second;
44  }
45 }
46 
47 Annotation* Annotations::operator[](const std::string& annotation) const
48 {
49  const MapType::const_iterator i = map_.find(annotation);
50  if (i == map_.end()) {
51  be_util::misc_error_and_abort(std::string("No such annotation: ") + annotation);
52  }
53  return i->second;
54 }
55 
57 : declaration_(0)
58 {
59 }
60 
62 {
63 }
64 
65 std::string Annotation::module() const
66 {
67  return "::";
68 }
69 
70 std::string Annotation::fullname() const
71 {
72  return module() + std::string("@") + name();
73 }
74 
75 AST_Annotation_Decl* Annotation::declaration() const
76 {
77  return declaration_;
78 }
79 
80 AST_Annotation_Appl* Annotation::find_on(AST_Decl* node) const
81 {
82  return node->annotations().find(declaration_);
83 }
84 
86 {
87  idl_global->eval(definition().c_str());
88  UTL_Scope* root = idl_global->scopes().bottom();
89  declaration_ = dynamic_cast<AST_Annotation_Decl*>(
90  root->lookup_by_name(fullname().c_str()));
91 }
92 
93 AST_Expression::AST_ExprValue* get_annotation_member_ev(
94  AST_Annotation_Appl* appl, const char* member_name, AST_Expression::ExprType type)
95 {
96  AST_Annotation_Member* member =
97  dynamic_cast<AST_Annotation_Member*>((*appl)[member_name]);
98  if (member) {
99  AST_Expression* e = member->value();
100  if (e && e->ev()) {
101  if (e->ev()->et != type) {
102  be_util::misc_error_and_abort(std::string("Found unexpected expression type "
103  "while getting value of member \"") +
104  member_name + "\" of annotation \"" +
105  appl->local_name()->get_string() + '"',
106  appl);
107  }
108  return e->ev();
109  }
110  }
111 
112  be_util::misc_error_and_abort(std::string("Found null pointer while getting value of member \"") +
113  member_name + "\" of annotation \"" +
114  appl->local_name()->get_string() + '"',
115  appl);
116  return 0;
117 }
118 
119 bool get_bool_annotation_member_value(AST_Annotation_Appl* appl,
120  const char* member_name)
121 {
122  AST_Expression::AST_ExprValue* const ev =
123  get_annotation_member_ev(appl, member_name, AST_Expression::EV_bool);
124  return ev->u.bval;
125 }
126 
127 ACE_UINT32 get_u32_annotation_member_value(AST_Annotation_Appl* appl,
128  const char* member_name)
129 {
130  AST_Expression::AST_ExprValue* const ev =
131  get_annotation_member_ev(appl, member_name, AST_Expression::EV_ulong);
132  return ev->u.ulval;
133 }
134 
135 std::string get_str_annotation_member_value(AST_Annotation_Appl* appl,
136  const char* member_name)
137 {
138  AST_Expression::AST_ExprValue* const ev =
139  get_annotation_member_ev(appl, member_name, AST_Expression::EV_string);
140  return ev->u.strval->get_string();
141 }
142 
143 template<>
144 bool AnnotationWithValue<bool>::value_from_appl(AST_Annotation_Appl* appl) const
145 {
146  return get_bool_annotation_member_value(appl, "value");
147 }
148 
149 template<>
150 ACE_UINT32 AnnotationWithValue<ACE_UINT32>::value_from_appl(AST_Annotation_Appl* appl) const
151 {
152  return get_u32_annotation_member_value(appl, "value");
153 }
154 
155 template<>
156 std::string AnnotationWithValue<std::string>::value_from_appl(AST_Annotation_Appl* appl) const
157 {
158  return get_str_annotation_member_value(appl, "value");
159 }
160 
161 // @key ======================================================================
162 
163 std::string KeyAnnotation::definition() const
164 {
165  return
166  "@annotation key {\n"
167  " boolean value default TRUE;\n"
168  "};\n";
169 }
170 
171 std::string KeyAnnotation::name() const
172 {
173  return "key";
174 }
175 
176 bool KeyAnnotation::union_value(AST_Union* node) const
177 {
178  AST_Annotation_Appl* appl = node->disc_annotations().find(declaration());
179  if (!appl) { return absent_value; }
180  return value_from_appl(appl);
181 }
182 
183 // @topic ====================================================================
184 
186 {
187 }
188 
189 std::string TopicAnnotation::definition() const
190 {
191  return
192  "@annotation topic {\n"
193  " string name default \"\";\n"
194  " string platform default \"*\";\n"
195  "};\n";
196 }
197 
198 std::string TopicAnnotation::name() const
199 {
200  return "topic";
201 }
202 
203 TopicValue TopicAnnotation::value_from_appl(AST_Annotation_Appl* appl) const
204 {
206  value.name = get_str_annotation_member_value(appl, "name");
207  value.platform = get_str_annotation_member_value(appl, "platform");
208  return value;
209 }
210 
211 // @nested ===================================================================
212 
213 std::string NestedAnnotation::definition() const
214 {
215  return
216  "@annotation nested {\n"
217  " boolean value default TRUE;\n"
218  "};\n";
219 }
220 
221 std::string NestedAnnotation::name() const
222 {
223  return "nested";
224 }
225 
226 // @default_nested ===========================================================
227 
229 {
230  return
231  "@annotation default_nested {\n"
232  " boolean value default TRUE;\n"
233  "};\n";
234 }
235 
236 std::string DefaultNestedAnnotation::name() const
237 {
238  return "default_nested";
239 }
240 
241 // @id =======================================================================
242 
243 std::string IdAnnotation::definition() const
244 {
245  return
246  "@annotation id {\n"
247  " unsigned long value;\n"
248  "};\n";
249 }
250 
251 std::string IdAnnotation::name() const
252 {
253  return "id";
254 }
255 
256 // @autoid ===================================================================
257 
258 std::string AutoidAnnotation::definition() const
259 {
260  return
261  "@annotation autoid {\n"
262  " enum AutoidKind {\n"
263  " SEQUENTIAL,\n"
264  " HASH\n"
265  " };\n"
266  " AutoidKind value default HASH;\n"
267  "};\n";
268 }
269 
270 std::string AutoidAnnotation::name() const
271 {
272  return "autoid";
273 }
274 
275 // @hashid ===================================================================
276 
277 std::string HashidAnnotation::definition() const
278 {
279  return
280  "@annotation hashid {\n"
281  " string value default \"\";"
282  "};\n";
283 }
284 
285 std::string HashidAnnotation::name() const
286 {
287  return "hashid";
288 }
289 
290 // @optional =================================================================
291 
293 {
294  return
295  "@annotation optional {\n"
296  " boolean value default TRUE;"
297  "};\n";
298 }
299 
300 std::string OptionalAnnotation::name() const
301 {
302  return "optional";
303 }
304 
305 // @must_understand =================================================================
306 
308 {
309  return
310  "@annotation must_understand {\n"
311  " boolean value default TRUE;"
312  "};\n";
313 }
314 
316 {
317  return "must_understand";
318 }
319 
320 // @external =================================================================
321 
323 {
324  return
325  "@annotation external {\n"
326  " boolean value default TRUE;"
327  "};\n";
328 }
329 
330 std::string ExternalAnnotation::name() const
331 {
332  return "external";
333 }
334 
335 // @extensibility ============================================================
336 
338 {
339  return
340  "@annotation extensibility {\n"
341  " enum ExtensibilityKind {\n"
342  " FINAL,\n"
343  " APPENDABLE,\n"
344  " MUTABLE\n"
345  " };\n"
346  " ExtensibilityKind value;\n"
347  "};\n";
348 }
349 
350 std::string ExtensibilityAnnotation::name() const
351 {
352  return "extensibility";
353 }
354 
355 // @final ====================================================================
356 
357 std::string FinalAnnotation::definition() const
358 {
359  return "@annotation final {};\n";
360 }
361 
362 std::string FinalAnnotation::name() const
363 {
364  return "final";
365 }
366 
367 // @appendable ===============================================================
368 
370 {
371  return "@annotation appendable {};\n";
372 }
373 
374 std::string AppendableAnnotation::name() const
375 {
376  return "appendable";
377 }
378 
379 // @mutable ==================================================================
380 
381 std::string MutableAnnotation::definition() const
382 {
383  return "@annotation mutable {};\n";
384 }
385 
386 std::string MutableAnnotation::name() const
387 {
388  return "mutable";
389 }
390 
391 // @try_construct ============================================================
392 
394 {
395  return
396  "@annotation try_construct {\n"
397  " enum TryConstructFailAction {\n"
398  " DISCARD,\n"
399  " USE_DEFAULT,\n"
400  " TRIM\n"
401  " };\n"
402  " TryConstructFailAction value default USE_DEFAULT;\n"
403  "};\n";
404 }
405 
406 std::string TryConstructAnnotation::name() const
407 {
408  return "try_construct";
409 }
410 
412 {
413  AST_Annotation_Appl* appl = node->base_type_annotations().find(declaration());
414  if (!appl) { return absent_value; }
415  return value_from_appl(appl);
416 }
417 
419 {
420  AST_Annotation_Appl* appl = node->base_type_annotations().find(declaration());
421  if (!appl) { return absent_value; }
422  return value_from_appl(appl);
423 }
424 
426 {
427  AST_Annotation_Appl* appl = node->disc_annotations().find(declaration());
428  if (!appl) { return absent_value; }
429  return value_from_appl(appl);
430 }
431 
433 namespace OpenDDS {
434 
435  // @OpenDDS::data_representation ===========================================
436 
437  std::string DataRepresentationAnnotation::definition() const
438  {
439  return
440  "module OpenDDS {\n"
441  " @annotation data_representation {\n"
442  " enum Kind_t {\n"
443  " XCDR1,\n"
444  " XML,\n"
445  " XCDR2,\n"
446  " UNALIGNED_CDR\n"
447  " };\n"
448  " Kind_t kind;\n"
449  " };\n"
450  "};\n";
451  }
452 
454  {
455  return "data_representation";
456  }
457 
458  bool DataRepresentationAnnotation::node_value_exists(
459  AST_Decl* node, DataRepresentation& value) const
460  {
461  value = DataRepresentation();
462  bool found = false;
463  if (node) {
464  for (AST_Annotation_Appls::iterator i = node->annotations().begin();
465  i != node->annotations().end(); ++i) {
466  AST_Annotation_Appl* appl = i->get();
467  if (appl && appl->annotation_decl() == declaration()) {
468  found = true;
469  value.add(value_from_appl(appl));
470  }
471  }
472  }
473  return found;
474  }
475 
476  DataRepresentation DataRepresentationAnnotation::value_from_appl(AST_Annotation_Appl* appl) const
477  {
479  if (appl && appl->annotation_decl() == declaration()) {
480  switch (get_u32_annotation_member_value(appl, "kind")) {
481  case 0: // Kind_t::XCDR1
482  value.xcdr1 = true;
483  break;
484  case 1: // Kind_t::XML
485  value.xml = true;
486  break;
487  case 2: // Kind_t::XCDR2
488  value.xcdr2 = true;
489  break;
490  case 3: // Kind_t::UNALIGNED_CDR
491  value.unaligned = true;
492  break;
493  }
494  }
495  return value;
496  }
497 
498  std::string DataRepresentationAnnotation::module() const
499  {
500  return "::OpenDDS::";
501  }
502 }
virtual std::string module() const
Definition: annotations.cpp:65
std::string name() const
std::string name() const
AST_Annotation_Decl * declaration_
Definition: annotations.h:78
AST_Annotation_Appl * find_on(AST_Decl *node) const
Definition: annotations.cpp:80
std::string name() const
std::string name() const
std::string definition() const
const LogLevel::Value value
Definition: debug.cpp:61
std::string definition() const
std::string definition() const
TryConstructFailAction union_value(AST_Union *node) const
std::string definition() const
TryConstructFailAction array_element_value(AST_Array *node) const
void add(const DataRepresentation &other)
Definition: annotations.h:355
std::string definition() const
TopicValue value_from_appl(AST_Annotation_Appl *appl) const
std::string definition() const
void cache()
Definition: annotations.cpp:85
TryConstructFailAction
Definition: annotations.h:316
Annotation * operator[](const std::string &annotation) const
Definition: annotations.cpp:47
std::string name() const
std::string name() const
std::string platform
Definition: annotations.h:172
std::string name() const
void register_all()
Definition: annotations.cpp:14
AST_Annotation_Decl * declaration() const
Definition: annotations.cpp:75
ACE_UINT32 get_u32_annotation_member_value(AST_Annotation_Appl *appl, const char *member_name)
MapType map_
Definition: annotations.h:57
virtual ~Annotation()
Definition: annotations.cpp:61
AST_Expression::AST_ExprValue * get_annotation_member_ev(AST_Annotation_Appl *appl, const char *member_name, AST_Expression::ExprType type)
Definition: annotations.cpp:93
std::string name() const
std::string name() const
std::string definition() const
std::string name() const
virtual T value_from_appl(AST_Annotation_Appl *) const
Definition: annotations.h:130
TryConstructFailAction sequence_element_value(AST_Sequence *node) const
bool get_bool_annotation_member_value(AST_Annotation_Appl *appl, const char *member_name)
virtual std::string definition() const =0
const char *const name
Definition: debug.cpp:60
virtual std::string name() const =0
std::string name() const
std::string name() const
std::string definition() const
std::string name() const
std::string definition() const
virtual std::string fullname() const
Definition: annotations.cpp:70
std::string name() const
std::string definition() const
std::string definition() const
#define OPENDDS_END_VERSIONED_NAMESPACE_DECL
std::string get_str_annotation_member_value(AST_Annotation_Appl *appl, const char *member_name)
std::string definition() const
bool union_value(AST_Union *node) const
std::string definition() const
std::string definition() const
void misc_error_and_abort(const std::string &message, AST_Decl *node=0)
Report a miscellaneous error and abort.
The Internal API and Implementation of OpenDDS.
Definition: AddressCache.h:28
std::string name() const
std::string definition() const
std::string name
Definition: annotations.h:171