OpenDDS  Snapshot(2023/04/28-20:55)
ValueReader.h
Go to the documentation of this file.
1 /*
2  * Distributed under the OpenDDS License.
3  * See: http://www.opendds.org/license.html
4  */
5 
6 #ifndef OPENDDS_DCPS_VALUE_READER_H
7 #define OPENDDS_DCPS_VALUE_READER_H
8 
9 #include "Definitions.h"
10 #include "XTypes/TypeObject.h"
11 
12 #include <FACE/Fixed.h>
14 
15 #include <ace/CDR_Base.h>
16 
17 #include <cstddef>
18 
20 
21 namespace OpenDDS {
22 namespace DCPS {
23 
24 /// A ValueReader produces events and values for the recitation of a
25 /// value. To use it, one manually or automatically, e.g., code
26 /// generation in the IDL compiler, defines a vread function for a
27 /// given type V.
28 ///
29 /// bool vread(ValueReader& vw, V& value)
30 ///
31 /// The vread function should invoke the appropriate methods of the
32 /// ValueReader and dispatch to other vread functions.
33 class MemberHelper {
34 public:
35  virtual ~MemberHelper() {}
36  virtual bool get_value(XTypes::MemberId& value,
37  const char* name) const = 0;
38 };
39 
41 public:
42  struct Pair {
43  const char* name;
45  };
46 
47  ListMemberHelper(const Pair* pairs)
48  : pairs_(pairs)
49  {}
50 
52  const char* name) const
53  {
54  for (const Pair* ptr = pairs_; ptr->name; ++ptr) {
55  if (std::strcmp(ptr->name, name) == 0) {
56  value = ptr->value;
57  return true;
58  }
59  }
60 
61  return false;
62  }
63 
64 private:
65  const Pair* pairs_;
66 };
67 
68 class EnumHelper {
69 public:
70  virtual ~EnumHelper() {}
71  virtual bool get_value(ACE_CDR::Long& value,
72  const char* name) const = 0;
73 };
74 
75 class ListEnumHelper : public EnumHelper {
76 public:
77  struct Pair {
78  const char* name;
80  };
81 
82  ListEnumHelper(const Pair* pairs)
83  : pairs_(pairs)
84  {}
85 
87  const char* name) const
88  {
89  for (const Pair* ptr = pairs_; ptr->name; ++ptr) {
90  if (std::strcmp(ptr->name, name) == 0) {
91  value = ptr->value;
92  return true;
93  }
94  }
95 
96  return false;
97  }
98 
99 private:
100  const Pair* pairs_;
101 };
102 
104 public:
106  virtual ~ValueReader() {}
107 
108  virtual bool begin_struct() = 0;
109  virtual bool end_struct() = 0;
110  virtual bool begin_struct_member(XTypes::MemberId& member_id, const MemberHelper& helper) = 0;
111  virtual bool end_struct_member() = 0;
112 
113  virtual bool begin_union() = 0;
114  virtual bool end_union() = 0;
115  virtual bool begin_discriminator() = 0;
116  virtual bool end_discriminator() = 0;
117  virtual bool begin_union_member() = 0;
118  virtual bool end_union_member() = 0;
119 
120  virtual bool begin_array() = 0;
121  virtual bool end_array() = 0;
122  virtual bool begin_sequence() = 0;
123  virtual bool elements_remaining() = 0;
124  virtual bool end_sequence() = 0;
125  virtual bool begin_element() = 0;
126  virtual bool end_element() = 0;
127 
128  virtual bool read_boolean(ACE_CDR::Boolean& value) = 0;
129  virtual bool read_byte(ACE_CDR::Octet& value) = 0;
130 #if OPENDDS_HAS_EXPLICIT_INTS
131  virtual bool read_int8(ACE_CDR::Int8& value) = 0;
132  virtual bool read_uint8(ACE_CDR::UInt8& value) = 0;
133 #endif
134  virtual bool read_int16(ACE_CDR::Short& value) = 0;
135  virtual bool read_uint16(ACE_CDR::UShort& value) = 0;
136  virtual bool read_int32(ACE_CDR::Long& value) = 0;
137  virtual bool read_uint32(ACE_CDR::ULong& value) = 0;
138  virtual bool read_int64(ACE_CDR::LongLong& value) = 0;
139  virtual bool read_uint64(ACE_CDR::ULongLong& value) = 0;
140  virtual bool read_float32(ACE_CDR::Float& value) = 0;
141  virtual bool read_float64(ACE_CDR::Double& value) = 0;
142  virtual bool read_float128(ACE_CDR::LongDouble& value) = 0;
143 
144 #ifdef NONNATIVE_LONGDOUBLE
145  bool read_float128(long double& value);
146 #endif
147 
148  virtual bool read_fixed(OpenDDS::FaceTypes::Fixed& value) = 0;
149  virtual bool read_char8(ACE_CDR::Char& value) = 0;
150  virtual bool read_char16(ACE_CDR::WChar& value) = 0;
151  virtual bool read_string(String& value) = 0;
152  virtual bool read_wstring(WString& value) = 0;
153 
154  virtual bool read_long_enum(ACE_CDR::Long& value, const EnumHelper& helper) = 0;
155  template <typename T>
156  bool read_enum(T& value, const EnumHelper& helper)
157  {
158  ACE_CDR::Long lvalue;
159  if (!read_long_enum(lvalue, helper)) {
160  return false;
161  }
162  value = static_cast<T>(lvalue);
163  return true;
164  }
165 
166  /// Array read operations
167  ///@{
168  virtual bool read_boolean_array(ACE_CDR::Boolean* value, size_t length);
169  virtual bool read_byte_array(ACE_CDR::Octet* value, size_t length);
170 #if OPENDDS_HAS_EXPLICIT_INTS
171  virtual bool read_int8_array(ACE_CDR::Int8* value, size_t length);
172  virtual bool read_uint8_array(ACE_CDR::UInt8* value, size_t length);
173 #endif
174  virtual bool read_int16_array(ACE_CDR::Short* value, size_t length);
175  virtual bool read_uint16_array(ACE_CDR::UShort* value, size_t length);
176  virtual bool read_int32_array(ACE_CDR::Long* value, size_t length);
177  virtual bool read_uint32_array(ACE_CDR::ULong* value, size_t length);
178  virtual bool read_int64_array(ACE_CDR::LongLong* value, size_t length);
179  virtual bool read_uint64_array(ACE_CDR::ULongLong* value, size_t length);
180  virtual bool read_float32_array(ACE_CDR::Float* value, size_t length);
181  virtual bool read_float64_array(ACE_CDR::Double* value, size_t length);
182  virtual bool read_float128_array(ACE_CDR::LongDouble* value, size_t length);
183  virtual bool read_char8_array(ACE_CDR::Char* value, size_t length);
184  virtual bool read_char16_array(ACE_CDR::WChar* value, size_t length);
185  ///@}
186 };
187 
188 } // namespace DCPS
189 } // namespace OpenDDS
190 
192 
193 #endif /* OPENDDS_DCPS_VALUE_READER_H */
ACE_Byte Octet
std::wstring WString
ACE_CDR::ULong MemberId
Definition: TypeObject.h:910
ACE_INT64 LongLong
const LogLevel::Value value
Definition: debug.cpp:61
std::string String
ListMemberHelper(const Pair *pairs)
Definition: ValueReader.h:47
#define OpenDDS_Dcps_Export
Definition: dcps_export.h:24
ACE_UINT64 ULongLong
ListEnumHelper(const Pair *pairs)
Definition: ValueReader.h:82
virtual bool get_value(XTypes::MemberId &value, const char *name) const =0
ACE_INT16 Short
bool read_enum(T &value, const EnumHelper &helper)
Definition: ValueReader.h:156
char Char
ACE_UINT16 UShort
ACE_UINT32 ULong
const char *const name
Definition: debug.cpp:60
bool get_value(ACE_CDR::Long &value, const char *name) const
Definition: ValueReader.h:86
ACE_INT32 Long
ACE_INT8 Int8
#define OPENDDS_END_VERSIONED_NAMESPACE_DECL
ACE_WCHAR_T WChar
bool Boolean
The Internal API and Implementation of OpenDDS.
Definition: AddressCache.h:28
bool get_value(XTypes::MemberId &value, const char *name) const
Definition: ValueReader.h:51
ACE_UINT8 UInt8