OpenDDS  Snapshot(2023/04/28-20:55)
Public Member Functions | Private Attributes | List of all members
metaclass_generator Class Reference

#include <metaclass_generator.h>

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

Public Member Functions

 metaclass_generator ()
 
bool gen_enum (AST_Enum *node, UTL_ScopedName *name, const std::vector< AST_EnumVal *> &contents, const char *repoid)
 
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 *node, UTL_ScopedName *name, AST_Type *type, const char *repoid)
 
bool gen_union (AST_Union *node, UTL_ScopedName *name, const std::vector< AST_UnionBranch *> &branches, AST_Type *type, const char *repoid)
 
- 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_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 Attributes

bool first_struct_
 

Additional Inherited Members

- Static Public Member Functions inherited from dds_generator
static std::string get_tag_name (const std::string &base_name, const std::string &qualifier="")
 
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 13 of file metaclass_generator.h.

Constructor & Destructor Documentation

◆ metaclass_generator()

metaclass_generator::metaclass_generator ( )
inline

Definition at line 15 of file metaclass_generator.h.

References gen_enum(), gen_struct(), gen_typedef(), gen_union(), and name.

16  : first_struct_(true)
17  {}

Member Function Documentation

◆ gen_enum()

bool metaclass_generator::gen_enum ( AST_Enum *  node,
UTL_ScopedName *  name,
const std::vector< AST_EnumVal *> &  contents,
const char *  repoid 
)
virtual

Reimplemented from dds_generator.

Definition at line 31 of file metaclass_generator.cpp.

References be_global, ACE_String_Base< char >::c_str(), canonical_name(), AstTypeClassification::CL_ARRAY, AstTypeClassification::CL_ENUM, AstTypeClassification::CL_SCALAR, AstTypeClassification::CL_SEQUENCE, AstTypeClassification::CL_STRING, AstTypeClassification::CL_STRUCTURE, AstTypeClassification::CL_UNION, AstTypeClassification::classify(), TopicKeys::count(), extensibilitykind_mutable, marshal_generator::gen_field_getValueFromSerialized(), NestedForLoops::index_, OpenDDS::XTypes::key_count(), name, AstTypeClassification::resolveActualType(), scoped(), dds_generator::scoped_helper(), and string_type().

Referenced by metaclass_generator().

33 {
34  NamespaceGuard ng;
35  std::string array_decl = "const char* gen_" + scoped_helper(name, "_") + "_names[]";
36  std::string size_decl = "const size_t gen_" + scoped_helper(name, "_") + "_names_size";
37  std::string decl_prefix = ((be_global->export_macro() == "") ? std::string("extern ") : (std::string(be_global->export_macro().c_str()) + " extern "));
38  be_global->header_ << decl_prefix << array_decl << ";\n";
39  be_global->header_ << decl_prefix << size_decl << ";\n";
40  be_global->impl_ << array_decl << " = {\n";
41  for (size_t i = 0; i < contents.size(); ++i) {
42  be_global->impl_ << " \"" << canonical_name(contents[i])
43  << ((i < contents.size() - 1) ? "\",\n" : "\"\n");
44  }
45  be_global->impl_ << "};\n";
46  be_global->impl_ << size_decl << " = " << contents.size() << ";\n";
47  return true;
48 }
static std::string scoped_helper(UTL_ScopedName *sn, const char *sep, EscapeContext cxt=EscapeContext_Normal)
const char * c_str(void) const
std::string canonical_name(UTL_ScopedName *sn)
const char *const name
Definition: debug.cpp:60
BE_GlobalData * be_global
Definition: be_global.cpp:44

◆ gen_struct()

bool metaclass_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 495 of file metaclass_generator.cpp.

References Function::addArg(), be_global, Function::endArgs(), generate_anon_fields(), and scoped().

Referenced by metaclass_generator().

497 {
498  const std::string clazz = scoped(name);
499 
500  be_global->add_include("dds/DCPS/PoolAllocator.h",
501  BE_GlobalData::STREAM_CPP);
502 
503  ContentSubscriptionGuard csg;
504  NamespaceGuard ng;
505 
506  if (!generate_metaclass(node, name, fields, first_struct_, clazz)) {
507  return false;
508  }
509 
510  generate_anon_fields(node);
511 
512  {
513  Function f("gen_skip_over", "bool");
514  f.addArg("ser", "Serializer&");
515  f.addArg("", clazz + "*");
516  f.endArgs();
517  be_global->impl_ <<
518  " MetaStructImpl<" << clazz << ">().getValue(ser, \"\");\n"
519  " return true;\n";
520  }
521  return true;
522 }
const char *const name
Definition: debug.cpp:60
std::string scoped(UTL_ScopedName *sn, EscapeContext ec=EscapeContext_Normal)
BE_GlobalData * be_global
Definition: be_global.cpp:44
void generate_anon_fields(AST_Structure *node)

