OpenDDS  Snapshot(2023/04/28-20:55)
field_info.cpp
Go to the documentation of this file.
1 /*
2  *
3  *
4  * Distributed under the OpenDDS License.
5  * See: http://www.opendds.org/license.html
6  */
7 
8 #include "field_info.h"
9 
10 #include <sstream>
11 #include <cstring>
12 
13 using namespace AstTypeClassification;
14 
16  : cls_(classify(type))
17  , len_(container_element_limit(type))
18 {
19  AST_Type* const base = resolveActualType(container_base_type(type));
20  const Classification base_cls = classify(base);
21  base_cls_ = base_cls;
22  base_name_ = base->full_name();
23  if (base_cls & (CL_ARRAY | CL_SEQUENCE)) {
24  base_container_ = Container(new EleLen(base));
25  }
26 }
27 
29 {
30  if (cls_ != o.cls_) {
31  return cls_ < o.cls_;
32  }
33  if (len_ != o.len_) {
34  return len_ < o.len_;
35  }
36  if (bool(base_container_.get()) != bool(o.base_container_.get())) {
37  return bool(base_container_.get()) < bool(o.base_container_.get());
38  }
39  if (base_container_.get()) {
40  return *base_container_ < *o.base_container_;
41  }
42  if (base_cls_ != o.base_cls_) {
43  return base_cls_ < o.base_cls_;
44  }
45  return std::strcmp(base_name_, o.base_name_) < 0;
46 }
47 
48 const std::string FieldInfo::scope_op = "::";
49 
51 {
52  return be_global->language_mapping() == BE_GlobalData::LANGMAP_CXX11;
53 }
54 
55 std::string FieldInfo::at_pfx()
56 {
57  return cxx11() ? "AnonymousType_" : "_";
58 }
59 
60 std::string FieldInfo::scoped_type(AST_Type& field_type, const std::string& field_name)
61 {
62  std::string n = scoped(field_type.name());
63  if (!field_type.anonymous()) {
64  return n;
65  }
66  n = n.substr(0, n.rfind(scope_op) + 2) + at_pfx() + field_name;
67  return (field_type.node_type() == AST_Decl::NT_sequence) ? (n + "_seq") : n;
68 }
69 
70 std::string FieldInfo::underscore(const std::string& scoped)
71 {
72  std::string s = scoped;
73  if (s.find(scope_op) == 1) {
74  s = s.substr(3);
75  }
76  for (std::size_t i = s.find(scope_op); i != s.npos; i = s.find(scope_op, i + 2)) {
77  s.replace(i, 2, "_");
78  }
79  return s;
80 }
81 
82 std::string FieldInfo::ref(const std::string& scoped, const std::string& const_s)
83 {
84  return "IDL::DistinctType<" + const_s + scoped + ", " + dds_generator::get_tag_name(scoped) + ">";
85 }
86 
87 FieldInfo::FieldInfo(AST_Field& field)
88  : type_(field.field_type())
89  , name_(field.local_name()->get_string())
92  , struct_name_(scoped_type_.substr(0, scoped_type_.rfind(scope_op)))
93  , type_name_(scoped_type_.substr(scoped_type_.rfind(scope_op) + 2))
95  , cls_(classify(act_))
96  , arr_(dynamic_cast<AST_Array*>(type_))
97  , seq_(dynamic_cast<AST_Sequence*>(type_))
103  , elem_ref_(as_base_ ? ref(scoped_elem_, "") : "")
106 {
107  if (arr_) {
108  std::ostringstream os;
109  os << n_elems_;
110  length_ = os.str();
111  arg_ = "arr";
112  } else if (seq_) {
113  length_ = "length";
114  arg_ = "seq";
115  }
116 
117  if (cxx11()) {
118  unwrap_ = scoped_type_ + "& " + arg_ + " = wrap;\n ACE_UNUSED_ARG(" + arg_ + ");\n";
119  const_unwrap_ = " const " + unwrap_;
120  unwrap_ = " " + unwrap_;
121  arg_ = "wrap";
122  ref_ = ref(scoped_type_, "");
124  ptr_ = ref_ + '*';
125  } else {
126  ref_ = scoped_type_ + (arr_ ? "_forany&" : "&");
127  const_ref_ = "const " + ref_;
128  ptr_ = scoped_type_ + (arr_ ? "_forany*" : "*");
129  }
130 }
131 
132 bool FieldInfo::is_new(EleLenSet& el_set) const
133 {
134  return cxx11() || el_set.insert(EleLen(type_)).second;
135 }
136 
138 {
139  return type_->anonymous() && as_base_ && (cls_ & (CL_ARRAY | CL_SEQUENCE));
140 }
bool is_new(EleLenSet &el_set) const
Definition: field_info.cpp:132
Classification classify(AST_Type *type)
std::string length_
Definition: field_info.h:59
static std::string ref(const std::string &scoped, const std::string &const_s="const ")
Definition: field_info.cpp:82
std::string const_unwrap_
Definition: field_info.h:62
static std::string get_tag_name(const std::string &base_name, const std::string &qualifier="")
AstTypeClassification::Classification base_cls_
Definition: field_info.h:24
std::string const_ref_
Definition: field_info.h:65
const std::string name_
Definition: field_info.h:41
static const std::string scope_op
Definition: field_info.h:33
X * get(void) const
std::string arg_
Definition: field_info.h:63
const Classification CL_ARRAY
std::string ptr_
Definition: field_info.h:66
std::string unwrap_
Definition: field_info.h:61
static std::string scoped_type(AST_Type &field_type, const std::string &field_name)
Definition: field_info.cpp:60
AST_Type * act_
Definition: field_info.h:46
static std::string at_pfx()
Definition: field_info.cpp:55
AST_Type * as_act_
Definition: field_info.h:51
const std::string scoped_elem_
Definition: field_info.h:53
EleLen(AST_Type *type)
Definition: field_info.cpp:15
const std::string underscored_
Definition: field_info.h:43
const AstTypeClassification::Classification cls_
Definition: field_info.h:47
ACE_CDR::ULong container_element_limit(AST_Type *type)
AstTypeClassification::Classification cls_
Definition: field_info.h:22
static bool cxx11()
Definition: field_info.cpp:50
const std::string scoped_type_
Definition: field_info.h:42
const std::string underscored_elem_
Definition: field_info.h:54
AST_Type * resolveActualType(AST_Type *element)
const Classification CL_UNKNOWN
static std::string underscore(const std::string &scoped)
Definition: field_info.cpp:70
Container base_container_
Definition: field_info.h:26
AST_Type * container_base_type(AST_Type *type)
const char *const name
Definition: debug.cpp:60
std::string scoped(UTL_ScopedName *sn, EscapeContext ec=EscapeContext_Normal)
AST_Type * type_
Definition: field_info.h:40
AST_Array * arr_
Definition: field_info.h:48
bool operator<(const EleLen &o) const
Definition: field_info.cpp:28
std::set< EleLen > EleLenSet
Definition: field_info.h:31
AST_Type * as_base_
Definition: field_info.h:50
BE_GlobalData * be_global
Definition: be_global.cpp:44
std::string ref_
Definition: field_info.h:64
const char * base_name_
Definition: field_info.h:23
const AstTypeClassification::Classification as_cls_
Definition: field_info.h:52
std::size_t n_elems_
Definition: field_info.h:58
const Classification CL_SEQUENCE
const std::string elem_const_ref_
Definition: field_info.h:56
AST_Sequence * seq_
Definition: field_info.h:49
ACE_Strong_Bound_Ptr< const EleLen, ACE_Null_Mutex > Container
Definition: field_info.h:25
const std::string elem_ref_
Definition: field_info.h:55
bool anonymous() const
Definition: field_info.cpp:137
FieldInfo(AST_Field &field)
Definition: field_info.cpp:87
const std::string struct_name_
Definition: field_info.h:44
std::size_t len_
Definition: field_info.h:27
const std::string type_name_
Definition: field_info.h:45