00001
00002
00003
00004
00005
00006
00007
00008 #ifndef OPENDDS_DCPS_SERIALIZER_H
00009 #define OPENDDS_DCPS_SERIALIZER_H
00010
00011 #include "ace/CDR_Base.h"
00012 #include "ace/CDR_Stream.h"
00013
00014 #include "tao/String_Alloc.h"
00015
00016 #include "dcps_export.h"
00017
00018 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00019 #pragma once
00020 #endif
00021
00022 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00023 class ACE_Message_Block;
00024 ACE_END_VERSIONED_NAMESPACE_DECL
00025
00026 namespace OpenDDS {
00027 namespace DCPS {
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 class OpenDDS_Dcps_Export Serializer {
00039 public:
00040
00041 enum Alignment {
00042 ALIGN_NONE,
00043 ALIGN_INITIALIZE,
00044 ALIGN_CDR
00045 #ifdef ACE_INITIALIZE_MEMORY_BEFORE_USE
00046 = ALIGN_INITIALIZE
00047 #endif
00048 };
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065 explicit Serializer(ACE_Message_Block* chain,
00066 bool swap_bytes = false,
00067 Alignment align = ALIGN_NONE);
00068
00069 virtual ~Serializer();
00070
00071
00072 void swap_bytes(bool do_swap);
00073
00074
00075 bool swap_bytes() const;
00076
00077
00078 Alignment alignment() const;
00079
00080
00081 void reset_alignment();
00082
00083
00084 bool good_bit() const;
00085
00086
00087 void read_string(ACE_CDR::Char*& dest,
00088 ACE_CDR::Char* str_alloc(ACE_CDR::ULong) = CORBA::string_alloc,
00089 void str_free(ACE_CDR::Char*) = CORBA::string_free);
00090
00091
00092 void read_string(ACE_CDR::WChar*& dest,
00093 ACE_CDR::WChar* str_alloc(ACE_CDR::ULong) = CORBA::wstring_alloc,
00094 void str_free(ACE_CDR::WChar*) = CORBA::wstring_free);
00095
00096
00097
00098
00099
00100
00101 bool skip(ACE_CDR::UShort n, int size = 1);
00102
00103
00104
00105
00106
00107
00108
00109 bool read_boolean_array(ACE_CDR::Boolean* x, ACE_CDR::ULong length);
00110 bool read_char_array(ACE_CDR::Char* x, ACE_CDR::ULong length);
00111 bool read_wchar_array(ACE_CDR::WChar* x, ACE_CDR::ULong length);
00112 bool read_octet_array(ACE_CDR::Octet* x, ACE_CDR::ULong length);
00113 bool read_short_array(ACE_CDR::Short* x, ACE_CDR::ULong length);
00114 bool read_ushort_array(ACE_CDR::UShort* x, ACE_CDR::ULong length);
00115 bool read_long_array(ACE_CDR::Long* x, ACE_CDR::ULong length);
00116 bool read_ulong_array(ACE_CDR::ULong* x, ACE_CDR::ULong length);
00117 bool read_longlong_array(ACE_CDR::LongLong* x, ACE_CDR::ULong length);
00118 bool read_ulonglong_array(ACE_CDR::ULongLong* x, ACE_CDR::ULong length);
00119 bool read_float_array(ACE_CDR::Float* x, ACE_CDR::ULong length);
00120 bool read_double_array(ACE_CDR::Double* x, ACE_CDR::ULong length);
00121 bool read_longdouble_array(ACE_CDR::LongDouble* x, ACE_CDR::ULong length);
00122
00123
00124
00125
00126
00127
00128 bool write_boolean_array(const ACE_CDR::Boolean* x, ACE_CDR::ULong length);
00129 bool write_char_array(const ACE_CDR::Char* x, ACE_CDR::ULong length);
00130 bool write_wchar_array(const ACE_CDR::WChar* x, ACE_CDR::ULong length);
00131 bool write_octet_array(const ACE_CDR::Octet* x, ACE_CDR::ULong length);
00132 bool write_short_array(const ACE_CDR::Short* x, ACE_CDR::ULong length);
00133 bool write_ushort_array(const ACE_CDR::UShort* x, ACE_CDR::ULong length);
00134 bool write_long_array(const ACE_CDR::Long* x, ACE_CDR::ULong length);
00135 bool write_ulong_array(const ACE_CDR::ULong* x, ACE_CDR::ULong length);
00136 bool write_longlong_array(const ACE_CDR::LongLong* x, ACE_CDR::ULong length);
00137 bool write_ulonglong_array(const ACE_CDR::ULongLong* x, ACE_CDR::ULong length);
00138 bool write_float_array(const ACE_CDR::Float* x, ACE_CDR::ULong length);
00139 bool write_double_array(const ACE_CDR::Double* x, ACE_CDR::ULong length);
00140 bool write_longdouble_array(const ACE_CDR::LongDouble* x, ACE_CDR::ULong length);
00141
00142
00143
00144 friend OpenDDS_Dcps_Export
00145 bool operator<<(Serializer& s, ACE_CDR::Char x);
00146 friend OpenDDS_Dcps_Export
00147 bool operator<<(Serializer& s, ACE_CDR::Short x);
00148 friend OpenDDS_Dcps_Export
00149 bool operator<<(Serializer& s, ACE_CDR::UShort x);
00150 friend OpenDDS_Dcps_Export
00151 bool operator<<(Serializer& s, ACE_CDR::Long x);
00152 friend OpenDDS_Dcps_Export
00153 bool operator<<(Serializer& s, ACE_CDR::ULong x);
00154 friend OpenDDS_Dcps_Export
00155 bool operator<<(Serializer& s, ACE_CDR::LongLong x);
00156 friend OpenDDS_Dcps_Export
00157 bool operator<<(Serializer& s, ACE_CDR::ULongLong x);
00158 friend OpenDDS_Dcps_Export
00159 bool operator<<(Serializer& s, ACE_CDR::LongDouble x);
00160 friend OpenDDS_Dcps_Export
00161 bool operator<<(Serializer& s, ACE_CDR::Float x);
00162 friend OpenDDS_Dcps_Export
00163 bool operator<<(Serializer& s, ACE_CDR::Double x);
00164 friend OpenDDS_Dcps_Export
00165 bool operator<<(Serializer& s, const ACE_CDR::Char* x);
00166 friend OpenDDS_Dcps_Export
00167 bool operator<<(Serializer& s, const ACE_CDR::WChar* x);
00168
00169
00170 friend OpenDDS_Dcps_Export
00171 bool operator<<(Serializer& s, ACE_OutputCDR::from_boolean x);
00172 friend OpenDDS_Dcps_Export
00173 bool operator<<(Serializer& s, ACE_OutputCDR::from_char x);
00174 friend OpenDDS_Dcps_Export
00175 bool operator<<(Serializer& s, ACE_OutputCDR::from_wchar x);
00176 friend OpenDDS_Dcps_Export
00177 bool operator<<(Serializer& s, ACE_OutputCDR::from_octet x);
00178 friend OpenDDS_Dcps_Export
00179 bool operator<<(Serializer& s, ACE_OutputCDR::from_string x);
00180 friend OpenDDS_Dcps_Export
00181 bool operator<<(Serializer& s, ACE_OutputCDR::from_wstring x);
00182
00183
00184 friend OpenDDS_Dcps_Export
00185 bool operator>>(Serializer& s, ACE_CDR::Char& x);
00186 friend OpenDDS_Dcps_Export
00187 bool operator>>(Serializer& s, ACE_CDR::Short& x);
00188 friend OpenDDS_Dcps_Export
00189 bool operator>>(Serializer& s, ACE_CDR::UShort& x);
00190 friend OpenDDS_Dcps_Export
00191 bool operator>>(Serializer& s, ACE_CDR::Long& x);
00192 friend OpenDDS_Dcps_Export
00193 bool operator>>(Serializer& s, ACE_CDR::ULong& x);
00194 friend OpenDDS_Dcps_Export
00195 bool operator>>(Serializer& s, ACE_CDR::LongLong& x);
00196 friend OpenDDS_Dcps_Export
00197 bool operator>>(Serializer& s, ACE_CDR::ULongLong& x);
00198 friend OpenDDS_Dcps_Export
00199 bool operator>>(Serializer& s, ACE_CDR::LongDouble& x);
00200 friend OpenDDS_Dcps_Export
00201 bool operator>>(Serializer& s, ACE_CDR::Float& x);
00202 friend OpenDDS_Dcps_Export
00203 bool operator>>(Serializer& s, ACE_CDR::Double& x);
00204 friend OpenDDS_Dcps_Export
00205 bool operator>>(Serializer& s, ACE_CDR::Char*& x);
00206 friend OpenDDS_Dcps_Export
00207 bool operator>>(Serializer& s, ACE_CDR::WChar*& x);
00208
00209
00210 friend OpenDDS_Dcps_Export
00211 bool operator>>(Serializer& s, ACE_InputCDR::to_boolean x);
00212 friend OpenDDS_Dcps_Export
00213 bool operator>>(Serializer& s, ACE_InputCDR::to_char x);
00214 friend OpenDDS_Dcps_Export
00215 bool operator>>(Serializer& s, ACE_InputCDR::to_wchar x);
00216 friend OpenDDS_Dcps_Export
00217 bool operator>>(Serializer& s, ACE_InputCDR::to_octet x);
00218 friend OpenDDS_Dcps_Export
00219 bool operator>>(Serializer& s, ACE_InputCDR::to_string x);
00220 friend OpenDDS_Dcps_Export
00221 bool operator>>(Serializer& s, ACE_InputCDR::to_wstring x);
00222
00223
00224
00225
00226 void buffer_read(char* dest, size_t size, bool swap);
00227
00228 private:
00229
00230
00231
00232
00233
00234 void read_array(char* x, size_t size, ACE_CDR::ULong length);
00235 void read_array(char* x, size_t size, ACE_CDR::ULong length, bool swap);
00236
00237
00238 void buffer_write(const char* src, size_t size, bool swap);
00239
00240
00241
00242
00243
00244
00245
00246 void write_array(const char* x, size_t size, ACE_CDR::ULong length);
00247 void write_array(const char* x, size_t size, ACE_CDR::ULong length, bool swap);
00248
00249
00250
00251 int align_r(size_t alignment);
00252
00253
00254
00255
00256 int align_w(size_t alignment);
00257
00258
00259
00260 void smemcpy(char* to, const char* from, size_t n);
00261
00262
00263
00264 void swapcpy(char* to, const char* from, size_t n);
00265
00266
00267 size_t doread(char* dest, size_t size, bool swap, size_t offset);
00268
00269
00270 size_t dowrite(const char* dest, size_t size, bool swap, size_t offset);
00271
00272
00273 void align_cont_r();
00274
00275
00276 void align_cont_w();
00277
00278
00279 ACE_Message_Block* current_;
00280
00281
00282 bool swap_bytes_;
00283
00284
00285 bool good_bit_;
00286
00287
00288 Alignment alignment_;
00289
00290
00291
00292 unsigned char align_rshift_;
00293
00294
00295
00296 unsigned char align_wshift_;
00297
00298 static const size_t MAX_ALIGN = 8;
00299 static const char ALIGN_PAD[MAX_ALIGN];
00300 static bool use_rti_serialization_;
00301
00302 public:
00303 static const size_t WCHAR_SIZE = 2;
00304
00305 static bool use_rti_serialization();
00306 static void set_use_rti_serialization(bool should_use);
00307
00308 #if defined ACE_LITTLE_ENDIAN
00309 static const bool SWAP_BE = true;
00310 #else
00311 static const bool SWAP_BE = false;
00312 #endif
00313 };
00314
00315 template<typename T> struct KeyOnly {
00316 explicit KeyOnly(T& mess) : t(mess) { }
00317 T& t;
00318 };
00319
00320 }
00321 }
00322
00323 #if defined (__ACE_INLINE__)
00324 # include "Serializer.inl"
00325 #else
00326
00327 #include <ace/CDR_Stream.h>
00328
00329 namespace OpenDDS {
00330 namespace DCPS {
00331
00332
00333 OpenDDS_Dcps_Export
00334 size_t gen_max_marshaled_size(const ACE_CDR::Short& x);
00335 OpenDDS_Dcps_Export
00336 size_t gen_max_marshaled_size(const ACE_CDR::UShort& x);
00337 OpenDDS_Dcps_Export
00338 size_t gen_max_marshaled_size(const ACE_CDR::Long& x);
00339 OpenDDS_Dcps_Export
00340 size_t gen_max_marshaled_size(const ACE_CDR::ULong& x);
00341 OpenDDS_Dcps_Export
00342 size_t gen_max_marshaled_size(const ACE_CDR::LongLong& x);
00343 OpenDDS_Dcps_Export
00344 size_t gen_max_marshaled_size(const ACE_CDR::ULongLong& x);
00345 OpenDDS_Dcps_Export
00346 size_t gen_max_marshaled_size(const ACE_CDR::LongDouble& x);
00347 OpenDDS_Dcps_Export
00348 size_t gen_max_marshaled_size(const ACE_CDR::Float& x);
00349 OpenDDS_Dcps_Export
00350 size_t gen_max_marshaled_size(const ACE_CDR::Double& x);
00351
00352
00353 OpenDDS_Dcps_Export
00354 size_t gen_max_marshaled_size(const ACE_OutputCDR::from_boolean x);
00355 OpenDDS_Dcps_Export
00356 size_t gen_max_marshaled_size(const ACE_OutputCDR::from_char x);
00357 OpenDDS_Dcps_Export
00358 size_t gen_max_marshaled_size(const ACE_OutputCDR::from_wchar x);
00359 OpenDDS_Dcps_Export
00360 size_t gen_max_marshaled_size(const ACE_OutputCDR::from_octet x);
00361
00362
00363 OpenDDS_Dcps_Export
00364 size_t max_marshaled_size_boolean();
00365 OpenDDS_Dcps_Export
00366 size_t max_marshaled_size_char();
00367 OpenDDS_Dcps_Export
00368 size_t max_marshaled_size_wchar();
00369 OpenDDS_Dcps_Export
00370 size_t max_marshaled_size_octet();
00371
00372
00373 OpenDDS_Dcps_Export
00374 size_t max_marshaled_size_ulong();
00375 OpenDDS_Dcps_Export
00376 void find_size_ulong(size_t& size, size_t& padding);
00377
00378 }
00379 }
00380
00381 #endif
00382
00383 #endif