OpenDDS  Snapshot(2023/04/28-20:55)
keys_generator.cpp
Go to the documentation of this file.
1 /*
2  *
3  *
4  * Distributed under the OpenDDS License.
5  * See: http://www.opendds.org/license.html
6  */
7 
8 #include "keys_generator.h"
9 #include "be_extern.h"
10 #include "topic_keys.h"
11 
12 #include "utl_identifier.h"
13 
14 #include <string>
15 using std::string;
16 
18  size_t n_;
19  const string cxx_name_;
20 
21  explicit KeyLessThanWrapper(UTL_ScopedName* name)
22  : n_(0)
23  , cxx_name_(scoped(name))
24  {
25  be_global->header_ << be_global->versioning_begin() << "\n";
26 
27  for (UTL_ScopedName* sn = name; sn && sn->tail();
28  sn = static_cast<UTL_ScopedName*>(sn->tail())) {
29  const string str = sn->head()->get_string();
30  if (!str.empty()) {
31  be_global->header_ << "namespace " << str << " {\n";
32  ++n_;
33  }
34  }
35 
36  be_global->header_ <<
37  "/// This structure supports use of std::map with one or more keys.\n"
38  "struct " << be_global->export_macro() << ' ' <<
39  name->last_component()->get_string() << "_OpenDDS_KeyLessThan {\n";
40  }
41 
42  void
44  {
45  be_global->header_ <<
46  " bool operator()(const " << cxx_name_ << "&, const " << cxx_name_ << "&) const\n"
47  " {\n"
48  " // With no keys, return false to allow use of\n"
49  " // map with just one entry\n";
50  }
51 
52  void
54  {
55  be_global->header_ <<
56  " bool operator()(const " << cxx_name_ << "& v1, const " << cxx_name_ << "& v2) const\n"
57  " {\n";
58  }
59 
60  void
61  key_compare(const string& member)
62  {
63  be_global->header_ <<
64  " if (v1." << member << " < v2." << member << ") return true;\n"
65  " if (v2." << member << " < v1." << member << ") return false;\n";
66  }
67 
69  {
70  be_global->header_ <<
71  " return false;\n"
72  " }\n};\n";
73 
74  for (size_t i = 0; i < n_; ++i) {
75  be_global->header_ << "}\n";
76  }
77 
78  be_global->header_ << be_global->versioning_end() << "\n";
79  }
80 };
81 
82 bool keys_generator::gen_struct(AST_Structure* node, UTL_ScopedName* name,
83  const std::vector<AST_Field*>&, AST_Type::SIZE_TYPE, const char*)
84 {
85  TopicKeys keys(node);
86  size_t key_count = 0;
87  const bool is_topic_type = be_global->is_topic_type(node);
88  IDL_GlobalData::DCPS_Data_Type_Info* info = idl_global->is_dcps_type(name);
89  if (is_topic_type) {
90  key_count = keys.count();
91  } else if (info) {
92  key_count = info->key_list_.size();
93  } else {
94  return true;
95  }
96 
97  {
98  KeyLessThanWrapper wrapper(name);
99 
100  if (key_count) {
101  const bool use_cxx11 = be_global->language_mapping() == BE_GlobalData::LANGMAP_CXX11;
102 
103  wrapper.has_keys_signature();
104  if (!use_cxx11) {
105  be_global->header_ <<
106  " using ::operator<; // TAO::String_Manager's operator< is "
107  "in global NS\n";
108  }
109 
110  if (is_topic_type) {
111  TopicKeys::Iterator finished = keys.end();
112  for (TopicKeys::Iterator i = keys.begin(); i != finished; ++i) {
113  string fname = i.path();
114  if (i.root_type() == TopicKeys::UnionType) {
115  fname += "._d()";
116  } else if (use_cxx11) {
117  fname = insert_cxx11_accessor_parens(fname, false);
118  }
119  wrapper.key_compare(fname);
120  }
121  } else if (info) {
122  IDL_GlobalData::DCPS_Data_Type_Info_Iter iter(info->key_list_);
123  for (ACE_TString* kp = 0; iter.next(kp) != 0; iter.advance()) {
124  string fname = ACE_TEXT_ALWAYS_CHAR(kp->c_str());
125  if (use_cxx11) {
126  fname = insert_cxx11_accessor_parens(fname, false);
127  }
128  wrapper.key_compare(fname);
129  }
130  }
131  } else {
132  wrapper.has_no_keys_signature();
133  }
134  }
135 
136  return true;
137 }
138 
140  AST_Union* node, UTL_ScopedName* name,
141  const std::vector<AST_UnionBranch*>&, AST_Type*, const char*)
142 {
143  if (be_global->is_topic_type(node)) {
144  KeyLessThanWrapper wrapper(name);
145  if (be_global->union_discriminator_is_key(node)) {
146  wrapper.has_keys_signature();
147  wrapper.key_compare("_d()");
148  } else {
149  wrapper.has_no_keys_signature();
150  }
151  }
152  return true;
153 }
Iterator end()
Definition: topic_keys.cpp:500
bool gen_union(AST_Union *node, UTL_ScopedName *name, const std::vector< AST_UnionBranch *> &, AST_Type *, const char *)
bool gen_struct(AST_Structure *node, UTL_ScopedName *name, const std::vector< AST_Field *> &fields, AST_Type::SIZE_TYPE size, const char *repoid)
#define ACE_TEXT_ALWAYS_CHAR(STRING)
std::string path()
Definition: topic_keys.cpp:335
const string cxx_name_
size_t count()
Definition: topic_keys.cpp:515
std::string insert_cxx11_accessor_parens(const std::string &full_var_name_, bool is_union_member=false)
const char *const name
Definition: debug.cpp:60
std::string scoped(UTL_ScopedName *sn, EscapeContext ec=EscapeContext_Normal)
void key_compare(const string &member)
KeyLessThanWrapper(UTL_ScopedName *name)
Iterator begin()
Definition: topic_keys.cpp:495
BE_GlobalData * be_global
Definition: be_global.cpp:44
DDS::ReturnCode_t key_count(DDS::DynamicType_ptr type, size_t &count)