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

Revision 9884, 31.8 kB (checked in by robert, 6 years ago)

From Stephan Huber,
"Attached you'll find a proposal for using different
protocols. The idea behind the new code is:

1.) plugins/apps register protocols which they can handle. This is done
via osgDB::Registry::registerProtocol(aProtocolName). Plugins register
supported protocols as usual via ReaderWriter::supportsProtocol(..), the
Registry is updated accordingly.

2.) osgDB::containsServerAddress checks first for an appearance of "://"
in the filename and then checks the protocol against the set of
registered protocols via Registry::isProtocolRegistered(aProtocollName)

3.) the other getServer*-functions changed as well, there's even a
getServerProtocol-function

With these changes filenames/Urls get routed to loaded plugins even with
different protocols than 'http'."

  • 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        /** registers a protocol */
483        void registerProtocol(const std::string& protocol);
484       
485        /** returns true, if named protocol is registered */
486        bool isProtocolRegistered(const std::string& protocol);
487       
488    protected:
489
490        virtual ~Registry();
491
492        typedef std::map< std::string, osg::ref_ptr<DotOsgWrapper> >    DotOsgWrapperMap;
493        typedef std::vector< osg::ref_ptr<DynamicLibrary> >             DynamicLibraryList;
494        typedef std::map< std::string, std::string>                     ExtensionAliasMap;
495        typedef std::vector< std::string>                               ArchiveExtensionList;
496       
497        typedef std::pair<osg::ref_ptr<osg::Object>, double >           ObjectTimeStampPair;
498        typedef std::map<std::string, ObjectTimeStampPair >             ObjectCache;
499        typedef std::map<std::string, osg::ref_ptr<osgDB::Archive> >    ArchiveCache;
500       
501        typedef std::set<std::string>                                   RegisteredProtocolsSet;
502
503        /** constructor is private, as its a singleton, preventing
504            construction other than via the instance() method and
505            therefore ensuring only one copy is ever constructed*/
506        Registry();
507       
508        /** get the attached library with specified name.*/
509        DynamicLibraryList::iterator getLibraryItr(const std::string& fileName);
510
511        ReaderWriter::Options::BuildKdTreesHint     _buildKdTreesHint;
512        osg::ref_ptr<osg::KdTreeBuilder>            _kdTreeBuilder;
513       
514        osg::ref_ptr<FileCache>                     _fileCache;
515       
516        osg::ref_ptr<AuthenticationMap>             _authenticationMap;
517       
518        bool                                        _createNodeFromImage;
519       
520        RegisteredProtocolsSet                      _registeredProtocols;
521
522        osg::Object*       readObject(DotOsgWrapperMap& dowMap,Input& fr);
523
524        void eraseWrapper(DotOsgWrapperMap& wrappermap,DotOsgWrapper* wrapper);
525
526    public:
527        /** Functor used in internal implementations.*/
528        struct ReadFunctor
529        {
530            ReadFunctor(const std::string& filename, const ReaderWriter::Options* options):
531                _filename(filename),
532                _options(options) {}
533
534            virtual ~ReadFunctor() {}
535            virtual ReaderWriter::ReadResult doRead(ReaderWriter& rw) const = 0;
536            virtual bool isValid(ReaderWriter::ReadResult& readResult) const = 0;
537            virtual bool isValid(osg::Object* object) const = 0;
538
539            std::string _filename;
540            const ReaderWriter::Options* _options;
541        };
542
543   protected:
544   
545        void destruct();
546   
547        // forward declare helper classes
548        struct ReadObjectFunctor;
549        struct ReadImageFunctor;
550        struct ReadHeightFieldFunctor;
551        struct ReadNodeFunctor;
552        struct ReadArchiveFunctor;
553        struct ReadShaderFunctor;
554       
555        // make helper classes friends to get round VS6.0 "issues"
556        friend struct ReadFunctor;
557        friend struct ReadObjectFunctor;
558        friend struct ReadImageFunctor;
559        friend struct ReadHeightFieldFunctor;
560        friend struct ReadNodeFunctor;
561        friend struct ReadArchiveFunctor;
562        friend struct ReadShaderFunctor;
563
564        ReaderWriter::ReadResult read(const ReadFunctor& readFunctor);
565        ReaderWriter::ReadResult readImplementation(const ReadFunctor& readFunctor,bool useObjectCache);
566
567
568        // forward declare helper class
569        class AvailableReaderWriterIterator;
570        friend class AvailableReaderWriterIterator;
571
572
573        osg::ref_ptr<ReadFileCallback>      _readFileCallback;
574        osg::ref_ptr<WriteFileCallback>     _writeFileCallback;
575
576        DotOsgWrapperMap   _objectWrapperMap;
577        DotOsgWrapperMap   _imageWrapperMap;
578        DotOsgWrapperMap   _drawableWrapperMap;
579        DotOsgWrapperMap   _stateAttrWrapperMap;
580        DotOsgWrapperMap   _uniformWrapperMap;
581        DotOsgWrapperMap   _nodeWrapperMap;
582        DotOsgWrapperMap   _shaderWrapperMap;
583       
584        DotOsgWrapperMap   _classNameWrapperMap;
585
586        OpenThreads::ReentrantMutex _pluginMutex;
587        ReaderWriterList            _rwList;
588        DynamicLibraryList          _dlList;
589
590        bool _openingLibrary;
591   
592        // map to alias to extensions to plugins.
593        ExtensionAliasMap  _extAliasMap;
594
595        // Utility: Removes whitespace from both ends of a string.
596        static std::string trim( const std::string& str );
597       
598        // options to pass to reader writers.
599        osg::ref_ptr<ReaderWriter::Options>     _options;
600       
601        FilePathList                            _dataFilePath;
602        FilePathList                            _libraryFilePath;
603
604        ObjectCache                             _objectCache;
605        OpenThreads::Mutex                      _objectCacheMutex;
606       
607        ArchiveCache                            _archiveCache;
608        OpenThreads::Mutex                      _archiveCacheMutex;
609
610        ArchiveExtensionList                    _archiveExtList;
611       
612        osg::ref_ptr<SharedStateManager>        _sharedStateManager;
613
614};
615
616/** read the command line arguments.*/
617inline void readCommandLine(osg::ArgumentParser& parser)
618{
619    Registry::instance()->readCommandLine(parser);
620}
621
622/**  Proxy class for automatic registration of DotOsgWrappers with the Registry.*/
623class RegisterDotOsgWrapperProxy
624{
625    public:
626   
627        RegisterDotOsgWrapperProxy(osg::Object* proto,
628                                   const std::string& name,
629                                   const std::string& associates,
630                                   DotOsgWrapper::ReadFunc readFunc,
631                                   DotOsgWrapper::WriteFunc writeFunc,
632                                   DotOsgWrapper::ReadWriteMode readWriteMode=DotOsgWrapper::READ_AND_WRITE)
633        {
634            if (Registry::instance())
635            {
636                _wrapper = new DotOsgWrapper(proto,name,associates,readFunc,writeFunc,readWriteMode);
637                Registry::instance()->addDotOsgWrapper(_wrapper.get());
638            }
639        }
640       
641        ~RegisterDotOsgWrapperProxy()
642        {
643            if (Registry::instance())
644            {
645                Registry::instance()->removeDotOsgWrapper(_wrapper.get());
646            }
647        }
648       
649    protected:
650        osg::ref_ptr<DotOsgWrapper> _wrapper;
651};
652
653template<class T>
654class TemplateRegisterDotOsgWrapperProxy : public RegisterDotOsgWrapperProxy, public T
655{
656    public:
657   
658        TemplateRegisterDotOsgWrapperProxy(osg::Object* proto,
659                                   const std::string& name,
660                                   const std::string& associates,
661                                   DotOsgWrapper::ReadFunc readFunc,
662                                   DotOsgWrapper::WriteFunc writeFunc,
663                                   DotOsgWrapper::ReadWriteMode readWriteMode=DotOsgWrapper::READ_AND_WRITE):
664            RegisterDotOsgWrapperProxy(proto, name, associates, readFunc, writeFunc, readWriteMode) {}
665       
666};
667
668/** Proxy class for automatic registration of reader/writers with the Registry.*/
669template<class T>
670class RegisterReaderWriterProxy
671{
672    public:
673        RegisterReaderWriterProxy()
674        {
675            if (Registry::instance())
676            {
677                _rw = new T;
678                Registry::instance()->addReaderWriter(_rw.get());
679            }
680        }
681
682        ~RegisterReaderWriterProxy()
683        {
684            if (Registry::instance())
685            {
686                Registry::instance()->removeReaderWriter(_rw.get());
687            }
688        }
689       
690        T* get() { return _rw.get(); }
691       
692    protected:
693        osg::ref_ptr<T> _rw;
694};
695
696
697struct PluginFunctionProxy
698{
699    PluginFunctionProxy(CPluginFunction function) { (function)(); }
700};
701
702#define USE_OSGPLUGIN(ext) \
703    extern "C" void osgdb_##ext(void); \
704    static osgDB::PluginFunctionProxy proxy_##ext(osgdb_##ext);
705
706#define REGISTER_OSGPLUGIN(ext, classname) \
707    extern "C" void osgdb_##ext(void) {} \
708    static osgDB::RegisterReaderWriterProxy<classname> g_proxy_##classname;
709
710
711#define USE_DOTOSGWRAPPER(classname) \
712    extern "C" void dotosgwrapper_##classname(void); \
713    static osgDB::PluginFunctionProxy proxy_dotosgwrapper_##classname(dotosgwrapper_##classname);
714
715#define REGISTER_DOTOSGWRAPPER(classname) \
716    extern "C" void dotosgwrapper_##classname(void) {} \
717    static osgDB::RegisterDotOsgWrapperProxy dotosgwrapper_proxy_##classname
718
719}
720
721#endif
Note: See TracBrowser for help on using the browser.