root/OpenSceneGraph/trunk/include/osgDB/Registry @ 9334

Revision 9334, 31.4 kB (checked in by robert, 6 years ago)

Added mechanism for registering proxy objects in the .osg plugin in a way that is compatible with static linking.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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
14#ifndef OSGDB_REGISTRY
15#define OSGDB_REGISTRY 1
16
17#include <OpenThreads/ReentrantMutex>
18
19#include <osg/ref_ptr>
20#include <osg/ArgumentParser>
21#include <osg/KdTree>
22
23#include <osgDB/DynamicLibrary>
24#include <osgDB/ReaderWriter>
25#include <osgDB/DotOsgWrapper>
26#include <osgDB/DatabasePager>
27#include <osgDB/FileCache>
28
29#include <vector>
30#include <map>
31#include <string>
32
33extern "C"
34{
35    typedef void (* CPluginFunction) (void);
36}
37
38namespace osgDB {
39
40/** basic structure for custom runtime inheritance checking */
41struct basic_type_wrapper {
42    virtual ~basic_type_wrapper() {}
43    virtual bool matches(const osg::Object *proto) const = 0;
44};
45
46/** a class template that checks inheritance between a given
47    Object's class and a class defined at compile time through
48        the template parameter T.
49        This is used in conjunction with readObjectOfType() to
50        specify an abstract class as reference type.
51**/
52template<class T>
53struct type_wrapper: basic_type_wrapper {
54    bool matches(const osg::Object *proto) const
55    {
56        return dynamic_cast<const T*>(proto) != 0;
57    }
58};
59
60
61/**
62    Registry is a singleton factory which stores
63    the reader/writers which are linked in
64    at runtime for reading non-native file formats.
65
66    The RegisterDotOsgWrapperProxy can be used to automatically register
67    DotOsgWrappers, at runtime with the Registry. A DotOsgWrapper encapsulates
68    the functions that can read and write to the .osg for each osg::Object.
69
70    The RegisterReaderWriterProxy can be used to automatically
71    register at runtime a reader/writer with the Registry.
72*/
73class OSGDB_EXPORT Registry : public osg::Referenced
74{
75    public:
76
77
78
79       
80        static Registry* instance(bool erase = false);
81
82        /** read the command line arguments.*/
83        void readCommandLine(osg::ArgumentParser& commandLine);
84
85        /** register an .fileextension alias to mapExt toExt, the later
86          * should the the extension name of the readerwriter plugin library.
87          * For example to map .tif files to the tiff loader, use
88          * addExtAlias("tif","tiff") which will enable .tif to be read
89          * by the libdb_tiff readerwriter plugin.*/
90        void addFileExtensionAlias(const std::string mapExt, const std::string toExt);
91
92        /** Reads a file that configures extension mappings. File is ASCII text
93          * and each line contains the parameters to the addFileExtensionAlias
94          * method. Lines can be commented out with an initial '#' character.*/
95        bool readPluginAliasConfigurationFile( const std::string& file );
96
97        void addDotOsgWrapper(DotOsgWrapper* wrapper);
98        void removeDotOsgWrapper(DotOsgWrapper* wrapper);
99
100        void addReaderWriter(ReaderWriter* rw);
101        void removeReaderWriter(ReaderWriter* rw);
102
103        /** create the platform specific library name associated with file.*/
104        std::string createLibraryNameForFile(const std::string& fileName);
105
106        /** create the platform specific library name associated with file extension.*/
107        std::string createLibraryNameForExtension(const std::string& ext);
108
109        /** create the platform specific library name associated with nodekit library name.*/
110        std::string createLibraryNameForNodeKit(const std::string& name);
111
112
113        enum LoadStatus {
114            NOT_LOADED = 0,
115            PREVIOUSLY_LOADED,
116            LOADED
117        };
118
119        /** find the library in the OSG_LIBRARY_PATH and load it.*/
120        LoadStatus loadLibrary(const std::string& fileName);
121       
122        /** close the attached library with specified name.*/
123        bool closeLibrary(const std::string& fileName);
124       
125        /** close all libraries.*/
126        void closeAllLibraries();
127
128        typedef std::vector< osg::ref_ptr<ReaderWriter> > ReaderWriterList;
129
130        /** get a reader writer which handles specified extension.*/
131        ReaderWriter* getReaderWriterForExtension(const std::string& ext);
132       
133        /** get list of all registered ReaderWriters.*/
134        ReaderWriterList& getReaderWriterList() { return _rwList; }
135
136        /** get const list of all registered ReaderWriters.*/
137        const ReaderWriterList& getReaderWriterList() const { return _rwList; }
138 
139        osg::Object*         readObjectOfType(const osg::Object& compObj,Input& fr);       
140        osg::Object*         readObjectOfType(const basic_type_wrapper &btw, Input& fr);
141
142        osg::Object*         readObject(Input& fr);
143        osg::Image*          readImage(Input& fr);
144        osg::Drawable*       readDrawable(Input& fr);
145        osg::Uniform*        readUniform(Input& fr);
146        osg::StateAttribute* readStateAttribute(Input& fr);
147        osg::Node*           readNode(Input& fr);
148        osg::Shader*         readShader(Input& fr);
149       
150        bool                 writeObject(const osg::Object& obj,Output& fw);
151
152        class ReadFileCallback : public virtual osg::Referenced
153        {
154        public:
155       
156            virtual ReaderWriter::ReadResult openArchive(const std::string& filename,ReaderWriter::ArchiveStatus status, unsigned int indexBlockSizeHint, const ReaderWriter::Options* useObjectCache)
157            {
158                return osgDB::Registry::instance()->openArchiveImplementation(filename, status, indexBlockSizeHint, useObjectCache);
159            }
160           
161            virtual ReaderWriter::ReadResult readObject(const std::string& filename, const ReaderWriter::Options* options)
162            {
163                return osgDB::Registry::instance()->readObjectImplementation(filename,options);
164            }
165
166            virtual ReaderWriter::ReadResult readImage(const std::string& filename, const ReaderWriter::Options* options)
167            {
168                return osgDB::Registry::instance()->readImageImplementation(filename,options);
169            }
170           
171            virtual ReaderWriter::ReadResult readHeightField(const std::string& filename, const ReaderWriter::Options* options)
172            {
173                return osgDB::Registry::instance()->readHeightFieldImplementation(filename,options);
174            }
175           
176            virtual ReaderWriter::ReadResult readNode(const std::string& filename, const ReaderWriter::Options* options)
177            {
178                return osgDB::Registry::instance()->readNodeImplementation(filename,options);
179            }
180
181            virtual ReaderWriter::ReadResult readShader(const std::string& filename, const ReaderWriter::Options* options)
182            {
183                return osgDB::Registry::instance()->readShaderImplementation(filename,options);
184            }
185           
186        protected:
187               virtual ~ReadFileCallback() {}
188        };
189
190        /** Set the Registry callback to use in place of the default readFile calls.*/
191        void setReadFileCallback( ReadFileCallback* cb) { _readFileCallback = cb; }
192
193        /** Get the readFile callback.*/
194        ReadFileCallback* getReadFileCallback() { return _readFileCallback.get(); }
195       
196        /** Get the const readFile callback.*/
197        const ReadFileCallback* getReadFileCallback() const { return _readFileCallback.get(); }
198
199
200        ReaderWriter::ReadResult openArchive(const std::string& fileName,ReaderWriter::ArchiveStatus status, unsigned int indexBlockSizeHint, const ReaderWriter::Options* options)
201        {
202            if (_readFileCallback.valid()) return _readFileCallback->openArchive(fileName, status, indexBlockSizeHint, options);
203            else return openArchiveImplementation(fileName, status, indexBlockSizeHint, options);
204        }
205        ReaderWriter::ReadResult openArchiveImplementation(const std::string& fileName, ReaderWriter::ArchiveStatus status, unsigned int indexBlockSizeHint, const ReaderWriter::Options* options);
206
207        ReaderWriter::ReadResult readObject(const std::string& fileName,const ReaderWriter::Options* options, bool buildKdTreeIfRequired=true)
208        {
209            ReaderWriter::ReadResult result = _readFileCallback.valid() ?
210                    _readFileCallback->readObject(fileName,options) :
211                    readObjectImplementation(fileName,options);
212                   
213            if (buildKdTreeIfRequired) _buildKdTreeIfRequired(result, options);
214                   
215            return result;
216        }
217        ReaderWriter::ReadResult readObjectImplementation(const std::string& fileName,const ReaderWriter::Options* options);
218       
219        ReaderWriter::ReadResult readImage(const std::string& fileName,const ReaderWriter::Options* options)
220        {
221            if (_readFileCallback.valid()) return _readFileCallback->readImage(fileName,options);
222            else return readImageImplementation(fileName,options);
223        }
224        ReaderWriter::ReadResult readImageImplementation(const std::string& fileName,const ReaderWriter::Options* options);
225
226        ReaderWriter::ReadResult readHeightField(const std::string& fileName,const ReaderWriter::Options* options)
227        {
228            if (_readFileCallback.valid()) return _readFileCallback->readHeightField(fileName,options);
229            else return readHeightFieldImplementation(fileName,options);
230        }
231        ReaderWriter::ReadResult readHeightFieldImplementation(const std::string& fileName,const ReaderWriter::Options* options);
232
233        ReaderWriter::ReadResult readNode(const std::string& fileName,const ReaderWriter::Options* options, bool buildKdTreeIfRequired=true)
234        {
235            ReaderWriter::ReadResult result = _readFileCallback.valid() ?
236                    _readFileCallback->readNode(fileName,options) :
237                    readNodeImplementation(fileName,options);
238                   
239            if (buildKdTreeIfRequired) _buildKdTreeIfRequired(result, options);
240                   
241            return result;
242        }
243        ReaderWriter::ReadResult readNodeImplementation(const std::string& fileName,const ReaderWriter::Options* options);
244
245        ReaderWriter::ReadResult readShader(const std::string& fileName,const ReaderWriter::Options* options)
246        {
247            if (_readFileCallback.valid()) return _readFileCallback->readShader(fileName,options);
248            else return readShaderImplementation(fileName,options);
249        }
250        ReaderWriter::ReadResult readShaderImplementation(const std::string& fileName,const ReaderWriter::Options* options);
251
252
253
254        class WriteFileCallback : public virtual osg::Referenced
255        {
256        public:
257       
258            virtual ReaderWriter::WriteResult writeObject(const osg::Object& obj, const std::string& fileName,const ReaderWriter::Options* options)
259            {
260                return osgDB::Registry::instance()->writeObjectImplementation(obj,fileName,options);
261            }
262
263            virtual ReaderWriter::WriteResult writeImage(const osg::Image& obj, const std::string& fileName,const ReaderWriter::Options* options)
264            {
265                return osgDB::Registry::instance()->writeImageImplementation(obj,fileName,options);
266            }
267           
268            virtual ReaderWriter::WriteResult writeHeightField(const osg::HeightField& obj, const std::string& fileName,const ReaderWriter::Options* options)
269            {
270                return osgDB::Registry::instance()->writeHeightFieldImplementation(obj,fileName,options);
271            }
272           
273            virtual ReaderWriter::WriteResult writeNode(const osg::Node& obj, const std::string& fileName,const ReaderWriter::Options* options)
274            {
275                return osgDB::Registry::instance()->writeNodeImplementation(obj,fileName,options);
276            }
277
278            virtual ReaderWriter::WriteResult writeShader(const osg::Shader& obj, const std::string& fileName,const ReaderWriter::Options* options)
279            {
280                return osgDB::Registry::instance()->writeShaderImplementation(obj,fileName,options);
281            }
282           
283        protected:
284               virtual ~WriteFileCallback() {}
285        };
286
287        /** Set the Registry callback to use in place of the default writeFile calls.*/
288        void setWriteFileCallback( WriteFileCallback* cb) { _writeFileCallback = cb; }
289
290        /** Get the writeFile callback.*/
291        WriteFileCallback* getWriteFileCallback() { return _writeFileCallback.get(); }
292       
293        /** Get the const writeFile callback.*/
294        const WriteFileCallback* getWriteFileCallback() const { return _writeFileCallback.get(); }
295
296
297        ReaderWriter::WriteResult writeObject(const osg::Object& obj, const std::string& fileName,const ReaderWriter::Options* options)
298        {
299            if (_writeFileCallback.valid()) return _writeFileCallback->writeObject(obj,fileName,options);
300            else return writeObjectImplementation(obj,fileName,options);
301        }
302        ReaderWriter::WriteResult writeObjectImplementation(const osg::Object& obj, const std::string& fileName,const ReaderWriter::Options* options);
303
304        ReaderWriter::WriteResult writeImage(const osg::Image& obj, const std::string& fileName,const ReaderWriter::Options* options)
305        {
306            if (_writeFileCallback.valid()) return _writeFileCallback->writeImage(obj,fileName,options);
307            else return writeImageImplementation(obj,fileName,options);
308        }
309        ReaderWriter::WriteResult writeImageImplementation(const osg::Image& obj, const std::string& fileName,const ReaderWriter::Options* options);
310
311        ReaderWriter::WriteResult writeHeightField(const osg::HeightField& obj, const std::string& fileName,const ReaderWriter::Options* options)
312        {
313            if (_writeFileCallback.valid()) return _writeFileCallback->writeHeightField(obj,fileName,options);
314            else return writeHeightFieldImplementation(obj,fileName,options);
315        }
316        ReaderWriter::WriteResult writeHeightFieldImplementation(const osg::HeightField& obj, const std::string& fileName,const ReaderWriter::Options* options);
317
318        ReaderWriter::WriteResult writeNode(const osg::Node& node, const std::string& fileName,const ReaderWriter::Options* options)
319        {
320            if (_writeFileCallback.valid()) return _writeFileCallback->writeNode(node,fileName,options);
321            else return writeNodeImplementation(node,fileName,options);
322        }
323        ReaderWriter::WriteResult writeNodeImplementation(const osg::Node& node, const std::string& fileName,const ReaderWriter::Options* options);
324       
325        ReaderWriter::WriteResult writeShader(const osg::Shader& obj, const std::string& fileName,const ReaderWriter::Options* options)
326        {
327            if (_writeFileCallback.valid()) return _writeFileCallback->writeShader(obj,fileName,options);
328            else return writeShaderImplementation(obj,fileName,options);
329        }
330        ReaderWriter::WriteResult writeShaderImplementation(const osg::Shader& obj, const std::string& fileName,const ReaderWriter::Options* options);
331       
332
333        inline void _buildKdTreeIfRequired(ReaderWriter::ReadResult& result, const ReaderWriter::Options* options)
334        {
335            bool doKdTreeBuilder = (options && options->getBuildKdTreesHint()!=ReaderWriter::Options::NO_PREFERENCE) ?
336                options->getBuildKdTreesHint() == ReaderWriter::Options::BUILD_KDTREES :
337                _buildKdTreesHint == ReaderWriter::Options::BUILD_KDTREES;
338
339            if (doKdTreeBuilder && _kdTreeBuilder.valid() && result.validNode())
340            {
341                osg::ref_ptr<osg::KdTreeBuilder> builder = _kdTreeBuilder->clone();
342                result.getNode()->accept(*builder);
343            }
344        }
345
346
347        /** Set whether the KdTrees should be built for geometry in the loader model. */
348        void setBuildKdTreesHint(ReaderWriter::Options::BuildKdTreesHint hint) { _buildKdTreesHint = hint; }
349
350        /** Get whether the KdTrees should be built for geometry in the loader model. */
351        ReaderWriter::Options::BuildKdTreesHint getBuildKdTreesHint() const { return _buildKdTreesHint; }
352
353        /** Set the KdTreeBuilder visitor that is used to build KdTree on loaded models.*/
354        void setKdTreeBuilder(osg::KdTreeBuilder* builder) { _kdTreeBuilder = builder; }
355
356        /** Get the KdTreeBuilder visitor that is used to build KdTree on loaded models.*/
357        osg::KdTreeBuilder* getKdTreeBuilder() { return _kdTreeBuilder.get(); }
358
359        /** Set the FileCache that is used to manage local storage of files downloaded from the internet.*/
360        void setFileCache(FileCache* fileCache) { _fileCache = fileCache; }
361
362        /** Get the FileCache that is used to manage local storage of files downloaded from the internet.*/
363        FileCache* getFileCache() { return _fileCache.get(); }
364
365        /** Get the const FileCache that is used to manage local storage of files downloaded from the internet.*/
366        const FileCache* getFileCache() const { return _fileCache.get(); }
367
368
369        /** Set the password map to be used by plugins when access files from secure locations.*/
370        void setAuthenticationMap(AuthenticationMap* authenticationMap) { _authenticationMap = authenticationMap; }
371
372        /** Get the password map to be used by plugins when access files from secure locations.*/
373        AuthenticationMap* getAuthenticationMap() { return _authenticationMap.get(); }
374
375        /** Get the password map to be used by plugins when access files from secure locations.*/
376        const AuthenticationMap* getAuthenticationMap() const { return _authenticationMap.get(); }
377
378
379        void setCreateNodeFromImage(bool flag) { _createNodeFromImage = flag; }
380        bool getCreateNodeFromImage() const { return _createNodeFromImage; }
381       
382
383        void setOptions(ReaderWriter::Options* opt) { _options = opt; }
384        ReaderWriter::Options* getOptions() { return _options.get(); }
385        const ReaderWriter::Options*  getOptions() const { return _options.get(); }
386
387
388        /** initialize both the Data and Library FilePaths, by default called by the
389          * constructor, so it should only be required if you want to force
390          * the re-reading of environmental variables.*/
391        void initFilePathLists() { initDataFilePathList(); initLibraryFilePathList(); }
392       
393        /** initialize the Data FilePath by reading the OSG_FILE_PATH environmental variable.*/
394        void initDataFilePathList();
395
396        /** Set the data file path using a list of paths stored in a FilePath, which is used when search for data files.*/
397        void setDataFilePathList(const FilePathList& filepath) { _dataFilePath = filepath; }
398
399        /** Set the data file path using a single string delimited either with ';' (Windows) or ':' (All other platforms), which is used when search for data files.*/
400        void setDataFilePathList(const std::string& paths);
401
402        /** get the data file path which is used when search for data files.*/
403        FilePathList& getDataFilePathList() { return _dataFilePath; }
404
405        /** get the const data file path which is used when search for data files.*/
406        const FilePathList& getDataFilePathList() const { return _dataFilePath; }
407
408        /** initialize the Library FilePath by reading the OSG_LIBRARY_PATH
409          * and the appropriate system environmental variables*/
410        void initLibraryFilePathList();
411
412        /** Set the library file path using a list of paths stored in a FilePath, which is used when search for data files.*/
413        void setLibraryFilePathList(const FilePathList& filepath) { _libraryFilePath = filepath; }
414
415        /** Set the library file path using a single string delimited either with ';' (Windows) or ':' (All other platforms), which is used when search for data files.*/
416        void setLibraryFilePathList(const std::string& paths);
417
418        /** get the library file path which is used when search for library (dso/dll's) files.*/
419        FilePathList& getLibraryFilePathList() { return _libraryFilePath; }
420       
421        /** get the const library file path which is used when search for library (dso/dll's) files.*/
422        const FilePathList& getLibraryFilePathList() const { return _libraryFilePath; }
423
424        /** For each object in the cache which has an reference count greater than 1
425          * (and therefore referenced by elsewhere in the application) set the time stamp
426          * for that object in the cache to specified time.
427          * This would typically be called once per frame by applications which are doing database paging,
428          * and need to prune objects that are no longer required.
429          * Time value is time in seconds.*/
430        void updateTimeStampOfObjectsInCacheWithExternalReferences(double currentTime);
431
432        /** Removed object in the cache which have a time stamp at or before the specified expiry time.
433          * This would typically be called once per frame by applications which are doing database paging,
434          * and need to prune objects that are no longer required, and called after the a called
435          * after the call to updateTimeStampOfObjectsInCacheWithExternalReferences(currentTime).
436          * Note, the currentTime is not the expiryTime, one would typically set the expiry time
437          * to a fixed amount of time before currentTime, such as expiryTime = currentTime-10.0.
438          * Time value is time in seconds.*/
439        void removeExpiredObjectsInCache(double expiryTime);
440       
441        /** Remove all objects in the cache regardless of having external references or expiry times.*/
442        void clearObjectCache();
443
444        /** Add a filename,object,timestamp triple to the Registry::ObjectCache.*/
445        void addEntryToObjectCache(const std::string& filename, osg::Object* object, double timestamp = 0.0);
446
447        /** Get an object from the object cache*/
448        osg::Object* getFromObjectCache(const std::string& fileName);
449       
450        /** Add archive to archive cache so that future calls reference this archive.*/
451        void addToArchiveCache(const std::string& fileName, osgDB::Archive* archive);
452
453        /** Remove archive from cache.*/
454        void removeFromArchiveCache(const std::string& fileName);
455       
456        /** Get an archive from the archive cache*/
457        osgDB::Archive* getFromArchiveCache(const std::string& fileName);
458       
459        /** Remove all archives from the archive cache.*/
460        void clearArchiveCache();
461       
462         /** If State is non-zero, this function releases OpenGL objects for
463           * the specified graphics context. Otherwise, releases OpenGL objexts
464           * for all graphics contexts. */
465        void releaseGLObjects(osg::State* state=0);
466
467        /** get the attached library with specified name.*/
468        DynamicLibrary*              getLibrary(const std::string& fileName);
469
470        /** Set the SharedStateManager.*/
471        void setSharedStateManager(SharedStateManager* SharedStateManager) { _sharedStateManager = SharedStateManager; }
472
473        /** Get the SharedStateManager, creating one if one is not already created.*/
474        SharedStateManager* getOrCreateSharedStateManager();
475       
476        /** Get the SharedStateManager. Return 0 if no SharedStateManager has been assigned.*/
477        SharedStateManager* getSharedStateManager() { return _sharedStateManager.get(); }
478
479        /** Add an Archive extension.*/
480        void addArchiveExtension(const std::string ext);
481
482    protected:
483
484        virtual ~Registry();
485
486        typedef std::map< std::string, osg::ref_ptr<DotOsgWrapper> >    DotOsgWrapperMap;
487        typedef std::vector< osg::ref_ptr<DynamicLibrary> >             DynamicLibraryList;
488        typedef std::map< std::string, std::string>                     ExtensionAliasMap;
489        typedef std::vector< std::string>                               ArchiveExtensionList;
490       
491        typedef std::pair<osg::ref_ptr<osg::Object>, double >           ObjectTimeStampPair;
492        typedef std::map<std::string, ObjectTimeStampPair >             ObjectCache;
493        typedef std::map<std::string, osg::ref_ptr<osgDB::Archive> >    ArchiveCache;
494
495        /** constructor is private, as its a singleton, preventing
496            construction other than via the instance() method and
497            therefore ensuring only one copy is ever constructed*/
498        Registry();
499       
500        /** get the attached library with specified name.*/
501        DynamicLibraryList::iterator getLibraryItr(const std::string& fileName);
502
503        ReaderWriter::Options::BuildKdTreesHint     _buildKdTreesHint;
504        osg::ref_ptr<osg::KdTreeBuilder>            _kdTreeBuilder;
505       
506        osg::ref_ptr<FileCache>                     _fileCache;
507       
508        osg::ref_ptr<AuthenticationMap>             _authenticationMap;
509       
510        bool                                        _createNodeFromImage;
511
512        osg::Object*       readObject(DotOsgWrapperMap& dowMap,Input& fr);
513
514        void eraseWrapper(DotOsgWrapperMap& wrappermap,DotOsgWrapper* wrapper);
515
516    public:
517        /** Functor used in internal implementations.*/
518        struct ReadFunctor
519        {
520            ReadFunctor(const std::string& filename, const ReaderWriter::Options* options):
521                _filename(filename),
522                _options(options) {}
523
524            virtual ~ReadFunctor() {}
525            virtual ReaderWriter::ReadResult doRead(ReaderWriter& rw) const = 0;
526            virtual bool isValid(ReaderWriter::ReadResult& readResult) const = 0;
527            virtual bool isValid(osg::Object* object) const = 0;
528
529            std::string _filename;
530            const ReaderWriter::Options* _options;
531        };
532
533   protected:
534   
535        void destruct();
536   
537        // forward declare helper classes
538        struct ReadObjectFunctor;
539        struct ReadImageFunctor;
540        struct ReadHeightFieldFunctor;
541        struct ReadNodeFunctor;
542        struct ReadArchiveFunctor;
543        struct ReadShaderFunctor;
544       
545        // make helper classes friends to get round VS6.0 "issues"
546        friend struct ReadFunctor;
547        friend struct ReadObjectFunctor;
548        friend struct ReadImageFunctor;
549        friend struct ReadHeightFieldFunctor;
550        friend struct ReadNodeFunctor;
551        friend struct ReadArchiveFunctor;
552        friend struct ReadShaderFunctor;
553
554        ReaderWriter::ReadResult read(const ReadFunctor& readFunctor);
555        ReaderWriter::ReadResult readImplementation(const ReadFunctor& readFunctor,bool useObjectCache);
556
557
558        // forward declare helper class
559        class AvailableReaderWriterIterator;
560        friend class AvailableReaderWriterIterator;
561
562
563        osg::ref_ptr<ReadFileCallback>      _readFileCallback;
564        osg::ref_ptr<WriteFileCallback>     _writeFileCallback;
565
566        DotOsgWrapperMap   _objectWrapperMap;
567        DotOsgWrapperMap   _imageWrapperMap;
568        DotOsgWrapperMap   _drawableWrapperMap;
569        DotOsgWrapperMap   _stateAttrWrapperMap;
570        DotOsgWrapperMap   _uniformWrapperMap;
571        DotOsgWrapperMap   _nodeWrapperMap;
572        DotOsgWrapperMap   _shaderWrapperMap;
573       
574        DotOsgWrapperMap   _classNameWrapperMap;
575
576        OpenThreads::ReentrantMutex _pluginMutex;
577        ReaderWriterList            _rwList;
578        DynamicLibraryList          _dlList;
579
580        bool _openingLibrary;
581   
582        // map to alias to extensions to plugins.
583        ExtensionAliasMap  _extAliasMap;
584
585        // Utility: Removes whitespace from both ends of a string.
586        static std::string trim( const std::string& str );
587       
588        // options to pass to reader writers.
589        osg::ref_ptr<ReaderWriter::Options>     _options;
590       
591        FilePathList                            _dataFilePath;
592        FilePathList                            _libraryFilePath;
593
594        ObjectCache                             _objectCache;
595        OpenThreads::Mutex                      _objectCacheMutex;
596       
597        ArchiveCache                            _archiveCache;
598        OpenThreads::Mutex                      _archiveCacheMutex;
599
600        ArchiveExtensionList                    _archiveExtList;
601       
602        osg::ref_ptr<SharedStateManager>        _sharedStateManager;
603
604};
605
606/** read the command line arguments.*/
607inline void readCommandLine(osg::ArgumentParser& parser)
608{
609    Registry::instance()->readCommandLine(parser);
610}
611
612/**  Proxy class for automatic registration of DotOsgWrappers with the Registry.*/
613class RegisterDotOsgWrapperProxy
614{
615    public:
616   
617        RegisterDotOsgWrapperProxy(osg::Object* proto,
618                                   const std::string& name,
619                                   const std::string& associates,
620                                   DotOsgWrapper::ReadFunc readFunc,
621                                   DotOsgWrapper::WriteFunc writeFunc,
622                                   DotOsgWrapper::ReadWriteMode readWriteMode=DotOsgWrapper::READ_AND_WRITE)
623        {
624            if (Registry::instance())
625            {
626                _wrapper = new DotOsgWrapper(proto,name,associates,readFunc,writeFunc,readWriteMode);
627                Registry::instance()->addDotOsgWrapper(_wrapper.get());
628            }
629        }
630       
631        ~RegisterDotOsgWrapperProxy()
632        {
633            if (Registry::instance())
634            {
635                Registry::instance()->removeDotOsgWrapper(_wrapper.get());
636            }
637        }
638       
639    protected:
640        osg::ref_ptr<DotOsgWrapper> _wrapper;
641};
642
643template<class T>
644class TemplateRegisterDotOsgWrapperProxy : public RegisterDotOsgWrapperProxy, public T
645{
646    public:
647   
648        TemplateRegisterDotOsgWrapperProxy(osg::Object* proto,
649                                   const std::string& name,
650                                   const std::string& associates,
651                                   DotOsgWrapper::ReadFunc readFunc,
652                                   DotOsgWrapper::WriteFunc writeFunc,
653                                   DotOsgWrapper::ReadWriteMode readWriteMode=DotOsgWrapper::READ_AND_WRITE):
654            RegisterDotOsgWrapperProxy(proto, name, associates, readFunc, writeFunc, readWriteMode) {}
655       
656};
657
658/** Proxy class for automatic registration of reader/writers with the Registry.*/
659template<class T>
660class RegisterReaderWriterProxy
661{
662    public:
663        RegisterReaderWriterProxy()
664        {
665            if (Registry::instance())
666            {
667                _rw = new T;
668                Registry::instance()->addReaderWriter(_rw.get());
669            }
670        }
671
672        ~RegisterReaderWriterProxy()
673        {
674            if (Registry::instance())
675            {
676                Registry::instance()->removeReaderWriter(_rw.get());
677            }
678        }
679       
680        T* get() { return _rw.get(); }
681       
682    protected:
683        osg::ref_ptr<T> _rw;
684};
685
686
687struct PluginFunctionProxy
688{
689    PluginFunctionProxy(CPluginFunction function) { (function)(); }
690};
691
692#define USE_OSGPLUGIN(ext) \
693    extern "C" void osgdb_##ext(void); \
694    static osgDB::PluginFunctionProxy proxy_##ext(osgdb_##ext);
695
696#define REGISTER_OSGPLUGIN(ext, classname) \
697    extern "C" void osgdb_##ext(void) {} \
698    static osgDB::RegisterReaderWriterProxy<classname> g_proxy_##classname;
699
700
701#define USE_DOTOSGWRAPPER(classname) \
702    extern "C" void dotosgwrapper_##classname(void); \
703    static osgDB::PluginFunctionProxy proxy_dotosgwrapper_##classname(dotosgwrapper_##classname);
704
705#define REGISTER_DOTOSGWRAPPER(classname) \
706    extern "C" void dotosgwrapper_##classname(void) {} \
707    static osgDB::RegisterDotOsgWrapperProxy dotosgwrapper_proxy_##classname
708
709}
710
711#endif
Note: See TracBrowser for help on using the browser.