| 1 | // [License]
|
---|
| 2 | // The Ariba-Underlay Copyright
|
---|
| 3 | //
|
---|
| 4 | // Copyright (c) 2008-2009, Institute of Telematics, UniversitÀt Karlsruhe (TH)
|
---|
| 5 | //
|
---|
| 6 | // Institute of Telematics
|
---|
| 7 | // UniversitÀt Karlsruhe (TH)
|
---|
| 8 | // Zirkel 2, 76128 Karlsruhe
|
---|
| 9 | // Germany
|
---|
| 10 | //
|
---|
| 11 | // Redistribution and use in source and binary forms, with or without
|
---|
| 12 | // modification, are permitted provided that the following conditions are
|
---|
| 13 | // met:
|
---|
| 14 | //
|
---|
| 15 | // 1. Redistributions of source code must retain the above copyright
|
---|
| 16 | // notice, this list of conditions and the following disclaimer.
|
---|
| 17 | // 2. Redistributions in binary form must reproduce the above copyright
|
---|
| 18 | // notice, this list of conditions and the following disclaimer in the
|
---|
| 19 | // documentation and/or other materials provided with the distribution.
|
---|
| 20 | //
|
---|
| 21 | // THIS SOFTWARE IS PROVIDED BY THE INSTITUTE OF TELEMATICS ``AS IS'' AND
|
---|
| 22 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
| 23 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
| 24 | // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ARIBA PROJECT OR
|
---|
| 25 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
| 26 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
---|
| 27 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
---|
| 28 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
---|
| 29 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
---|
| 30 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
---|
| 31 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 32 | //
|
---|
| 33 | // The views and conclusions contained in the software and documentation
|
---|
| 34 | // are those of the authors and should not be interpreted as representing
|
---|
| 35 | // official policies, either expressed or implied, of the Institute of
|
---|
| 36 | // Telematics.
|
---|
| 37 | // [License]
|
---|
| 38 |
|
---|
| 39 | #ifndef SERIALIZATION_HPP_
|
---|
| 40 | #define SERIALIZATION_HPP_
|
---|
| 41 |
|
---|
| 42 | //---------------------------------------------------------------------------
|
---|
| 43 | //= namespace adaption =
|
---|
| 44 | #ifndef SERIALIZATION_NS
|
---|
| 45 |
|
---|
| 46 | #define SERIALIZATION_NS ariba::utility::serialization
|
---|
| 47 | #define SERIALIZATION_NS_INT SERIALIZATION_NS::internal
|
---|
| 48 |
|
---|
| 49 | //---------------------------------------------------------------------------
|
---|
| 50 | //= some words on the namespaces =
|
---|
| 51 | #define SERIALIZATION_NS_BEGIN() \
|
---|
| 52 | namespace ariba { namespace utility { namespace serialization {
|
---|
| 53 |
|
---|
| 54 | #define SERIALIZATION_NS_END() }}}
|
---|
| 55 |
|
---|
| 56 | #endif
|
---|
| 57 |
|
---|
| 58 | //---------------------------------------------------------------------------
|
---|
| 59 | //= some definition of method and class names =
|
---|
| 60 | #define SERIALIZATION_METHOD_NAME __mSerialization
|
---|
| 61 | #define SERIALIZATION_CLASS_NAME __cSerialization
|
---|
| 62 |
|
---|
| 63 | //---------------------------------------------------------------------------
|
---|
| 64 | //= namespace specifications =
|
---|
| 65 | #define USING_SERIALIZATION using namespace SERIALIZATION_NS;
|
---|
| 66 | #define using_serialization USING_SERIALIZATION;
|
---|
| 67 |
|
---|
| 68 | //---------------------------------------------------------------------------
|
---|
| 69 | //== prototypes ==
|
---|
| 70 |
|
---|
| 71 | // public serializer prototype
|
---|
| 72 | template<typename __Y, int __V> class SERIALIZATION_CLASS_NAME;
|
---|
| 73 |
|
---|
| 74 | SERIALIZATION_NS_BEGIN()
|
---|
| 75 |
|
---|
| 76 | // the internal namespace
|
---|
| 77 | namespace internal {}
|
---|
| 78 |
|
---|
| 79 | // default variants
|
---|
| 80 | const int DEFAULT_V = 0;
|
---|
| 81 | const int STRING_V = 1;
|
---|
| 82 |
|
---|
| 83 | // serialization mode
|
---|
| 84 | enum Mode {
|
---|
| 85 | UNDEFINED = 0,
|
---|
| 86 | SERIALIZE = 1,
|
---|
| 87 | DESERIALIZE = 2,
|
---|
| 88 | MEASURE = 3
|
---|
| 89 | };
|
---|
| 90 |
|
---|
| 91 | SERIALIZATION_NS_END()
|
---|
| 92 |
|
---|
| 93 | //---------------------------------------------------------------------------
|
---|
| 94 | //= Serialization Macros =
|
---|
| 95 |
|
---|
| 96 | #define SERIALIZATION_USE_INTERNAL_NS \
|
---|
| 97 | using namespace SERIALIZATION_NS_INT; \
|
---|
| 98 | using namespace SERIALIZATION_NS;
|
---|
| 99 |
|
---|
| 100 | //= serialization method =
|
---|
| 101 | #define ISERIALIZATION_METHOD_BEGIN( Buffer, Const ) \
|
---|
| 102 | public: template<typename __X> \
|
---|
| 103 | finline bool SERIALIZATION_METHOD_NAME( __X& Buffer ) Const { \
|
---|
| 104 | SERIALIZATION_USE_INTERNAL_NS \
|
---|
| 105 | bool __ok = false; do {
|
---|
| 106 |
|
---|
| 107 | #define RSERIALIZATION_METHOD_BEGIN( Buffer ) \
|
---|
| 108 | ISERIALIZATION_METHOD_BEGIN( Buffer, )
|
---|
| 109 |
|
---|
| 110 | #define SERIALIZATION_METHOD_BEGIN( Buffer ) \
|
---|
| 111 | ISERIALIZATION_METHOD_BEGIN( Buffer, const )
|
---|
| 112 |
|
---|
| 113 | #define SERIALIZATION_METHOD_END() \
|
---|
| 114 | __ok = true; } while (false); return __ok; }
|
---|
| 115 |
|
---|
| 116 | // convenience
|
---|
| 117 | #define sznMethodBegin( Buffer ) SERIALIZATION_METHOD_BEGIN( Buffer )
|
---|
| 118 | #define sznMethodEnd() SERIALIZATION_METHOD_END()
|
---|
| 119 |
|
---|
| 120 | //= serialization class stub =
|
---|
| 121 | #define SERIALIZEABLE \
|
---|
| 122 | public: template<typename __Y,int __V> \
|
---|
| 123 | friend class ::SERIALIZATION_CLASS_NAME;
|
---|
| 124 |
|
---|
| 125 | #define sznStub SERIALIZEABLE
|
---|
| 126 |
|
---|
| 127 | //= serialization code generation =
|
---|
| 128 | #define SERIALIZATION_BEGIN( Class, Variant, Buffer ) \
|
---|
| 129 | template<> \
|
---|
| 130 | class SERIALIZATION_CLASS_NAME \
|
---|
| 131 | <Class, SERIALIZATION_NS::Variant> : Class { \
|
---|
| 132 | RSERIALIZATION_METHOD_BEGIN( Buffer )
|
---|
| 133 |
|
---|
| 134 | #define SERIALIZATION_END() \
|
---|
| 135 | SERIALIZATION_METHOD_END() };
|
---|
| 136 |
|
---|
| 137 | // convenience
|
---|
| 138 | #define sznBegin(Class,Variant,Buffer) \
|
---|
| 139 | SERIALIZATION_BEGIN( Class, Variant, Buffer )
|
---|
| 140 | #define sznBeginDefault( Class, Buffer ) \
|
---|
| 141 | SERIALIZATION_BEGIN( Class, DEFAULT_V, Buffer )
|
---|
| 142 | #define sznEnd() SERIALIZATION_END()
|
---|
| 143 |
|
---|
| 144 | //= virtual serialization =
|
---|
| 145 | #define VSERIALIZEABLE \
|
---|
| 146 | SERIALIZEABLE \
|
---|
| 147 | virtual size_t SERIALIZATION_METHOD_NAME( \
|
---|
| 148 | SERIALIZATION_NS::Mode __mode, \
|
---|
| 149 | Data& __data, \
|
---|
| 150 | int __variant = SERIALIZATION_NS::DEFAULT_V \
|
---|
| 151 | );
|
---|
| 152 |
|
---|
| 153 | #define VSERIALIZATION_BEGIN( Class ) \
|
---|
| 154 | size_t Class::SERIALIZATION_METHOD_NAME( \
|
---|
| 155 | SERIALIZATION_NS::Mode __mode, \
|
---|
| 156 | Data& __data, \
|
---|
| 157 | int __variant \
|
---|
| 158 | ) { \
|
---|
| 159 | USING_SERIALIZATION; \
|
---|
| 160 | SERIALIZATION_USE_INTERNAL_NS; \
|
---|
| 161 | switch (__variant) {
|
---|
| 162 |
|
---|
| 163 | #define VSERIALIZATION_END() \
|
---|
| 164 | } return 0; }
|
---|
| 165 |
|
---|
| 166 | #define VSERIALIZATION_REG( __variant ) \
|
---|
| 167 | case __variant: \
|
---|
| 168 | switch (__mode) { \
|
---|
| 169 | case SERIALIZE: \
|
---|
| 170 | __data = data_serialize_v<__variant>(*this); \
|
---|
| 171 | return __data.getLength(); \
|
---|
| 172 | case DESERIALIZE: \
|
---|
| 173 | return data_deserialize_v<__variant>(*this, __data); \
|
---|
| 174 | case MEASURE: \
|
---|
| 175 | return data_length_v<__variant>(*this); \
|
---|
| 176 | case UNDEFINED: \
|
---|
| 177 | return 0; \
|
---|
| 178 | } break;
|
---|
| 179 |
|
---|
| 180 | #define VSERIALIZATION_DEFAULT( Class ) \
|
---|
| 181 | USING_SERIALIZATION \
|
---|
| 182 | VSERIALIZATION_BEGIN( Class ) \
|
---|
| 183 | VSERIALIZATION_REG( DEFAULT_V ) \
|
---|
| 184 | VSERIALIZATION_END( )
|
---|
| 185 |
|
---|
| 186 | // convenience
|
---|
| 187 | #define vsznStub VSERIALIZEABLE
|
---|
| 188 | #define vsznBegin( Class ) VSERIALIZAION_BEGIN( Class )
|
---|
| 189 | #define vsznEnd() VSERIALIZATION_END()
|
---|
| 190 | #define vsznRegister( Variant ) VSERIALIZATION_REG( Variant )
|
---|
| 191 | #define vsznDefault( Class ) VSERIALIZATION_DEFAULT( Class )
|
---|
| 192 |
|
---|
| 193 | // new: convenience
|
---|
| 194 | #define sznImplBegin(Class) VSERIALIZAION_BEGIN( Class )
|
---|
| 195 | #define sznImplDefault(Class) VSERIALIZATION_DEFAULT( Class )
|
---|
| 196 |
|
---|
| 197 | //---------------------------------------------------------------------------
|
---|
| 198 | //== includes ==
|
---|
| 199 | #include "../internal/Utilities.hpp"
|
---|
| 200 | #include <typeinfo>
|
---|
| 201 | #include <iostream>
|
---|
| 202 |
|
---|
| 203 | //---------------------------------------------------------------------------
|
---|
| 204 | //= Public Serialization Classes =
|
---|
| 205 |
|
---|
| 206 | /**
|
---|
| 207 | * TODO: doc
|
---|
| 208 | */
|
---|
| 209 | template<typename __Y, int __V>
|
---|
| 210 | class SERIALIZATION_CLASS_NAME: __Y {
|
---|
| 211 | public:
|
---|
| 212 | template<class __X>
|
---|
| 213 | finline bool SERIALIZATION_METHOD_NAME(__X& buffer) {
|
---|
| 214 | printf("Serialization not supported for type '%s' "
|
---|
| 215 | "with variant %d for Stream %s.\n",
|
---|
| 216 | typeid(__Y).name(), __V, typeid(__X).name());
|
---|
| 217 | return false;
|
---|
| 218 | }
|
---|
| 219 | };
|
---|
| 220 |
|
---|
| 221 | class Serializeable {};
|
---|
| 222 |
|
---|
| 223 | #include "Data.hpp"
|
---|
| 224 |
|
---|
| 225 | class VSerializeable : public Serializeable {
|
---|
| 226 | public:
|
---|
| 227 | /**
|
---|
| 228 | * Serializes/Deserializes an this object in the specified variant.
|
---|
| 229 | * The special case is, that virtual serializeable objects are bound
|
---|
| 230 | * to byte boundaries -- so they cannot be smaller than a byte.
|
---|
| 231 | *
|
---|
| 232 | * @param data BitData object that holds or is used to serialize data
|
---|
| 233 | * @param mode Mode of operation (serialize/deserialize)
|
---|
| 234 | * @param variant Variant of encoding/decoding
|
---|
| 235 | * @return size of the binary object in bits or zero if unsuccessful
|
---|
| 236 | */
|
---|
| 237 | virtual size_t SERIALIZATION_METHOD_NAME(
|
---|
| 238 | SERIALIZATION_NS::Mode __mode,
|
---|
| 239 | Data& __binary,
|
---|
| 240 | int __variant = SERIALIZATION_NS::DEFAULT_V
|
---|
| 241 | );
|
---|
| 242 | };
|
---|
| 243 |
|
---|
| 244 | /**
|
---|
| 245 | * TODO: doc
|
---|
| 246 | */
|
---|
| 247 | class ExplicitSerializer {
|
---|
| 248 | SERIALIZATION_METHOD_BEGIN(X)
|
---|
| 249 | std::cerr << "Serialization unimplemented" << std::endl;
|
---|
| 250 | SERIALIZATION_METHOD_END()
|
---|
| 251 | };
|
---|
| 252 |
|
---|
| 253 | //---------------------------------------------------------------------------
|
---|
| 254 | //= Serialization Namespace =
|
---|
| 255 |
|
---|
| 256 | SERIALIZATION_NS_BEGIN()
|
---|
| 257 |
|
---|
| 258 | /**
|
---|
| 259 | * TODO: doc
|
---|
| 260 | */
|
---|
| 261 | template<int V, typename Y>
|
---|
| 262 | finline static SERIALIZATION_CLASS_NAME<Y, V>& get_serializer(Y& obj) {
|
---|
| 263 | return *((SERIALIZATION_CLASS_NAME<Y, V>*)(&obj));
|
---|
| 264 | }
|
---|
| 265 | SERIALIZATION_NS_END()
|
---|
| 266 |
|
---|
| 267 | #endif /* SERIALIZATION_HPP_ */
|
---|