9 #include "global_extern.h" 13 #include <utl_identifier.h> 14 #include <utl_labellist.h> 15 #include <ast_fixed.h> 21 void generate_write(
const std::string& expression, AST_Type* type,
const std::string& idx,
int level = 1);
23 std::string primitive_type(AST_PredefinedType::PredefinedType pt)
26 case AST_PredefinedType::PT_long:
28 case AST_PredefinedType::PT_ulong:
30 case AST_PredefinedType::PT_longlong:
32 case AST_PredefinedType::PT_ulonglong:
34 case AST_PredefinedType::PT_short:
36 case AST_PredefinedType::PT_ushort:
38 #if OPENDDS_HAS_EXPLICIT_INTS 39 case AST_PredefinedType::PT_int8:
41 case AST_PredefinedType::PT_uint8:
44 case AST_PredefinedType::PT_float:
46 case AST_PredefinedType::PT_double:
48 case AST_PredefinedType::PT_longdouble:
50 case AST_PredefinedType::PT_char:
52 case AST_PredefinedType::PT_wchar:
54 case AST_PredefinedType::PT_boolean:
56 case AST_PredefinedType::PT_octet:
63 void array_helper(
const std::string& expression, AST_Array* array,
64 size_t dim_idx,
const std::string& idx,
int level)
66 const bool use_cxx11 =
be_global->language_mapping() == BE_GlobalData::LANGMAP_CXX11;
67 const std::string indent(level * 2,
' ');
73 if ((primitive && (dim_idx < array->n_dims() - 1)) || (!primitive && (dim_idx < array->n_dims()))) {
74 const size_t dim = array->dims()[dim_idx]->ev()->u.ulval;
76 indent <<
"value_writer.begin_array();\n";
78 indent <<
"for (" << (use_cxx11 ?
"size_t " :
"::CORBA::ULong ") << idx <<
" = 0; " 79 << idx <<
" != " << dim <<
"; ++" << idx <<
") {\n" <<
80 indent <<
" value_writer.begin_element(" << idx <<
");\n";
81 array_helper(expression +
"[" + idx +
"]", array, dim_idx + 1, idx +
"i", level + 1);
83 indent <<
" value_writer.end_element();\n" <<
85 indent <<
"value_writer.end_array();\n";
88 const size_t dim = array->dims()[dim_idx]->ev()->u.ulval;
90 const AST_PredefinedType::PredefinedType pt =
91 dynamic_cast<AST_PredefinedType*
>(actual)->pt();
93 indent <<
"value_writer.begin_array();\n";
95 "value_writer.write_" << primitive_type(pt) <<
"_array (" << expression << (use_cxx11 ?
".data()" :
"") <<
", " << dim <<
");\n";
97 indent <<
"value_writer.end_array();\n";
100 generate_write(expression, array->base_type(), idx +
"i", level);
105 void sequence_helper(
const std::string& expression, AST_Sequence* sequence,
106 const std::string& idx,
int level)
108 const bool use_cxx11 =
be_global->language_mapping() == BE_GlobalData::LANGMAP_CXX11;
109 const char*
const length_func = use_cxx11 ?
"size" :
"length";
110 const std::string indent(level * 2,
' ');
111 be_global->impl_ << indent <<
"value_writer.begin_sequence();\n";
115 bool use_optimized_write_ =
false;
116 if (c & CL_PRIMITIVE) {
118 const AST_PredefinedType::PredefinedType pt =
dynamic_cast<AST_PredefinedType*
>(actual)->pt();
119 use_optimized_write_ = !(pt == AST_PredefinedType::PT_boolean);
121 use_optimized_write_ =
true;
125 if (use_optimized_write_) {
126 const AST_PredefinedType::PredefinedType pt =
127 dynamic_cast<AST_PredefinedType*
>(actual)->pt();
129 "value_writer.write_" << primitive_type(pt) <<
"_array (" << expression << (use_cxx11 ?
".data()" :
".get_buffer()") <<
", " << expression <<
"." << length_func <<
"());\n";
132 indent <<
"for (" << (use_cxx11 ?
"size_t " :
"::CORBA::ULong ") << idx <<
" = 0; " 133 << idx <<
" != " << expression <<
"." << length_func <<
"(); ++" << idx <<
") {\n" <<
134 indent <<
" value_writer.begin_element(" << idx <<
");\n";
135 generate_write(expression +
"[" + idx +
"]", sequence->base_type(), idx +
"i", level + 1);
137 indent <<
" value_writer.end_element();\n" <<
142 indent <<
"value_writer.end_sequence();\n";
145 void generate_write(
const std::string& expression, AST_Type* type,
const std::string& idx,
int level)
151 AST_Sequence*
const sequence =
dynamic_cast<AST_Sequence*
>(actual);
152 sequence_helper(expression, sequence, idx, level);
156 AST_Array*
const array =
dynamic_cast<AST_Array*
>(actual);
157 array_helper(expression, array, 0, idx, level);
161 be_global->impl_ << std::string(level * 2,
' ');
165 "value_writer.write_fixed(" << expression <<
");\n";
169 "value_writer.write_" << ((c &
CL_WIDE) ?
"w" :
"") <<
"string(" << expression <<
");\n";
171 }
else if (c & CL_PRIMITIVE) {
172 const AST_PredefinedType::PredefinedType pt =
173 dynamic_cast<AST_PredefinedType*
>(actual)->pt();
175 "value_writer.write_" << primitive_type(pt) <<
'(' << expression <<
");\n";
179 "vwrite(value_writer, " << expression <<
");\n";
183 std::string branch_helper(
const std::string&,
185 const std::string& field_name,
193 " value_writer.begin_union_member(\"" <<
canonical_name(branch) <<
"\");\n";
194 generate_write(
"value." + field_name +
"()", type,
"i", 2);
196 " value_writer.end_union_member();\n";
202 UTL_ScopedName*
name,
203 const std::vector<AST_EnumVal*>& contents,
206 be_global->add_include(
"dds/DCPS/ValueWriter.h", BE_GlobalData::STREAM_H);
208 const std::string type_name =
scoped(name);
209 const bool use_cxx11 =
be_global->language_mapping() == BE_GlobalData::LANGMAP_CXX11;
215 write.
addArg(
"value_writer",
"OpenDDS::DCPS::ValueWriter&");
216 write.
addArg(
"value",
"const " + type_name +
"&");
220 " switch (value) {\n";
221 for (std::vector<AST_EnumVal*>::const_iterator pos = contents.begin(), limit = contents.end();
222 pos != limit; ++pos) {
223 AST_EnumVal*
const val = *pos;
224 const std::string value_name = (use_cxx11 ? (type_name +
"::") :
module_scope(name))
225 + val->local_name()->get_string();
227 " case " << value_name <<
":\n" 228 " value_writer.write_enum(\"" <<
canonical_name(val) <<
"\", " << value_name <<
");\n" 246 UTL_ScopedName*
name,
247 const std::vector<AST_Field*>& fields,
251 be_global->add_include(
"dds/DCPS/ValueWriter.h", BE_GlobalData::STREAM_H);
253 const std::string type_name =
scoped(name);
254 const bool use_cxx11 =
be_global->language_mapping() == BE_GlobalData::LANGMAP_CXX11;
255 const std::string accessor_suffix = use_cxx11 ?
"()" :
"";
261 write.
addArg(
"value_writer",
"OpenDDS::DCPS::ValueWriter&");
262 write.
addArg(
"value",
"const " + type_name +
"&");
266 " value_writer.begin_struct();\n";
267 for (std::vector<AST_Field*>::const_iterator pos = fields.begin(), limit = fields.end();
268 pos != limit; ++pos) {
269 AST_Field*
const field = *pos;
270 const std::string field_name = field->local_name()->get_string();
273 " value_writer.begin_struct_member(XTypes::MemberDescriptorImpl(\"" << idl_name <<
"\", " 274 << (
be_global->is_key(field) ?
"true" :
"false") <<
"));\n";
275 generate_write(
"value." + field_name + accessor_suffix, field->field_type(),
"i");
277 " value_writer.end_struct_member();\n";
280 " value_writer.end_struct();\n";
288 UTL_ScopedName*
name,
289 const std::vector<AST_UnionBranch*>& branches,
290 AST_Type* discriminator,
293 be_global->add_include(
"dds/DCPS/ValueWriter.h", BE_GlobalData::STREAM_H);
295 const std::string type_name =
scoped(name);
301 write.
addArg(
"value_writer",
"OpenDDS::DCPS::ValueWriter&");
302 write.
addArg(
"value",
"const " + type_name +
"&");
306 " value_writer.begin_union();\n" 307 " value_writer.begin_discriminator();\n";
308 generate_write(
"value._d()" , discriminator,
"i");
310 " value_writer.end_discriminator();\n";
313 discriminator,
"",
"", type_name.c_str(),
316 " value_writer.end_union();\n";
Classification classify(AST_Type *type)
const Classification CL_STRING
const Classification CL_WIDE
bool gen_enum(AST_Enum *, UTL_ScopedName *name, const std::vector< AST_EnumVal *> &contents, const char *repoid)
const Classification CL_PRIMITIVE
const Classification CL_ARRAY
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 canonical_name(UTL_ScopedName *sn)
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_union(AST_Union *, UTL_ScopedName *, const std::vector< AST_UnionBranch *> &, AST_Type *, const char *)
AST_Type * resolveActualType(AST_Type *element)
const Classification CL_FIXED
std::string scoped(UTL_ScopedName *sn, EscapeContext ec=EscapeContext_Normal)
void addArg(const char *name, const std::string &type)
BE_GlobalData * be_global
std::string module_scope(UTL_ScopedName *sn)
::DDS::ReturnCode_t write(in<%SCOPED%> instance_data, in ::DDS::InstanceHandle_t handle)
bool gen_typedef(AST_Typedef *, UTL_ScopedName *, AST_Type *, const char *)
const Classification CL_SEQUENCE