◆ gen_typedef()

bool metaclass_generator::gen_typedef ( AST_Typedef *  node,
UTL_ScopedName *  name,
AST_Type *  type,
const char *  repoid 
)
virtual

Implements dds_generator.

Definition at line 525 of file metaclass_generator.cpp.

References Function::addArg(), array_element_count(), be_global, AstTypeClassification::CL_ARRAY, AstTypeClassification::CL_ENUM, AstTypeClassification::CL_PRIMITIVE, AstTypeClassification::CL_SEQUENCE, AstTypeClassification::CL_STRING, AstTypeClassification::CL_STRUCTURE, AstTypeClassification::classify(), deepest_named_type(), Function::endArgs(), marshal_generator::generate_dheader_code(), dds_generator::get_tag_name(), name, AstTypeClassification::resolveActualType(), scoped(), and to_cxx_type().

Referenced by metaclass_generator().

527 {
528  AST_Array* arr = dynamic_cast<AST_Array*>(type);
529  AST_Sequence* seq = 0;
530  if (!arr && !(seq = dynamic_cast<AST_Sequence*>(type))) {
531  return true;
532  }
533  const bool use_cxx11 = be_global->language_mapping() == BE_GlobalData::LANGMAP_CXX11;
534  const std::string clazz = scoped(name);
535 
536  ContentSubscriptionGuard csg;
537  NamespaceGuard ng;
538  Function f("gen_skip_over", "bool");
539  f.addArg("ser", "Serializer&");
540  if (use_cxx11) {
541  f.addArg("", "IDL::DistinctType<" + clazz + ", " + dds_generator::get_tag_name(clazz) +">*");
542  } else {
543  f.addArg("", clazz + (arr ? "_forany*" : "*"));
544  }
545  f.endArgs();
546 
547  AST_Type* elem;
548  if (seq != 0) {
549  elem = seq->base_type();
550  } else {
551  elem = arr->base_type();
552  }
553  be_global->impl_ <<
554  " const Encoding& encoding = ser.encoding();\n"
555  " ACE_UNUSED_ARG(encoding);\n";
556  Classification elem_cls = classify(elem);
557  const bool primitive = elem_cls & CL_PRIMITIVE;
559  " if (!ser.read_delimiter(total_size)) {\n"
560  " return false;\n"
561  " }\n", !primitive, true);
562 
563  std::string len;
564 
565  if (arr) {
566  std::ostringstream strstream;
567  strstream << array_element_count(arr);
568  len = strstream.str();
569  } else { // Sequence
570  be_global->impl_ <<
571  " ACE_CDR::ULong length;\n"
572  " if (!(ser >> length)) return false;\n";
573  len = "length";
574  }
575 
576  const std::string cxx_elem = scoped(elem->name());
577  AST_Type* elem_orig = elem;
578  elem = resolveActualType(elem);
579  elem_cls = classify(elem);
580 
581  if ((elem_cls & (CL_PRIMITIVE | CL_ENUM))) {
582  // fixed-length sequence/array element -> skip all elements at once
583  size_t sz = 0;
584  to_cxx_type(elem, sz);
585  be_global->impl_ <<
586  " return ser.skip(" << len << ", " << sz << ");\n";
587  } else {
588  be_global->impl_ <<
589  " for (ACE_CDR::ULong i = 0; i < " << len << "; ++i) {\n";
590  if (elem_cls & CL_STRING) {
591  be_global->impl_ <<
592  " ACE_CDR::ULong strlength;\n"
593  " if (!(ser >> strlength && ser.skip(strlength))) return false;\n";
594  } else if (elem_cls & (CL_ARRAY | CL_SEQUENCE | CL_STRUCTURE)) {
595  std::string pre, post;
596  if (!use_cxx11 && (elem_cls & CL_ARRAY)) {
597  post = "_forany";
598  } else if (use_cxx11 && (elem_cls & (CL_ARRAY | CL_SEQUENCE))) {
599  pre = "IDL::DistinctType<";
600  post = ", " + dds_generator::get_tag_name(scoped_helper(deepest_named_type(elem_orig)->name(), "::")) + ">";
601  }
602  be_global->impl_ <<
603  " if (!gen_skip_over(ser, static_cast<" << pre << cxx_elem << post
604  << "*>(0))) return false;\n";
605  }
606  be_global->impl_ <<
607  " }\n"
608  " return true;\n";
609  }
610 
611  return true;
612 }
std::string to_cxx_type(AST_Type *type, std::size_t &size)
Classification classify(AST_Type *type)
static std::string scoped_helper(UTL_ScopedName *sn, const char *sep, EscapeContext cxt=EscapeContext_Normal)
const Classification CL_STRING
const Classification CL_PRIMITIVE
static std::string get_tag_name(const std::string &base_name, const std::string &qualifier="")
AST_Type * deepest_named_type(AST_Type *type)
const Classification CL_ARRAY
AST_Type * resolveActualType(AST_Type *element)
static void generate_dheader_code(const std::string &code, bool dheader_required, bool is_ser_func=true)
const char *const name
Definition: debug.cpp:60
std::string scoped(UTL_ScopedName *sn, EscapeContext ec=EscapeContext_Normal)
const Classification CL_STRUCTURE
BE_GlobalData * be_global
Definition: be_global.cpp:44
ACE_CDR::ULong array_element_count(AST_Array *arr)
const Classification CL_ENUM
const Classification CL_SEQUENCE

