11 #include <utl_identifier.h> 20 DynamicDataAdapterGuard()
28 NoSafetyProfileGuard()
34 std::string op(
bool set)
36 return set ?
"set" :
"get";
39 bool access_details(
bool set, AST_Decl* node, AST_Type* type, std::string& op_type,
42 const bool cxx11 =
be_global->language_mapping() == BE_GlobalData::LANGMAP_CXX11;
44 AST_Type*
const node_as_type =
dynamic_cast<AST_Type*
>(node);
45 switch (t->node_type()) {
46 case AST_Decl::NT_pre_defined:
47 if (
set && cxx11 && node_as_type &&
49 dynamic_cast<AST_PredefinedType*
>(t)->pt() == AST_PredefinedType::PT_boolean) {
51 op_type =
"bool_vector_elem";
56 case AST_Decl::NT_enum:
59 case AST_Decl::NT_string:
64 extra_access =
".inout()";
69 case AST_Decl::NT_wstring:
71 op_type =
"cpp11_s16";
74 extra_access =
".inout()";
79 case AST_Decl::NT_struct:
80 case AST_Decl::NT_union:
81 case AST_Decl::NT_sequence:
83 op_type =
set ?
"direct_complex" :
"complex";
85 case AST_Decl::NT_array:
88 op_type = cxx11 ?
"direct_complex" :
"indirect_complex";
98 void generate_op(
unsigned indent,
bool set, AST_Type* type,
const std::string& type_name,
99 const std::string& op_type,
bool is_complex,
const std::string&
value,
100 const char* rc_dest =
"return")
103 type_wrapper.dynamic_data_adapter_ =
true;
105 std::string template_params;
106 if (type_wrapper.needs_dda_tag()) {
107 template_params =
"<" + type_name +
", " + type_wrapper.get_tag_name() +
">";
108 }
else if (is_complex) {
109 template_params =
"<" + type_name +
", " + type_name +
">";
113 std::string(indent * 2,
' ') << rc_dest <<
" " <<
114 op(
set) <<
"_" << op_type <<
"_raw_value" << template_params <<
"(method, ";
116 be_global->impl_ << value <<
", id, source, tk";
118 be_global->impl_ <<
"dest, tk, " << value <<
", id";
123 void generate_dynamic_data_adapter_access_field(
124 AST_Union* union_node,
bool set,
127 const bool use_cxx11 =
be_global->language_mapping() == BE_GlobalData::LANGMAP_CXX11;
129 const std::string cpp_field_name = disc ?
"_d" : field->local_name()->get_string();
134 be_global->impl_ <<
"OpenDDS::XTypes::DISCRIMINATOR_ID";
142 std::string extra_access;
143 bool is_complex =
false;
144 if ((field && !
be_global->dynamic_data_adapter(field->field_type())) ||
145 !access_details(
set, field, field_type, op_type, extra_access, is_complex)) {
147 " ACE_UNUSED_ARG(" << (
set ?
"source" :
"dest") <<
");\n" 148 " ACE_UNUSED_ARG(tk);\n" 149 " return missing_dda(method, id);\n";
153 std::string value =
"value_.";
154 const char* rc_dest =
"return";
159 " " << type_name <<
" temp;\n";
163 value += cpp_field_name +
"()";
166 value = std::string(
"*reinterpret_cast<") + type_name +
"*>(" + value +
")";
170 value += (use_cxx11 ?
"_" :
"") + cpp_field_name;
172 generate_op(4,
set, field_type, type_name, op_type, is_complex,
173 value + extra_access, rc_dest);
174 if (union_node &&
set) {
176 " if (rc == DDS::RETCODE_OK) {\n" 177 " value_." << cpp_field_name <<
"(temp);\n" 187 bool generate_dynamic_data_adapter_access(AST_Decl* node,
RefWrapper& wrapper,
bool set)
189 AST_Structure* struct_node = 0;
190 AST_Union* union_node = 0;
191 AST_Sequence* seq_node = 0;
192 AST_Array* array_node = 0;
196 switch (node->node_type()) {
197 case AST_Decl::NT_struct:
198 struct_node =
dynamic_cast<AST_Structure*
>(node);
200 case AST_Decl::NT_union:
201 union_node =
dynamic_cast<AST_Union*
>(node);
203 case AST_Decl::NT_sequence:
204 seq_node =
dynamic_cast<AST_Sequence*
>(node);
206 case AST_Decl::NT_array:
207 array_node =
dynamic_cast<AST_Array*
>(node);
213 be_global->impl_ <<
" DDS::ReturnCode_t " << op(
set) <<
"_raw_value(const char* method, ";
215 be_global->impl_ <<
"DDS::MemberId id, const void* source, DDS::TypeKind tk";
217 be_global->impl_ <<
"void* dest, DDS::TypeKind tk, DDS::MemberId id";
224 " DDS::ReturnCode_t rc = assert_mutable(method);\n" 225 " if (rc != DDS::RETCODE_OK) {\n" 230 if (struct_node || union_node) {
235 generate_dynamic_data_adapter_access_field(
239 const Fields fields(union_node ? union_node : struct_node);
241 AST_Field*
const field = *i;
242 generate_dynamic_data_adapter_access_field(
243 union_node,
set,
be_global->get_id(field), field->field_type(), field);
248 " return invalid_id(method, id);\n" 250 }
else if (seq_node || array_node) {
251 AST_Type*
const base_type = seq_node ? seq_node->base_type() : array_node->base_type();
255 std::string extra_access;
256 bool is_complex =
false;
257 if (access_details(
set, node, base_type, op_type, extra_access, is_complex)) {
260 be_global->impl_ <<
"const DDS::ReturnCode_t ";
262 be_global->impl_ <<
"rc = check_index(method, id, ";
269 " if (rc != DDS::RETCODE_OK) {\n" 272 generate_op(2,
set, base_type,
scoped(named_type->name()), op_type, is_complex,
276 " ACE_UNUSED_ARG(" << (
set ?
"source" :
"dest") <<
");\n" 277 " ACE_UNUSED_ARG(tk);\n" 278 " return missing_dda(method, id);\n";
290 void generate_get_dynamic_data_adapter(
291 const std::string& cpp_name,
const std::string& tag,
const std::string& export_macro,
292 bool is_const,
bool dda_generated)
296 export_macro <<
"DDS::DynamicData_ptr get_dynamic_data_adapter<" << cpp_name << tag <<
297 ">(DDS::DynamicType_ptr type, " << (is_const ?
"const " :
"") << cpp_name <<
"& value);\n" 302 "DDS::DynamicData_ptr get_dynamic_data_adapter<" << cpp_name << tag <<
303 ">(DDS::DynamicType_ptr type, " << (is_const ?
"const " :
"") << cpp_name <<
"& value)\n" 307 "# if OPENDDS_HAS_DYNAMIC_DATA_ADAPTER\n" 309 " return new DynamicDataAdapterImpl<" << cpp_name << tag <<
">(type, value);\n" 314 " ACE_UNUSED_ARG(type);\n" 315 " ACE_UNUSED_ARG(value);\n";
326 template <
typename Type>
327 bool is_collection_of_interface_or_value_type(Type* type)
329 const AST_Decl::NodeType kind =
resolveActualType(type->base_type())->node_type();
330 return kind == AST_Decl::NT_interface || kind == AST_Decl::NT_valuetype;
333 bool generate_dynamic_data_adapter(
334 AST_Decl* node,
const std::string* use_scoped_name = 0, AST_Typedef* typedef_node = 0)
336 AST_Structure* struct_node = 0;
337 AST_Union* union_node = 0;
338 AST_Sequence* seq_node = 0;
339 AST_Array* array_node = 0;
343 switch (node->node_type()) {
344 case AST_Decl::NT_struct:
345 struct_node =
dynamic_cast<AST_Structure*
>(node);
347 case AST_Decl::NT_union:
348 union_node =
dynamic_cast<AST_Union*
>(node);
350 case AST_Decl::NT_sequence:
351 seq_node =
dynamic_cast<AST_Sequence*
>(node);
352 if (is_collection_of_interface_or_value_type(seq_node)) {
356 case AST_Decl::NT_array:
357 array_node =
dynamic_cast<AST_Array*
>(node);
358 if (is_collection_of_interface_or_value_type(array_node)) {
365 const std::string cpp_name = use_scoped_name ? *use_scoped_name :
scoped(node->name());
366 const bool generate =
be_global->dynamic_data_adapter(typedef_node ? typedef_node : node);
367 RefWrapper wrapper(dynamic_cast<AST_Type*>(node), cpp_name,
"value_",
false);
371 be_global->add_include(
"dds/DCPS/XTypes/DynamicDataAdapter.h", BE_GlobalData::STREAM_H);
373 if (struct_node || union_node) {
374 const Fields fields(union_node ? union_node : struct_node);
377 AST_Field*
const field = *i;
378 if (field->field_type()->anonymous()) {
380 if (af.arr_ || (af.seq_ && af.is_new(anonymous_seq_generated))) {
381 if (!generate_dynamic_data_adapter(af.type_, &af.scoped_type_)) {
383 "Failed to generate adapter for anonymous type of field", field);
390 std::vector<std::string> ns;
391 ns.push_back(
"OpenDDS");
392 ns.push_back(
"XTypes");
399 tag =
", " + cpp_name;
403 DynamicDataAdapterGuard ddag;
407 "class DynamicDataAdapterImpl<" << cpp_name << tag <<
" > : public DynamicDataAdapter_T<" 408 << cpp_name <<
"> {\n" 410 " DynamicDataAdapterImpl(DDS::DynamicType_ptr type, " << cpp_name <<
"& value)\n" 411 " : DynamicDataAdapter_T<" << cpp_name <<
">(type, value)\n" 415 " DynamicDataAdapterImpl(DDS::DynamicType_ptr type, const " << cpp_name <<
"& value)\n" 416 " : DynamicDataAdapter_T<" << cpp_name <<
">(type, value)\n" 420 if (struct_node || seq_node || array_node) {
422 " DDS::UInt32 get_item_count()\n" 426 be_global->impl_ << struct_node->nfields();
427 }
else if (seq_node) {
429 }
else if (array_node) {
440 " DDS::MemberId get_member_id_at_index_impl(DDS::UInt32 index)\n" 442 " const DDS::UInt32 count = " << wrapper.
seq_get_length() <<
";\n" 443 " if (!read_only_ && index >= count) {\n" 447 " return check_index(\"get_member_id_at_index\", index, count)" 448 " == DDS::RETCODE_OK ? index : MEMBER_ID_INVALID;\n" 456 if (!generate_dynamic_data_adapter_access(node, wrapper,
false)) {
460 if (!generate_dynamic_data_adapter_access(node, wrapper,
true)) {
470 NoSafetyProfileGuard nspg;
475 if (export_macro.size()) {
479 generate_get_dynamic_data_adapter(cpp_name, tag, export_macro,
true, generate);
480 generate_get_dynamic_data_adapter(cpp_name, tag, export_macro,
false, generate);
484 export_macro <<
"const " << cpp_name <<
"* get_dynamic_data_adapter_value<" <<
485 cpp_name << tag <<
">(DDS::DynamicData_ptr dd);\n" 490 "const " << cpp_name <<
"* get_dynamic_data_adapter_value<" <<
491 cpp_name << tag <<
">(DDS::DynamicData_ptr dd)\n" 493 " ACE_UNUSED_ARG(dd);\n";
496 "# if OPENDDS_HAS_DYNAMIC_DATA_ADAPTER\n" 497 " typedef DynamicDataAdapterImpl<" << cpp_name << tag <<
"> Dda;\n" 498 " const Dda* const dda = dynamic_cast<Dda*>(dd);\n" 500 " return &dda->wrapped();\n" 514 const std::vector<AST_Field*>&, AST_Type::SIZE_TYPE,
const char*)
516 return generate_dynamic_data_adapter(node);
520 AST_Type* type,
const char*)
522 const AST_Decl::NodeType nt = type->node_type();
523 if (nt != AST_Decl::NT_sequence && nt != AST_Decl::NT_array) {
526 const std::string cpp_name =
scoped(name);
527 return generate_dynamic_data_adapter(type, &cpp_name, typedef_node);
531 const std::vector<AST_UnionBranch*>&, AST_Type*,
const char*)
533 return generate_dynamic_data_adapter(node);
Classification classify(AST_Type *type)
const char * c_str(void) const
bool is_complex(TypeKind tk)
const LogLevel::Value value
std::string get_tag_name() const
std::string field_type_name(AST_Field *field, AST_Type *field_type)
std::string seq_get_length() const
const ACE_CDR::ULong DISCRIMINATOR_ID
Implementation specific sentinel for a union discriminator used in DynamicData.
AST_Type * deepest_named_type(AST_Type *type)
const Classification CL_ARRAY
AST_Type * resolveActualType(AST_Type *element)
bool gen_struct(AST_Structure *node, UTL_ScopedName *name, const std::vector< AST_Field *> &fields, AST_Type::SIZE_TYPE size, const char *repoid)
std::string flat_collection_access(std::string index) const
std::string scoped(UTL_ScopedName *sn, EscapeContext ec=EscapeContext_Normal)
std::string seq_resize(const std::string &new_size) const
std::set< EleLen > EleLenSet
BE_GlobalData * be_global
ACE_CDR::ULong array_element_count(AST_Array *arr)
void generate_tag() const
bool needs_dda_tag() const
RefWrapper & done(Intro *intro=0)
bool dynamic_data_adapter_
void misc_error_and_abort(const std::string &message, AST_Decl *node=0)
Report a miscellaneous error and abort.
bool gen_union(AST_Union *node, UTL_ScopedName *name, const std::vector< AST_UnionBranch *> &branches, AST_Type *type, const char *repoid)
void generate(AST_Structure *node)
called directly by dds_visitor::visit_structure() if -Wb,java
bool gen_typedef(AST_Typedef *node, UTL_ScopedName *name, AST_Type *type, const char *repoid)