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

Revision 13788, 8.3 kB (checked in by robert, 2 hours ago)

Removed checks against Drawable as these are no longer required.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
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    ObjectWrapper( osg::Object* proto, const std::string& domain, const std::string& name,
54                   const std::string& associates );
55
56    void setUpdatedVersion( int ver ) { _version = ver; }
57    int getUpdatedVersion() const { return _version; }
58
59    const osg::Object* getProto() const { return _proto.get(); }
60    const std::string& getName() const { return _name; }
61    const StringList& getAssociates() const { return _associates; }
62
63    void addSerializer( BaseSerializer* s, BaseSerializer::Type t=BaseSerializer::RW_UNDEFINED );
64    void markSerializerAsRemoved( const std::string& name );
65    BaseSerializer* getSerializer( const std::string& name );
66    void addFinishedObjectReadCallback ( FinishedObjectReadCallback* forc) { _finishedObjectReadCallbacks.push_back(forc); }
67
68    bool read( InputStream&, osg::Object& );
69    bool write( OutputStream&, const osg::Object& );
70
71    bool readSchema( const StringList& properties, const std::vector<int>& types );
72    void writeSchema( StringList& properties, std::vector<int>& types );
73    void resetSchema()
74    { if ( _backupSerializers.size()>0 ) _serializers = _backupSerializers; }
75
76protected:
77    ObjectWrapper() : _version(0) {}
78    virtual ~ObjectWrapper() {}
79
80    osg::ref_ptr<osg::Object> _proto;
81    std::string _domain;
82    std::string _name;
83    StringList _associates;
84    SerializerList _serializers;
85    SerializerList _backupSerializers;
86    std::vector<int> _typeList;
87    FinishedObjectReadCallbackList _finishedObjectReadCallbacks;
88    int _version;  // Last updated version of the wrapper
89};
90
91struct UpdateWrapperVersionProxy
92{
93    UpdateWrapperVersionProxy( ObjectWrapper* w, int v ): _wrapper(w)
94    {
95        _lastVersion = w->getUpdatedVersion();
96        w->setUpdatedVersion(v);
97    }
98
99    ~UpdateWrapperVersionProxy()
100    {
101        _wrapper->setUpdatedVersion(_lastVersion);
102    }
103
104    ObjectWrapper* _wrapper;
105    int _lastVersion;
106};
107
108class Registry;
109
110class OSGDB_EXPORT ObjectWrapperManager : public osg::Referenced
111{
112public:
113
114    // Wrapper handlers
115    void addWrapper( ObjectWrapper* wrapper );
116    void removeWrapper( ObjectWrapper* wrapper );
117    ObjectWrapper* findWrapper( const std::string& name );
118
119    typedef std::map< std::string, osg::ref_ptr<ObjectWrapper> > WrapperMap;
120    WrapperMap& getWrapperMap() { return _wrappers; }
121    const WrapperMap& getWrapperMap() const { return _wrappers; }
122
123    // Compressor handlers
124    void addCompressor( BaseCompressor* compressor );
125    void removeCompressor( BaseCompressor* compressor );
126    BaseCompressor* findCompressor( const std::string& name );
127
128    typedef std::map< std::string, osg::ref_ptr<BaseCompressor> > CompressorMap;
129    CompressorMap& getCompressorMap() { return _compressors; }
130    const CompressorMap& getCompressorMap() const { return _compressors; }
131
132    typedef std::map<std::string, IntLookup> IntLookupMap;
133    IntLookup::Value getValue( const std::string& group, const std::string& str ) { return findLookup(group).getValue(str.c_str()); }
134    const std::string& getString( const std::string& group, IntLookup::Value value ) { return findLookup(group).getString(value); }
135
136    IntLookupMap& getLookupMap() { return _globalMap; }
137    const IntLookupMap& getLookupMap() const { return _globalMap; }
138
139protected:
140
141    friend class osgDB::Registry;
142
143    ObjectWrapperManager();
144    virtual ~ObjectWrapperManager();
145
146    WrapperMap _wrappers;
147    CompressorMap _compressors;
148
149    IntLookup& findLookup( const std::string& group )
150    {
151        IntLookupMap::iterator itr = _globalMap.find(group);
152        if ( itr!=_globalMap.end() ) return itr->second;
153        else return _globalMap["GL"];
154    }
155
156    IntLookupMap _globalMap;
157};
158
159
160class OSGDB_EXPORT RegisterWrapperProxy
161{
162public:
163    typedef void (*AddPropFunc)( ObjectWrapper* );
164
165    RegisterWrapperProxy( osg::Object* proto, const std::string& name,
166                          const std::string& associates, AddPropFunc func );
167
168    virtual ~RegisterWrapperProxy();
169
170protected:
171    osg::ref_ptr<ObjectWrapper> _wrapper;
172};
173
174class OSGDB_EXPORT RegisterCustomWrapperProxy
175{
176public:
177    typedef void (*AddPropFunc)( const char*, ObjectWrapper* );
178
179    RegisterCustomWrapperProxy( osg::Object* proto, const std::string& domain, const std::string& name,
180                                const std::string& associates, AddPropFunc func );
181
182    virtual ~RegisterCustomWrapperProxy();
183
184protected:
185    osg::ref_ptr<ObjectWrapper> _wrapper;
186};
187
188#define REGISTER_OBJECT_WRAPPER(NAME, PROTO, CLASS, ASSOCIATES) \
189    extern "C" void wrapper_serializer_##NAME(void) {} \
190    extern void wrapper_propfunc_##NAME(osgDB::ObjectWrapper*); \
191    static osgDB::RegisterWrapperProxy wrapper_proxy_##NAME( \
192        PROTO, #CLASS, ASSOCIATES, &wrapper_propfunc_##NAME); \
193    typedef CLASS MyClass; \
194    void wrapper_propfunc_##NAME(osgDB::ObjectWrapper* wrapper)
195
196#define REGISTER_OBJECT_WRAPPER2(NAME, PROTO, CLASS, CLASSNAME, ASSOCIATES) \
197    extern "C" void wrapper_serializer_##NAME(void) {} \
198    extern void wrapper_propfunc_##NAME(osgDB::ObjectWrapper*); \
199    static osgDB::RegisterWrapperProxy wrapper_proxy_##NAME( \
200        PROTO, CLASSNAME, ASSOCIATES, &wrapper_propfunc_##NAME); \
201    typedef CLASS MyClass; \
202    void wrapper_propfunc_##NAME(osgDB::ObjectWrapper* wrapper)
203
204#define REGISTER_CUSTOM_OBJECT_WRAPPER(DOMAIN, NAME, PROTO, CLASS, ASSOCIATES) \
205    extern "C" void wrapper_serializer_##NAME(void) {} \
206    extern void wrapper_propfunc_##NAME(const char*, osgDB::ObjectWrapper*); \
207    static osgDB::RegisterCustomWrapperProxy wrapper_proxy_##NAME( \
208        PROTO, #DOMAIN, #CLASS, ASSOCIATES, &wrapper_propfunc_##NAME); \
209    typedef CLASS MyClass; \
210    void wrapper_propfunc_##NAME(const char* domain, osgDB::ObjectWrapper* wrapper)
211
212#define REGISTER_CUSTOM_OBJECT_WRAPPER2(DOMAIN, NAME, PROTO, CLASS, CLASSNAME, ASSOCIATES) \
213    extern "C" void wrapper_serializer_##NAME(void) {} \
214    extern void wrapper_propfunc_##NAME(const char*, osgDB::ObjectWrapper*); \
215    static osgDB::RegisterCustomWrapperProxy wrapper_proxy_##NAME( \
216        PROTO, #DOMAIN, CLASSNAME, ASSOCIATES, &wrapper_propfunc_##NAME); \
217    typedef CLASS MyClass; \
218    void wrapper_propfunc_##NAME(const char* domain, osgDB::ObjectWrapper* wrapper)
219
220class OSGDB_EXPORT RegisterCompressorProxy
221{
222public:
223    RegisterCompressorProxy( const std::string& name, BaseCompressor* compressor );
224    ~RegisterCompressorProxy();
225
226protected:
227    osg::ref_ptr<BaseCompressor> _compressor;
228};
229
230#define REGISTER_COMPRESSOR(NAME, CLASS) \
231    extern "C" void wrapper_compressor_##CLASS(void) {} \
232    static osgDB::RegisterCompressorProxy compressor_proxy_##CLASS(NAME, new CLASS);
233
234}
235
236#endif
Note: See TracBrowser for help on using the browser.