Serializer 32.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield
 *
 * This library is open source and may be redistributed and/or modified under
 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
 * (at your option) any later version.  The full license is in LICENSE file
 * included with this distribution, and on the openscenegraph.org website.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * OpenSceneGraph Public License for more details.
*/
// Written by Wang Rui, (C) 2010

#ifndef OSGDB__SERIALIZER
#define OSGDB__SERIALIZER

#include <osg/ref_ptr>
#include <osg/Notify>
#include <osg/Object>
#include <osgDB/InputStream>
#include <osgDB/OutputStream>

#include <string>
#include <sstream>
#include <limits.h>

namespace osgDB
{

#ifndef OBJECT_CAST
    #define OBJECT_CAST static_cast
#endif

class IntLookup
{
public:
    typedef int Value;
    typedef std::map<std::string, Value> StringToValue;
    typedef std::map<Value, std::string> ValueToString;
    
    IntLookup() {}
    unsigned int size() const { return _stringToValue.size(); }
    
    void add( const char* str, Value value )
    {
        if ( _valueToString.find(value)!=_valueToString.end() )
        {
            osg::notify(osg::WARN) << "Duplicate enum value " << value
                                   << " with old string: " << _valueToString[value]
                                   << " and new string: " << str << std::endl;
        }
        _valueToString[value] = str;
        _stringToValue[str] = value;
    }
    
    Value getValue( const char* str )
    {
        StringToValue::iterator itr = _stringToValue.find(str);
        if ( itr==_stringToValue.end() )
        {
            Value value;
            std::stringstream stream;
            stream << str; stream >> value;
            _stringToValue[str] = value;
            return value;
        }
        return itr->second;
    }
    
    const std::string& getString( Value value )
    {
        ValueToString::iterator itr = _valueToString.find(value);
        if ( itr==_valueToString.end() )
        {
            std::string str;
            std::stringstream stream;
            stream << value; stream >> str;
            _valueToString[value] = str;
            return _valueToString[value];
        }
        return itr->second;
    }
    
protected:
    StringToValue _stringToValue;
    ValueToString _valueToString;
};

class UserLookupTableProxy
{
public:
    typedef void (*AddValueFunc)( IntLookup* lookup );
    UserLookupTableProxy( AddValueFunc func ) { if ( func ) (*func)(&_lookup); }
    
