OpenDDS  Snapshot(2023/04/07-19:43)
Public Member Functions | Private Member Functions | Private Attributes | List of all members
ts_generator Class Reference

#include <ts_generator.h>

Inheritance diagram for ts_generator:
Inheritance graph
[legend]
Collaboration diagram for ts_generator:
Collaboration graph
[legend]

Public Member Functions

 ts_generator ()
 
bool gen_struct (AST_Structure *node, UTL_ScopedName *name, const std::vector< AST_Field *> &fields, AST_Type::SIZE_TYPE size, const char *repoid)
 
bool gen_typedef (AST_Typedef *, UTL_ScopedName *, AST_Type *, const char *)
 
bool gen_union (AST_Union *node, UTL_ScopedName *name, const std::vector< AST_UnionBranch *> &, AST_Type *, const char *)
 
- Public Member Functions inherited from dds_generator
virtual ~dds_generator ()=0
 
virtual bool do_included_files () const
 
virtual void gen_prologue ()
 
virtual void gen_epilogue ()
 
virtual bool gen_const (UTL_ScopedName *, bool, AST_Constant *)
 
virtual bool gen_enum (AST_Enum *, UTL_ScopedName *, const std::vector< AST_EnumVal *> &, const char *)
 
virtual bool gen_struct_fwd (UTL_ScopedName *, AST_Type::SIZE_TYPE)
 
virtual bool gen_interf (AST_Interface *, UTL_ScopedName *, bool, const std::vector< AST_Interface *> &, const std::vector< AST_Interface *> &, const std::vector< AST_Attribute *> &, const std::vector< AST_Operation *> &, const char *)
 
virtual bool gen_interf_fwd (UTL_ScopedName *)
 
virtual bool gen_native (AST_Native *, UTL_ScopedName *, const char *)
 
virtual bool gen_union_fwd (AST_UnionFwd *, UTL_ScopedName *, AST_Type::SIZE_TYPE)
 

Private Member Functions

bool generate_ts (AST_Decl *node, UTL_ScopedName *name)
 

Private Attributes

std::string idl_template_
 

Additional Inherited Members

- Static Public Member Functions inherited from dds_generator
static std::string get_tag_name (const std::string &base_name, bool nested_key_only=false)
 
static std::string get_xtag_name (UTL_ScopedName *name)
 
static bool cxx_escaped (const std::string &s)
 
static std::string valid_var_name (const std::string &str)
 
static std::string to_string (Identifier *id, EscapeContext ec=EscapeContext_Normal)
 
static std::string scoped_helper (UTL_ScopedName *sn, const char *sep, EscapeContext cxt=EscapeContext_Normal)
 
static std::string module_scope_helper (UTL_ScopedName *sn, const char *sep, EscapeContext cxt=EscapeContext_Normal)
 

Detailed Description

Definition at line 23 of file ts_generator.h.

Constructor & Destructor Documentation

◆ ts_generator()

ts_generator::ts_generator ( )

Definition at line 63 of file ts_generator.cpp.

References ACE_TEXT_ALWAYS_CHAR, be_global, TopicKeys::begin(), and TopicKeys::end().

64  : idl_template_(read_template("IDL"))
65 {
66 }
std::string idl_template_
Definition: ts_generator.h:40

Member Function Documentation

◆ gen_struct()

bool ts_generator::gen_struct ( AST_Structure *  node,
UTL_ScopedName *  name,
const std::vector< AST_Field *> &  fields,
AST_Type::SIZE_TYPE  size,
const char *  repoid 
)
virtual

Implements dds_generator.

Definition at line 403 of file ts_generator.cpp.

References generate_ts().

405 {
406  return generate_ts(node, name);
407 }
const char *const name
Definition: debug.cpp:60
bool generate_ts(AST_Decl *node, UTL_ScopedName *name)

◆ gen_typedef()

bool ts_generator::gen_typedef ( AST_Typedef *  ,
UTL_ScopedName *  ,
AST_Type *  ,
const char *   
)
inlinevirtual

Implements dds_generator.

Definition at line 31 of file ts_generator.h.

32  { return true; }

◆ gen_union()

bool ts_generator::gen_union ( AST_Union *  node,
UTL_ScopedName *  name,
const std::vector< AST_UnionBranch *> &  ,
AST_Type *  ,
const char *   
)
virtual

Implements dds_generator.

Definition at line 409 of file ts_generator.cpp.

References generate_ts().

411 {
412  return generate_ts(node, name);
413 }
const char *const name
Definition: debug.cpp:60
bool generate_ts(AST_Decl *node, UTL_ScopedName *name)