◆ gen_union()

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

Implements dds_generator.

Definition at line 654 of file metaclass_generator.cpp.

References Function::addArg(), be_global, Function::endArgs(), extensibilitykind_final, extensibilitykind_mutable, gen_union_branch(), generate_anon_fields(), marshal_generator::generate_dheader_code(), generateSwitchForUnion(), getWrapper(), scoped(), and WD_INPUT.

Referenced by metaclass_generator().

657 {
658  const std::string clazz = scoped(name);
659 
660  ContentSubscriptionGuard csg;
661  NamespaceGuard ng;
662 
663  std::vector<AST_Field*> dummy_field_list;
664  if (!generate_metaclass(node, name, dummy_field_list, first_struct_, clazz)) {
665  return false;
666  }
667 
668  generate_anon_fields(node);
669 
670  {
671  Function f("gen_skip_over", "bool");
672  f.addArg("ser", "Serializer&");
673  f.addArg("", clazz + "*");
674  f.endArgs();
675 
676  const ExtensibilityKind exten = be_global->extensibility(node);
677  const bool not_final = exten != extensibilitykind_final;
678  const bool is_mutable = exten == extensibilitykind_mutable;
679  be_global->impl_ <<
680  " const Encoding& encoding = ser.encoding();\n"
681  " ACE_UNUSED_ARG(encoding);\n"
682  " const bool is_mutable = " << is_mutable <<";\n"
683  " unsigned member_id;\n"
684  " size_t field_size;\n"
685  " bool must_understand = false;\n";
687  " if (!ser.read_delimiter(total_size)) {\n"
688  " return false;\n"
689  " }\n", not_final);
690  if (is_mutable) {
691  be_global->impl_ <<
692  " if (!ser.read_parameter_id(member_id, field_size, must_understand)) {\n"
693  " return false;\n"
694  " }\n";
695  }
696  be_global->impl_ <<
697  " " << scoped(discriminator->name()) << " disc;\n"
698  " if (!(ser >> " << getWrapper("disc", discriminator, WD_INPUT) << ")) {\n"
699  " return false;\n"
700  " }\n";
701  if (generateSwitchForUnion(node, "disc", gen_union_branch, branches, discriminator, "", "", "",
702  false, true, false)) {
703  be_global->impl_ <<
704  " return true;\n";
705  }
706  }
707  return true;
708 }
ExtensibilityKind
Definition: annotations.h:278
static std::string gen_union_branch(const std::string &, AST_Decl *branch, const std::string &, AST_Type *br_type, const std::string &, bool, Intro &, const std::string &)
bool generateSwitchForUnion(AST_Union *u, const char *switchExpr, CommonFn commonFn, const std::vector< AST_UnionBranch *> &branches, AST_Type *discriminator, const char *statementPrefix, const char *namePrefix="", const char *uni="", bool forceDisableDefault=false, bool parens=true, bool breaks=true, CommonFn commonFn2=0)
returns true if a default: branch was generated (no default: label in IDL)
std::string getWrapper(const std::string &name, AST_Type *type, WrapDirection wd)
static void generate_dheader_code(const std::string &code, bool dheader_required, bool is_ser_func=true)
const char *const name
Definition: debug.cpp:60
std::string scoped(UTL_ScopedName *sn, EscapeContext ec=EscapeContext_Normal)
BE_GlobalData * be_global
Definition: be_global.cpp:44
void generate_anon_fields(AST_Structure *node)

Member Data Documentation

◆ first_struct_

bool metaclass_generator::first_struct_
private

Definition at line 33 of file metaclass_generator.h.


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