OpenDDS  Snapshot(2023/04/07-19:43)
Public Member Functions | Public Attributes | List of all members
GeneratorBase::GenerateUnionAccessors Struct Reference

Public Member Functions

 GenerateUnionAccessors (AST_Union *u, AST_Type *d)
 
void operator() (AST_UnionBranch *branch)
 

Public Attributes

AST_Union * the_union
 
AST_Type * discriminator
 

Detailed Description

Definition at line 288 of file langmap_generator.cpp.

Constructor & Destructor Documentation

◆ GenerateUnionAccessors()

GeneratorBase::GenerateUnionAccessors::GenerateUnionAccessors ( AST_Union *  u,
AST_Type *  d 
)
inline

Definition at line 293 of file langmap_generator.cpp.

Member Function Documentation

◆ operator()()

void GeneratorBase::GenerateUnionAccessors::operator() ( AST_UnionBranch *  branch)
inline

Definition at line 298 of file langmap_generator.cpp.

References be_global, AstTypeClassification::CL_ARRAY, AstTypeClassification::CL_ENUM, AstTypeClassification::CL_FIXED, AstTypeClassification::CL_PRIMITIVE, AstTypeClassification::CL_SEQUENCE, AstTypeClassification::CL_STRING, AstTypeClassification::CL_STRUCTURE, AstTypeClassification::CL_UNION, AstTypeClassification::CL_WIDE, AstTypeClassification::classify(), getEnumLabel(), and AstTypeClassification::resolveActualType().

299  {
300  const char* field_name = branch->local_name()->get_string();
301  std::stringstream first_label;
302  {
303  AST_UnionLabel* label = branch->label(0);
304  if (label->label_kind() == AST_UnionLabel::UL_default) {
305  first_label << generateDefaultValue(the_union);
306  } else if (discriminator->node_type() == AST_Decl::NT_enum) {
307  first_label << getEnumLabel(label->label_val(), discriminator);
308  } else {
309  first_label << *label->label_val()->ev();
310  }
311  }
312 
313  AST_Type* field_type = branch->field_type();
314  const std::string field_type_string = generator_->map_type(field_type);
315  AST_Type* actual_field_type = resolveActualType(field_type);
316  const Classification cls = classify(actual_field_type);
317  if (cls & (CL_PRIMITIVE | CL_ENUM)) {
318  be_global->lang_header_ <<
319  " void " << field_name << " (" << field_type_string << " x) {\n"
320  " _reset();\n"
321  " this->_u." << field_name << " = x;\n"
322  " _discriminator = " << first_label.str() << ";\n"
323  " }\n"
324  " " << field_type_string << ' ' << field_name << " () const {\n"
325  " return this->_u." << field_name << ";\n"
326  " }\n";
327  } else if (cls & CL_STRING) {
328  const std::string& primtype = (cls & CL_WIDE) ? primtype_[AST_PredefinedType::PT_wchar] : primtype_[AST_PredefinedType::PT_char];
329  const std::string& helper = (cls & CL_WIDE) ? helpers_[HLP_WSTR_VAR] : helpers_[HLP_STR_VAR];
330  be_global->lang_header_ <<
331  " void " << field_name << " (" << primtype << "* x) {\n"
332  " _reset();\n" <<
333  " this->_u." << field_name << " = x;\n"
334  " _discriminator = " << first_label.str() << ";\n"
335  " }\n"
336  " void " << field_name << " (const " << primtype << "* x) {\n"
337  " _reset();\n"
338  " this->_u." << field_name << " = " << string_ns << "::string_dup(x);\n"
339  " _discriminator = " << first_label.str() << ";\n"
340  " }\n"
341  " void " << field_name << " (const " << helper << "& x) {\n"
342  " _reset();\n" <<
343  " this->_u." << field_name << " = " << string_ns << "::string_dup(x.in());\n"
344  " _discriminator = " << first_label.str() << ";\n"
345  " }\n"
346  " const " << primtype << "* " << field_name << " () const {\n"
347  " return this->_u." << field_name << ";\n"
348  " }\n";
349  } else if (cls & CL_ARRAY) {
350  be_global->lang_header_ <<
351  " void " << field_name << " (" << field_type_string << " x) {\n"
352  " _reset();\n" <<
353  " this->_u." << field_name << " = " << field_type_string << "_dup(x);\n"
354  " _discriminator = " << first_label.str() << ";\n"
355  " }\n"
356  " " << field_type_string << "_slice* " << field_name << " () const {\n"
357  " return this->_u." << field_name << ";\n"
358  " }\n";
359  } else if (cls & (CL_STRUCTURE | CL_UNION | CL_SEQUENCE | CL_FIXED)) {
360  be_global->lang_header_ <<
361  " void " << field_name << " (const " << field_type_string << "& x) {\n"
362  " _reset();\n"
363  " this->_u." << field_name << " = new " << field_type_string << "(x);\n"
364  " _discriminator = " << first_label.str() << ";\n"
365  " }\n"
366  " const " << field_type_string << "& " << field_name << " () const {\n"
367  " return *this->_u." << field_name << ";\n"
368  " }\n"
369  " " << field_type_string << "& " << field_name << " () {\n"
370  " return *this->_u." << field_name << ";\n"
371  " }\n";
372  } else {
373  std::cerr << "Unsupported type for union element\n";
374  }
375  }
const Classification CL_ARRAY
const Classification CL_FIXED
const Classification CL_UNION
const Classification CL_STRUCTURE
AST_Type * resolveActualType(AST_Type *element)
const Classification CL_PRIMITIVE
Classification classify(AST_Type *type)
const Classification CL_STRING
const Classification CL_ENUM
const Classification CL_SEQUENCE
std::string getEnumLabel(AST_Expression *label_val, AST_Type *disc)
static std::string generateDefaultValue(AST_Union *the_union)
BE_GlobalData * be_global
Definition: be_global.cpp:43
const Classification CL_WIDE

Member Data Documentation

◆ discriminator

AST_Type* GeneratorBase::GenerateUnionAccessors::discriminator

Definition at line 291 of file langmap_generator.cpp.

◆ the_union

AST_Union* GeneratorBase::GenerateUnionAccessors::the_union

Definition at line 290 of file langmap_generator.cpp.


The documentation for this struct was generated from the following file: