| 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() ) |
|---|
| 141 | { |
|---|
| 142 | bool ok = false; is >> ok; |
|---|
| 143 | if ( !ok ) return true; |
|---|
| 144 | } |
|---|
| 145 | else |
|---|
| 146 | { |
|---|
| 147 | if ( !is.matchString(_name) ) |
|---|
| 148 | return true; |
|---|
| 149 | } |
|---|
| 150 | return (*_reader)(is, object); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | virtual bool write( OutputStream& os, const osg::Object& obj ) |
|---|
| 154 | { |
|---|
| 155 | const C& object = OBJECT_CAST<const C&>(obj); |
|---|
| 156 | bool ok = (*_checker)(object); |
|---|
| 157 | if ( os.isBinary() ) |
|---|
| 158 | { |
|---|
| 159 | os << ok; |
|---|
| 160 | if ( !ok ) return true; |
|---|
| 161 | } |
|---|
| 162 | else |
|---|
| 163 | { |
|---|
| 164 | if ( !ok ) return true; |
|---|
| 165 | os << PROPERTY(_name.c_str()); |
|---|
| 166 | } |
|---|
| 167 | return (*_writer)(os, object); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | virtual const std::string& getName() const { return _name; } |
|---|
| 171 | |
|---|
| 172 | protected: |
|---|
| 173 | std::string _name; |
|---|
| 174 | Checker _checker; |
|---|
| 175 | |
|---|
| 176 | public: |
|---|
| 177 | Reader _reader; |
|---|
| 178 | Writer _writer; |
|---|
| 179 | }; |
|---|
| 180 | |
|---|
| 181 | template<typename P> |
|---|
| 182 | class TemplateSerializer : public BaseSerializer |
|---|
| 183 | { |
|---|
| 184 | public: |
|---|
| 185 | TemplateSerializer( const char* name ) |
|---|
| 186 | : _name(name) {} |
|---|
| 187 | |
|---|
| 188 | virtual bool read( InputStream& is, osg::Object& obj ) = 0; |
|---|
| 189 | virtual bool write( OutputStream& os, const osg::Object& obj ) = 0; |
|---|
| 190 | virtual const std::string& getName() const { return _name; } |
|---|
| 191 | |
|---|
| 192 | protected: |
|---|
| 193 | std::string _name; |
|---|
| 194 | P _defaultValue; |
|---|
| 195 | }; |
|---|
| 196 | |
|---|
| 197 | template<typename C, typename P> |
|---|
| 198 | class PropByValSerializer : public TemplateSerializer<P> |
|---|
| 199 | { |
|---|
| 200 | public: |
|---|
| 201 | typedef TemplateSerializer<P> ParentType; |
|---|
| 202 | typedef P (C::*Getter)() const; |
|---|
| 203 | typedef void (C::*Setter)( P ); |
|---|
| 204 | |
|---|
| 205 | PropByValSerializer( const char* name, P def, Getter gf, Setter sf, bool useHex=false ) |
|---|
| 206 | : ParentType(name), _getter(gf), _setter(sf), _useHex(useHex) |
|---|
| 207 | { ParentType::_defaultValue = def; } |
|---|
| 208 | |
|---|
| 209 | virtual bool read( InputStream& is, osg::Object& obj ) |
|---|
| 210 | { |
|---|
| 211 | C& object = OBJECT_CAST<C&>(obj); |
|---|
| 212 | P value; |
|---|
| 213 | if ( is.isBinary() ) |
|---|
| 214 | { |
|---|
| 215 | is >> value; |
|---|
| 216 | if ( ParentType::_defaultValue!=value ) |
|---|
| 217 | (object.*_setter)( value ); |
|---|
| 218 | } |
|---|
| 219 | else if ( is.matchString(ParentType::_name) ) |
|---|
| 220 | { |
|---|
| 221 | if ( _useHex ) is >> std::hex; |
|---|
| 222 | is >> value; |
|---|
| 223 | if ( _useHex ) is >> std::dec; |
|---|
| 224 | (object.*_setter)( value ); |
|---|
| 225 | } |
|---|
| 226 | return true; |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | virtual bool write( OutputStream& os, const osg::Object& obj ) |
|---|
| 230 | { |
|---|
| 231 | const C& object = OBJECT_CAST<const C&>(obj); |
|---|
| 232 | if ( os.isBinary() ) |
|---|
| 233 | { |
|---|
| 234 | os << (object.*_getter)(); |
|---|
| 235 | } |
|---|
| 236 | else if ( ParentType::_defaultValue!=(object.*_getter)() ) |
|---|
| 237 | { |
|---|
| 238 | os << PROPERTY((ParentType::_name).c_str()); |
|---|
| 239 | if ( _useHex ) os << std::hex; |
|---|
| 240 | os << (object.*_getter)(); |
|---|
| 241 | if ( _useHex ) os << std::dec; |
|---|
| 242 | os << std::endl; |
|---|
| 243 | } |
|---|
| 244 | return true; |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | public: |
|---|
| 248 | Getter _getter; |
|---|
| 249 | Setter _setter; |
|---|
| 250 | |
|---|
| 251 | protected: |
|---|
| 252 | bool _useHex; |
|---|
| 253 | }; |
|---|
| 254 | |
|---|
| 255 | template<typename C, typename P> |
|---|
| 256 | class PropByRefSerializer : public TemplateSerializer<P> |
|---|
| 257 | { |
|---|
| 258 | public: |
|---|
| 259 | typedef TemplateSerializer<P> ParentType; |
|---|
| 260 | typedef const P& CP; |
|---|
| 261 | typedef CP (C::*Getter)() const; |
|---|
| 262 | typedef void (C::*Setter)( CP ); |
|---|
| 263 | |
|---|
| 264 | PropByRefSerializer( const char* name, CP def, Getter gf, Setter sf ) |
|---|
| 265 | : ParentType(name), _getter(gf), _setter(sf) |
|---|
| 266 | { ParentType::_defaultValue = def; } |
|---|
| 267 | |
|---|
| 268 | virtual bool read( InputStream& is, osg::Object& obj ) |
|---|
| 269 | { |
|---|
| 270 | C& object = OBJECT_CAST<C&>(obj); |
|---|
| 271 | P value; |
|---|
| 272 | if ( is.isBinary() ) |
|---|
| 273 | { |
|---|
| 274 | is >> value; |
|---|
| 275 | if ( ParentType::_defaultValue!=value ) |
|---|
| 276 | (object.*_setter)( value ); |
|---|
| 277 | } |
|---|
| 278 | else if ( is.matchString(ParentType::_name) ) |
|---|
| 279 | { |
|---|
| 280 | is >> value; |
|---|
| 281 | (object.*_setter)( value ); |
|---|
| 282 | } |
|---|
| 283 | return true; |
|---|
| 284 | } |
|---|
| 285 | |
|---|
| 286 | virtual bool write( OutputStream& os, const osg::Object& obj ) |
|---|
| 287 | { |
|---|
| 288 | const C& object = OBJECT_CAST<const C&>(obj); |
|---|
| 289 | if ( os.isBinary() ) |
|---|
| 290 | { |
|---|
| 291 | os << (object.*_getter)(); |
|---|
| 292 | } |
|---|
| 293 | else if ( ParentType::_defaultValue!=(object.*_getter)() ) |
|---|
| 294 | { |
|---|
| 295 | os << PROPERTY((ParentType::_name).c_str()) << (object.*_getter)() << std::endl; |
|---|
| 296 | } |
|---|
| 297 | return true; |
|---|
| 298 | } |
|---|
| 299 | |
|---|
| 300 | public: |
|---|
| 301 | Getter _getter; |
|---|
| 302 | Setter _setter; |
|---|
| 303 | }; |
|---|
| 304 | |
|---|
| 305 | template<typename C> |
|---|
| 306 | class MatrixSerializer : public TemplateSerializer<osg::Matrix> |
|---|
| 307 | { |
|---|
| 308 | public: |
|---|
| 309 | typedef TemplateSerializer<osg::Matrix> ParentType; |
|---|
| 310 | typedef const osg::Matrix& (C::*Getter)() const; |
|---|
| 311 | typedef void (C::*Setter)( const osg::Matrix& ); |
|---|
| 312 | |
|---|
| 313 | MatrixSerializer( const char* name, const osg::Matrix& def, Getter gf, Setter sf ) |
|---|
| 314 | : ParentType(name), _getter(gf), _setter(sf) |
|---|
| 315 | { ParentType::_defaultValue = def; } |
|---|
| 316 | |
|---|
| 317 | virtual bool read( InputStream& is, osg::Object& obj ) |
|---|
| 318 | { |
|---|
| 319 | C& object = OBJECT_CAST<C&>(obj); |
|---|
| 320 | osg::Matrix value; |
|---|
| 321 | if ( is.isBinary() ) |
|---|
| 322 | { |
|---|
| 323 | readMatrixImplementation( is, value ); |
|---|
| 324 | if ( ParentType::_defaultValue!=value ) |
|---|
| 325 | (object.*_setter)( value ); |
|---|
| 326 | } |
|---|
| 327 | else if ( is.matchString(ParentType::_name) ) |
|---|
| 328 | { |
|---|
| 329 | readMatrixImplementation( is, value ); |
|---|
| 330 | (object.*_setter)( value ); |
|---|
| 331 | } |
|---|
| 332 | return true; |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | virtual bool write( OutputStream& os, const osg::Object& obj ) |
|---|
| 336 | { |
|---|
| 337 | const C& object = OBJECT_CAST<const C&>(obj); |
|---|
| 338 | if ( os.isBinary() ) |
|---|
| 339 | { |
|---|
| 340 | os << (object.*_getter)(); |
|---|
| 341 | } |
|---|
| 342 | else if ( ParentType::_defaultValue!=(object.*_getter)() ) |
|---|
| 343 | { |
|---|
| 344 | os << PROPERTY((ParentType::_name).c_str()) << (object.*_getter)() << std::endl; |
|---|
| 345 | } |
|---|
| 346 | return true; |
|---|
| 347 | } |
|---|
| 348 | |
|---|
| 349 | protected: |
|---|
| 350 | void readMatrixImplementation( InputStream& is, osg::Matrix& matrix ) |
|---|
| 351 | { |
|---|
| 352 | if ( is.getUseFloatMatrix() ) |
|---|
| 353 | { |
|---|
| 354 | osg::Matrixf realValue; is >> realValue; |
|---|
| 355 | matrix = realValue; |
|---|
| 356 | } |
|---|
| 357 | else |
|---|
| 358 | { |
|---|
| 359 | osg::Matrixd realValue; is >> realValue; |
|---|
| 360 | matrix = realValue; |
|---|
| 361 | } |
|---|
| 362 | } |
|---|
| 363 | |
|---|
| 364 | public: |
|---|
| 365 | Getter _getter; |
|---|
| 366 | Setter _setter; |
|---|
| 367 | }; |
|---|
| 368 | |
|---|
| 369 | template<typename C, typename P> |
|---|
| 370 | class GLenumSerializer : public TemplateSerializer<P> |
|---|
| 371 | { |
|---|
| 372 | public: |
|---|
| 373 | typedef TemplateSerializer<P> ParentType; |
|---|
| 374 | typedef P (C::*Getter)() const; |
|---|
| 375 | typedef void (C::*Setter)( P ); |
|---|
| 376 | |
|---|
| 377 | GLenumSerializer( const char* name, P def, Getter gf, Setter sf ) |
|---|
| 378 | : ParentType(name), _getter(gf), _setter(sf) |
|---|
| 379 | { ParentType::_defaultValue = def; } |
|---|
| 380 | |
|---|
| 381 | virtual bool read( InputStream& is, osg::Object& obj ) |
|---|
| 382 | { |
|---|
| 383 | C& object = OBJECT_CAST<C&>(obj); |
|---|
| 384 | if ( is.isBinary() ) |
|---|
| 385 | { |
|---|
| 386 | GLenum value; is >> value; |
|---|
| 387 | if ( ParentType::_defaultValue!=static_cast<P>(value) ) |
|---|
| 388 | (object.*_setter)( static_cast<P>(value) ); |
|---|
| 389 | } |
|---|
| 390 | else if ( is.matchString(ParentType::_name) ) |
|---|
| 391 | { |
|---|
| 392 | DEF_GLENUM(value); is >> value; |
|---|
| 393 | (object.*_setter)( static_cast<P>(value.get()) ); |
|---|
| 394 | } |
|---|
| 395 | return true; |
|---|
| 396 | } |
|---|
| 397 | |
|---|
| 398 | virtual bool write( OutputStream& os, const osg::Object& obj ) |
|---|
| 399 | { |
|---|
| 400 | const C& object = OBJECT_CAST<const C&>(obj); |
|---|
| 401 | if ( os.isBinary() ) |
|---|
| 402 | { |
|---|
| 403 | os << static_cast<GLenum>((object.*_getter)()); |
|---|
| 404 | } |
|---|
| 405 | else if ( ParentType::_defaultValue!=(object.*_getter)() ) |
|---|
| 406 | { |
|---|
| 407 | os << PROPERTY((ParentType::_name).c_str()) << GLENUM((object.*_getter)()) << std::endl; |
|---|
| 408 | } |
|---|
| 409 | return true; |
|---|
| 410 | } |
|---|
| 411 | |
|---|
| 412 | public: |
|---|
| 413 | Getter _getter; |
|---|
| 414 | Setter _setter; |
|---|
| 415 | }; |
|---|
| 416 | |
|---|
| 417 | template<typename C> |
|---|
| 418 | class StringSerializer : public TemplateSerializer<std::string> |
|---|
| 419 | { |
|---|
| 420 | public: |
|---|
| 421 | typedef TemplateSerializer<std::string> ParentType; |
|---|
| 422 | typedef const std::string& (C::*Getter)() const; |
|---|
| 423 | typedef void (C::*Setter)( const std::string& ); |
|---|
| 424 | |
|---|
| 425 | StringSerializer( const char* name, const std::string& def, Getter gf, Setter sf ) |
|---|
| 426 | : ParentType(name), _getter(gf), _setter(sf) |
|---|
| 427 | { ParentType::_defaultValue = def; } |
|---|
| 428 | |
|---|
| 429 | virtual bool read( InputStream& is, osg::Object& obj ) |
|---|
| 430 | { |
|---|
| 431 | C& object = OBJECT_CAST<C&>(obj); |
|---|
| 432 | std::string value; |
|---|
| 433 | if ( is.isBinary() ) |
|---|
| 434 | { |
|---|
| 435 | is >> value; |
|---|
| 436 | if ( ParentType::_defaultValue!=value ) |
|---|
| 437 | (object.*_setter)( value ); |
|---|
| 438 | } |
|---|
| 439 | else if ( is.matchString(ParentType::_name) ) |
|---|
| 440 | { |
|---|
| 441 | is.readWrappedString( value ); |
|---|
| 442 | if ( !value.empty() ) |
|---|
| 443 | (object.*_setter)( value ); |
|---|
| 444 | } |
|---|
| 445 | return true; |
|---|
| 446 | } |
|---|
| 447 | |
|---|
| 448 | virtual bool write( OutputStream& os, const osg::Object& obj ) |
|---|
| 449 | { |
|---|
| 450 | const C& object = OBJECT_CAST<const C&>(obj); |
|---|
| 451 | if ( os.isBinary() ) |
|---|
| 452 | { |
|---|
| 453 | os << (object.*_getter)(); |
|---|
| 454 | } |
|---|
| 455 | else if ( ParentType::_defaultValue!=(object.*_getter)() ) |
|---|
| 456 | { |
|---|
| 457 | os << PROPERTY((ParentType::_name).c_str()); |
|---|
| 458 | os.writeWrappedString( (object.*_getter)() ); |
|---|
| 459 | os << std::endl; |
|---|
| 460 | } |
|---|
| 461 | return true; |
|---|
| 462 | } |
|---|
| 463 | |
|---|
| 464 | public: |
|---|
| 465 | Getter _getter; |
|---|
| 466 | Setter _setter; |
|---|
| 467 | }; |
|---|
| 468 | |
|---|
| 469 | template<typename C, typename P> |
|---|
| 470 | class ObjectSerializer : public TemplateSerializer<P*> |
|---|
| 471 | { |
|---|
| 472 | public: |
|---|
| 473 | typedef TemplateSerializer<P*> ParentType; |
|---|
| 474 | typedef const P* (C::*Getter)() const; |
|---|
| 475 | typedef void (C::*Setter)( P* ); |
|---|
| 476 | |
|---|
| 477 | ObjectSerializer( const char* name, P* def, Getter gf, Setter sf ) |
|---|
| 478 | : ParentType(name), _getter(gf), _setter(sf) |
|---|
| 479 | { ParentType::_defaultValue = def; } |
|---|
| 480 | |
|---|
| 481 | virtual bool read( InputStream& is, osg::Object& obj ) |
|---|
| 482 | { |
|---|
| 483 | C& object = OBJECT_CAST<C&>(obj); |
|---|
| 484 | bool hasObject = false; |
|---|
| 485 | if ( is.isBinary() ) |
|---|
| 486 | { |
|---|
| 487 | is >> hasObject; |
|---|
| 488 | if ( hasObject ) |
|---|
| 489 | { |
|---|
| 490 | P* value = dynamic_cast<P*>( is.readObject() ); |
|---|
| 491 | if ( ParentType::_defaultValue!=value ) |
|---|
| 492 | (object.*_setter)( value ); |
|---|
| 493 | } |
|---|
| 494 | } |
|---|
| 495 | else if ( is.matchString(ParentType::_name) ) |
|---|
| 496 | { |
|---|
| 497 | is >> hasObject; |
|---|
| 498 | if ( hasObject ) |
|---|
| 499 | { |
|---|
| 500 | is >> BEGIN_BRACKET; |
|---|
| 501 | P* value = dynamic_cast<P*>( is.readObject() ); |
|---|
| 502 | (object.*_setter)( value ); |
|---|
| 503 | is >> END_BRACKET; |
|---|
| 504 | } |
|---|
| 505 | } |
|---|
| 506 | return true; |
|---|
| 507 | } |
|---|
| 508 | |
|---|
| 509 | virtual bool write( OutputStream& os, const osg::Object& obj ) |
|---|
| 510 | { |
|---|
| 511 | const C& object = OBJECT_CAST<const C&>(obj); |
|---|
| 512 | bool hasObject = ((object.*_getter)()!=NULL); |
|---|
| 513 | if ( os.isBinary() ) |
|---|
| 514 | { |
|---|
| 515 | os << hasObject; |
|---|
| 516 | os.writeObject( (object.*_getter)() ); |
|---|
| 517 | } |
|---|
| 518 | else if ( ParentType::_defaultValue!=(object.*_getter)() ) |
|---|
| 519 | { |
|---|
| 520 | os << PROPERTY((ParentType::_name).c_str()) << hasObject; |
|---|
| 521 | if ( hasObject ) |
|---|
| 522 | { |
|---|
| 523 | os << BEGIN_BRACKET << std::endl; |
|---|
| 524 | os.writeObject( (object.*_getter)() ); |
|---|
| 525 | os << END_BRACKET; |
|---|
| 526 | } |
|---|
| 527 | os << std::endl; |
|---|
| 528 | } |
|---|
| 529 | return true; |
|---|
| 530 | } |
|---|
| 531 | |
|---|
| 532 | public: |
|---|
| 533 | Getter _getter; |
|---|
| 534 | Setter _setter; |
|---|
| 535 | }; |
|---|
| 536 | |
|---|
| 537 | template<typename C, typename P> |
|---|
| 538 | class ImageSerializer : public TemplateSerializer<P*> |
|---|
| 539 | { |
|---|
| 540 | public: |
|---|
| 541 | typedef TemplateSerializer<P*> ParentType; |
|---|
| 542 | typedef const P* (C::*Getter)() const; |
|---|
| 543 | typedef void (C::*Setter)( P* ); |
|---|
| 544 | |
|---|
| 545 | ImageSerializer( const char* name, P* def, Getter gf, Setter sf ) |
|---|
| 546 | : ParentType(name), _getter(gf), _setter(sf) |
|---|
| 547 | { ParentType::_defaultValue = def; } |
|---|
| 548 | |
|---|
| 549 | virtual bool read( InputStream& is, osg::Object& obj ) |
|---|
| 550 | { |
|---|
| 551 | C& object = OBJECT_CAST<C&>(obj); |
|---|
| 552 | bool hasObject = false; |
|---|
| 553 | if ( is.isBinary() ) |
|---|
| 554 | { |
|---|
| 555 | is >> hasObject; |
|---|
| 556 | if ( hasObject ) |
|---|
| 557 | { |
|---|
| 558 | P* value = dynamic_cast<P*>( is.readImage() ); |
|---|
| 559 | if ( ParentType::_defaultValue!=value ) |
|---|
| 560 | (object.*_setter)( value ); |
|---|
| 561 | } |
|---|
| 562 | } |
|---|
| 563 | else if ( is.matchString(ParentType::_name) ) |
|---|
| 564 | { |
|---|
| 565 | is >> hasObject; |
|---|
| 566 | if ( hasObject ) |
|---|
| 567 | { |
|---|
| 568 | is >> BEGIN_BRACKET; |
|---|
| 569 | P* value = dynamic_cast<P*>( is.readImage() ); |
|---|
| 570 | (object.*_setter)( value ); |
|---|
| 571 | is >> END_BRACKET; |
|---|
| 572 | } |
|---|
| 573 | } |
|---|
| 574 | return true; |
|---|
| 575 | } |
|---|
| 576 | |
|---|
| 577 | virtual bool write( OutputStream& os, const osg::Object& obj ) |
|---|
| 578 | { |
|---|
| 579 | const C& object = OBJECT_CAST<const C&>(obj); |
|---|
| 580 | bool hasObject = ((object.*_getter)()!=NULL); |
|---|
| 581 | if ( os.isBinary() ) |
|---|
| 582 | { |
|---|
| 583 | os << hasObject; |
|---|
| 584 | os.writeImage( (object.*_getter)() ); |
|---|
| 585 | } |
|---|
| 586 | else if ( ParentType::_defaultValue!=(object.*_getter)() ) |
|---|
| 587 | { |
|---|
| 588 | os << PROPERTY((ParentType::_name).c_str()) << hasObject; |
|---|
| 589 | if ( hasObject ) |
|---|
| 590 | { |
|---|
| 591 | os << BEGIN_BRACKET << std::endl; |
|---|
| 592 | os.writeImage( (object.*_getter)() ); |
|---|
| 593 | os << END_BRACKET; |
|---|
| 594 | } |
|---|
| 595 | os << std::endl; |
|---|
| 596 | } |
|---|
| 597 | return true; |
|---|
| 598 | } |
|---|
| 599 | |
|---|
| 600 | public: |
|---|
| 601 | Getter _getter; |
|---|
| 602 | Setter _setter; |
|---|
| 603 | }; |
|---|
| 604 | |
|---|
| 605 | template<typename C, typename P, typename B> |
|---|
| 606 | class EnumSerializer : public TemplateSerializer<P> |
|---|
| 607 | { |
|---|
| 608 | public: |
|---|
| 609 | typedef TemplateSerializer<P> ParentType; |
|---|
| 610 | typedef P (C::*Getter)() const; |
|---|
| 611 | typedef B (C::*Setter)( P ); |
|---|
| 612 | |
|---|
| 613 | EnumSerializer( const char* name, P def, Getter gf, Setter sf ) |
|---|
| 614 | : ParentType(name), _getter(gf), _setter(sf) |
|---|
| 615 | { ParentType::_defaultValue = def; } |
|---|
| 616 | |
|---|
| 617 | void add( const char* str, P value ) |
|---|
| 618 | { _lookup.add(str, static_cast<osgDB::IntLookup::Value>(value)); } |
|---|
| 619 | |
|---|
| 620 | P getValue( const char* str ) |
|---|
| 621 | { return static_cast<P>(_lookup.getValue(str)); } |
|---|
| 622 | |
|---|
| 623 | const std::string& getString( P value ) |
|---|
| 624 | { return _lookup.getString(static_cast<osgDB::IntLookup::Value>(value)); } |
|---|
| 625 | |
|---|
| 626 | virtual bool read( InputStream& is, osg::Object& obj ) |
|---|
| 627 | { |
|---|
| 628 | C& object = OBJECT_CAST<C&>(obj); |
|---|
| 629 | IntLookup::Value value; |
|---|
| 630 | if ( is.isBinary() ) |
|---|
| 631 | { |
|---|
| 632 | is >> value; |
|---|
| 633 | if ( ParentType::_defaultValue!=static_cast<P>(value) ) |
|---|
| 634 | (object.*_setter)( static_cast<P>(value) ); |
|---|
| 635 | } |
|---|
| 636 | else if ( is.matchString(ParentType::_name) ) |
|---|
| 637 | { |
|---|
| 638 | std::string str; is >> str; |
|---|
| 639 | (object.*_setter)( getValue(str.c_str()) ); |
|---|
| 640 | } |
|---|
| 641 | return true; |
|---|
| 642 | } |
|---|
| 643 | |
|---|
| 644 | virtual bool write( osgDB::OutputStream& os, const osg::Object& obj ) |
|---|
| 645 | { |
|---|
| 646 | const C& object = OBJECT_CAST<const C&>(obj); |
|---|
| 647 | if ( os.isBinary() ) |
|---|
| 648 | { |
|---|
| 649 | os << (osgDB::IntLookup::Value)(object.*_getter)(); |
|---|
| 650 | } |
|---|
| 651 | else if ( ParentType::_defaultValue!=(object.*_getter)() ) |
|---|
| 652 | { |
|---|
| 653 | os << PROPERTY((ParentType::_name).c_str()) << getString((object.*_getter)()) << std::endl; |
|---|
| 654 | } |
|---|
| 655 | return true; |
|---|
| 656 | } |
|---|
| 657 | |
|---|
| 658 | public: |
|---|
| 659 | Getter _getter; |
|---|
| 660 | Setter _setter; |
|---|
| 661 | |
|---|
| 662 | protected: |
|---|
| 663 | IntLookup _lookup; |
|---|
| 664 | }; |
|---|
| 665 | |
|---|
| 666 | template<typename C, typename P> |
|---|
| 667 | class ListSerializer : public TemplateSerializer<P> |
|---|
| 668 | { |
|---|
| 669 | public: |
|---|
| 670 | typedef TemplateSerializer<P> ParentType; |
|---|
| 671 | typedef typename P::value_type ValueType; |
|---|
| 672 | typedef typename P::const_iterator ConstIterator; |
|---|
| 673 | typedef const P& (C::*Getter)() const; |
|---|
| 674 | typedef void (C::*Setter)( const P& ); |
|---|
| 675 | |
|---|
| 676 | ListSerializer( const char* name, Getter gf, Setter sf ) |
|---|
| 677 | : ParentType(name), _getter(gf), _setter(sf) {} |
|---|
| 678 | |
|---|
| 679 | virtual bool read( InputStream& is, osg::Object& obj ) |
|---|
| 680 | { |
|---|
| 681 | C& object = OBJECT_CAST<C&>(obj); |
|---|
| 682 | unsigned int size = 0; |
|---|
| 683 | P list; |
|---|
| 684 | if ( is.isBinary() ) |
|---|
| 685 | { |
|---|
| 686 | is >> size; |
|---|
| 687 | for ( unsigned int i=0; i<size; ++i ) |
|---|
| 688 | { |
|---|
| 689 | ValueType value; |
|---|
| 690 | is >> value; |
|---|
| 691 | list.push_back( value ); |
|---|
| 692 | } |
|---|
| 693 | if ( size>0 ) (object.*_setter)( list ); |
|---|
| 694 | } |
|---|
| 695 | else if ( is.matchString(ParentType::_name) ) |
|---|
| 696 | { |
|---|
| 697 | is >> size; |
|---|
| 698 | if ( size>0 ) is >> BEGIN_BRACKET; |
|---|
| 699 | for ( unsigned int i=0; i<size; ++i ) |
|---|
| 700 | { |
|---|
| 701 | ValueType value; |
|---|
| 702 | is >> value; |
|---|
| 703 | list.push_back( value ); |
|---|
| 704 | } |
|---|
| 705 | if ( size>0 ) |
|---|
| 706 | { |
|---|
| 707 | is >> END_BRACKET; |
|---|
| 708 | (object.*_setter)( list ); |
|---|
| 709 | } |
|---|
| 710 | } |
|---|
| 711 | return true; |
|---|
| 712 | } |
|---|
| 713 | |
|---|
| 714 | virtual bool write( OutputStream& os, const osg::Object& obj ) |
|---|
| 715 | { |
|---|
| 716 | const C& object = OBJECT_CAST<const C&>(obj); |
|---|
| 717 | const P& list = (object.*_getter)(); |
|---|
| 718 | unsigned int size = (unsigned int)list.size(); |
|---|
| 719 | if ( os.isBinary() ) |
|---|
| 720 | { |
|---|
| 721 | os << size; |
|---|
| 722 | for ( ConstIterator itr=list.begin(); |
|---|
| 723 | itr!=list.end(); ++itr ) |
|---|
| 724 | { |
|---|
| 725 | os << (*itr); |
|---|
| 726 | } |
|---|
| 727 | } |
|---|
| 728 | else if ( size>0 ) |
|---|
| 729 | { |
|---|
| 730 | os << PROPERTY((ParentType::_name).c_str()) << size << BEGIN_BRACKET << std::endl; |
|---|
| 731 | for ( ConstIterator itr=list.begin(); |
|---|
| 732 | itr!=list.end(); ++itr ) |
|---|
| 733 | { |
|---|
| 734 | os << (*itr); |
|---|
| 735 | } |
|---|
| 736 | os << END_BRACKET << std::endl; |
|---|
| 737 | } |
|---|
| 738 | return true; |
|---|
| 739 | } |
|---|
| 740 | |
|---|
| 741 | public: |
|---|
| 742 | Getter _getter; |
|---|
| 743 | Setter _setter; |
|---|
| 744 | }; |
|---|
| 745 | |
|---|
| 746 | // ADDING MANIPULATORS |
|---|
| 747 | #define ADD_SERIALIZER(S) \ |
|---|
| 748 | wrapper->addSerializer(S) |
|---|
| 749 | |
|---|
| 750 | #define ADD_USER_SERIALIZER(PROP) \ |
|---|
| 751 | wrapper->addSerializer( new osgDB::UserSerializer<MyClass>( \ |
|---|
| 752 | #PROP, &check##PROP, &read##PROP, &write##PROP) ) |
|---|
| 753 | |
|---|
| 754 | #define ADD_BOOL_SERIALIZER(PROP, DEF) \ |
|---|
| 755 | wrapper->addSerializer( new osgDB::PropByValSerializer< MyClass, bool >( \ |
|---|
| 756 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 757 | |
|---|
| 758 | #define ADD_SHORT_SERIALIZER(PROP, DEF) \ |
|---|
| 759 | wrapper->addSerializer( new osgDB::PropByValSerializer< MyClass, short >( \ |
|---|
| 760 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 761 | |
|---|
| 762 | #define ADD_USHORT_SERIALIZER(PROP, DEF) \ |
|---|
| 763 | wrapper->addSerializer( new osgDB::PropByValSerializer< MyClass, unsigned short >( \ |
|---|
| 764 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 765 | |
|---|
| 766 | #define ADD_HEXSHORT_SERIALIZER(PROP, DEF) \ |
|---|
| 767 | wrapper->addSerializer( new osgDB::PropByValSerializer< MyClass, unsigned short >( \ |
|---|
| 768 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP, true) ) |
|---|
| 769 | |
|---|
| 770 | #define ADD_INT_SERIALIZER(PROP, DEF) \ |
|---|
| 771 | wrapper->addSerializer( new osgDB::PropByValSerializer< MyClass, int >( \ |
|---|
| 772 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 773 | |
|---|
| 774 | #define ADD_UINT_SERIALIZER(PROP, DEF) \ |
|---|
| 775 | wrapper->addSerializer( new osgDB::PropByValSerializer< MyClass, unsigned int >( \ |
|---|
| 776 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 777 | |
|---|
| 778 | #define ADD_HEXINT_SERIALIZER(PROP, DEF) \ |
|---|
| 779 | wrapper->addSerializer( new osgDB::PropByValSerializer< MyClass, unsigned int >( \ |
|---|
| 780 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP, true) ) |
|---|
| 781 | |
|---|
| 782 | #define ADD_FLOAT_SERIALIZER(PROP, DEF) \ |
|---|
| 783 | wrapper->addSerializer( new osgDB::PropByValSerializer< MyClass, float >( \ |
|---|
| 784 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 785 | |
|---|
| 786 | #define ADD_DOUBLE_SERIALIZER(PROP, DEF) \ |
|---|
| 787 | wrapper->addSerializer( new osgDB::PropByValSerializer< MyClass, double >( \ |
|---|
| 788 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 789 | |
|---|
| 790 | #define ADD_VEC3F_SERIALIZER(PROP, DEF) \ |
|---|
| 791 | wrapper->addSerializer( new osgDB::PropByRefSerializer< MyClass, osg::Vec3f >( \ |
|---|
| 792 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 793 | |
|---|
| 794 | #define ADD_VEC3D_SERIALIZER(PROP, DEF) \ |
|---|
| 795 | wrapper->addSerializer( new osgDB::PropByRefSerializer< MyClass, osg::Vec3d >( \ |
|---|
| 796 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 797 | |
|---|
| 798 | #define ADD_VEC3_SERIALIZER(PROP, DEF) ADD_VEC3F_SERIALIZER(PROP, DEF) |
|---|
| 799 | |
|---|
| 800 | #define ADD_VEC4F_SERIALIZER(PROP, DEF) \ |
|---|
| 801 | wrapper->addSerializer( new osgDB::PropByRefSerializer< MyClass, osg::Vec4f >( \ |
|---|
| 802 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 803 | |
|---|
| 804 | #define ADD_VEC4D_SERIALIZER(PROP, DEF) \ |
|---|
| 805 | wrapper->addSerializer( new osgDB::PropByRefSerializer< MyClass, osg::Vec4d >( \ |
|---|
| 806 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 807 | |
|---|
| 808 | #define ADD_VEC4_SERIALIZER(PROP, DEF) ADD_VEC4F_SERIALIZER(PROP, DEF) |
|---|
| 809 | |
|---|
| 810 | #define ADD_QUAT_SERIALIZER(PROP, DEF) \ |
|---|
| 811 | wrapper->addSerializer( new osgDB::PropByRefSerializer< MyClass, osg::Quat >( \ |
|---|
| 812 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 813 | |
|---|
| 814 | #define ADD_PLANE_SERIALIZER(PROP, DEF) \ |
|---|
| 815 | wrapper->addSerializer( new osgDB::PropByRefSerializer< MyClass, osg::Plane >( \ |
|---|
| 816 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 817 | |
|---|
| 818 | #define ADD_MATRIXF_SERIALIZER(PROP, DEF) \ |
|---|
| 819 | wrapper->addSerializer( new osgDB::PropByRefSerializer< MyClass, osg::Matrixf >( \ |
|---|
| 820 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 821 | |
|---|
| 822 | #define ADD_MATRIXD_SERIALIZER(PROP, DEF) \ |
|---|
| 823 | wrapper->addSerializer( new osgDB::PropByRefSerializer< MyClass, osg::Matrixd >( \ |
|---|
| 824 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 825 | |
|---|
| 826 | #define ADD_MATRIX_SERIALIZER(PROP, DEF) \ |
|---|
| 827 | wrapper->addSerializer( new osgDB::MatrixSerializer< MyClass >( \ |
|---|
| 828 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 829 | |
|---|
| 830 | #define ADD_GLENUM_SERIALIZER(PROP, TYPE, DEF) \ |
|---|
| 831 | wrapper->addSerializer( new osgDB::GLenumSerializer< MyClass, TYPE >( \ |
|---|
| 832 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 833 | |
|---|
| 834 | #define ADD_STRING_SERIALIZER(PROP, DEF) \ |
|---|
| 835 | wrapper->addSerializer( new osgDB::StringSerializer< MyClass >( \ |
|---|
| 836 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 837 | |
|---|
| 838 | #define ADD_OBJECT_SERIALIZER(PROP, TYPE, DEF) \ |
|---|
| 839 | wrapper->addSerializer( new osgDB::ObjectSerializer< MyClass, TYPE >( \ |
|---|
| 840 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 841 | |
|---|
| 842 | #define ADD_IMAGE_SERIALIZER(PROP, TYPE, DEF) \ |
|---|
| 843 | wrapper->addSerializer( new osgDB::ImageSerializer< MyClass, TYPE >( \ |
|---|
| 844 | #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 845 | |
|---|
| 846 | #define ADD_LIST_SERIALIZER(PROP, TYPE) \ |
|---|
| 847 | wrapper->addSerializer( new osgDB::ListSerializer< MyClass, TYPE >( \ |
|---|
| 848 | #PROP, &MyClass::get##PROP, &MyClass::set##PROP) ) |
|---|
| 849 | |
|---|
| 850 | #define BEGIN_ENUM_SERIALIZER(PROP, DEF) \ |
|---|
| 851 | { typedef osgDB::EnumSerializer<MyClass, MyClass::PROP, void> MySerializer; \ |
|---|
| 852 | osg::ref_ptr<MySerializer> serializer = new MySerializer( \ |
|---|
| 853 | #PROP, MyClass::DEF, &MyClass::get##PROP, &MyClass::set##PROP) |
|---|
| 854 | |
|---|
| 855 | #define BEGIN_ENUM_SERIALIZER2(PROP, TYPE, DEF) \ |
|---|
| 856 | { typedef osgDB::EnumSerializer<MyClass, TYPE, void> MySerializer; \ |
|---|
| 857 | osg::ref_ptr<MySerializer> serializer = new MySerializer( \ |
|---|
| 858 | #PROP, MyClass::DEF, &MyClass::get##PROP, &MyClass::set##PROP) |
|---|
| 859 | |
|---|
| 860 | #define BEGIN_ENUM_SERIALIZER3(PROP, DEF) \ |
|---|
| 861 | { typedef osgDB::EnumSerializer<MyClass, MyClass::PROP, bool> MySerializer; \ |
|---|
| 862 | osg::ref_ptr<MySerializer> serializer = new MySerializer( \ |
|---|
| 863 | #PROP, MyClass::DEF, &MyClass::get##PROP, &MyClass::set##PROP) |
|---|
| 864 | |
|---|
| 865 | #define ADD_ENUM_VALUE(VALUE) \ |
|---|
| 866 | serializer->add(#VALUE, MyClass::VALUE) |
|---|
| 867 | |
|---|
| 868 | #define END_ENUM_SERIALIZER() \ |
|---|
| 869 | wrapper->addSerializer(serializer.get()); } |
|---|
| 870 | |
|---|
| 871 | |
|---|
| 872 | } |
|---|
| 873 | |
|---|
| 874 | #endif |
|---|