◆ generate_ts()

bool ts_generator::generate_ts ( AST_Decl *  node,
UTL_ScopedName *  name 
)
private

Definition at line 94 of file ts_generator.cpp.

References be_global, ACE_String_Base< char >::c_str(), TopicKeys::count(), EscapeContext_ForGenIdl, EscapeContext_FromGenIdl, EscapeContext_StripEscapes, face_ts_generator::generate(), dds_generator::get_xtag_name(), idl_template_, OpenDDS::XTypes::key_count(), ACE_String_Base< char >::length(), scoped(), dds_generator::scoped_helper(), and dds_generator::to_string().

Referenced by gen_struct(), and gen_union().

95 {
96  if (idl_template_.empty()) {
97  // error reported in read_template
98  return false;
99  }
100 
101  AST_Structure* struct_node = 0;
102  AST_Union* union_node = 0;
103  if (!node || !name) {
104  return false;
105  }
106  if (node->node_type() == AST_Decl::NT_struct) {
107  struct_node = dynamic_cast<AST_Structure*>(node);
108  } else if (node->node_type() == AST_Decl::NT_union) {
109  union_node = dynamic_cast<AST_Union*>(node);
110  } else {
111  return false;
112  }
113 
114  if (!struct_node && !union_node) {
115  idl_global->err()->misc_error(
116  "Could not cast AST Nodes to valid types", node);
117  return false;
118  }
119 
120  size_t key_count = 0;
121  IDL_GlobalData::DCPS_Data_Type_Info* info = 0;
122  TopicKeys keys;
123  if (struct_node) {
124  info = idl_global->is_dcps_type(name);
125  if (be_global->is_topic_type(struct_node)) {
126  keys = TopicKeys(struct_node);
127  key_count = keys.count();
128  } else if (info) {
129  key_count = info->key_list_.size();
130  } else {
131  return true;
132  }
133  } else if (be_global->is_topic_type(union_node)) {
134  key_count = be_global->union_discriminator_is_key(union_node) ? 1 : 0;
135  } else {
136  return true;
137  }
138 
139  const std::string cxx_name = scoped(name);
140  const std::string short_name = name->last_component()->get_string();
141  const std::string ts_name = scoped(name, EscapeContext_FromGenIdl);
142  const std::string ts_short_name = to_string(
143  name->last_component(), EscapeContext_FromGenIdl);
144  const std::string unescaped_name =
146  const std::string name_underscores = dds_generator::scoped_helper(name, "_");
147  static const std::string ns("OpenDDS::DCPS::");
148  const std::string xtag = ns + get_xtag_name(name);
149 
150  static const char* idl_includes[] = {
151  "dds/DdsDcpsInfrastructure.idl", "dds/DdsDcpsTopic.idl",
152  "dds/DdsDcpsPublication.idl", "dds/DdsDcpsSubscriptionExt.idl",
153  "dds/DdsDcpsTypeSupportExt.idl"
154  };
155  add_includes(idl_includes, BE_GlobalData::STREAM_IDL);
156 
157  std::string dc = be_global->header_name_.c_str();
158  dc.replace(dc.end() - 6, dc.end() - 2, "C"); // s/Impl.h$/C.h/
159  be_global->add_include(dc.c_str());
160 
161  static const char* h_includes[] = {
162  "dds/DCPS/TypeSupportImpl.h", "dds/DCPS/ValueDispatcher.h"
163  };
164  add_includes(h_includes, BE_GlobalData::STREAM_H);
165 
166  static const char* cpp_includes[] = {
167  "dds/DCPS/debug.h", "dds/DCPS/Registered_Data_Types.h",
168  "dds/DdsDcpsDomainC.h", "dds/DCPS/Service_Participant.h",
169  "dds/DCPS/Qos_Helper.h", "dds/DCPS/PublicationInstance.h",
170  "dds/DCPS/PublisherImpl.h", "dds/DCPS/SubscriberImpl.h",
171  "dds/DCPS/ReceivedDataElementList.h", "dds/DCPS/RakeResults_T.h",
172  "dds/DCPS/BuiltInTopicUtils.h", "dds/DCPS/Util.h",
173  "dds/DCPS/ContentFilteredTopicImpl.h", "dds/DCPS/RakeData.h",
174  "dds/DCPS/MultiTopicDataReader_T.h", "dds/DCPS/DataWriterImpl_T.h",
175  "dds/DCPS/DataReaderImpl_T.h", "dds/DCPS/XTypes/TypeObject.h"
176  };
177  add_includes(cpp_includes, BE_GlobalData::STREAM_CPP);
178 
179  std::map<std::string, std::string> replacements;
180  replacements["SCOPED"] = scoped(name, EscapeContext_ForGenIdl);
181  // SCOPED_NOT_GLOBAL is EscapeContext_FromGenIdl, because
182  // DCPS_DATA_SEQUENCE_TYPE is strange.
183  replacements["SCOPED_NOT_GLOBAL"] =
185  replacements["TYPE"] = to_string(name->last_component(), EscapeContext_ForGenIdl);
186  replacements["EXPORT"] = be_global->export_macro().c_str();
187  replacements["SEQ"] = be_global->sequence_suffix().c_str();
188 
189  ScopedNamespaceGuard idlGuard(name, be_global->idl_, "module");
190  std::string idl = idl_template_;
191  replaceAll(idl, replacements);
192  be_global->idl_ << idl;
193 
194  be_global->header_ << be_global->versioning_begin() << "\n";
195  {
196  ScopedNamespaceGuard hGuard(name, be_global->header_);
197 
198  be_global->header_ <<
199  "class " << ts_short_name << "TypeSupportImpl;\n";
200  }
201  be_global->header_ << be_global->versioning_end() << "\n";
202 
203  be_global->header_ <<
204  "OPENDDS_BEGIN_VERSIONED_NAMESPACE_DECL\n"
205  "namespace OpenDDS {\n"
206  "namespace DCPS {\n"
207  "template <>\n"
208  "struct DDSTraits<" << cxx_name << "> {\n"
209  " typedef " << cxx_name << " MessageType;\n"
210  " typedef " << ts_name << be_global->sequence_suffix() << " MessageSequenceType;\n"
211  " typedef " << ts_name << be_global->sequence_suffix() << "::PrivateMemberAccess MessageSequenceAdapterType;\n"
212  " typedef " << ts_name << "TypeSupport TypeSupportType;\n"
213  " typedef " << ts_name << "TypeSupportImpl TypeSupportImplType;\n"
214  " typedef " << ts_name << "DataWriter DataWriterType;\n"
215  " typedef " << ts_name << "DataReader DataReaderType;\n"
216  " typedef " << cxx_name << "_OpenDDS_KeyLessThan LessThanType;\n"
217  " typedef OpenDDS::DCPS::KeyOnly<const " << cxx_name << "> KeyOnlyType;\n"
218  " typedef " << xtag << " XtagType;\n"
219  "\n"
220  " static const char* type_name() { return \"" << unescaped_name << "\"; }\n"
221  " static size_t key_count() { return " << key_count << "; }\n"
222  " static bool is_key(const char*);\n"
223  "};\n"
224  "} // namespace DCPS\n"
225  "} // namespace OpenDDS\n"
226  "OPENDDS_END_VERSIONED_NAMESPACE_DECL\n\n";
227 
228  be_global->impl_ <<
229  "OPENDDS_BEGIN_VERSIONED_NAMESPACE_DECL\n"
230  "namespace OpenDDS {\n"
231  "namespace DCPS {\n"
232  "bool DDSTraits<" << cxx_name << ">::is_key(const char* field)\n"
233  "{\n"
234  " ACE_UNUSED_ARG(field);\n";
235  if (struct_node && key_count) {
236  if (info) {
237  gen_isDcpsKey(info);
238  } else {
239  gen_isDcpsKey(keys);
240  }
241  }
242  be_global->impl_ <<
243  " return false;\n"
244  "}\n"
245  "} // namespace DCPS\n"
246  "} // namespace OpenDDS\n"
247  "OPENDDS_END_VERSIONED_NAMESPACE_DECL\n\n";
248 
249  be_global->header_ << be_global->versioning_begin() << "\n";
250  {
251  ScopedNamespaceGuard hGuard(name, be_global->header_);
252 
253  be_global->header_ <<
254  "class " << be_global->export_macro() << " " << ts_short_name << "TypeSupportImpl\n"
255  " : public virtual OpenDDS::DCPS::LocalObject<" << ts_short_name << "TypeSupport>\n"
256  " , public virtual OpenDDS::DCPS::TypeSupportImpl_T<" << short_name << ">\n"
257  " , public virtual OpenDDS::DCPS::ValueDispatcher_T<" << short_name << ">\n"
258  "{\n"
259  "public:\n"
260  " typedef " << ts_short_name << "TypeSupport TypeSupportType;\n"
261  " typedef " << ts_short_name << "TypeSupport::_var_type _var_type;\n"
262  " typedef " << ts_short_name << "TypeSupport::_ptr_type _ptr_type;\n"
263  "\n"
264  " " << ts_short_name << "TypeSupportImpl() {}\n"
265  " virtual ~" << ts_short_name << "TypeSupportImpl() {}\n"
266  "\n"
267  " virtual " << be_global->versioning_name() << "::DDS::DataWriter_ptr create_datawriter();\n"
268  " virtual " << be_global->versioning_name() << "::DDS::DataReader_ptr create_datareader();\n"
269  "#ifndef OPENDDS_NO_MULTI_TOPIC\n"
270  " virtual " << be_global->versioning_name() << "::DDS::DataReader_ptr create_multitopic_datareader();\n"
271  "#endif /* !OPENDDS_NO_MULTI_TOPIC */\n"
272  "#ifndef OPENDDS_NO_CONTENT_SUBSCRIPTION_PROFILE\n"
273  " virtual const OpenDDS::DCPS::MetaStruct& getMetaStructForType() const;\n"
274  "#endif /* !OPENDDS_NO_CONTENT_SUBSCRIPTION_PROFILE */\n"
275  "\n"
276  " virtual const OpenDDS::XTypes::TypeIdentifier& getMinimalTypeIdentifier() const;\n"
277  " virtual const OpenDDS::XTypes::TypeMap& getMinimalTypeMap() const;\n"
278  "\n"
279  " virtual const OpenDDS::XTypes::TypeIdentifier& getCompleteTypeIdentifier() const;\n"
280  " virtual const OpenDDS::XTypes::TypeMap& getCompleteTypeMap() const;\n"
281  "\n"
282  " static " << ts_short_name << "TypeSupport::_ptr_type _narrow(CORBA::Object_ptr obj);\n"
283  "};\n\n";
284  }
285  be_global->header_ << be_global->versioning_end() << "\n";
286 
287  be_global->impl_ << be_global->versioning_begin() << "\n";
288  {
289  ScopedNamespaceGuard cppGuard(name, be_global->impl_);
290  be_global->impl_ <<
291  "::DDS::DataWriter_ptr " << ts_short_name << "TypeSupportImpl::create_datawriter()\n"
292  "{\n"
293  " typedef OpenDDS::DCPS::DataWriterImpl_T<" << short_name << "> DataWriterImplType;\n"
294  " ::DDS::DataWriter_ptr writer_impl = ::DDS::DataWriter::_nil();\n"
295  " ACE_NEW_NORETURN(writer_impl,\n"
296  " DataWriterImplType());\n"
297  " return writer_impl;\n"
298  "}\n\n"
299  "::DDS::DataReader_ptr " << ts_short_name << "TypeSupportImpl::create_datareader()\n"
300  "{\n"
301  " typedef OpenDDS::DCPS::DataReaderImpl_T<" << short_name << "> DataReaderImplType;\n"
302  " ::DDS::DataReader_ptr reader_impl = ::DDS::DataReader::_nil();\n"
303  " ACE_NEW_NORETURN(reader_impl,\n"
304  " DataReaderImplType());\n"
305  " return reader_impl;\n"
306  "}\n\n"
307  "#ifndef OPENDDS_NO_MULTI_TOPIC\n"
308  "::DDS::DataReader_ptr " << ts_short_name << "TypeSupportImpl::create_multitopic_datareader()\n"
309  "{\n"
310  " typedef OpenDDS::DCPS::DataReaderImpl_T<" << short_name << "> DataReaderImplType;\n"
311  " typedef OpenDDS::DCPS::MultiTopicDataReader_T<" << short_name << ", DataReaderImplType> MultiTopicDataReaderImplType;\n"
312  " ::DDS::DataReader_ptr multitopic_reader_impl = ::DDS::DataReader::_nil();\n"
313  " ACE_NEW_NORETURN(multitopic_reader_impl,\n"
314  " MultiTopicDataReaderImplType());\n"
315  " return multitopic_reader_impl;\n"
316  "}\n"
317  "#endif /* !OPENDDS_NO_MULTI_TOPIC */\n\n"
318  "#ifndef OPENDDS_NO_CONTENT_SUBSCRIPTION_PROFILE\n"
319  "const OpenDDS::DCPS::MetaStruct& " << ts_short_name << "TypeSupportImpl::getMetaStructForType() const\n"
320  "{\n"
321  " return OpenDDS::DCPS::getMetaStruct<" << short_name << ">();\n"
322  "}\n"
323  "#endif /* !OPENDDS_NO_CONTENT_SUBSCRIPTION_PROFILE */\n\n"
324  "namespace {\n"
325  " OpenDDS::DCPS::TypeSupportInitializer<" << ts_short_name << "TypeSupportImpl> ts_init_" << name_underscores << ";\n"
326  "}\n"
327  "\n"
328  "const OpenDDS::XTypes::TypeIdentifier& " << ts_short_name << "TypeSupportImpl::getMinimalTypeIdentifier() const\n"
329  "{\n";
330 
331  const bool java_ts_only = be_global->java_arg().length() > 0;
332  const bool generate_xtypes = !be_global->suppress_xtypes() && !java_ts_only;
333  if (generate_xtypes) {
334  be_global->impl_ <<
335  " return OpenDDS::DCPS::getMinimalTypeIdentifier<" << xtag << ">();\n";
336  } else {
337  be_global->impl_ <<
338  " static OpenDDS::XTypes::TypeIdentifier ti;\n"
339  " return ti;\n";
340  }
341  be_global->impl_ <<
342  "}\n\n"
343  "const OpenDDS::XTypes::TypeMap& " << ts_short_name << "TypeSupportImpl::getMinimalTypeMap() const\n"
344  "{\n";
345 
346  if (generate_xtypes) {
347  be_global->impl_ <<
348  " return OpenDDS::DCPS::getMinimalTypeMap<" << xtag << ">();\n";
349  } else {
350  be_global->impl_ <<
351  " static OpenDDS::XTypes::TypeMap tm;\n"
352  " return tm;\n";
353  }
354  be_global->impl_ <<
355  "}\n\n"
356  "const OpenDDS::XTypes::TypeIdentifier& " << ts_short_name << "TypeSupportImpl::getCompleteTypeIdentifier() const\n"
357  "{\n";
358 
359  const bool generate_xtypes_complete = generate_xtypes && be_global->xtypes_complete();
360  if (generate_xtypes_complete) {
361  be_global->impl_ <<
362  " return OpenDDS::DCPS::getCompleteTypeIdentifier<" << xtag << ">();\n";
363  } else {
364  be_global->impl_ <<
365  " static OpenDDS::XTypes::TypeIdentifier ti;\n"
366  " return ti;\n";
367  }
368  be_global->impl_ <<
369  "}\n\n"
370  "const OpenDDS::XTypes::TypeMap& " << ts_short_name << "TypeSupportImpl::getCompleteTypeMap() const\n"
371  "{\n";
372 
373  if (generate_xtypes_complete) {
374  be_global->impl_ <<
375  " return OpenDDS::DCPS::getCompleteTypeMap<" << xtag << ">();\n";
376  } else {
377  be_global->impl_ <<
378  " static OpenDDS::XTypes::TypeMap tm;\n"
379  " return tm;\n";
380  }
381  be_global->impl_ <<
382  "}\n\n"
383  << ts_short_name << "TypeSupport::_ptr_type " << ts_short_name << "TypeSupportImpl::_narrow(CORBA::Object_ptr obj)\n"
384  "{\n"
385  " return TypeSupportType::_narrow(obj);\n"
386  "}\n";
387  }
388  be_global->impl_ << be_global->versioning_end() << "\n";
389 
390  if (be_global->face_ts()) {
391  if (node->node_type() == AST_Decl::NT_struct) {
393  } else {
394  idl_global->err()->misc_error(
395  "Generating FACE type support for Union topic types is not supported", node);
396  return false;
397  }
398  }
399 
400  return true;
401 }
const char * c_str(void) const
static std::string to_string(Identifier *id, EscapeContext ec=EscapeContext_Normal)
void generate(UTL_ScopedName *name)
size_t count()
Definition: topic_keys.cpp:515
DDS::ReturnCode_t key_count(DDS::DynamicType_ptr type, size_t &count)
static std::string get_xtag_name(UTL_ScopedName *name)
Strip any escapes.
Definition: dds_generator.h:43
This is for generated IDL. (Like *TypeSupport.idl)
Definition: dds_generator.h:39
std::string scoped(UTL_ScopedName *sn, EscapeContext ec=EscapeContext_Normal)
This is for a name coming from generated IDL. (Like *TypeSupportC.h)
Definition: dds_generator.h:41
const char *const name
Definition: debug.cpp:60
BE_GlobalData * be_global
Definition: be_global.cpp:43
size_type length(void) const
std::string idl_template_
Definition: ts_generator.h:40
static std::string scoped_helper(UTL_ScopedName *sn, const char *sep, EscapeContext cxt=EscapeContext_Normal)

Member Data Documentation

◆ idl_template_

std::string ts_generator::idl_template_
private

Definition at line 40 of file ts_generator.h.

Referenced by generate_ts().


The documentation for this class was generated from the following files: