OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ObjectWrapper.h
Go to the documentation of this file.
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 <OpenThreads/ReentrantMutex>
19 #include <osgDB/Serializer>
20 #include <osg/ScriptEngine>
21 
22 namespace osgDB
23 {
24 
26 {
27  typedef std::vector< osg::ref_ptr<osg::Object> > Parameters;
28 
29  virtual bool run(void* objectPtr, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const = 0;
30 
31  virtual ~MethodObject() {}
32 };
33 
35 {
36 public:
38  void setName( const std::string& name ) { _name = name; }
39  const std::string& getName() const { return _name; }
40 
41  virtual bool compress( std::ostream&, const std::string& ) = 0;
42  virtual bool decompress( std::istream&, std::string& ) = 0;
43 
44 protected:
45  std::string _name;
46 };
47 
49 {
50  virtual void objectRead(osgDB::InputStream& is, osg::Object& obj) = 0;
51 };
52 
54 {
55 public:
56  typedef std::vector< BaseSerializer::Type > TypeList;
57  typedef std::vector< osg::ref_ptr<BaseSerializer> > SerializerList;
58  typedef std::vector< osg::ref_ptr<FinishedObjectReadCallback> > FinishedObjectReadCallbackList;
60 
61  ObjectWrapper( CreateInstanceFunc* createInstanceFunc, const std::string& name,
62  const std::string& associates );
63  ObjectWrapper( CreateInstanceFunc* createInstanceFunc, const std::string& domain, const std::string& name,
64  const std::string& associates );
65 
66  void setUpdatedVersion( int ver ) { _version = ver; }
67  int getUpdatedVersion() const { return _version; }
68 
69  osg::Object* createInstance() const { return _createInstanceFunc(); }
70  const std::string& getDomain() const { return _domain; }
71  const std::string& getName() const { return _name; }
72  const StringList& getAssociates() const { return _associates; }
73 
74  SerializerList& getSerializerList() { return _serializers; }
75  const SerializerList& getSerializerList() const { return _serializers; }
76 
77  TypeList& getTypeList() { return _typeList; }
78  const TypeList& getTypeList() const { return _typeList; }
79 
81  void markSerializerAsRemoved( const std::string& name );
82  BaseSerializer* getLastSerializer() { return _serializers.empty() ? 0 : _serializers.back().get(); }
83  BaseSerializer* getSerializer( const std::string& name );
84  BaseSerializer* getSerializer( const std::string& name, BaseSerializer::Type& type);
85 
86  void addFinishedObjectReadCallback ( FinishedObjectReadCallback* forc) { _finishedObjectReadCallbacks.push_back(forc); }
87 
88  bool read( InputStream&, osg::Object& );
89  bool write( OutputStream&, const osg::Object& );
90 
91  bool readSchema( const StringList& properties, const TypeList& types );
92  void writeSchema( StringList& properties, TypeList& types );
93  void resetSchema() { if ( _backupSerializers.size()>0 ) _serializers = _backupSerializers; }
94 
95  void addMethodObject(const std::string& methodName, MethodObject* mo);
96 
97  typedef std::multimap< std::string, osg::ref_ptr<MethodObject> > MethodObjectMap;
98 
99  MethodObjectMap& getMethodObjectMap() { return _methodObjectMap; }
100  const MethodObjectMap& getMethodObjectMap() const { return _methodObjectMap; }
101 
102 protected:
103  ObjectWrapper() : _version(0) {}
104  virtual ~ObjectWrapper() {}
105 
106  CreateInstanceFunc* _createInstanceFunc;
107  std::string _domain;
108  std::string _name;
110  SerializerList _serializers;
111  SerializerList _backupSerializers;
112  TypeList _typeList;
113  FinishedObjectReadCallbackList _finishedObjectReadCallbacks;
114  MethodObjectMap _methodObjectMap;
115  int _version; // Last updated version of the wrapper
116 };
117 
119 {
121  {
123  w->setUpdatedVersion(v);
124  }
125 
127  {
129  }
130 
133 };
134 
135 class Registry;
136 
138 {
139 public:
140 
141  // Wrapper handlers
142  void addWrapper( ObjectWrapper* wrapper );
143  void removeWrapper( ObjectWrapper* wrapper );
144  ObjectWrapper* findWrapper( const std::string& name );
145 
146  typedef std::map< std::string, osg::ref_ptr<ObjectWrapper> > WrapperMap;
147  WrapperMap& getWrapperMap() { return _wrappers; }
148  const WrapperMap& getWrapperMap() const { return _wrappers; }
149 
150  // Compressor handlers
151  void addCompressor( BaseCompressor* compressor );
152  void removeCompressor( BaseCompressor* compressor );
153  BaseCompressor* findCompressor( const std::string& name );
154 
155  typedef std::map< std::string, osg::ref_ptr<BaseCompressor> > CompressorMap;
156  CompressorMap& getCompressorMap() { return _compressors; }
157  const CompressorMap& getCompressorMap() const { return _compressors; }
158 
159  typedef std::map<std::string, IntLookup> IntLookupMap;
160  IntLookup::Value getValue( const std::string& group, const std::string& str ) { return findLookup(group).getValue(str.c_str()); }
161  const std::string& getString( const std::string& group, IntLookup::Value value ) { return findLookup(group).getString(value); }
162 
163  IntLookupMap& getLookupMap() { return _globalMap; }
164  const IntLookupMap& getLookupMap() const { return _globalMap; }
165 
166 protected:
167 
168  friend class osgDB::Registry;
169 
171  virtual ~ObjectWrapperManager();
172 
174 
175  WrapperMap _wrappers;
176  CompressorMap _compressors;
177 
178  IntLookup& findLookup( const std::string& group )
179  {
180  IntLookupMap::iterator itr = _globalMap.find(group);
181  if ( itr!=_globalMap.end() ) return itr->second;
182  else return _globalMap["GL"];
183  }
184 
185  IntLookupMap _globalMap;
186 };
187 
188 
190 {
191 public:
192  typedef void (*AddPropFunc)( ObjectWrapper* );
193 
194  RegisterWrapperProxy( ObjectWrapper::CreateInstanceFunc *createInstanceFunc, const std::string& name,
195  const std::string& associates, AddPropFunc func );
196 
197  virtual ~RegisterWrapperProxy();
198 
199 protected:
201 };
202 
204 {
205 public:
206  typedef void (*AddPropFunc)( const char*, ObjectWrapper* );
207 
208  RegisterCustomWrapperProxy( ObjectWrapper::CreateInstanceFunc *createInstanceFunc, const std::string& domain, const std::string& name,
209  const std::string& associates, AddPropFunc func );
210 
211  virtual ~RegisterCustomWrapperProxy();
212 
213 protected:
215 };
216 
217 #define REGISTER_OBJECT_WRAPPER(NAME, CREATEINSTANCE, CLASS, ASSOCIATES) \
218  extern "C" void wrapper_serializer_##NAME(void) {} \
219  extern void wrapper_propfunc_##NAME(osgDB::ObjectWrapper*); \
220  static osg::Object* wrapper_createinstancefunc##NAME() { return CREATEINSTANCE; } \
221  static osgDB::RegisterWrapperProxy wrapper_proxy_##NAME( \
222  wrapper_createinstancefunc##NAME, #CLASS, ASSOCIATES, &wrapper_propfunc_##NAME); \
223  typedef CLASS MyClass; \
224  void wrapper_propfunc_##NAME(osgDB::ObjectWrapper* wrapper)
225 
226 #define REGISTER_OBJECT_WRAPPER2(NAME, CREATEINSTANCE, CLASS, CLASSNAME, ASSOCIATES) \
227  extern "C" void wrapper_serializer_##NAME(void) {} \
228  extern void wrapper_propfunc_##NAME(osgDB::ObjectWrapper*); \
229  static osg::Object* wrapper_createinstancefunc##NAME() { return CREATEINSTANCE; } \
230  static osgDB::RegisterWrapperProxy wrapper_proxy_##NAME( \
231  wrapper_createinstancefunc##NAME, CLASSNAME, ASSOCIATES, &wrapper_propfunc_##NAME); \
232  typedef CLASS MyClass; \
233  void wrapper_propfunc_##NAME(osgDB::ObjectWrapper* wrapper)
234 
235 #define REGISTER_CUSTOM_OBJECT_WRAPPER(DOMAIN, NAME, CREATEINSTANCE, CLASS, ASSOCIATES) \
236  extern "C" void wrapper_serializer_##NAME(void) {} \
237  extern void wrapper_propfunc_##NAME(const char*, osgDB::ObjectWrapper*); \
238  static osg::Object* wrapper_createinstancefunc##NAME() { return CREATEINSTANCE; } \
239  static osgDB::RegisterCustomWrapperProxy wrapper_proxy_##NAME( \
240  wrapper_createinstancefunc##NAME, #DOMAIN, #CLASS, ASSOCIATES, &wrapper_propfunc_##NAME); \
241  typedef CLASS MyClass; \
242  void wrapper_propfunc_##NAME(const char* domain, osgDB::ObjectWrapper* wrapper)
243 
244 #define REGISTER_CUSTOM_OBJECT_WRAPPER2(DOMAIN, NAME, CREATEINSTANCE, CLASS, CLASSNAME, ASSOCIATES) \
245  extern "C" void wrapper_serializer_##NAME(void) {} \
246  extern void wrapper_propfunc_##NAME(const char*, osgDB::ObjectWrapper*); \
247  static osg::Object* wrapper_createinstancefunc##NAME() { return CREATEINSTANCE; } \
248  static osgDB::RegisterCustomWrapperProxy wrapper_proxy_##NAME( \
249  wrapper_createinstancefunc##NAME, #DOMAIN, CLASSNAME, ASSOCIATES, &wrapper_propfunc_##NAME); \
250  typedef CLASS MyClass; \
251  void wrapper_propfunc_##NAME(const char* domain, osgDB::ObjectWrapper* wrapper)
252 
254 {
255 public:
256  RegisterCompressorProxy( const std::string& name, BaseCompressor* compressor );
258 
259 protected:
261 };
262 
263 #define REGISTER_COMPRESSOR(NAME, CLASS) \
264  extern "C" void wrapper_compressor_##CLASS(void) {} \
265  static osgDB::RegisterCompressorProxy compressor_proxy_##CLASS(NAME, new CLASS);
266 
267 }
268 
269 #endif
UpdateWrapperVersionProxy(ObjectWrapper *w, int v)
FinishedObjectReadCallbackList _finishedObjectReadCallbacks
std::multimap< std::string, osg::ref_ptr< MethodObject > > MethodObjectMap
Definition: ObjectWrapper.h:97
std::map< std::string, osg::ref_ptr< BaseCompressor > > CompressorMap
TypeList & getTypeList()
Definition: ObjectWrapper.h:77
MethodObjectMap _methodObjectMap
SerializerList _backupSerializers
const std::string & getString(const std::string &group, IntLookup::Value value)
int getUpdatedVersion() const
Definition: ObjectWrapper.h:67
std::vector< osg::ref_ptr< BaseSerializer > > SerializerList
Definition: ObjectWrapper.h:57
IntLookupMap & getLookupMap()
IntLookup & findLookup(const std::string &group)
std::vector< std::string > StringList
Definition: Serializer.h:31
std::map< std::string, IntLookup > IntLookupMap
IntLookup::Value getValue(const std::string &group, const std::string &str)
SerializerList _serializers
virtual bool get(const osg::Object &, void *)
Definition: Serializer.h:158
const std::string & getName() const
Definition: ObjectWrapper.h:71
std::vector< osg::ref_ptr< osg::Object > > Parameters
Definition: Callback.h:92
osg::Object * createInstance() const
Definition: ObjectWrapper.h:69
SerializerList & getSerializerList()
Definition: ObjectWrapper.h:74
const SerializerList & getSerializerList() const
Definition: ObjectWrapper.h:75
const StringList & getAssociates() const
Definition: ObjectWrapper.h:72
osg::ref_ptr< BaseCompressor > _compressor
OpenThreads::ReentrantMutex _wrapperMutex
typedef void(GL_APIENTRY *GLTexImage3DProc)(GLenum target
virtual bool run(void *objectPtr, osg::Parameters &inputParameters, osg::Parameters &outputParameters) const =0
void setName(const std::string &name)
Definition: ObjectWrapper.h:38
void setUpdatedVersion(int ver)
Definition: ObjectWrapper.h:66
const std::string & getDomain() const
Definition: ObjectWrapper.h:70
BaseSerializer * getLastSerializer()
Definition: ObjectWrapper.h:82
Definition: Archive.h:24
CompressorMap & getCompressorMap()
void addFinishedObjectReadCallback(FinishedObjectReadCallback *forc)
Definition: ObjectWrapper.h:86
GLint GLenum GLsizei GLsizei GLsizei GLint GLenum GLenum type
Definition: GLU.h:71
osg::ref_ptr< ObjectWrapper > _wrapper
#define OSGDB_EXPORT
Definition: Export.h:39
MethodObjectMap & getMethodObjectMap()
Definition: ObjectWrapper.h:99
virtual void objectRead(osgDB::InputStream &is, osg::Object &obj)=0
osg::ref_ptr< ObjectWrapper > _wrapper
std::vector< osg::ref_ptr< osg::Object > > Parameters
Definition: ObjectWrapper.h:27
std::map< std::string, osg::ref_ptr< ObjectWrapper > > WrapperMap
std::vector< osg::ref_ptr< FinishedObjectReadCallback > > FinishedObjectReadCallbackList
Definition: ObjectWrapper.h:58
const CompressorMap & getCompressorMap() const
osg::Object * CreateInstanceFunc()
Definition: ObjectWrapper.h:59
const WrapperMap & getWrapperMap() const
const IntLookupMap & getLookupMap() const
const TypeList & getTypeList() const
Definition: ObjectWrapper.h:78
const MethodObjectMap & getMethodObjectMap() const
virtual ~MethodObject()
Definition: ObjectWrapper.h:31
CreateInstanceFunc * _createInstanceFunc
const std::string & getName() const
Definition: ObjectWrapper.h:39
std::vector< BaseSerializer::Type > TypeList
Definition: ObjectWrapper.h:56