13 #include <global_extern.h> 14 #include <utl_identifier.h> 15 #include <utl_labellist.h> 16 #include <ast_fixed.h> 22 void generate_read(
const std::string& expression,
const std::string& accessor,
23 AST_Type* type,
const std::string& idx,
int level = 1);
25 std::string primitive_type(AST_PredefinedType::PredefinedType pt)
28 case AST_PredefinedType::PT_long:
30 case AST_PredefinedType::PT_ulong:
32 case AST_PredefinedType::PT_longlong:
34 case AST_PredefinedType::PT_ulonglong:
36 case AST_PredefinedType::PT_short:
38 case AST_PredefinedType::PT_ushort:
40 #if OPENDDS_HAS_EXPLICIT_INTS 41 case AST_PredefinedType::PT_int8:
43 case AST_PredefinedType::PT_uint8:
46 case AST_PredefinedType::PT_float:
48 case AST_PredefinedType::PT_double:
50 case AST_PredefinedType::PT_longdouble:
52 case AST_PredefinedType::PT_char:
54 case AST_PredefinedType::PT_wchar:
56 case AST_PredefinedType::PT_boolean:
58 case AST_PredefinedType::PT_octet:
65 void array_helper(
const std::string& expression, AST_Array* array,
66 size_t dim_idx,
const std::string& idx,
int level)
68 const bool use_cxx11 =
be_global->language_mapping() == BE_GlobalData::LANGMAP_CXX11;
69 const std::string indent(level * 2,
' ');
75 if ((primitive && (dim_idx < array->n_dims() - 1)) || (!primitive && (dim_idx < array->n_dims()))) {
76 const size_t dim = array->dims()[dim_idx]->ev()->u.ulval;
78 indent <<
"if (!value_reader.begin_array()) return false;\n" <<
79 indent <<
"for (" << (use_cxx11 ?
"size_t " :
"unsigned int ") << idx <<
" = 0; " 80 << idx <<
" != " << dim <<
"; ++" << idx <<
") {\n" <<
81 indent <<
" if (!value_reader.begin_element()) return false;\n";
82 array_helper(expression +
"[" + idx +
"]", array, dim_idx + 1, idx +
"i", level + 1);
84 indent <<
" if (!value_reader.end_element()) return false;\n" <<
86 indent <<
"if (!value_reader.end_array()) return false;\n";
89 const size_t dim = array->dims()[dim_idx]->ev()->u.ulval;
91 const AST_PredefinedType::PredefinedType pt =
92 dynamic_cast<AST_PredefinedType*
>(actual)->pt();
94 indent <<
"if (!value_reader.begin_array()) return false;\n";
96 "if (!value_reader.read_" << primitive_type(pt) <<
"_array (" << expression << (use_cxx11 ?
".data()" :
"") <<
", " << dim <<
")) return false;\n";
98 indent <<
"if (!value_reader.end_array()) return false;\n";
101 generate_read(expression,
"", array->base_type(), idx +
"i", level);
106 void sequence_helper(
const std::string& expression, AST_Sequence* sequence,
107 const std::string& idx,
int level)
110 const bool use_cxx11 =
be_global->language_mapping() == BE_GlobalData::LANGMAP_CXX11;
111 const std::string indent(level * 2,
' ');
113 indent <<
"if (!value_reader.begin_sequence()) return false;\n" <<
114 indent <<
"for (" << (use_cxx11 ?
"size_t " :
"unsigned int ") << idx <<
" = 0; " 115 "value_reader.elements_remaining(); ++" << idx <<
") {\n";
117 be_global->impl_ << indent <<
" " << expression <<
".resize(" << expression <<
".size() + 1);\n";
119 be_global->impl_ << indent <<
" OpenDDS::DCPS::grow(" << expression <<
");\n";
122 indent <<
" if (!value_reader.begin_element()) return false;\n";
123 generate_read(expression +
"[" + idx +
"]",
"", sequence->base_type(), idx +
"i", level + 1);
125 indent <<
" if (!value_reader.end_element()) return false;\n" <<
127 indent <<
"if (!value_reader.end_sequence()) return false;\n";
130 void generate_read(
const std::string& expression,
const std::string& accessor,
131 AST_Type* type,
const std::string& idx,
int level)
137 AST_Sequence*
const sequence =
dynamic_cast<AST_Sequence*
>(actual);
138 sequence_helper(expression + accessor, sequence, idx, level);
142 AST_Array*
const array =
dynamic_cast<AST_Array*
>(actual);
143 array_helper(expression + accessor, array, 0, idx, level);
147 const bool use_cxx11 =
be_global->language_mapping() == BE_GlobalData::LANGMAP_CXX11;
148 const std::string indent(level * 2,
' ');
151 indent <<
"if (!value_reader.read_fixed(" << expression <<
")) return false;\n";
156 indent <<
" " << ((c &
CL_WIDE) ?
"WString" :
"String") <<
" x;\n" <<
157 indent <<
" if (!value_reader.read_" << ((c &
CL_WIDE) ?
"w" :
"")
158 <<
"string(x)) return false;\n" <<
159 indent <<
" " << expression << accessor <<
" = x" << (use_cxx11 ?
"" :
".c_str()")
163 }
else if (c & CL_PRIMITIVE) {
164 const AST_PredefinedType::PredefinedType pt =
165 dynamic_cast<AST_PredefinedType*
>(actual)->pt();
166 if (pt == AST_PredefinedType::PT_boolean) {
169 indent <<
" " <<
scoped(type->name()) <<
" bx;\n" <<
170 indent <<
" if (!value_reader.read_" << primitive_type(pt)
171 <<
"(bx)) return false;\n" <<
172 indent <<
" " << expression << accessor <<
" = bx;\n" <<
176 indent <<
"if (!value_reader.read_" << primitive_type(pt) <<
'(' << expression << accessor <<
")) return false;\n";
180 indent <<
"if (!vread(value_reader, " << expression << accessor <<
181 ")) return false;\n";
185 std::string branch_helper(
const std::string&, AST_Decl* branch,
186 const std::string& field_name,
194 std::string decl =
field_type_name(dynamic_cast<AST_Field*>(branch), type);
198 decl = (c &
CL_WIDE) ?
"std::wstring" :
"std::string";
202 " if (!value_reader.begin_union_member()) return false;\n" 203 " " << decl <<
" bv;\n";
204 generate_read(
"bv",
"", type,
"i", 2);
206 " value." << field_name <<
"(bv" << ((c &
CL_STRING) ?
".c_str()" :
"") <<
");\n" <<
207 " if (!value_reader.end_union_member()) return false;\n";
213 UTL_ScopedName*
name,
214 const std::vector<AST_EnumVal*>& contents,
217 be_global->add_include(
"dds/DCPS/Util.h", BE_GlobalData::STREAM_H);
218 be_global->add_include(
"dds/DCPS/ValueReader.h", BE_GlobalData::STREAM_H);
220 const std::string type_name =
scoped(name);
226 read.
addArg(
"value_reader",
"OpenDDS::DCPS::ValueReader&");
227 read.
addArg(
"value", type_name +
"&");
231 " static const ListEnumHelper::Pair pairs[] = {";
233 for (
size_t i = 0; i != contents.size(); ++i) {
239 '{' <<
'"' << idl_name <<
'"' <<
',' << contents[i]->constant_value()->ev()->u.eval <<
'}';
244 " ListEnumHelper helper(pairs);\n" 245 " return value_reader.read_enum(value, helper);\n";
260 UTL_ScopedName*
name,
261 const std::vector<AST_Field*>& fields,
265 be_global->add_include(
"dds/DCPS/Util.h", BE_GlobalData::STREAM_H);
266 be_global->add_include(
"dds/DCPS/ValueReader.h", BE_GlobalData::STREAM_H);
268 const std::string type_name =
scoped(name);
269 const bool use_cxx11 =
be_global->language_mapping() == BE_GlobalData::LANGMAP_CXX11;
270 const std::string accessor = use_cxx11 ?
"()" :
"";
276 read.
addArg(
"value_reader",
"OpenDDS::DCPS::ValueReader&");
277 read.
addArg(
"value", type_name +
"&");
281 " static const ListMemberHelper::Pair pairs[] = {";
283 for (
size_t i = 0; i != fields.size(); ++i) {
289 '{' <<
'"' << idl_name <<
'"' <<
',' <<
be_global->get_id(fields[i]) <<
'}';
294 " ListMemberHelper helper(pairs);\n";
297 " if (!value_reader.begin_struct()) return false;\n" 298 " XTypes::MemberId member_id;\n" 299 " while (value_reader.begin_struct_member(member_id, helper)) {\n" 300 " switch (member_id) {\n";
302 for (std::vector<AST_Field*>::const_iterator pos = fields.begin(), limit = fields.end();
303 pos != limit; ++pos) {
304 AST_Field*
const field = *pos;
305 const std::string field_name = field->local_name()->get_string();
307 " case " <<
be_global->get_id(field) <<
": {\n";
308 generate_read(
"value." + field_name, accessor, field->field_type(),
"i", 3);
316 " if (!value_reader.end_struct_member()) return false;\n" 318 " if (!value_reader.end_struct()) return false;\n" 327 UTL_ScopedName*
name,
328 const std::vector<AST_UnionBranch*>& branches,
329 AST_Type* discriminator,
332 be_global->add_include(
"dds/DCPS/Util.h", BE_GlobalData::STREAM_H);
333 be_global->add_include(
"dds/DCPS/ValueReader.h", BE_GlobalData::STREAM_H);
335 const std::string type_name =
scoped(name);
341 read.
addArg(
"value_reader",
"OpenDDS::DCPS::ValueReader&");
342 read.
addArg(
"value", type_name +
"&");
346 " if (!value_reader.begin_union()) return false;\n" 347 " if (!value_reader.begin_discriminator()) return false;\n" 349 " " <<
scoped(discriminator->name()) <<
" d;\n";
350 generate_read(
"d",
"", discriminator,
"i", 2);
354 " if (!value_reader.end_discriminator()) return false;\n";
357 discriminator,
"",
"", type_name.c_str(),
360 " if (!value_reader.end_union()) return false;\n"
Classification classify(AST_Type *type)
const Classification CL_STRING
const Classification CL_WIDE
std::string field_type_name(AST_Field *field, AST_Type *field_type)
const Classification CL_PRIMITIVE
bool gen_typedef(AST_Typedef *, UTL_ScopedName *, AST_Type *, const char *)
const Classification CL_ARRAY
bool gen_union(AST_Union *, UTL_ScopedName *, const std::vector< AST_UnionBranch *> &, AST_Type *, const char *)
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)
AST_Type * resolveActualType(AST_Type *element)
bool gen_enum(AST_Enum *, UTL_ScopedName *name, const std::vector< AST_EnumVal *> &contents, const char *repoid)
ssize_t read(ACE_HANDLE handle, void *buf, size_t len)
bool gen_struct(AST_Structure *node, UTL_ScopedName *name, const std::vector< AST_Field *> &fields, AST_Type::SIZE_TYPE size, const char *repoid)
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
const Classification CL_SEQUENCE