    IntLookup _lookup;
};

#define BEGIN_USER_TABLE(NAME, CLASS) \
    static void add_user_value_func_##NAME(osgDB::IntLookup*); \
    static osgDB::UserLookupTableProxy s_user_lookup_table_##NAME(&add_user_value_func_##NAME); \
    static void add_user_value_func_##NAME(osgDB::IntLookup* lookup) { typedef CLASS MyClass
#define ADD_USER_VALUE(VALUE) lookup->add(#VALUE, MyClass::VALUE)
#define END_USER_TABLE() }

#define USER_READ_FUNC(NAME, FUNCNAME) \
    static int FUNCNAME(osgDB::InputStream& is) { \
        int value; if (is.isBinary()) is >> value; \
        else { std::string str; is >> str; \
        value = (s_user_lookup_table_##NAME)._lookup.getValue(str.c_str()); } \
        return value; }

#define USER_WRITE_FUNC(NAME, FUNCNAME) \
    static void FUNCNAME(osgDB::OutputStream& os, int value) { \
        if (os.isBinary()) os << value; \
        else os << (s_user_lookup_table_##NAME)._lookup.getString(value); } \

class BaseSerializer : public osg::Referenced
{
    friend class ObjectWrapper;
public:
    enum Type
    {
        RW_UNDEFINED = 0, RW_USER, RW_OBJECT, RW_IMAGE, RW_LIST,
        RW_BOOL, RW_CHAR, RW_UCHAR, RW_SHORT, RW_USHORT, RW_INT, RW_UINT, RW_FLOAT, RW_DOUBLE,
        RW_VEC2F, RW_VEC2D, RW_VEC3F, RW_VEC3D, RW_VEC4F, RW_VEC4D, RW_QUAT, RW_PLANE,
        RW_MATRIXF, RW_MATRIXD, RW_MATRIX, RW_GLENUM, RW_STRING, RW_ENUM
    };

    BaseSerializer() : _firstVersion(0), _lastVersion(INT_MAX) {}

    virtual bool read( InputStream&, osg::Object& ) = 0;
    virtual bool write( OutputStream&, const osg::Object& ) = 0;
    virtual const std::string& getName() const = 0;

protected:
    int _firstVersion;  // Library version when the serializer is first introduced
    int _lastVersion;  // Library version when the serializer is last required.
};

template<typename C>
class UserSerializer : public BaseSerializer
{
public:
    typedef bool (*Checker)( const C& );
    typedef bool (*Reader)( InputStream&, C& );
    typedef bool (*Writer)( OutputStream&, const C& );
    
    UserSerializer( const char* name, Checker cf, Reader rf, Writer wf )
    : BaseSerializer(), _name(name), _checker(cf), _reader(rf), _writer(wf) {}
    
    virtual bool read( InputStream& is, osg::Object& obj )
    {
        C& object = OBJECT_CAST<C&>(obj);
        if ( is.isBinary() )
        {
            bool ok = false; is >> ok;
            if ( !ok ) return true;
        }
        else
        {
            if ( !is.matchString(_name) )
                return true;
        }
        return (*_reader)(is, object);
    }
    
    virtual bool write( OutputStream& os, const osg::Object& obj )
    {
        const C& object = OBJECT_CAST<const C&>(obj);
        bool ok = (*_checker)(object);
        if ( os.isBinary() )
        {
            os << ok;
            if ( !ok ) return true;
        }
        else
        {
            if ( !ok ) return true;
            os << PROPERTY(_name.c_str());
        }
        return (*_writer)(os, object);
    }
    
    virtual const std::string& getName() const { return _name; }
    
protected:
    std::string _name;
    Checker _checker;
    
public:
    Reader _reader;
    Writer _writer;
};

template<typename P>
class TemplateSerializer : public BaseSerializer
{
public:

    TemplateSerializer( const char* name, P def)
    : BaseSerializer(), _name(name), _defaultValue(def) {}

    virtual bool read( InputStream& is, osg::Object& obj ) = 0;
    virtual bool write( OutputStream& os, const osg::Object& obj ) = 0;
    virtual const std::string& getName() const { return _name; }

protected:
    std::string _name;
    P _defaultValue;
};

template<typename C, typename P>
class PropByValSerializer : public TemplateSerializer<P>
{
public:
    typedef TemplateSerializer<P> ParentType;
    typedef P (C::*Getter)() const;
    typedef void (C::*Setter)( P );
    
    PropByValSerializer( const char* name, P def, Getter gf, Setter sf, bool useHex=false )
    : ParentType(name, def), _getter(gf), _setter(sf), _useHex(useHex) {}
    
    virtual bool read( InputStream& is, osg::Object& obj )
    {
        C& object = OBJECT_CAST<C&>(obj);
        P value;
        if ( is.isBinary() )
        {
            is >> value;
            if ( ParentType::_defaultValue!=value )
                (object.*_setter)( value );
        }
        else if ( is.matchString(ParentType::_name) )
        {
            if ( _useHex ) is >> std::hex;
            is >> value;
            if ( _useHex ) is >> std::dec;
            (object.*_setter)( value );
        }
        return true;
    }
    
    virtual bool write( OutputStream& os, const osg::Object& obj )
    {
        const C& object = OBJECT_CAST<const C&>(obj);
        P value = (object.*_getter)();
        if ( os.isBinary() )
        {
            os << value;
        }
        else if ( ParentType::_defaultValue!=value )
        {
            os << PROPERTY((ParentType::_name).c_str());
            if ( _useHex ) os << std::hex;
            os << value;
            if ( _useHex ) os << std::dec;
            os << std::endl;
        }
        return true;
    }
    
public:
    Getter _getter;
    Setter _setter;
    
protected:
    bool _useHex;
};

template<typename C, typename P>
class PropByRefSerializer : public TemplateSerializer<P>
{
public:
    typedef TemplateSerializer<P> ParentType;
    typedef const P& CP;
    typedef CP (C::*Getter)() const;
    typedef void (C::*Setter)( CP );
    
    PropByRefSerializer( const char* name, CP def, Getter gf, Setter sf )
    : ParentType(name, def), _getter(gf), _setter(sf) {}
    
    virtual bool read( InputStream& is, osg::Object& obj )
    {
        C& object = OBJECT_CAST<C&>(obj);
        P value;
        if ( is.isBinary() )
        {
            is >> value;
            if ( ParentType::_defaultValue!=value )
                (object.*_setter)( value );
        }
        else if ( is.matchString(ParentType::_name) )
        {
            is >> value;
            (object.*_setter)( value );
        }
        return true;
    }
    
    virtual bool write( OutputStream& os, const osg::Object& obj )
    {
        const C& object = OBJECT_CAST<const C&>(obj);
        CP value = (object.*_getter)();
        if ( os.isBinary() )
        {
            os << value;
        }
        else if ( ParentType::_defaultValue!=value )
        {
            os << PROPERTY((ParentType::_name).c_str()) << value << std::endl;
        }
        return true;
    }
    
public:
    Getter _getter;
    Setter _setter;
};

template<typename C>
class MatrixSerializer : public TemplateSerializer<osg::Matrix>
{
public:
    typedef TemplateSerializer<osg::Matrix> ParentType;
    typedef const osg::Matrix& (C::*Getter)() const;
    typedef void (C::*Setter)( const osg::Matrix& );
    
    MatrixSerializer( const char* name, const osg::Matrix& def, Getter gf, Setter sf )
    : ParentType(name, def), _getter(gf), _setter(sf) {}
    
    virtual bool read( InputStream& is, osg::Object& obj )
    {
        C& object = OBJECT_CAST<C&>(obj);
        osg::Matrix value;
        if ( is.isBinary() )
        {
            readMatrixImplementation( is, value );
            if ( ParentType::_defaultValue!=value )
                (object.*_setter)( value );
        }
        else if ( is.matchString(ParentType::_name) )
        {
            readMatrixImplementation( is, value );
            (object.*_setter)( value );
        }
        return true;
    }
    
    virtual bool write( OutputStream& os, const osg::Object& obj )
    {

        const C& object = OBJECT_CAST<const C&>(obj);
        const osg::Matrix& value = (object.*_getter)();
        if ( os.isBinary() )
        {
            os << value;
        }
        else if ( ParentType::_defaultValue!=value )
        {
            os << PROPERTY((ParentType::_name).c_str()) << value << std::endl;
        }
        return true;
    }
    
protected:
    void readMatrixImplementation( InputStream& is, osg::Matrix& matrix )
    {
#if 1
        is >> matrix;
#else
        if ( is.getUseFloatMatrix() )
        {
            osg::Matrixf realValue; is >> realValue;
            matrix = realValue;
        }
        else
        {
            osg::Matrixd realValue; is >> realValue;
            matrix = realValue;
        }
#endif
    }
    
public:
    Getter _getter;
    Setter _setter;
};

template<typename C, typename P>
class GLenumSerializer : public TemplateSerializer<P>
{
public:
    typedef TemplateSerializer<P> ParentType;
    typedef P (C::*Getter)() const;
    typedef void (C::*Setter)( P );
    
    GLenumSerializer( const char* name, P def, Getter gf, Setter sf )
    : ParentType(name, def), _getter(gf), _setter(sf) {}
    
    virtual bool read( InputStream& is, osg::Object& obj )
    {
        C& object = OBJECT_CAST<C&>(obj);
        if ( is.isBinary() )
        {
            GLenum value; is >> value;
            if ( ParentType::_defaultValue!=static_cast<P>(value) )
                (object.*_setter)( static_cast<P>(value) );
        }
        else if ( is.matchString(ParentType::_name) )
        {
            DEF_GLENUM(value); is >> value;
            (object.*_setter)( static_cast<P>(value.get()) );
        }
        return true;
    }
    
    virtual bool write( OutputStream& os, const osg::Object& obj )
    {
        const C& object = OBJECT_CAST<const C&>(obj);
        const P value = (object.*_getter)();
        if ( os.isBinary() )
        {
            os << static_cast<GLenum>(value);
        }
        else if ( ParentType::_defaultValue!=value )
        {
            os << PROPERTY((ParentType::_name).c_str()) << GLENUM(value) << std::endl;
        }
        return true;
    }
    
public:
    Getter _getter;
    Setter _setter;
};

template<typename C>
class StringSerializer : public TemplateSerializer<std::string>
{
public:
    typedef TemplateSerializer<std::string> ParentType;
    typedef const std::string& (C::*Getter)() const;
    typedef void (C::*Setter)( const std::string& );
    
    StringSerializer( const char* name, const std::string& def, Getter gf, Setter sf )
    : ParentType(name, def), _getter(gf), _setter(sf) {}
    
    virtual bool read( InputStream& is, osg::Object& obj )
    {
        C& object = OBJECT_CAST<C&>(obj);
        std::string value;
        if ( is.isBinary() )
        {
            is >> value;
            if ( ParentType::_defaultValue!=value )
                (object.*_setter)( value );
        }
        else if ( is.matchString(ParentType::_name) )
        {
            is.readWrappedString( value );
            if ( !value.empty() )
                (object.*_setter)( value );
        }
        return true;
    }
    
    virtual bool write( OutputStream& os, const osg::Object& obj )
    {
        const C& object = OBJECT_CAST<const C&>(obj);
        const std::string& value = (object.*_getter)();
        if ( os.isBinary() )
        {
            os << value;
        }
        else if ( ParentType::_defaultValue!=value )
        {
            os << PROPERTY((ParentType::_name).c_str());
            os.writeWrappedString( value );
            os << std::endl;
        }
        return true;
    }
    
public:
    Getter _getter;
    Setter _setter;
};

template<typename C, typename P>
class ObjectSerializer : public TemplateSerializer<P*>
{
public:
    typedef TemplateSerializer<P*> ParentType;
    typedef const P* (C::*Getter)() const;
    typedef void (C::*Setter)( P* );
    
    ObjectSerializer( const char* name, P* def, Getter gf, Setter sf )
    : ParentType(name, def), _getter(gf), _setter(sf) {}
    
    virtual bool read( InputStream& is, osg::Object& obj )
    {
        C& object = OBJECT_CAST<C&>(obj);
        bool hasObject = false;
        if ( is.isBinary() )
        {
            is >> hasObject;
            if ( hasObject )
            {
                P* value = dynamic_cast<P*>( is.readObject() );
                if ( ParentType::_defaultValue!=value )
                    (object.*_setter)( value );
            }
        }
        else if ( is.matchString(ParentType::_name) )
        {
            is >> hasObject;
            if ( hasObject )
            {
                is >> BEGIN_BRACKET;
                P* value = dynamic_cast<P*>( is.readObject() );
                if ( ParentType::_defaultValue!=value )
                    (object.*_setter)( value );
                is >> END_BRACKET;
            }
        }
        return true;
    }
    
    virtual bool write( OutputStream& os, const osg::Object& obj )
    {
        const C& object = OBJECT_CAST<const C&>(obj);
        const P* value = (object.*_getter)();
        bool hasObject = (value!=NULL);
        if ( os.isBinary() )
        {
            os << hasObject;
            os.writeObject( value );
        }
        else if ( ParentType::_defaultValue!=value )
        {
            os << PROPERTY((ParentType::_name).c_str()) << hasObject;
            if ( hasObject )
            {
                os << BEGIN_BRACKET << std::endl;
                os.writeObject( value );
                os << END_BRACKET;
            }
            os << std::endl;
        }
        return true;
    }
    
public:
    Getter _getter;
    Setter _setter;
};

template<typename C, typename P>
class ImageSerializer : public TemplateSerializer<P*>
{
public:
    typedef TemplateSerializer<P*> ParentType;
    typedef const P* (C::*Getter)() const;
    typedef void (C::*Setter)( P* );
    
    ImageSerializer( const char* name, P* def, Getter gf, Setter sf )
    : ParentType(name, def), _getter(gf), _setter(sf) {}
    
    virtual bool read( InputStream& is, osg::Object& obj )
    {
        C& object = OBJECT_CAST<C&>(obj);
        bool hasObject = false;
        if ( is.isBinary() )
        {
            is >> hasObject;
            if ( hasObject )
            {
                P* value = dynamic_cast<P*>( is.readImage() );
                if ( ParentType::_defaultValue!=value )
                    (object.*_setter)( value );
            }
        }
        else if ( is.matchString(ParentType::_name) )
        {
            is >> hasObject;
            if ( hasObject )
            {
                is >> BEGIN_BRACKET;
                P* value = dynamic_cast<P*>( is.readImage() );
                if ( ParentType::_defaultValue!=value )
                    (object.*_setter)( value );
                is >> END_BRACKET;
            }
        }
        return true;
    }
    
    virtual bool write( OutputStream& os, const osg::Object& obj )
    {
        const C& object = OBJECT_CAST<const C&>(obj);
        const P* value = (object.*_getter)();
        bool hasObject = (value!=NULL);
        if ( os.isBinary() )
        {
            os << hasObject;
            os.writeImage( value );
        }
        else if ( ParentType::_defaultValue!=value )
        {
            os << PROPERTY((ParentType::_name).c_str()) << hasObject;
            if ( hasObject )
            {
                os << BEGIN_BRACKET << std::endl;
                os.writeImage( value );
                os << END_BRACKET;
            }
            os << std::endl;
        }
        return true;
    }
    
public:
    Getter _getter;
    Setter _setter;
};

template<typename C, typename P, typename B>
class EnumSerializer : public TemplateSerializer<P>
{
public:
    typedef TemplateSerializer<P> ParentType;
    typedef P (C::*Getter)() const;
    typedef B (C::*Setter)( P );
    
    EnumSerializer( const char* name, P def, Getter gf, Setter sf )
    : ParentType(name, def), _getter(gf), _setter(sf) {}
    
    void add( const char* str, P value )
    { _lookup.add(str, static_cast<IntLookup::Value>(value)); }
    
    P getValue( const char* str )
    { return static_cast<P>(_lookup.getValue(str)); }
    
    const std::string& getString( P value )
    { return _lookup.getString(static_cast<IntLookup::Value>(value)); }
    
    virtual bool read( InputStream& is, osg::Object& obj )
    {
        C& object = OBJECT_CAST<C&>(obj);
        IntLookup::Value value;
        if ( is.isBinary() )
        {
            is >> value;
            if ( ParentType::_defaultValue!=static_cast<P>(value) )
                (object.*_setter)( static_cast<P>(value) );
        }
        else if ( is.matchString(ParentType::_name) )
        {
            std::string str; is >> str;
            (object.*_setter)( getValue(str.c_str()) );
        }
        return true;
    }
    
    virtual bool write( osgDB::OutputStream& os, const osg::Object& obj )
    {
        const C& object = OBJECT_CAST<const C&>(obj);
        const P value = (object.*_getter)();
        if ( os.isBinary() )
        {
            os << (IntLookup::Value)value;
        }
        else if ( ParentType::_defaultValue!=value )
        {
            os << PROPERTY((ParentType::_name).c_str()) << getString(value) << std::endl;
        }
        return true;
    }
    
public:
    Getter _getter;
    Setter _setter;
    
protected:
    IntLookup _lookup;
};


template<typename C, typename P>
class ListSerializer : public BaseSerializer
{
public:
    typedef typename P::value_type ValueType;
    typedef typename P::const_iterator ConstIterator;
    typedef const P& (C::*Getter)() const;
    typedef void (C::*Setter)( const P& );
    
    ListSerializer( const char* name, Getter gf, Setter sf )
    : _name(name), _getter(gf), _setter(sf) {}
    
    virtual const std::string& getName() const { return _name; }

    virtual bool read( InputStream& is, osg::Object& obj )
    {
        C& object = OBJECT_CAST<C&>(obj);
        unsigned int size = 0;
        P list;
        if ( is.isBinary() )
        {
            is >> size;
            for ( unsigned int i=0; i<size; ++i )
            {
                ValueType value;
                is >> value;
                list.push_back( value );
            }
            if ( size>0 ) (object.*_setter)( list );
        }
        else if ( is.matchString(_name) )
        {
            is >> size;
            if ( size>0 ) is >> BEGIN_BRACKET;
            for ( unsigned int i=0; i<size; ++i )
            {
                ValueType value;
                is >> value;
                list.push_back( value );
            }
            if ( size>0 )
            {
                is >> END_BRACKET;
                (object.*_setter)( list );
            }
        }
        return true;
    }
    
    virtual bool write( OutputStream& os, const osg::Object& obj )
    {
        const C& object = OBJECT_CAST<const C&>(obj);
        const P& list = (object.*_getter)();
        unsigned int size = (unsigned int)list.size();
        if ( os.isBinary() )
        {
            os << size;
            for ( ConstIterator itr=list.begin();
                  itr!=list.end(); ++itr )
            {
                os << (*itr);
            }
        }
        else if ( size>0 )
        {
            os << PROPERTY((_name).c_str()) << size << BEGIN_BRACKET << std::endl;
            for ( ConstIterator itr=list.begin();
                  itr!=list.end(); ++itr )
            {
                os << (*itr);
            }
            os << std::endl;
            os << END_BRACKET << std::endl;
        }
        return true;
    }
    
public:
    std::string _name;
    Getter _getter;
    Setter _setter;
};

// ADDING MANIPULATORS
#define ADD_SERIALIZER(S) \
    wrapper->addSerializer( (S) )

#define ADD_USER_SERIALIZER(PROP) \
    wrapper->addSerializer( new osgDB::UserSerializer<MyClass>( \
        #PROP, &check##PROP, &read##PROP, &write##PROP), osgDB::BaseSerializer::RW_USER )

#define ADD_BOOL_SERIALIZER(PROP, DEF) \
    wrapper->addSerializer( new osgDB::PropByValSerializer< MyClass, bool >( \
        #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_BOOL )

#define ADD_CHAR_SERIALIZER(PROP, DEF) \
    wrapper->addSerializer( new osgDB::PropByValSerializer< MyClass, char >( \
        #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_CHAR )

#define ADD_UCHAR_SERIALIZER(PROP, DEF) \
    wrapper->addSerializer( new osgDB::PropByValSerializer< MyClass, unsigned char >( \
        #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_UCHAR )

#define ADD_SHORT_SERIALIZER(PROP, DEF) \
    wrapper->addSerializer( new osgDB::PropByValSerializer< MyClass, short >( \
        #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_SHORT )

#define ADD_USHORT_SERIALIZER(PROP, DEF) \
    wrapper->addSerializer( new osgDB::PropByValSerializer< MyClass, unsigned short >( \
        #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_USHORT )

#define ADD_HEXSHORT_SERIALIZER(PROP, DEF) \
    wrapper->addSerializer( new osgDB::PropByValSerializer< MyClass, unsigned short >( \
        #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP, true), osgDB::BaseSerializer::RW_USHORT )

#define ADD_INT_SERIALIZER(PROP, DEF) \
    wrapper->addSerializer( new osgDB::PropByValSerializer< MyClass, int >( \
        #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_INT )

#define ADD_UINT_SERIALIZER(PROP, DEF) \
    wrapper->addSerializer( new osgDB::PropByValSerializer< MyClass, unsigned int >( \
        #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_UINT )
        
#define ADD_GLINT_SERIALIZER(PROP, DEF) \
    wrapper->addSerializer( new osgDB::PropByValSerializer< MyClass, GLint >( \
        #PROP, ((int)(DEF)), &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_INT )

#define ADD_HEXINT_SERIALIZER(PROP, DEF) \
    wrapper->addSerializer( new osgDB::PropByValSerializer< MyClass, unsigned int >( \
        #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP, true), osgDB::BaseSerializer::RW_UINT )

#define ADD_FLOAT_SERIALIZER(PROP, DEF) \
    wrapper->addSerializer( new osgDB::PropByValSerializer< MyClass, float >( \
        #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_FLOAT )

#define ADD_DOUBLE_SERIALIZER(PROP, DEF) \
    wrapper->addSerializer( new osgDB::PropByValSerializer< MyClass, double >( \
        #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_DOUBLE )

#define ADD_REF_BOOL_SERIALIZER(PROP, DEF) \
    wrapper->addSerializer( new osgDB::PropByRefSerializer< MyClass, bool >( \
        #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_BOOL )

#define ADD_REF_CHAR_SERIALIZER(PROP, DEF) \
    wrapper->addSerializer( new osgDB::PropByRefSerializer< MyClass, char >( \
        #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_CHAR )

#define ADD_REF_UCHAR_SERIALIZER(PROP, DEF) \
    wrapper->addSerializer( new osgDB::PropByRefSerializer< MyClass, unsigned char >( \
        #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_UCHAR )

#define ADD_REF_SHORT_SERIALIZER(PROP, DEF) \
    wrapper->addSerializer( new osgDB::PropByRefSerializer< MyClass, short >( \
        #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_SHORT )

#define ADD_REF_USHORT_SERIALIZER(PROP, DEF) \
    wrapper->addSerializer( new osgDB::PropByRefSerializer< MyClass, unsigned short >( \
        #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_USHORT )

#define ADD_REF_HEXSHORT_SERIALIZER(PROP, DEF) \
    wrapper->addSerializer( new osgDB::PropByRefSerializer< MyClass, unsigned short >( \
        #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP, true), osgDB::BaseSerializer::RW_USHORT )

#define ADD_REF_INT_SERIALIZER(PROP, DEF) \
    wrapper->addSerializer( new osgDB::PropByRefSerializer< MyClass, int >( \
        #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_INT )

#define ADD_REF_UINT_SERIALIZER(PROP, DEF) \
    wrapper->addSerializer( new osgDB::PropByRefSerializer< MyClass, unsigned int >( \
        #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_UINT )

#define ADD_REF_GLINT_SERIALIZER(PROP, DEF) \
    wrapper->addSerializer( new osgDB::PropByRefSerializer< MyClass, GLint >( \
        #PROP, ((int)(DEF)), &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_INT )

#define ADD_REF_HEXINT_SERIALIZER(PROP, DEF) \
    wrapper->addSerializer( new osgDB::PropByRefSerializer< MyClass, unsigned int >( \
        #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP, true), osgDB::BaseSerializer::RW_UINT )

#define ADD_REF_FLOAT_SERIALIZER(PROP, DEF) \
    wrapper->addSerializer( new osgDB::PropByRefSerializer< MyClass, float >( \
        #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_FLOAT )

#define ADD_REF_DOUBLE_SERIALIZER(PROP, DEF) \
    wrapper->addSerializer( new osgDB::PropByRefSerializer< MyClass, double >( \
        #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_DOUBLE )

#define ADD_VEC2F_SERIALIZER(PROP, DEF) \
    wrapper->addSerializer( new osgDB::PropByRefSerializer< MyClass, osg::Vec2f >( \
        #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_VEC2F )

#define ADD_VEC2D_SERIALIZER(PROP, DEF) \
    wrapper->addSerializer( new osgDB::PropByRefSerializer< MyClass, osg::Vec2d >( \
        #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_VEC2D )

#define ADD_VEC2_SERIALIZER(PROP, DEF) ADD_VEC2F_SERIALIZER(PROP, DEF)

#define ADD_VEC3F_SERIALIZER(PROP, DEF) \
    wrapper->addSerializer( new osgDB::PropByRefSerializer< MyClass, osg::Vec3f >( \
        #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_VEC3F )

#define ADD_VEC3D_SERIALIZER(PROP, DEF) \
    wrapper->addSerializer( new osgDB::PropByRefSerializer< MyClass, osg::Vec3d >( \
        #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_VEC3D )

#define ADD_VEC3_SERIALIZER(PROP, DEF) ADD_VEC3F_SERIALIZER(PROP, DEF)

#define ADD_VEC4F_SERIALIZER(PROP, DEF) \
    wrapper->addSerializer( new osgDB::PropByRefSerializer< MyClass, osg::Vec4f >( \
        #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_VEC4F )

#define ADD_VEC4D_SERIALIZER(PROP, DEF) \
    wrapper->addSerializer( new osgDB::PropByRefSerializer< MyClass, osg::Vec4d >( \
        #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_VEC4D )

#define ADD_VEC4_SERIALIZER(PROP, DEF) ADD_VEC4F_SERIALIZER(PROP, DEF)

#define ADD_QUAT_SERIALIZER(PROP, DEF) \
    wrapper->addSerializer( new osgDB::PropByRefSerializer< MyClass, osg::Quat >( \
        #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_QUAT )

#define ADD_PLANE_SERIALIZER(PROP, DEF) \
    wrapper->addSerializer( new osgDB::PropByRefSerializer< MyClass, osg::Plane >( \
        #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_PLANE )

#define ADD_MATRIXF_SERIALIZER(PROP, DEF) \
    wrapper->addSerializer( new osgDB::PropByRefSerializer< MyClass, osg::Matrixf >( \
        #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_MATRIXF )

#define ADD_MATRIXD_SERIALIZER(PROP, DEF) \
    wrapper->addSerializer( new osgDB::PropByRefSerializer< MyClass, osg::Matrixd >( \
        #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_MATRIXD )

#define ADD_MATRIX_SERIALIZER(PROP, DEF) \
    wrapper->addSerializer( new osgDB::MatrixSerializer< MyClass >( \
        #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_MATRIX )

#define ADD_GLENUM_SERIALIZER(PROP, TYPE, DEF) \
    wrapper->addSerializer( new osgDB::GLenumSerializer< MyClass, TYPE >( \
        #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_GLENUM )

#define ADD_STRING_SERIALIZER(PROP, DEF) \
    wrapper->addSerializer( new osgDB::StringSerializer< MyClass >( \
        #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_STRING )

#define ADD_OBJECT_SERIALIZER(PROP, TYPE, DEF) \
    wrapper->addSerializer( new osgDB::ObjectSerializer< MyClass, TYPE >( \
        #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_OBJECT )

#define ADD_IMAGE_SERIALIZER(PROP, TYPE, DEF) \
    wrapper->addSerializer( new osgDB::ImageSerializer< MyClass, TYPE >( \
        #PROP, DEF, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_IMAGE )

#define ADD_LIST_SERIALIZER(PROP, TYPE) \
    wrapper->addSerializer( new osgDB::ListSerializer< MyClass, TYPE >( \
        #PROP, &MyClass::get##PROP, &MyClass::set##PROP), osgDB::BaseSerializer::RW_LIST )

#define BEGIN_ENUM_SERIALIZER(PROP, DEF) \
    { typedef osgDB::EnumSerializer<MyClass, MyClass::PROP, void> MySerializer; \
    osg::ref_ptr<MySerializer> serializer = new MySerializer( \
        #PROP, MyClass::DEF, &MyClass::get##PROP, &MyClass::set##PROP)

#define BEGIN_ENUM_SERIALIZER2(PROP, TYPE, DEF) \
    { typedef osgDB::EnumSerializer<MyClass, TYPE, void> MySerializer; \
    osg::ref_ptr<MySerializer> serializer = new MySerializer( \
        #PROP, MyClass::DEF, &MyClass::get##PROP, &MyClass::set##PROP)

#define BEGIN_ENUM_SERIALIZER3(PROP, DEF) \
    { typedef osgDB::EnumSerializer<MyClass, MyClass::PROP, bool> MySerializer; \
    osg::ref_ptr<MySerializer> serializer = new MySerializer( \
        #PROP, MyClass::DEF, &MyClass::get##PROP, &MyClass::set##PROP)

#define BEGIN_ENUM_SERIALIZER4(PROPERTIES_CLASS, PROP, DEF) \
    { typedef osgDB::EnumSerializer<MyClass, PROPERTIES_CLASS::PROP, void> MySerializer; \
    osg::ref_ptr<MySerializer> serializer = new MySerializer( \
        #PROP, PROPERTIES_CLASS::DEF, &MyClass::get##PROP, &MyClass::set##PROP)

#define ADD_ENUM_VALUE(VALUE) \
    serializer->add(#VALUE, MyClass::VALUE)

#define ADD_ENUM_CLASS_VALUE(CLASS, VALUE) \
    serializer->add(#VALUE, CLASS::VALUE)

#define END_ENUM_SERIALIZER() \
    wrapper->addSerializer(serializer.get(), osgDB::BaseSerializer::RW_ENUM); }

// VERSION CONTROL OPERATORS
#define UPDATE_TO_VERSION(VER) \
    wrapper->setUpdatedVersion( (VER) );

#define REMOVE_SERIALIZER(PROP) \
    wrapper->markSerializerAsRemoved( #PROP );

}

#endif