00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef CSTREAM_H
00029 #define CSTREAM_H
00030
00031 #include <mrpt/utils/utils_defs.h>
00032 #include <mrpt/utils/CUncopiable.h>
00033 #include <mrpt/utils/CObject.h>
00034 #include <mrpt/utils/exceptions.h>
00035
00036 namespace mrpt
00037 {
00038 namespace utils
00039 {
00040 class BASE_IMPEXP CSerializable;
00041 struct BASE_IMPEXP CSerializablePtr;
00042 class BASE_IMPEXP CMessage;
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 class BASE_IMPEXP CStream
00055 {
00056 public:
00057
00058
00059 enum TSeekOrigin
00060 {
00061 sFromBeginning = 0,
00062 sFromCurrent = 1,
00063 sFromEnd = 2
00064 };
00065
00066 protected:
00067
00068
00069 virtual size_t Read(void *Buffer, size_t Count) = 0;
00070
00071
00072
00073
00074 virtual size_t Write(const void *Buffer, size_t Count) = 0;
00075 public:
00076
00077
00078 CStream() { }
00079
00080
00081
00082 virtual ~CStream();
00083
00084
00085
00086
00087 size_t ReadBuffer(void *Buffer, size_t Count);
00088
00089
00090
00091
00092
00093
00094 virtual size_t ReadBufferImmediate(void *Buffer, size_t Count)
00095 {
00096 return ReadBuffer(Buffer, Count);
00097 }
00098
00099
00100
00101
00102 void WriteBuffer (const void *Buffer, size_t Count);
00103
00104
00105
00106 size_t CopyFrom(CStream* Source, size_t Count);
00107
00108
00109
00110
00111
00112
00113
00114
00115 virtual uint64_t Seek(long Offset, CStream::TSeekOrigin Origin = sFromBeginning) = 0;
00116
00117
00118
00119 virtual uint64_t getTotalBytesCount() = 0;
00120
00121
00122
00123 virtual uint64_t getPosition() =0;
00124
00125
00126
00127 void WriteObject( const CSerializable *o );
00128
00129
00130
00131
00132
00133 CSerializablePtr ReadObject();
00134
00135
00136
00137
00138
00139
00140 void ReadObject(CSerializable *existingObj);
00141
00142
00143 CStream& operator << (const CSerializablePtr & pObj);
00144
00145 CStream& operator << (const CSerializable &obj);
00146
00147 CStream& operator >> (CSerializablePtr &pObj);
00148 CStream& operator >> (CSerializable &obj);
00149
00150
00151
00152
00153
00154
00155 virtual int printf(const char *fmt,...) MRPT_printf_format_check(2,3);
00156
00157
00158 template <typename T>
00159 void printf_vector(const char *fmt, const std::vector<T> &V )
00160 {
00161 this->printf("[");
00162 size_t N = V.size();
00163 for (size_t i=0;i<N;i++)
00164 {
00165 this->printf(fmt,V[i]);
00166 if (i!=(N-1)) this->printf(",");
00167 }
00168 this->printf("]");
00169 }
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198 void sendMessage( const utils::CMessage &msg);
00199
00200
00201
00202
00203
00204
00205 bool receiveMessage( utils::CMessage &msg );
00206
00207
00208
00209
00210 bool getline(std::string &out_str);
00211
00212
00213 };
00214
00215
00216 #define DECLARE_CSTREAM_READ_WRITE_SIMPLE_TYPE( T ) \
00217 BASE_IMPEXP CStream& operator<<(CStream&out, const T &a); \
00218 BASE_IMPEXP CStream& operator>>(CStream&in, T &a);
00219
00220
00221 DECLARE_CSTREAM_READ_WRITE_SIMPLE_TYPE( bool )
00222 DECLARE_CSTREAM_READ_WRITE_SIMPLE_TYPE( uint8_t )
00223 DECLARE_CSTREAM_READ_WRITE_SIMPLE_TYPE( int8_t )
00224 DECLARE_CSTREAM_READ_WRITE_SIMPLE_TYPE( uint16_t )
00225 DECLARE_CSTREAM_READ_WRITE_SIMPLE_TYPE( int16_t )
00226 DECLARE_CSTREAM_READ_WRITE_SIMPLE_TYPE( uint32_t )
00227 DECLARE_CSTREAM_READ_WRITE_SIMPLE_TYPE( int32_t )
00228 DECLARE_CSTREAM_READ_WRITE_SIMPLE_TYPE( uint64_t )
00229 DECLARE_CSTREAM_READ_WRITE_SIMPLE_TYPE( int64_t )
00230 DECLARE_CSTREAM_READ_WRITE_SIMPLE_TYPE( float )
00231 DECLARE_CSTREAM_READ_WRITE_SIMPLE_TYPE( double )
00232 #ifdef HAVE_LONG_DOUBLE
00233 DECLARE_CSTREAM_READ_WRITE_SIMPLE_TYPE( long double )
00234 #endif
00235
00236
00237
00238
00239
00240
00241 BASE_IMPEXP CStream& operator << (CStream&, const char *a);
00242 BASE_IMPEXP CStream& operator << (CStream&, const std::string &str);
00243
00244 BASE_IMPEXP CStream& operator << (CStream&, const vector_int &a);
00245 BASE_IMPEXP CStream& operator << (CStream&, const vector_uint &a);
00246 BASE_IMPEXP CStream& operator << (CStream&, const vector_word &a);
00247 BASE_IMPEXP CStream& operator << (CStream&, const vector_signed_word &a);
00248 BASE_IMPEXP CStream& operator << (CStream&, const vector_long &a);
00249 BASE_IMPEXP CStream& operator << (CStream&, const vector_float &a);
00250 BASE_IMPEXP CStream& operator << (CStream&, const vector_double &a);
00251 BASE_IMPEXP CStream& operator << (CStream&, const vector_byte &a);
00252 BASE_IMPEXP CStream& operator << (CStream&, const vector_signed_byte &a);
00253 BASE_IMPEXP CStream& operator << (CStream&, const vector_bool &a);
00254
00255
00256 inline CStream& operator << (CStream&s, const std::vector<float> &a) { return s<<vector_float(a); }
00257 inline CStream& operator << (CStream&s, const std::vector<double> &a) { return s<<vector_double(a); }
00258 inline CStream& operator << (CStream&s, const std::vector<int8_t> &a) { return s<<vector_signed_byte(a); }
00259 inline CStream& operator << (CStream&s, const std::vector<int16_t> &a) { return s<<vector_signed_word(a); }
00260 inline CStream& operator << (CStream&s, const std::vector<int32_t> &a) { return s<<vector_int(a); }
00261 inline CStream& operator << (CStream&s, const std::vector<int64_t> &a) { return s<<vector_long(a); }
00262 inline CStream& operator << (CStream&s, const std::vector<uint8_t> &a) { return s<<vector_byte(a); }
00263 inline CStream& operator << (CStream&s, const std::vector<uint16_t> &a) { return s<<vector_word(a); }
00264 inline CStream& operator << (CStream&s, const std::vector<uint32_t> &a) { return s<<vector_uint(a); }
00265
00266 #if MRPT_WORD_SIZE!=32 // If it's 32 bit, size_t <=> uint32_t
00267 inline CStream& operator << (CStream&s, const std::vector<size_t> &a) { return s<<vector_size_t(a); }
00268 #endif
00269
00270 BASE_IMPEXP CStream& operator>>(CStream&in, char *a);
00271 BASE_IMPEXP CStream& operator>>(CStream&in, std::string &str);
00272 BASE_IMPEXP CStream& operator>>(CStream&in, vector_int &a);
00273 BASE_IMPEXP CStream& operator>>(CStream&in, vector_uint &a);
00274 BASE_IMPEXP CStream& operator>>(CStream&in, vector_word &a);
00275 BASE_IMPEXP CStream& operator>>(CStream&in, vector_signed_word &a);
00276 BASE_IMPEXP CStream& operator>>(CStream&in, vector_long &a);
00277 BASE_IMPEXP CStream& operator>>(CStream&in, vector_double &a);
00278 BASE_IMPEXP CStream& operator>>(CStream&in, vector_float &a);
00279 BASE_IMPEXP CStream& operator>>(CStream&in, vector_byte &a);
00280 BASE_IMPEXP CStream& operator>>(CStream&in, vector_signed_byte &a);
00281 BASE_IMPEXP CStream& operator>>(CStream&in, vector_bool &a);
00282 BASE_IMPEXP CStream &operator<<(CStream &,const std::vector<std::string> &);
00283 BASE_IMPEXP CStream &operator>>(CStream &,std::vector<std::string> &);
00284
00285
00286 inline CStream& operator >> (CStream&s, std::vector<float> &a) { vector_float d; s>>d; a=d; return s; }
00287 inline CStream& operator >> (CStream&s, std::vector<double> &a) { vector_double d; s>>d; a=d; return s; }
00288 inline CStream& operator >> (CStream&s, std::vector<int8_t> &a) { vector_signed_byte d; s>>d; a=d; return s; }
00289 inline CStream& operator >> (CStream&s, std::vector<int16_t> &a) { vector_signed_word d; s>>d; a=d; return s; }
00290 inline CStream& operator >> (CStream&s, std::vector<int32_t> &a) { vector_int d; s>>d; a=d; return s; }
00291 inline CStream& operator >> (CStream&s, std::vector<int64_t> &a) { vector_long d; s>>d; a=d; return s; }
00292 inline CStream& operator >> (CStream&s, std::vector<uint8_t> &a) { vector_byte d; s>>d; a=d; return s; }
00293 inline CStream& operator >> (CStream&s, std::vector<uint16_t> &a) { vector_word d; s>>d; a=d; return s; }
00294 inline CStream& operator >> (CStream&s, std::vector<uint32_t> &a) { vector_uint d; s>>d; a=d; return s; }
00295
00296 #if MRPT_WORD_SIZE!=32 // If it's 32 bit, size_t <=> uint32_t
00297 inline CStream& operator >> (CStream&s, std::vector<size_t> &a) { vector_size_t d; s>>d; a=d; return s; }
00298 #endif
00299
00300
00301 }
00302 }
00303
00304 #endif