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

Revision 10174, 32.7 kB (checked in by robert, 5 years ago)

Moved Registry::ReadFileCallback? + WriteFileCallback?, and osgDB::ReaderWriter::Options into their own separate Options file and into the osgDB namespace.

Introduced a new callback osgDB::FindFileCallback? that overrides the default behavior of findDataFile/findLibraryFile.

Introduced support for assigning ReaderWriter::Options directory to PagedLOD.

Introduced new osgDB::FileLocationCallback? for assistancing the DatabasePager? to know when a file is hosted on a local or remote file system.

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