Line data Source code
1 : /* 2 : * Distributed under the OpenDDS License. 3 : * See: http://www.opendds.org/license.html 4 : */ 5 : 6 : #ifndef OPENDDS_DCPS_PRINTER_VALUE_WRITER_H 7 : #define OPENDDS_DCPS_PRINTER_VALUE_WRITER_H 8 : 9 : #include "ValueWriter.h" 10 : #include "ValueHelper.h" 11 : #include "dcps_export.h" 12 : #include "Definitions.h" 13 : 14 : #include <dds/DdsDcpsCoreTypeSupportImpl.h> 15 : #include <dds/DdsDcpsTopicC.h> 16 : 17 : #include <iosfwd> 18 : #include <sstream> 19 : #include <vector> 20 : 21 : #if !defined (ACE_LACKS_PRAGMA_ONCE) 22 : #pragma once 23 : #endif /* ACE_LACKS_PRAGMA_ONCE */ 24 : 25 : OPENDDS_BEGIN_VERSIONED_NAMESPACE_DECL 26 : 27 : namespace OpenDDS { 28 : namespace DCPS { 29 : 30 : /// Convert values to a readable format. 31 : class PrinterValueWriter : public ValueWriter { 32 : public: 33 54 : explicit PrinterValueWriter(unsigned int a_indent = TheServiceParticipant->printer_value_writer_indent()) 34 108 : : indent_(a_indent) 35 54 : , current_indent_(0) 36 54 : , at_newline_(true) 37 54 : {} 38 : 39 : void begin_struct(); 40 : void end_struct(); 41 : void begin_struct_member(const DDS::MemberDescriptor& /*descriptor*/); 42 : void end_struct_member(); 43 : 44 : void begin_union(); 45 : void end_union(); 46 : void begin_discriminator(); 47 : void end_discriminator(); 48 : void begin_union_member(const char* name); 49 : void end_union_member(); 50 : 51 : void begin_array(); 52 : void end_array(); 53 : void begin_sequence(); 54 : void end_sequence(); 55 : void begin_element(size_t idx); 56 : void end_element(); 57 : 58 : void write_boolean(ACE_CDR::Boolean value); 59 : void write_byte(ACE_CDR::Octet value); 60 : #if OPENDDS_HAS_EXPLICIT_INTS 61 : void write_int8(ACE_CDR::Int8 value); 62 : void write_uint8(ACE_CDR::UInt8 value); 63 : #endif 64 : void write_int16(ACE_CDR::Short value); 65 : void write_uint16(ACE_CDR::UShort value); 66 : void write_int32(ACE_CDR::Long value); 67 : void write_uint32(ACE_CDR::ULong value); 68 : void write_int64(ACE_CDR::LongLong value); 69 : void write_uint64(ACE_CDR::ULongLong value); 70 : void write_float32(ACE_CDR::Float value); 71 : void write_float64(ACE_CDR::Double value); 72 : void write_float128(ACE_CDR::LongDouble value); 73 : void write_fixed(const OpenDDS::FaceTypes::Fixed& value); 74 : void write_char8(ACE_CDR::Char value); 75 : void write_char16(ACE_CDR::WChar value); 76 : void write_string(const ACE_CDR::Char* value, size_t length); 77 : void write_wstring(const ACE_CDR::WChar* value, size_t length); 78 : void write_enum(const char* /*name*/, ACE_CDR::Long value); 79 : 80 54 : std::string str() const 81 : { 82 54 : return stream_.str(); 83 : } 84 : 85 : private: 86 21 : std::string newline() 87 : { 88 21 : if (at_newline_) { 89 14 : return ""; 90 : } else { 91 7 : at_newline_ = false; 92 7 : return "\n"; 93 : } 94 : } 95 : 96 : const unsigned int indent_; 97 : std::stringstream stream_; 98 : unsigned int current_indent_; 99 : bool at_newline_; 100 : }; 101 : 102 6 : void PrinterValueWriter::begin_struct() 103 : { 104 6 : current_indent_ += indent_; 105 6 : } 106 : 107 3 : void PrinterValueWriter::end_struct() 108 : { 109 3 : current_indent_ -= indent_; 110 3 : } 111 : 112 5 : void PrinterValueWriter::begin_struct_member(const DDS::MemberDescriptor& descriptor) 113 : { 114 5 : stream_ << newline() << std::string(current_indent_, ' ') << descriptor.name() << ": "; 115 5 : at_newline_ = false; 116 5 : } 117 : 118 4 : void PrinterValueWriter::end_struct_member() 119 4 : {} 120 : 121 6 : void PrinterValueWriter::begin_union() 122 : { 123 6 : current_indent_ += indent_; 124 6 : } 125 : 126 1 : void PrinterValueWriter::end_union() 127 : { 128 1 : current_indent_ -= indent_; 129 1 : } 130 : 131 2 : void PrinterValueWriter::begin_discriminator() 132 : { 133 2 : stream_ << newline() << std::string(current_indent_, ' ') << "$discriminator: "; 134 2 : at_newline_ = false; 135 2 : } 136 : 137 1 : void PrinterValueWriter::end_discriminator() 138 1 : {} 139 : 140 2 : void PrinterValueWriter::begin_union_member(const char* name) 141 : { 142 2 : stream_ << newline() << std::string(current_indent_, ' ') << name << ": "; 143 2 : at_newline_ = false; 144 2 : } 145 : 146 1 : void PrinterValueWriter::end_union_member() 147 1 : {} 148 : 149 5 : void PrinterValueWriter::begin_array() 150 : { 151 5 : current_indent_ += indent_; 152 5 : } 153 : 154 4 : void PrinterValueWriter::end_array() 155 : { 156 4 : current_indent_ -= indent_; 157 4 : } 158 : 159 6 : void PrinterValueWriter::begin_sequence() 160 : { 161 6 : current_indent_ += indent_; 162 6 : } 163 : 164 3 : void PrinterValueWriter::end_sequence() 165 : { 166 3 : current_indent_ -= indent_; 167 3 : } 168 : 169 12 : void PrinterValueWriter::begin_element(size_t idx) 170 : { 171 12 : stream_ << newline() << std::string(current_indent_, ' ') << '[' << idx << "]: "; 172 12 : at_newline_ = false; 173 12 : } 174 : 175 11 : void PrinterValueWriter::end_element() 176 11 : {} 177 : 178 2 : void PrinterValueWriter::write_boolean(ACE_CDR::Boolean value) 179 : { 180 2 : stream_ << (value ? "true" : "false"); 181 2 : } 182 : 183 2 : void PrinterValueWriter::write_byte(ACE_CDR::Octet value) 184 : { 185 2 : stream_ << std::hex << "0x" << static_cast<unsigned int>(value) << std::dec; 186 2 : } 187 : 188 : #if OPENDDS_HAS_EXPLICIT_INTS 189 : 190 2 : void PrinterValueWriter::write_int8(ACE_CDR::Int8 value) 191 : { 192 2 : stream_ << static_cast<int>(value); 193 2 : } 194 : 195 2 : void PrinterValueWriter::write_uint8(ACE_CDR::UInt8 value) 196 : { 197 2 : stream_ << static_cast<unsigned int>(value); 198 2 : } 199 : #endif 200 : 201 19 : void PrinterValueWriter::write_int16(ACE_CDR::Short value) 202 : { 203 19 : stream_ << value; 204 19 : } 205 : 206 2 : void PrinterValueWriter::write_uint16(ACE_CDR::UShort value) 207 : { 208 2 : stream_ << value; 209 2 : } 210 : 211 2 : void PrinterValueWriter::write_int32(ACE_CDR::Long value) 212 : { 213 2 : stream_ << value; 214 2 : } 215 : 216 2 : void PrinterValueWriter::write_uint32(ACE_CDR::ULong value) 217 : { 218 2 : stream_ << value; 219 2 : } 220 : 221 4 : void PrinterValueWriter::write_int64(ACE_CDR::LongLong value) 222 : { 223 4 : stream_ << value; 224 4 : } 225 : 226 2 : void PrinterValueWriter::write_uint64(ACE_CDR::ULongLong value) 227 : { 228 2 : stream_ << value; 229 2 : } 230 : 231 1 : void PrinterValueWriter::write_float32(ACE_CDR::Float value) 232 : { 233 1 : stream_ << value; 234 1 : } 235 : 236 1 : void PrinterValueWriter::write_float64(ACE_CDR::Double value) 237 : { 238 1 : stream_ << value; 239 1 : } 240 : 241 1 : void PrinterValueWriter::write_float128(ACE_CDR::LongDouble value) 242 : { 243 1 : stream_ << value; 244 1 : } 245 : 246 1 : void PrinterValueWriter::write_fixed(const OpenDDS::FaceTypes::Fixed& /*value*/) 247 : { 248 : // FUTURE 249 1 : stream_ << "fixed"; 250 1 : } 251 : 252 2 : void PrinterValueWriter::write_char8(ACE_CDR::Char value) 253 : { 254 2 : char_helper(stream_, value); 255 2 : } 256 : 257 2 : void PrinterValueWriter::write_char16(ACE_CDR::WChar value) 258 : { 259 2 : char_helper(stream_, value); 260 2 : } 261 : 262 1 : void PrinterValueWriter::write_string(const ACE_CDR::Char* value, size_t length) 263 : { 264 1 : string_helper(stream_, value, length); 265 1 : } 266 : 267 0 : void PrinterValueWriter::write_wstring(const ACE_CDR::WChar* value, size_t length) 268 : { 269 0 : string_helper(stream_, value, length); 270 0 : } 271 : 272 1 : void PrinterValueWriter::write_enum(const char* name, 273 : ACE_CDR::Long value) 274 : { 275 1 : stream_ << name << " (" << value << ")"; 276 1 : } 277 : 278 : } // namespace DCPS 279 : } // namespace OpenDDS 280 : 281 : OPENDDS_END_VERSIONED_NAMESPACE_DECL 282 : 283 : #endif /* OPENDDS_DCPS_PRINTER_VALUE_WRITER_H */