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

Revision 13541, 8.2 kB (checked in by robert, 11 hours ago)

From Jason Beverage, "It looks like the Callback header got accidentally removed from the CMakeLists.txt in the submission yesterday for the geometry instancing example."

  • 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
136protected:
137
138    friend class osgDB::Registry;
139
140    ObjectWrapperManager();
141    virtual ~ObjectWrapperManager();
142
143    WrapperMap _wrappers;
144    CompressorMap _compressors;
145
146    IntLookup& findLookup( const std::string& group )
147    {
148        IntLookupMap::iterator itr = _globalMap.find(group);
149        if ( itr!=_globalMap.end() ) return itr->second;
150        else return _globalMap["GL"];
151    }
152
153    IntLookupMap _globalMap;
154};
155
156
157class OSGDB_EXPORT RegisterWrapperProxy
158{
159public:
160    typedef void (*AddPropFunc)( ObjectWrapper* );
161
162    RegisterWrapperProxy( osg::Object* proto, const std::string& name,
163                          const std::string& associates, AddPropFunc func );
164
165    virtual ~RegisterWrapperProxy();
166
167protected:
168    osg::ref_ptr<ObjectWrapper> _wrapper;
169};
170
171class OSGDB_EXPORT RegisterCustomWrapperProxy
172{
173public:
174    typedef void (*AddPropFunc)( const char*, ObjectWrapper* );
175
176    RegisterCustomWrapperProxy( osg::Object* proto, const std::string& domain, const std::string& name,
177                                const std::string& associates, AddPropFunc func );
178
179    virtual ~RegisterCustomWrapperProxy();
180
181protected:
182    osg::ref_ptr<ObjectWrapper> _wrapper;
183};
184
185#define REGISTER_OBJECT_WRAPPER(NAME, PROTO, CLASS, ASSOCIATES) \
186    extern "C" void wrapper_serializer_##NAME(void) {} \
187    extern void wrapper_propfunc_##NAME(osgDB::ObjectWrapper*); \
188    static osgDB::RegisterWrapperProxy wrapper_proxy_##NAME( \
189        PROTO, #CLASS, ASSOCIATES, &wrapper_propfunc_##NAME); \
190    typedef CLASS MyClass; \
191    void wrapper_propfunc_##NAME(osgDB::ObjectWrapper* wrapper)
192
193#define REGISTER_OBJECT_WRAPPER2(NAME, PROTO, CLASS, CLASSNAME, ASSOCIATES) \
194    extern "C" void wrapper_serializer_##NAME(void) {} \
195    extern void wrapper_propfunc_##NAME(osgDB::ObjectWrapper*); \
196    static osgDB::RegisterWrapperProxy wrapper_proxy_##NAME( \
197        PROTO, CLASSNAME, ASSOCIATES, &wrapper_propfunc_##NAME); \
198    typedef CLASS MyClass; \
199    void wrapper_propfunc_##NAME(osgDB::ObjectWrapper* wrapper)
200
201#define REGISTER_CUSTOM_OBJECT_WRAPPER(DOMAIN, NAME, PROTO, CLASS, ASSOCIATES) \
202    extern "C" void wrapper_serializer_##NAME(void) {} \
203    extern void wrapper_propfunc_##NAME(const char*, osgDB::ObjectWrapper*); \
204    static osgDB::RegisterCustomWrapperProxy wrapper_proxy_##NAME( \
205        PROTO, #DOMAIN, #CLASS, ASSOCIATES, &wrapper_propfunc_##NAME); \
206    typedef CLASS MyClass; \
207    void wrapper_propfunc_##NAME(const char* domain, osgDB::ObjectWrapper* wrapper)
208
209#define REGISTER_CUSTOM_OBJECT_WRAPPER2(DOMAIN, NAME, PROTO, CLASS, CLASSNAME, ASSOCIATES) \
210    extern "C" void wrapper_serializer_##NAME(void) {} \
211    extern void wrapper_propfunc_##NAME(const char*, osgDB::ObjectWrapper*); \
212    static osgDB::RegisterCustomWrapperProxy wrapper_proxy_##NAME( \
213        PROTO, #DOMAIN, CLASSNAME, ASSOCIATES, &wrapper_propfunc_##NAME); \
214    typedef CLASS MyClass; \
215    void wrapper_propfunc_##NAME(const char* domain, osgDB::ObjectWrapper* wrapper)
216
217class OSGDB_EXPORT RegisterCompressorProxy
218{
219public:
220    RegisterCompressorProxy( const std::string& name, BaseCompressor* compressor );
221    ~RegisterCompressorProxy();
222
223protected:
224    osg::ref_ptr<BaseCompressor> _compressor;
225};
226
227#define REGISTER_COMPRESSOR(NAME, CLASS) \
228    extern "C" void wrapper_compressor_##CLASS(void) {} \
229    static osgDB::RegisterCompressorProxy compressor_proxy_##CLASS(NAME, new CLASS);
230
231}
232
233#endif
Note: See TracBrowser for help on using the browser.