OpenDDS  Snapshot(2023/04/28-20:55)
ValueHelper.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_HELPER_H
7 #define OPENDDS_DCPS_VALUE_HELPER_H
8 
10 #include "Definitions.h"
11 
12 #include <ostream>
13 #include <iostream>
14 #include <iomanip>
15 
17 
18 namespace OpenDDS {
19 namespace DCPS {
20 
21 template <typename IntType>
22 std::ostream& signed_int_helper(std::ostream& o, IntType value, IntType min)
23 {
24  /*
25  * It seems that in C/C++ the minus sign and the bare number are parsed
26  * separately for negative integer literals. This can cause compilers
27  * to complain when using the minimum value of a signed integer because
28  * the number without the minus sign is 1 past the max signed value.
29  *
30  * https://stackoverflow.com/questions/65007935
31  *
32  * Apparently the workaround is to write it as `VALUE_PLUS_ONE - 1`.
33  */
34  const bool min_value = value == min;
35  if (min_value) ++value;
36  o << value;
37  if (min_value) o << " - 1";
38  return o;
39 }
40 
41 #if ACE_SIZEOF_LONG_DOUBLE != 16
43 {
45  return os;
46 }
47 #endif
48 
49 inline
50 std::ostream& hex_value(std::ostream& o, unsigned value, size_t bytes)
51 {
53  o << std::hex << std::setw(bytes * 2) << std::setfill('0') << value;
54  return o;
55 }
56 
57 template <typename CharType>
58 unsigned char_value(CharType value)
59 {
60  return value;
61 }
62 
63 #if CHAR_MIN < 0
64 /*
65  * If char is signed, then it needs to be reinterpreted as unsigned char or
66  * else static casting '\xff' to a 32-bit unsigned int would result in
67  * 0xffffffff because those are both the signed 2's complement forms of -1.
68  */
69 template <>
70 inline unsigned char_value<char>(char value)
71 {
72  return reinterpret_cast<unsigned char&>(value);
73 }
74 #endif
75 
76 template <typename CharType>
77 std::ostream& char_helper(std::ostream& o, CharType value)
78 {
79  switch (value) {
80  case '\'':
81  case '\"':
82  case '\\':
83  case '\?':
84  return o << '\\' << static_cast<char>(value);
85  case '\n':
86  return o << "\\n";
87  case '\t':
88  return o << "\\t";
89  case '\v':
90  return o << "\\v";
91  case '\b':
92  return o << "\\b";
93  case '\r':
94  return o << "\\r";
95  case '\f':
96  return o << "\\f";
97  case '\a':
98  return o << "\\a";
99  }
100  const unsigned cvalue = char_value(value);
101  if (cvalue <= UCHAR_MAX && isprint(cvalue)) {
102  return o << static_cast<char>(value);
103  }
104  return hex_value(o << "\\x", cvalue, sizeof(CharType) == 1 ? 1 : 2);
105 }
106 
107 template <typename CharType>
108 std::ostream& string_helper(std::ostream& o, const CharType* value)
109 {
110  for (size_t i = 0; value[i] != 0; ++i) {
111  char_helper<CharType>(o, value[i]);
112  }
113  return o;
114 }
115 
116 template <typename CharType>
117 std::ostream& string_helper(std::ostream& o, const CharType* value, size_t length)
118 {
119  for (size_t i = 0; i != length; ++i) {
120  char_helper<CharType>(o, value[i]);
121  }
122  return o;
123 }
124 
125 } // namespace DCPS
126 } // namespace OpenDDS
127 
129 
130 #endif /* OPENDDS_DCPS_VALUE_HELPER_H */
const LogLevel::Value value
Definition: debug.cpp:61
std::ostream & hex_value(std::ostream &o, unsigned value, size_t bytes)
Definition: ValueHelper.h:50
long double NativeImpl
ACE_CDR::Boolean operator<<(Serializer &serializer, CoherentChangeControl &value)
Marshal/Insertion into a buffer.
std::ostream & signed_int_helper(std::ostream &o, IntType value, IntType min)
Definition: ValueHelper.h:22
std::ostream & string_helper(std::ostream &o, const CharType *value)
Definition: ValueHelper.h:108
#define OPENDDS_END_VERSIONED_NAMESPACE_DECL
unsigned char_value(CharType value)
Definition: ValueHelper.h:58
The Internal API and Implementation of OpenDDS.
Definition: AddressCache.h:28
std::ostream & char_helper(std::ostream &o, CharType value)
Definition: ValueHelper.h:77