| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield |
|---|
| 2 | * |
|---|
| 3 | * This library is open source and may be redistributed and/or modified under |
|---|
| 4 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
|---|
| 5 | * (at your option) any later version. The full license is in LICENSE file |
|---|
| 6 | * included with this distribution, and on the openscenegraph.org website. |
|---|
| 7 | * |
|---|
| 8 | * This library is distributed in the hope that it will be useful, |
|---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | * OpenSceneGraph Public License for more details. |
|---|
| 12 | */ |
|---|
| 13 | // Written by Wang Rui, (C) 2010 |
|---|
| 14 | |
|---|
| 15 | #ifndef OSGDB__SERIALIZER |
|---|
| 16 | #define OSGDB__SERIALIZER |
|---|
| 17 | |
|---|
| 18 | #include <osg/ref_ptr> |
|---|
| 19 | #include <osg/Notify> |
|---|
| 20 | #include <osg/Object> |
|---|
| 21 | #include <osgDB/InputStream> |
|---|
| 22 | #include <osgDB/OutputStream> |
|---|
| 23 | #include <string> |
|---|
| 24 | #include <sstream> |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | namespace osgDB |
|---|
| 28 | { |
|---|
| 29 | |
|---|
| 30 | #ifndef OBJECT_CAST |
|---|
| 31 | #define OBJECT_CAST static_cast |
|---|
| 32 | #endif |
|---|
| 33 | |
|---|
| 34 | class IntLookup |
|---|
| 35 | { |
|---|
| 36 | public: |
|---|
| 37 | typedef int Value; |
|---|
| 38 | typedef std::map<std::string, Value> StringToValue; |
|---|
| 39 | typedef std::map<Value, std::string> ValueToString; |
|---|
| 40 | |
|---|
| 41 | IntLookup() {} |
|---|
| 42 | unsigned int size() const { return _stringToValue.size(); } |
|---|
| 43 | |
|---|
| 44 | void add( const char* str, Value value ) |
|---|
| 45 | { |
|---|
| 46 | if ( _valueToString.find(value)!=_valueToString.end() ) |
|---|
| 47 | { |
|---|
| 48 | osg::notify(osg::WARN) << "Duplicate enum value " << value |
|---|
| 49 | << " with old string: " << _valueToString[value] |
|---|
| 50 | << " and new string: " << str << std::endl; |
|---|
| 51 | } |
|---|
| 52 | _valueToString[value] = str; |
|---|
| 53 | _stringToValue[str] = value; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | Value getValue( const char* str ) |
|---|
| 57 | { |
|---|
| 58 | StringToValue::iterator itr = _stringToValue.find(str); |
|---|
| 59 | if ( itr==_stringToValue.end() ) |
|---|
| 60 | { |
|---|
| 61 | Value value; |
|---|
| 62 | std::stringstream stream; |
|---|
| 63 | stream << str; stream >> value; |
|---|
| 64 | _stringToValue[str] = value; |
|---|
| 65 | return value; |
|---|
| 66 | } |
|---|
| 67 | return itr->second; |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | const std::string& getString( Value value ) |
|---|
| 71 | { |
|---|
| 72 | ValueToString::iterator itr = _valueToString.find(value); |
|---|
| 73 | if ( itr==_valueToString.end() ) |
|---|
| 74 | { |
|---|
| 75 | std::string str; |
|---|
| 76 | std::stringstream stream; |
|---|
| 77 | stream << value; stream >> str; |
|---|
| 78 | _valueToString[value] = str; |
|---|
| 79 | return _valueToString[value]; |
|---|
| 80 | } |
|---|
| 81 | return itr->second; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | protected: |
|---|
| 85 | StringToValue _stringToValue; |
|---|
| 86 | ValueToString _valueToString; |
|---|
| 87 | }; |
|---|
| 88 | |
|---|
| 89 | class UserLookupTableProxy |
|---|
| 90 | { |
|---|
| 91 | public: |
|---|
| 92 | typedef void (*AddValueFunc)( IntLookup* lookup ); |
|---|
| 93 | UserLookupTableProxy( AddValueFunc func ) { if ( func ) (*func)(&_lookup); } |
|---|
| 94 | |
|---|
| 95 | IntLookup _lookup; |
|---|
| 96 | }; |
|---|
| 97 | |
|---|
| 98 | #define BEGIN_USER_TABLE(NAME, CLASS) \ |
|---|
| 99 | static void add_user_value_func_##NAME(osgDB::IntLookup*); \ |
|---|
| 100 | static osgDB::UserLookupTableProxy s_user_lookup_table_##NAME(&add_user_value_func_##NAME); \ |
|---|
| 101 | static void add_user_value_func_##NAME(osgDB::IntLookup* lookup) { typedef CLASS MyClass |
|---|
| 102 | #define ADD_USER_VALUE(VALUE) lookup->add(#VALUE, MyClass::VALUE) |
|---|
| 103 | #define END_USER_TABLE() } |
|---|
| 104 | |
|---|
| 105 | #define USER_READ_FUNC(NAME, FUNCNAME) \ |
|---|
| 106 | static int FUNCNAME(osgDB::InputStream& is) { \ |
|---|
| 107 | int value; if (is.isBinary()) is >> value; \ |
|---|
| 108 | else { std::string str; is >> str; \ |
|---|
| 109 | value = (s_user_lookup_table_##NAME)._lookup.getValue(str.c_str()); } \ |
|---|
| 110 | return value; } |
|---|
| 111 | |
|---|
| 112 | #define USER_WRITE_FUNC(NAME, FUNCNAME) \ |
|---|
| 113 | static void FUNCNAME(osgDB::OutputStream& os, int value) { \ |
|---|
| 114 | if (os.isBinary()) os << value; \ |
|---|
| 115 | else os << (s_user_lookup_table_##NAME)._lookup.getString(value); } \ |
|---|
| 116 | |
|---|
| 117 | class BaseSerializer : public osg::Referenced |
|---|
| 118 | { |
|---|
| 119 | public: |
|---|
| 120 | BaseSerializer() {} |
|---|
| 121 | virtual bool read( InputStream&, osg::Object& ) = 0; |
|---|
| 122 | virtual bool write( OutputStream&, const osg::Object& ) = 0; |
|---|
| 123 | virtual const std::string& getName() const = 0; |
|---|
| 124 | }; |
|---|
| 125 | |
|---|
| 126 | template<typename C> |
|---|
| 127 | class UserSerializer : public BaseSerializer |
|---|
| 128 | { |
|---|
| 129 | public: |
|---|
| 130 | typedef bool (*Checker)( const C& ); |
|---|
| 131 | typedef bool (*Reader)( InputStream&, C& ); |
|---|
| 132 | typedef bool (*Writer)( OutputStream&, const C& ); |
|---|
| 133 | |
|---|
| 134 | UserSerializer( const char* name, Checker cf, Reader rf, Writer wf ) |
|---|
| 135 | : _name(name), _checker(cf), _reader(rf), _writer(wf) {} |
|---|
| 136 | |
|---|
| 137 | virtual bool read( InputStream& is, osg::Object& obj ) |
|---|
| 138 | { |
|---|
| 139 | C& object = OBJECT_CAST<C&>(obj); |
|---|
| 140 | if ( !is.isBinary() && !is.matchString(_name) ) |
|---|
| 141 | return true; |
|---|
| 142 | return (*_reader)(is, object); |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | virtual bool write( OutputStream& os, const osg::Object& obj ) |
|---|
| 146 | { |
|---|
| 147 | const C& object = OBJECT_CAST<const C&>(obj); |
|---|
| 148 | if ( !os.isBinary() ) |
|---|
| 149 | { |
|---|
| 150 | if ( !(*_checker)(object) ) return true; |
|---|
| 151 | os << _name; |
|---|
| 152 | } |
|---|
| 153 | return (*_writer)(os, object); |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | virtual const std::string& getName() const { return _name; } |
|---|
| 157 | |
|---|
| 158 | protected: |
|---|
| 159 | std::string _name; |
|---|
| 160 | Checker _checker; |
|---|
| 161 | Reader _reader; |
|---|
| 162 | Writer _writer; |
|---|
| 163 | }; |
|---|
| 164 | |
|---|
| 165 | template<typename P> |
|---|
| 166 | class TemplateSerializer : public BaseSerializer |
|---|
| 167 | { |
|---|
| 168 | public: |
|---|
| 169 | TemplateSerializer( const char* name ) |
|---|
| 170 | : _name(name) {} |
|---|
| 171 | |
|---|
| 172 | virtual bool read( InputStream& is, osg::Object& obj ) = 0; |
|---|
| 173 | virtual bool write( OutputStream& os, const osg::Object& obj ) = 0; |
|---|
| 174 | virtual const std::string& getName() const { return _name; } |
|---|
| 175 | |
|---|
| 176 | protected: |
|---|
| 177 | std::string _name; |
|---|
| 178 | P _defaultValue; |
|---|
| 179 | }; |
|---|
| 180 | |
|---|
| 181 | template<typename C, typename P> |
|---|
| 182 | class PropByValSerializer : public TemplateSerializer<P> |
|---|
| 183 | { |
|---|
| 184 | public: |
|---|
| 185 | typedef TemplateSerializer<P> ParentType; |
|---|
| 186 | typedef P (C::*Getter)() const; |
|---|
| 187 | typedef void (C::*Setter)( P ); |
|---|
| 188 | |
|---|
| 189 | PropByValSerializer( const char* name, P def, Getter gf, Setter sf, bool useHex=false ) |
|---|
| 190 | : ParentType(name), _getter(gf), _setter(sf), _useHex(useHex) |
|---|
| 191 | { ParentType::_defaultValue = def; } |
|---|
| 192 | |
|---|
| 193 | virtual bool read( InputStream& is, osg::Object& obj ) |
|---|
| 194 | { |
|---|
| 195 | C& object = OBJECT_CAST<C&>(obj); |
|---|
| 196 | P value; |
|---|
| 197 | if ( is.isBinary() ) |
|---|
| 198 | { |
|---|
| 199 | is >> value; |
|---|
| 200 | if ( ParentType::_defaultValue!=value ) |
|---|
| 201 | (object.*_setter)( value ); |
|---|
| 202 | } |
|---|
| 203 | else if ( is.matchString(ParentType::_name) ) |
|---|
| 204 | { |
|---|
| 205 | if ( _useHex ) is >> std::hex; |
|---|
| 206 | is >> value; |
|---|
| 207 | if ( _useHex ) is >> std::dec; |
|---|
| 208 | (object.*_setter)( value ); |
|---|
| 209 | } |
|---|
| 210 | return true; |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | virtual bool write( OutputStream& os, const osg::Object& obj ) |
|---|
| 214 | { |
|---|
| 215 | const C& object = OBJECT_CAST<const C&>(obj); |
|---|
| 216 | if ( os.isBinary() ) |
|---|
| 217 | { |
|---|
| 218 | os << (object.*_getter)(); |
|---|
| 219 | } |
|---|
| 220 | else if ( ParentType::_defaultValue!=(object.*_getter)() ) |
|---|
| 221 | { |
|---|
| 222 | os << ParentType::_name; |
|---|
| 223 | if ( _useHex ) os << std::hex; |
|---|
| 224 | os << (object.*_getter)(); |
|---|
| 225 | if ( _useHex ) os << std::dec; |
|---|
| 226 | os << std::endl; |
|---|
| 227 | } |
|---|
| 228 | return true; |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | protected: |
|---|
| 232 | Getter _getter; |
|---|
| 233 | Setter _setter; |
|---|
| 234 | bool _useHex; |
|---|
| 235 | }; |
|---|
| 236 | |
|---|
| 237 | template<typename C, typename P> |
|---|
| 238 | class PropByRefSerializer : public TemplateSerializer<P> |
|---|
| 239 | { |
|---|
| 240 | public: |
|---|
| 241 | typedef TemplateSerializer<P> ParentType; |
|---|
| 242 | typedef const P& CP; |
|---|
| 243 | typedef CP (C::*Getter)() const; |
|---|
| 244 | typedef void (C::*Setter)( CP ); |
|---|
| 245 | |
|---|
| 246 | PropByRefSerializer( const char* name, CP def, Getter gf, Setter sf ) |
|---|
| 247 | : ParentType(name), _getter(gf), _setter(sf) |
|---|
| 248 | { ParentType::_defaultValue = def; } |
|---|
| 249 | |
|---|
| 250 | virtual bool read( InputStream& is, osg::Object& obj ) |
|---|
| 251 | { |
|---|
| 252 | C& object = OBJECT_CAST<C&>(obj); |
|---|
| 253 | P value; |
|---|
| 254 | if ( is.isBinary() ) |
|---|
| 255 | { |
|---|
| 256 | is >> value; |
|---|
| 257 | if ( ParentType::_defaultValue!=value ) |
|---|
| 258 | (object.*_setter)( value ); |
|---|
| 259 | } |
|---|
| 260 | else if ( is.matchString(ParentType::_name) ) |
|---|
| 261 | { |
|---|
| 262 | is >> value; |
|---|
| 263 | (object.*_setter)( value ); |
|---|
| 264 | } |
|---|
| 265 | return true; |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | virtual bool write( OutputStream& os, const osg::Object& obj ) |
|---|
| 269 | { |
|---|
| 270 | const C& object = OBJECT_CAST<const C&>(obj); |
|---|
| 271 | if ( os.isBinary() ) |
|---|
| 272 | { |
|---|
| 273 | os << (object.*_getter)(); |
|---|
| 274 | } |
|---|
| 275 | else if ( ParentType::_defaultValue!=(object.*_getter)() ) |
|---|
| 276 | { |
|---|
| 277 | os << ParentType::_name << (object.*_getter)() << std::endl; |
|---|
| 278 | } |
|---|
| 279 | return true; |
|---|
| 280 | } |
|---|
| 281 | |
|---|
| 282 | protected: |
|---|
| 283 | Getter _getter; |
|---|
| 284 | Setter _setter; |
|---|
| 285 | }; |
|---|
| 286 | |
|---|
| 287 | template<typename C> |
|---|
| 288 | class MatrixSerializer : public TemplateSerializer<osg::Matrix> |
|---|
| 289 | { |
|---|
| 290 | public: |
|---|
| 291 | typedef TemplateSerializer<osg::Matrix> ParentType; |
|---|
| 292 | typedef const osg::Matrix& (C::*Getter)() const; |
|---|
| 293 | typedef void (C::*Setter)( const osg::Matrix& ); |
|---|
| 294 | |
|---|
| 295 | MatrixSerializer( const char* name, const osg::Matrix& def, Getter gf, Setter sf ) |
|---|
| 296 | : ParentType(name), _getter(gf), _setter(sf) |
|---|
| 297 | { ParentType::_defaultValue = def; } |
|---|
| 298 | |
|---|
| 299 | virtual bool read( InputStream& is, osg::Object& obj ) |
|---|
| 300 | { |
|---|
| 301 | C& object = OBJECT_CAST<C&>(obj); |
|---|
| 302 | osg::Matrix value; |
|---|
| 303 | if ( is.isBinary() ) |
|---|
| 304 | { |
|---|
| 305 | readMatrixImplementation( is, value ); |
|---|
| 306 | if ( ParentType::_defaultValue!=value ) |
|---|
| 307 | (object.*_setter)( value ); |
|---|
| 308 | } |
|---|
| 309 | else if ( is.matchString(ParentType::_name) ) |
|---|
| 310 | { |
|---|
| 311 | readMatrixImplementation( is, value ); |
|---|
| 312 | (object.*_setter)( value ); |
|---|
| 313 | } |
|---|
| 314 | return true; |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | virtual bool write( OutputStream& os, const osg::Object& obj ) |
|---|
| 318 | { |
|---|
| 319 | const C& object = OBJECT_CAST<const C&>(obj); |
|---|
| 320 | if ( os.isBinary() ) |
|---|
| 321 | { |
|---|
| 322 | os << (object.*_getter)(); |
|---|
| 323 | } |
|---|
| 324 | else if ( ParentType::_defaultValue!=(object.*_getter)() ) |
|---|
| 325 | { |
|---|
| 326 | os << ParentType::_name << (object.*_getter)() << std::endl; |
|---|
| 327 | } |
|---|
| 328 | return true; |
|---|
| 329 | } |
|---|
| 330 | |
|---|
| 331 | protected: |
|---|
| 332 | void readMatrixImplementation( InputStream& is, osg::Matrix& matrix ) |
|---|
| 333 | { |
|---|
| 334 | if ( is.getUseFloatMatrix() ) |
|---|
| 335 | { |
|---|
| 336 | osg::Matrixf realValue; is >> realValue; |
|---|
| 337 | matrix = realValue; |
|---|
| 338 | } |
|---|
| 339 | else |
|---|
| 340 | { |
|---|
| 341 | osg::Matrixd realValue; is >> realValue; |
|---|
| 342 | matrix = realValue; |
|---|
| 343 | } |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | Getter _getter; |
|---|
| 347 | Setter _setter; |
|---|
| 348 | }; |
|---|
| 349 | |
|---|
| 350 | template<typename C, typename P> |
|---|
| 351 | class GLenumSerializer : public TemplateSerializer<P> |
|---|
| 352 | { |
|---|
| 353 | public: |
|---|
| 354 | typedef TemplateSerializer<P> ParentType; |
|---|
| 355 | typedef P (C::*Getter)() const; |
|---|
| 356 | typedef void (C::*Setter)( P ); |
|---|
| 357 | |
|---|
| 358 | GLenumSerializer( const char* name, P def, Getter gf, Setter sf ) |
|---|
| 359 | : ParentType(name), _getter(gf), _setter(sf) |
|---|
| 360 | { ParentType::_defaultValue = def; } |
|---|
| 361 | |
|---|
| 362 | virtual bool read( InputStream& is, osg::Object& obj ) |
|---|
| 363 | { |
|---|
| 364 | C& object = OBJECT_CAST<C&>(obj); |
|---|
| 365 | if ( is.isBinary() ) |
|---|
| 366 | { |
|---|
| 367 | GLenum value; is >> value; |
|---|
| 368 | if ( ParentType::_defaultValue!=static_cast<P>(value) ) |
|---|
| 369 | (object.*_setter)( static_cast<P>(value) ); |
|---|
| 370 | } |
|---|
| 371 | else if ( is.matchString(ParentType::_name) ) |
|---|
| 372 | { |
|---|
| 373 | DEF_GLENUM(value); is >> value; |
|---|
| 374 | (object.*_setter)( static_cast<P>(value.get()) ); |
|---|
| 375 | } |
|---|
| 376 | return true; |
|---|
| 377 | } |
|---|
| 378 | |
|---|
| 379 | virtual bool write( OutputStream& os, const osg::Object& obj ) |
|---|
| 380 | { |
|---|
| 381 | const C& object = OBJECT_CAST<const C&>(obj); |
|---|
| 382 | if ( os.isBinary() ) |
|---|
| 383 | { |
|---|
| 384 | os << static_cast<GLenum>((object.*_getter)()); |
|---|
| 385 | } |
|---|
| 386 | else if ( ParentType::_defaultValue!=(object.*_getter)() ) |
|---|
| 387 | { |
|---|
| 388 | os << ParentType::_name << GLENUM((object.*_getter)()) << std::endl; |
|---|
| 389 | } |
|---|
| 390 | return true; |
|---|
| 391 | } |
|---|
| 392 | |
|---|
| 393 | protected: |
|---|
| 394 | Getter _getter; |
|---|
| 395 | Setter _setter; |
|---|
| 396 | }; |
|---|
| 397 | |
|---|
| 398 | template<typename C> |
|---|
| 399 | class StringSerializer : public TemplateSerializer<std::string> |
|---|
| 400 | { |
|---|
| 401 | public: |
|---|
| 402 | typedef TemplateSerializer<std::string> ParentType; |
|---|
| 403 | typedef const std::string& (C::*Getter)() const; |
|---|
| 404 | typedef void (C::*Setter)( const std::string& ); |
|---|
| 405 | |
|---|
| 406 | StringSerializer( const char* name, const std::string& def, Getter gf, Setter sf ) |
|---|
| 407 | : ParentType(name), _getter(gf), _setter(sf) |
|---|
| 408 | { ParentType::_defaultValue = def; } |
|---|
| 409 | |
|---|
| 410 | virtual bool read( InputStream& is, osg::Object& obj ) |
|---|
| 411 | { |
|---|
| 412 | C& object = OBJECT_CAST<C&>(obj); |
|---|
| 413 | std::string value; |
|---|
| 414 | if ( is.isBinary() ) |
|---|
| 415 | { |
|---|
| 416 | is >> value; |
|---|
| 417 | if ( ParentType::_defaultValue!=value ) |
|---|
| 418 | (object.*_setter)( value ); |
|---|
| 419 | } |
|---|
| 420 | else if ( is.matchString(ParentType::_name) ) |
|---|
| 421 | { |
|---|
| 422 | is.readWrappedString( value ); |
|---|
| 423 | if ( !value.empty() ) |
|---|
| 424 | (object.*_setter)( value ); |
|---|
| 425 | } |
|---|
| 426 | return true; |
|---|
| 427 | } |
|---|
| 428 | |
|---|
| 429 | virtual bool write( OutputStream& os, const osg::Object& obj ) |
|---|
| 430 | { |
|---|
| 431 | const C& object = OBJECT_CAST<const C&>(obj); |
|---|
| 432 | if ( os.isBinary() ) |
|---|
| 433 | { |
|---|
| 434 | os << (object.*_getter)(); |
|---|
| 435 | } |
|---|
| 436 | else if ( ParentType::_defaultValue!=(object.*_getter)() ) |
|---|
| 437 | { |
|---|
| 438 | os << ParentType::_name; |
|---|
| 439 | os.writeWrappedString( (object.*_getter)() ); |
|---|
| 440 | os << std::endl; |
|---|
| 441 | } |
|---|
| 442 | return true; |
|---|
| 443 | } |
|---|
| 444 | |
|---|
| 445 | protected: |
|---|
| 446 | Getter _getter; |
|---|
| 447 | Setter _setter; |
|---|
| 448 | }; |
|---|
| 449 | |
|---|
| 450 | template<typename C, typename P> |
|---|
| 451 | class ObjectSerializer : public TemplateSerializer<P*> |
|---|
| 452 | { |
|---|
| 453 | public: |
|---|
| 454 | typedef TemplateSerializer<P*> ParentType; |
|---|
| 455 | typedef const P* (C::*Getter)() const; |
|---|
| 456 | typedef void (C::*Setter)( P* ); |
|---|
| 457 | |
|---|
| 458 | ObjectSerializer( const char* name, P* def, Getter gf, Setter sf ) |
|---|
| 459 | : ParentType(name), _getter(gf), _setter(sf) |
|---|
| 460 | { ParentType::_defaultValue = def; } |
|---|
| 461 | |
|---|
| 462 | virtual bool read( InputStream& is, osg::Object& obj ) |
|---|
| 463 | { |
|---|
| 464 | C& object = OBJECT_CAST<C&>(obj); |
|---|
| 465 | bool hasObject = false; |
|---|
| 466 | if ( is.isBinary() ) |
|---|
| 467 | { |
|---|
| 468 | is >> hasObject; |
|---|
| 469 | if ( hasObject ) |
|---|
| 470 | { |
|---|
| 471 | P* value = dynamic_cast<P*>( is.readObject() ); |
|---|
| 472 | if ( ParentType::_defaultValue!=value ) |
|---|
| 473 | (object.*_setter)( value ); |
|---|
| 474 | } |
|---|
| 475 | } |
|---|
| 476 | else if ( is.matchString(ParentType::_name) ) |
|---|
| 477 | { |
|---|
| 478 | is >> hasObject; |
|---|
| 479 | if ( hasObject ) |
|---|
| 480 | { |
|---|
| 481 | is >> BEGIN_BRACKET; |
|---|
| 482 | P* value = dynamic_cast<P*>( is.readObject() ); |
|---|
| 483 | (object.*_setter)( value ); |
|---|
| 484 | is >> END_BRACKET; |
|---|
| 485 | } |
|---|
| 486 | } |
|---|
| 487 | return true; |
|---|
| 488 | } |
|---|
| 489 | |
|---|
| 490 | virtual bool write( OutputStream& os, const osg::Object& obj ) |
|---|
| 491 | { |
|---|
| 492 | const C& object = OBJECT_CAST<const C&>(obj); |
|---|
| 493 | bool hasObject = ((object.*_getter)()!=NULL); |
|---|
| 494 | if ( os.isBinary() ) |
|---|
| 495 | { |
|---|
| 496 | os << hasObject; |
|---|
| 497 | os.writeObject( (object.*_getter)() ); |
|---|
| 498 | } |
|---|
| 499 | else if ( ParentType::_defaultValue!=(object.*_getter)() ) |
|---|
| 500 | { |
|---|
| 501 | os << ParentType::_name << hasObject; |
|---|
| 502 | if ( hasObject ) |
|---|
| 503 | { |
|---|
| 504 | os << BEGIN_BRACKET << std::endl; |
|---|
| 505 | os.writeObject( (object.*_getter)() ); |
|---|
| 506 | os << END_BRACKET; |
|---|
| 507 | } |
|---|
| 508 | os << std::endl; |
|---|
| 509 | } |
|---|
| 510 | return true; |
|---|
| 511 | } |
|---|
| 512 | |
|---|
| 513 | protected: |
|---|
| 514 | Getter _getter; |
|---|
| 515 | Setter _setter; |
|---|
| 516 | }; |
|---|
| 517 | |
|---|
| 518 | template<typename C, typename P> |
|---|
| 519 | class ImageSerializer : public TemplateSerializer<P*> |
|---|
| 520 | { |
|---|
| 521 | public: |
|---|
| 522 | typedef TemplateSerializer<P*> ParentType; |
|---|
| 523 | typedef const P* (C::*Getter)() const; |
|---|
| 524 | typedef void (C::*Setter)( P* ); |
|---|
| 525 | |
|---|
| 526 | ImageSerializer( const char* name, P* def, Getter gf, Setter sf ) |
|---|
| 527 | : ParentType(name), _getter(gf), _setter(sf) |
|---|
| 528 | { ParentType::_defaultValue = def; } |
|---|
| 529 | |
|---|
| 530 | virtual bool read( InputStream& is, osg::Object& obj ) |
|---|
| 531 | { |
|---|
| 532 | C& object = OBJECT_CAST<C&>(obj); |
|---|
| 533 | bool hasObject = false; |
|---|
| 534 | if ( is.isBinary() ) |
|---|
| 535 | { |
|---|
| 536 | is >> hasObject; |
|---|
| 537 | if ( hasObject ) |
|---|
| 538 | { |
|---|
| 539 | P* value = dynamic_cast<P*>( is.readImage() ); |
|---|
| 540 | if ( ParentType::_defaultValue!=value ) |
|---|
| 541 | (object.*_setter)( value ); |
|---|
| 542 | } |
|---|
| 543 | } |
|---|
| 544 | else if ( is.matchString(ParentType::_name) ) |
|---|
| 545 | { |
|---|
| 546 | is >> hasObject; |
|---|
| 547 | if ( hasObject ) |
|---|
| 548 | { |
|---|
| 549 | is >> BEGIN_BRACKET; |
|---|
| 550 | P* value = dynamic_cast<P*>( is.readImage() ); |
|---|
| 551 | (object.*_setter)( value ); |
|---|
| 552 | is >> END_BRACKET; |
|---|
| 553 | } |
|---|
| 554 | } |
|---|
| 555 | return true; |
|---|
| 556 | } |
|---|
| 557 | |
|---|
| 558 | virtual bool write( OutputStream& os, const osg::Object& obj ) |
|---|
| 559 | { |
|---|
| 560 | const C& object = OBJECT_CAST<const C&>(obj); |
|---|
| 561 | bool hasObject = ((object.*_getter)()!=NULL); |
|---|
| 562 | if ( os.isBinary() ) |
|---|
| 563 | { |
|---|
| 564 | os << hasObject; |
|---|
| 565 | os.writeImage( (object.*_getter)() ); |
|---|
| 566 | } |
|---|
| 567 | else if ( ParentType::_defaultValue!=(object.*_getter)() ) |
|---|
| 568 | { |
|---|
| 569 | os << ParentType::_name << hasObject; |
|---|
| 570 | if ( hasObject ) |
|---|
| 571 | { |
|---|
| 572 | os << BEGIN_BRACKET << std::endl; |
|---|
| 573 | os.writeImage( (object.*_getter)() ); |
|---|
| 574 | os << END_BRACKET; |
|---|
| 575 | } |
|---|
| 576 | os << std::endl; |
|---|
| 577 | } |
|---|
| 578 | return true; |
|---|
| 579 | } |
|---|
| 580 | |
|---|
| 581 | protected: |
|---|
| 582 | Getter _getter; |
|---|
| 583 | Setter _setter; |
|---|
| 584 | }; |
|---|
| 585 | |
|---|
| 586 | template<typename C, typename P, typename B> |
|---|
| 587 | class EnumSerializer : public TemplateSerializer<P> |
|---|
| 588 | { |
|---|
| 589 | public: |
|---|
| 590 | typedef TemplateSerializer<P> ParentType; |
|---|
| 591 | typedef P (C::*Getter)() const; |
|---|
| 592 | typedef B (C::*Setter)( P ); |
|---|
| 593 | |
|---|
| 594 | EnumSerializer( const char* name, P def, Getter gf, Setter sf ) |
|---|
| 595 | : ParentType(name), _getter(gf), _setter(sf) |
|---|
| 596 | { ParentType::_defaultValue = def; } |
|---|
| 597 | |
|---|
| 598 | void add( const char* str, P value ) |
|---|
| 599 | { _lookup.add(str, static_cast<osgDB::IntLookup::Value>(value)); } |
|---|
| 600 | |
|---|
| 601 | P getValue( const char* str ) |
|---|
| 602 | { return static_cast<P>(_lookup.getValue(str)); } |
|---|
| 603 | |
|---|
| 604 | const std::string& getString( P value ) |
|---|
| 605 | { return _lookup.getString(static_cast<osgDB::IntLookup::Value>(value)); } |
|---|
| 606 | |
|---|
| 607 | virtual bool read( InputStream& is, osg::Object& obj ) |
|---|
| 608 | { |
|---|
| 609 | C& object = OBJECT_CAST<C&>(obj); |
|---|
| 610 | IntLookup::Value value; |
|---|
| 611 | if ( is.isBinary() ) |
|---|
| 612 | { |
|---|
| 613 | is >> value; |
|---|
| 614 | if ( ParentType::_defaultValue!=static_cast<P>(value) ) |
|---|
| 615 | (object.*_setter)( static_cast<P>(value) ); |
|---|
| 616 | } |
|---|
| 617 | else if ( is.matchString(ParentType::_name) ) |
|---|
| 618 | { |
|---|
| 619 | std::string str; is >> str; |
|---|
| 620 | (object.*_setter)( getValue(str.c_str()) ); |
|---|
| 621 | } |
|---|
| 622 | return true; |
|---|
| 623 | } |
|---|
| 624 | |
|---|
| 625 | virtual bool write( osgDB::OutputStream& os, const osg::Object& obj ) |
|---|
| 626 | { |
|---|
| 627 | const C& object = OBJECT_CAST<const C&>(obj); |
|---|
| 628 | if ( os.isBinary() ) |
|---|
| 629 | { |
|---|
| 630 | os << (osgDB::IntLookup::Value)(object.*_getter)(); |
|---|
| 631 | } |
|---|
| 632 | else if ( ParentType::_defaultValue!=(object.*_getter)() ) |
|---|
| 633 | { |
|---|
| 634 | os << ParentType::_name << getString((object.*_getter)()) << std::endl; |
|---|
| 635 | } |
|---|
| 636 | return true; |
|---|
| 637 | } |
|---|
| 638 | |
|---|
| 639 | protected: |
|---|
| 640 | Getter _getter; |
|---|
| 641 | Setter _setter; |
|---|
| 642 | IntLookup _lookup; |
|---|
| 643 | }; |
|---|
| 644 | |
|---|
| 645 | template<typename C, typename P> |
|---|
| 646 | class ListSerializer : public TemplateSerializer<P> |
|---|
| 647 | { |
|---|
| 648 | public: |
|---|
| 649 | typedef TemplateSerializer<P> ParentType; |
|---|
| 650 | typedef typename P::value_type ValueType; |
|---|
| 651 | typedef typename P::const_iterator ConstIterator; |
|---|
| 652 | typedef const P& (C::*Getter)() const; |
|---|
| 653 | typedef void (C::*Setter)( const P& ); |
|---|
| 654 | |
|---|
| 655 | ListSerializer( const char* name, Getter gf, Setter sf ) |
|---|
| 656 | : ParentType(name), _getter(gf), _setter(sf) {} |
|---|
| 657 | |
|---|
| 658 | virtual bool read( InputStream& is, osg::Object& obj ) |
|---|
| 659 | { |
|---|
| 660 | C& object = OBJECT_CAST<C&>(obj); |
|---|
| 661 | unsigned int size = 0; |
|---|
| 662 | P list; |
|---|
| 663 | if ( is.isBinary() ) |
|---|
| 664 | { |
|---|
| 665 | is >> size; |
|---|
| 666 | for ( unsigned int i=0; i<size; ++i ) |
|---|
| 667 | { |
|---|
| 668 | ValueType value; |
|---|
| 669 | is >> value; |
|---|
| 670 | list.push_back( value ); |
|---|
| 671 | } |
|---|
| 672 | if ( size>0 ) (object.*_setter)( list ); |
|---|
| 673 | } |
|---|
| 674 | else if ( is.matchString(ParentType::_name) ) |
|---|
| 675 | { |
|---|
| 676 | is >> size; |
|---|
| 677 | if ( size>0 ) is >> BEGIN_BRACKET; |
|---|
| 678 | for ( unsigned int i=0; i<size; ++i ) |
|---|
| 679 | { |
|---|
| 680 | ValueType value; |
|---|
| 681 | is >> value; |
|---|
| 682 | list.push_back( value ); |
|---|
| 683 | } |
|---|
| 684 | if ( size>0 ) |
|---|
| 685 | { |
|---|
| 686 | is >> END_BRACKET; |
|---|
| 687 | (object.*_setter)( list ); |
|---|
| 688 | } |
|---|
| 689 | } |
|---|
| 690 | return true; |
|---|
| 691 | } |
|---|
| 692 | |
|---|
| 693 | virtual bool write( OutputStream& os, const osg::Object& obj ) |
|---|
| 694 | { |
|---|
| 695 | const C& object = OBJECT_CAST<const C&>(obj); |
|---|
| 696 | const P& list = (object.*_getter)(); |
|---|
| 697 | unsigned int size = (unsigned int)list.size(); |
|---|
| 698 | if ( os.isBinary() ) |
|---|
| 699 | { |
|---|
| 700 | os << size; |
|---|
| 701 | for ( ConstIterator itr=list.begin(); |
|---|
| 702 | itr!=list.end(); ++itr ) |
|---|
| 703 | { |
|---|
| 704 | os << (*itr); |
|---|
| 705 | } |
|---|
| 706 | } |
|---|
| 707 | else if ( size>0 ) |
|---|
| 708 | { |
|---|
| 709 | os << ParentType::_name << size << BEGIN_BRACKET << std::endl; |
|---|
| 710 | for ( ConstIterator itr=list.begin(); |
|---|
| 711 | itr!=list.end(); ++itr ) |
|---|
| 712 | { |
|---|
| 713 | os << (*itr); |
|---|
| 714 | } |
|---|
| 715 | os << END_BRACKET << std::endl; |
|---|
| 716 | } |
|---|
| 717 | return true; |
|---|
| 718 | } |
|---|
| 719 | |
|---|
| 720 | protected: |
|---|
| 721 | Getter _getter; |
|---|
| 722 | Setter _setter; |
|---|
| 723 | }; |
|---|
| 724 | |
|---|
| 725 | // ADDING MANIPULATORS |
|---|
| 726 | #define ADD_SERIALIZER(S) \ |
|---|
| 727 | wrapper->addSerializer(S) |
|---|
| 728 | |
|---|
| 729 | #define ADD_USER_SERIALIZER(PROP) \ |
|---|
| 730 | wrapper->addSerializer( new osgDB::UserSerializer<MyClass>( \ |
|---|
| 731 | #PROP, &check##PROP, &read##PROP, &write##PROP) ) |
|---|
| 732 | |
|---|
| 733 | #define ADD_BOOL_SERIALIZER(PROP, DEF) \ |
|---|
| 734 | wrapper->addSerializer( new osgDB::PropByValSerializer<MyClass, bool>( \ |
|---|
| 735 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 736 | |
|---|
| 737 | #define ADD_SHORT_SERIALIZER(PROP, DEF) \ |
|---|
| 738 | wrapper->addSerializer( new osgDB::PropByValSerializer<MyClass, short>( \ |
|---|
| 739 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 740 | |
|---|
| 741 | #define ADD_USHORT_SERIALIZER(PROP, DEF) \ |
|---|
| 742 | wrapper->addSerializer( new osgDB::PropByValSerializer<MyClass, unsigned short>( \ |
|---|
| 743 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 744 | |
|---|
| 745 | #define ADD_HEXSHORT_SERIALIZER(PROP, DEF) \ |
|---|
| 746 | wrapper->addSerializer( new osgDB::PropByValSerializer<MyClass, unsigned short>( \ |
|---|
| 747 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP, true) ) |
|---|
| 748 | |
|---|
| 749 | #define ADD_INT_SERIALIZER(PROP, DEF) \ |
|---|
| 750 | wrapper->addSerializer( new osgDB::PropByValSerializer<MyClass, int>( \ |
|---|
| 751 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 752 | |
|---|
| 753 | #define ADD_UINT_SERIALIZER(PROP, DEF) \ |
|---|
| 754 | wrapper->addSerializer( new osgDB::PropByValSerializer<MyClass, unsigned int>( \ |
|---|
| 755 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 756 | |
|---|
| 757 | #define ADD_HEXINT_SERIALIZER(PROP, DEF) \ |
|---|
| 758 | wrapper->addSerializer( new osgDB::PropByValSerializer<MyClass, unsigned int>( \ |
|---|
| 759 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP, true) ) |
|---|
| 760 | |
|---|
| 761 | #define ADD_FLOAT_SERIALIZER(PROP, DEF) \ |
|---|
| 762 | wrapper->addSerializer( new osgDB::PropByValSerializer<MyClass, float>( \ |
|---|
| 763 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 764 | |
|---|
| 765 | #define ADD_DOUBLE_SERIALIZER(PROP, DEF) \ |
|---|
| 766 | wrapper->addSerializer( new osgDB::PropByValSerializer<MyClass, double>( \ |
|---|
| 767 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 768 | |
|---|
| 769 | #define ADD_VEC3F_SERIALIZER(PROP, DEF) \ |
|---|
| 770 | wrapper->addSerializer( new osgDB::PropByRefSerializer<MyClass, osg::Vec3f>( \ |
|---|
| 771 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 772 | |
|---|
| 773 | #define ADD_VEC3D_SERIALIZER(PROP, DEF) \ |
|---|
| 774 | wrapper->addSerializer( new osgDB::PropByRefSerializer<MyClass, osg::Vec3d>( \ |
|---|
| 775 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 776 | |
|---|
| 777 | #define ADD_VEC3_SERIALIZER(PROP, DEF) ADD_VEC3F_SERIALIZER(PROP, DEF) |
|---|
| 778 | |
|---|
| 779 | #define ADD_VEC4F_SERIALIZER(PROP, DEF) \ |
|---|
| 780 | wrapper->addSerializer( new osgDB::PropByRefSerializer<MyClass, osg::Vec4f>( \ |
|---|
| 781 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 782 | |
|---|
| 783 | #define ADD_VEC4D_SERIALIZER(PROP, DEF) \ |
|---|
| 784 | wrapper->addSerializer( new osgDB::PropByRefSerializer<MyClass, osg::Vec4d>( \ |
|---|
| 785 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 786 | |
|---|
| 787 | #define ADD_VEC4_SERIALIZER(PROP, DEF) ADD_VEC4F_SERIALIZER(PROP, DEF) |
|---|
| 788 | |
|---|
| 789 | #define ADD_QUAT_SERIALIZER(PROP, DEF) \ |
|---|
| 790 | wrapper->addSerializer( new osgDB::PropByRefSerializer<MyClass, osg::Quat>( \ |
|---|
| 791 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 792 | |
|---|
| 793 | #define ADD_PLANE_SERIALIZER(PROP, DEF) \ |
|---|
| 794 | wrapper->addSerializer( new osgDB::PropByRefSerializer<MyClass, osg::Plane>( \ |
|---|
| 795 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 796 | |
|---|
| 797 | #define ADD_MATRIXF_SERIALIZER(PROP, DEF) \ |
|---|
| 798 | wrapper->addSerializer( new osgDB::PropByRefSerializer<MyClass, osg::Matrixf>( \ |
|---|
| 799 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 800 | |
|---|
| 801 | #define ADD_MATRIXD_SERIALIZER(PROP, DEF) \ |
|---|
| 802 | wrapper->addSerializer( new osgDB::PropByRefSerializer<MyClass, osg::Matrixd>( \ |
|---|
| 803 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 804 | |
|---|
| 805 | #define ADD_MATRIX_SERIALIZER(PROP, DEF) \ |
|---|
| 806 | wrapper->addSerializer( new osgDB::MatrixSerializer<MyClass>( \ |
|---|
| 807 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 808 | |
|---|
| 809 | #define ADD_GLENUM_SERIALIZER(PROP, TYPE, DEF) \ |
|---|
| 810 | wrapper->addSerializer( new osgDB::GLenumSerializer<MyClass, TYPE>( \ |
|---|
| 811 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 812 | |
|---|
| 813 | #define ADD_STRING_SERIALIZER(PROP, DEF) \ |
|---|
| 814 | wrapper->addSerializer( new osgDB::StringSerializer<MyClass>( \ |
|---|
| 815 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 816 | |
|---|
| 817 | #define ADD_OBJECT_SERIALIZER(PROP, TYPE, DEF) \ |
|---|
| 818 | wrapper->addSerializer( new osgDB::ObjectSerializer<MyClass, TYPE>( \ |
|---|
| 819 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 820 | |
|---|
| 821 | #define ADD_IMAGE_SERIALIZER(PROP, TYPE, DEF) \ |
|---|
| 822 | wrapper->addSerializer( new osgDB::ImageSerializer<MyClass, TYPE>( \ |
|---|
| 823 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 824 | |
|---|
| 825 | #define ADD_LIST_SERIALIZER(PROP, TYPE) \ |
|---|
| 826 | wrapper->addSerializer( new osgDB::ListSerializer<MyClass, TYPE>( \ |
|---|
| 827 | #PROP, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 828 | |
|---|
| 829 | #define BEGIN_ENUM_SERIALIZER(PROP, DEF) \ |
|---|
| 830 | { typedef osgDB::EnumSerializer<MyClass, MyClass::PROP, void> MySerializer; \ |
|---|
| 831 | osg::ref_ptr<MySerializer> serializer = new MySerializer( \ |
|---|
| 832 | #PROP, MyClass::DEF, &MyClass::get##PROP, &MyClass::set##PROP) |
|---|
| 833 | |
|---|
| 834 | #define BEGIN_ENUM_SERIALIZER2(PROP, TYPE, DEF) \ |
|---|
| 835 | { typedef osgDB::EnumSerializer<MyClass, TYPE, void> MySerializer; \ |
|---|
| 836 | osg::ref_ptr<MySerializer> serializer = new MySerializer( \ |
|---|
| 837 | #PROP, MyClass::DEF, &MyClass::get##PROP, &MyClass::set##PROP) |
|---|
| 838 | |
|---|
| 839 | #define BEGIN_ENUM_SERIALIZER3(PROP, DEF) \ |
|---|
| 840 | { typedef osgDB::EnumSerializer<MyClass, MyClass::PROP, bool> MySerializer; \ |
|---|
| 841 | osg::ref_ptr<MySerializer> serializer = new MySerializer( \ |
|---|
| 842 | #PROP, MyClass::DEF, &MyClass::get##PROP, &MyClass::set##PROP) |
|---|
| 843 | |
|---|
| 844 | #define ADD_ENUM_VALUE(VALUE) \ |
|---|
| 845 | serializer->add(#VALUE, MyClass::VALUE) |
|---|
| 846 | |
|---|
| 847 | #define END_ENUM_SERIALIZER() \ |
|---|
| 848 | wrapper->addSerializer(serializer.get()); } |
|---|
| 849 | |
|---|
| 850 | |
|---|
| 851 | } |
|---|
| 852 | |
|---|
| 853 | #endif |
|---|