root/OpenSceneGraph/trunk/include/osgDB/ObjectWrapper @ 11903

Revision 11903, 6.3 kB (checked in by robert, 4 years ago)

Refactored the versioning of serializers so it now uses a _firstVersion and _lastVersion make it possible
to specify what range of versions support each serializer.

Line 
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_OBJECTWRAPPER
16#define OSGDB_OBJECTWRAPPER
17
18#include <osgDB/Serializer>
19
20namespace osgDB
21{
22
23typedef std::vector<std::string> StringList;
24extern OSGDB_EXPORT void split( const std::string& src, StringList& list, char separator=' ' );
25
26class OSGDB_EXPORT BaseCompressor : public osg::Referenced
27{
28public:
29    BaseCompressor() {}
30    void setName( const std::string& name ) { _name = name; }
31    const std::string& getName() const { return _name; }
32
33    virtual bool compress( std::ostream&, const std::string& ) = 0;
34    virtual bool decompress( std::istream&, std::string& ) = 0;
35
36protected:
37    std::string _name;
38};
39
40struct FinishedObjectReadCallback : public osg::Referenced
41{
42    virtual void objectRead(osgDB::InputStream& is, osg::Object& obj) = 0;
43};
44
45class OSGDB_EXPORT ObjectWrapper : public osg::Referenced
46{
47public:
48    typedef std::vector< osg::ref_ptr<BaseSerializer> > SerializerList;
49    typedef std::vector< osg::ref_ptr<FinishedObjectReadCallback> > FinishedObjectReadCallbackList;
50   
51    ObjectWrapper( osg::Object* proto, const std::string& name,
52                   const std::string& associates );
53    void setUpdatedVersion( int ver ) { _version = ver; }
54   
55    const osg::Object* getProto() const { return _proto.get(); }
56    const std::string& getName() const { return _name; }
57    const StringList& getAssociates() const { return _associates; }
58
59    void addSerializer( BaseSerializer* s, BaseSerializer::Type t=BaseSerializer::RW_UNDEFINED );
60    void markSerializerAsRemoved( const std::string& name );
61    BaseSerializer* getSerializer( const std::string& name );
62    void addFinishedObjectReadCallback ( FinishedObjectReadCallback* forc) { _finishedObjectReadCallbacks.push_back(forc); }
63
64    bool read( InputStream&, osg::Object& );
65    bool write( OutputStream&, const osg::Object& );
66
67    bool readSchema( const StringList& properties, const std::vector<int>& types );
68    void writeSchema( StringList& properties, std::vector<int>& types );
69    void resetSchema()
70    { if ( _backupSerializers.size()>0 ) _serializers = _backupSerializers; }
71   
72protected:
73    ObjectWrapper() : _version(0) {}
74    virtual ~ObjectWrapper() {}
75
76    osg::ref_ptr<osg::Object> _proto;
77    std::string _name;
78    StringList _associates;
79    SerializerList _serializers;
80    SerializerList _backupSerializers;
81    std::vector<int> _typeList;
82    FinishedObjectReadCallbackList _finishedObjectReadCallbacks;
83    int _version;  // Last updated version of the wrapper
84};
85
86class Registry;
87
88class OSGDB_EXPORT ObjectWrapperManager : public osg::Referenced
89{
90public:
91
92    // Wrapper handlers
93    void addWrapper( ObjectWrapper* wrapper );
94    void removeWrapper( ObjectWrapper* wrapper );
95    ObjectWrapper* findWrapper( const std::string& name );
96
97    typedef std::map< std::string, osg::ref_ptr<ObjectWrapper> > WrapperMap;
98    WrapperMap& getWrapperMap() { return _wrappers; }
99    const WrapperMap& getWrapperMap() const { return _wrappers; }
100
101    // Compressor handlers
102    void addCompressor( BaseCompressor* compressor );
103    void removeCompressor( BaseCompressor* compressor );
104    BaseCompressor* findCompressor( const std::string& name );
105
106    typedef std::map< std::string, osg::ref_ptr<BaseCompressor> > CompressorMap;
107    CompressorMap& getCompressorMap() { return _compressors; }
108    const CompressorMap& getCompressorMap() const { return _compressors; }
109
110    typedef std::map<std::string, IntLookup> IntLookupMap;
111    IntLookup::Value getValue( const std::string& group, const std::string& str ) { return findLookup(group).getValue(str.c_str()); }
112    const std::string& getString( const std::string& group, IntLookup::Value value ) { return findLookup(group).getString(value); }
113
114protected:
115
116    friend class osgDB::Registry;
117
118    ObjectWrapperManager();
119    virtual ~ObjectWrapperManager();
120
121    WrapperMap _wrappers;
122    CompressorMap _compressors;
123
124    IntLookup& findLookup( const std::string& group )
125    {
126        IntLookupMap::iterator itr = _globalMap.find(group);
127        if ( itr!=_globalMap.end() ) return itr->second;
128        else return _globalMap["GL"];
129    }
130
131    IntLookupMap _globalMap;
132};
133
134
135class OSGDB_EXPORT RegisterWrapperProxy
136{
137public:
138    typedef void (*AddPropFunc)( ObjectWrapper* );
139
140    RegisterWrapperProxy( osg::Object* proto, const std::string& name,
141                          const std::string& associates, AddPropFunc func );
142
143    virtual ~RegisterWrapperProxy();
144
145protected:
146    osg::ref_ptr<ObjectWrapper> _wrapper;
147};
148
149#define REGISTER_OBJECT_WRAPPER(NAME, PROTO, CLASS, ASSOCIATES) \
150    extern "C" void wrapper_serializer_##NAME(void) {} \
151    extern void wrapper_propfunc_##NAME(osgDB::ObjectWrapper*); \
152    static osgDB::RegisterWrapperProxy wrapper_proxy_##NAME( \
153        PROTO, #CLASS, ASSOCIATES, &wrapper_propfunc_##NAME); \
154    typedef CLASS MyClass; \
155    void wrapper_propfunc_##NAME(osgDB::ObjectWrapper* wrapper)
156
157#define REGISTER_OBJECT_WRAPPER2(NAME, PROTO, CLASS, CLASSNAME, ASSOCIATES) \
158    extern "C" void wrapper_serializer_##NAME(void) {} \
159    extern void wrapper_propfunc_##NAME(osgDB::ObjectWrapper*); \
160    static osgDB::RegisterWrapperProxy wrapper_proxy_##NAME( \
161        PROTO, CLASSNAME, ASSOCIATES, &wrapper_propfunc_##NAME); \
162    typedef CLASS MyClass; \
163    void wrapper_propfunc_##NAME(osgDB::ObjectWrapper* wrapper)
164
165class OSGDB_EXPORT RegisterCompressorProxy
166{
167public:
168    RegisterCompressorProxy( const std::string& name, BaseCompressor* compressor );
169    ~RegisterCompressorProxy();
170
171protected:
172    osg::ref_ptr<BaseCompressor> _compressor;
173};
174
175#define REGISTER_COMPRESSOR(NAME, CLASS) \
176    extern "C" void wrapper_compressor_##CLASS(void) {} \
177    static osgDB::RegisterCompressorProxy compressor_proxy_##CLASS(NAME, new CLASS);
178
179}
180
181#endif
Note: See TracBrowser for help on using the browser.