00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef _TypeStream_incl_
00020 #define _TypeStream_incl_
00021
00022 #include <rel/StreamCoder.h>
00023
00024 #include <string>
00025
00026 namespace rel
00027 {
00028
00029
00030
00031 struct BeginKey
00032 {
00033 OpaqueValue name;
00034
00035 BeginKey() {}
00036 BeginKey(const BeginKey &src)
00037 : name(src.name) {}
00038 };
00039
00040
00041
00042 struct EndKey
00043 {
00044 };
00045
00046
00057 class TypeStream
00058 {
00059 public:
00060 typedef StreamCoder::size_type size_type;
00061
00062 typedef bool (*CodingWriteFunction)(TypeStream &, const OpaqueValue &);
00063
00064
00065
00066
00067
00068
00069
00070
00071 static void addEncoder(CodingWriteFunction func, const char *typeName);
00072
00073 TypeStream(const SmartPtr<StreamCoder> &streamCoder);
00074 virtual ~TypeStream();
00075
00076
00077 bool atEnd() const;
00078 void flush();
00079 int getReadDescriptor() const;
00080 bool isDirectAccess() const;
00081 size_type at() const;
00082 void at(size_type offset);
00083
00084
00085
00086 bool read(uint8_t &) const;
00087 bool read(int32_t &) const;
00088 bool read(int64_t &) const;
00089
00090 bool read(double &) const;
00091
00092 bool read(std::string &) const;
00093
00094
00095 bool read(OpaqueValue &) const;
00096
00097
00098
00099 bool write(const uint8_t &);
00100 bool write(const int32_t &);
00101 bool write(const int64_t &);
00102
00103 bool write(const double &);
00104
00105 bool write(const std::string &);
00106 bool write(const char *bytes, size_type length);
00107
00108
00109 bool write(const OpaqueValue &);
00110
00111
00112
00113 bool writeBeginKey();
00114 bool writeEndKey();
00115
00116 bool readBeginKey() const;
00117 bool readEndKey() const;
00118
00119
00120 bool writeNull();
00121 bool readNull() const;
00122
00123 bool append(const TypeStream &src);
00124
00125
00126 SmartPtr<char*> print();
00127
00128 private:
00129 void init();
00130 bool checkType(uint8_t expectedType) const;
00131 bool readConstructable(OpaqueValue &value) const;
00132 static void addEncoder(CodingWriteFunction func, const char *typeName,
00133 uint8_t marker);
00134
00135 struct TypeStreamImpl *impl;
00136 };
00137
00138 class OperationError : public std::exception
00139 {
00140 string _what;
00141 public:
00142 OperationError(const char *operationType, const char *operandType);
00143 OperationError(const char *operationType,
00144 const std::string &operandType);
00145 const char *what() const;
00146 };
00147
00148 }
00149
00150
00151
00152
00153 #define TYPEDIOINTERFACE(Type) \
00154 rel::TypeStream &operator << (rel::TypeStream &, const Type &); \
00155 const rel::TypeStream &operator >> (const rel::TypeStream &, Type &);
00156
00157 TYPEDIOINTERFACE(uint8_t)
00158 TYPEDIOINTERFACE(int32_t)
00159 TYPEDIOINTERFACE(int64_t)
00160 TYPEDIOINTERFACE(double)
00161 TYPEDIOINTERFACE(std::string)
00162 TYPEDIOINTERFACE(rel::OpaqueValue)
00163
00164
00165 rel::TypeStream &operator << (rel::TypeStream &, const char *);
00166
00167
00168
00169 rel::TypeStream &operator << (rel::TypeStream &, const rel::TypeStream &);
00170
00171 rel::TypeStream &operator >> (rel::TypeStream &, rel::TypeStream &);
00172
00173 #endif
00174