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

Revision 10520, 32.8 kB (checked in by robert, 5 years ago)

Moved the updating and expiry of the Registry object cache from DatabasePager? into osgViewer::Viewer/CompositeViewer.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[5328]1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
[1529]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*/
[51]13
[8]14#ifndef OSGDB_REGISTRY
15#define OSGDB_REGISTRY 1
16
[8929]17#include <OpenThreads/ReentrantMutex>
18
[8]19#include <osg/ref_ptr>
[1610]20#include <osg/ArgumentParser>
[8535]21#include <osg/KdTree>
[8]22
23#include <osgDB/DynamicLibrary>
24#include <osgDB/ReaderWriter>
[10171]25#include <osgDB/Options>
[8]26#include <osgDB/DotOsgWrapper>
[2614]27#include <osgDB/DatabasePager>
[9038]28#include <osgDB/FileCache>
[8]29
[773]30#include <vector>
31#include <map>
32#include <string>
33
[6769]34extern "C"
35{
36    typedef void (* CPluginFunction) (void);
37}
[773]38
[8]39namespace osgDB {
40
[2057]41/** basic structure for custom runtime inheritance checking */
42struct basic_type_wrapper {
[4461]43    virtual ~basic_type_wrapper() {}
[2877]44    virtual bool matches(const osg::Object *proto) const = 0;
[2057]45};
[225]46
[2057]47/** a class template that checks inheritance between a given
48    Object's class and a class defined at compile time through
[2877]49        the template parameter T.
50        This is used in conjunction with readObjectOfType() to
51        specify an abstract class as reference type.
[2057]52**/
53template<class T>
54struct type_wrapper: basic_type_wrapper {
[2877]55    bool matches(const osg::Object *proto) const
56    {
57        return dynamic_cast<const T*>(proto) != 0;
58    }
[2057]59};
60
[773]61
[8]62/**
63    Registry is a singleton factory which stores
[31]64    the reader/writers which are linked in
[8]65    at runtime for reading non-native file formats.
66
[31]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.
[8]70
71    The RegisterReaderWriterProxy can be used to automatically
72    register at runtime a reader/writer with the Registry.
73*/
[1463]74class OSGDB_EXPORT Registry : public osg::Referenced
[8]75{
76    public:
77
[3580]78
[2877]79
80       
[2534]81        static Registry* instance(bool erase = false);
[434]82
[1610]83        /** read the command line arguments.*/
84        void readCommandLine(osg::ArgumentParser& commandLine);
85
[8]86        /** register an .fileextension alias to mapExt toExt, the later
[4461]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);
[8]92
[6089]93        /** Reads a file that configures extension mappings. File is ASCII text
[7648]94          * and each line contains the parameters to the addFileExtensionAlias
[6089]95          * method. Lines can be commented out with an initial '#' character.*/
96        bool readPluginAliasConfigurationFile( const std::string& file );
97
[10000]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
[8]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.*/
[2016]114        std::string createLibraryNameForExtension(const std::string& ext);
[8]115
[742]116        /** create the platform specific library name associated with nodekit library name.*/
117        std::string createLibraryNameForNodeKit(const std::string& name);
118
[8947]119
120        enum LoadStatus {
121            NOT_LOADED = 0,
122            PREVIOUSLY_LOADED,
123            LOADED
124        };
125
[5697]126        /** find the library in the OSG_LIBRARY_PATH and load it.*/
[8947]127        LoadStatus loadLibrary(const std::string& fileName);
[5697]128       
[8]129        /** close the attached library with specified name.*/
130        bool closeLibrary(const std::string& fileName);
[5697]131       
[2534]132        /** close all libraries.*/
133        void closeAllLibraries();
[8]134
[8582]135        typedef std::vector< osg::ref_ptr<ReaderWriter> > ReaderWriterList;
136
[2016]137        /** get a reader writer which handles specified extension.*/
138        ReaderWriter* getReaderWriterForExtension(const std::string& ext);
[10000]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);
[8582]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; }
[2016]149 
[2057]150        osg::Object*         readObjectOfType(const osg::Object& compObj,Input& fr);       
151        osg::Object*         readObjectOfType(const basic_type_wrapper &btw, Input& fr);
[8]152
153        osg::Object*         readObject(Input& fr);
154        osg::Image*          readImage(Input& fr);
155        osg::Drawable*       readDrawable(Input& fr);
[4061]156        osg::Uniform*        readUniform(Input& fr);
[8]157        osg::StateAttribute* readStateAttribute(Input& fr);
158        osg::Node*           readNode(Input& fr);
[7908]159        osg::Shader*         readShader(Input& fr);
[8]160       
161        bool                 writeObject(const osg::Object& obj,Output& fw);
162
[10171]163
164        typedef class osgDB::FindFileCallback FindFileCallback;
165        typedef class osgDB::ReadFileCallback ReadFileCallback;
166        typedef class osgDB::WriteFileCallback WriteFileCallback;
[10174]167        typedef class osgDB::FileLocationCallback FileLocationCallback;
[10171]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)
[2877]180        {
[10171]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);
[8]186
[10171]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);
[7908]194
[8]195
[10171]196
[2877]197        /** Set the Registry callback to use in place of the default readFile calls.*/
198        void setReadFileCallback( ReadFileCallback* cb) { _readFileCallback = cb; }
[2398]199
[2877]200        /** Get the readFile callback.*/
201        ReadFileCallback* getReadFileCallback() { return _readFileCallback.get(); }
[8]202       
[2877]203        /** Get the const readFile callback.*/
204        const ReadFileCallback* getReadFileCallback() const { return _readFileCallback.get(); }
205
206
[10171]207        ReaderWriter::ReadResult openArchive(const std::string& fileName,ReaderWriter::ArchiveStatus status, unsigned int indexBlockSizeHint, const Options* options)
[3580]208        {
[10171]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);
[3689]211            else return openArchiveImplementation(fileName, status, indexBlockSizeHint, options);
[3580]212        }
[10171]213        ReaderWriter::ReadResult openArchiveImplementation(const std::string& fileName, ReaderWriter::ArchiveStatus status, unsigned int indexBlockSizeHint, const Options* options);
[3580]214
[10171]215        ReaderWriter::ReadResult readObject(const std::string& fileName,const Options* options, bool buildKdTreeIfRequired=true)
[2877]216        {
[10171]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
[9038]222            if (buildKdTreeIfRequired) _buildKdTreeIfRequired(result, options);
[10171]223
[8535]224            return result;
[2877]225        }
[10171]226        ReaderWriter::ReadResult readObjectImplementation(const std::string& fileName,const Options* options);
227
228        ReaderWriter::ReadResult readImage(const std::string& fileName,const Options* options)
[2877]229        {
[10171]230            if (options && options->getReadFileCallback()) return options->getReadFileCallback()->readImage(fileName,options);
231            else if (_readFileCallback.valid()) return _readFileCallback->readImage(fileName,options);
[3689]232            else return readImageImplementation(fileName,options);
[2877]233        }
[10171]234        ReaderWriter::ReadResult readImageImplementation(const std::string& fileName,const Options* options);
[2877]235
[10171]236        ReaderWriter::ReadResult readHeightField(const std::string& fileName,const Options* options)
[2877]237        {
[10171]238            if (options && options->getReadFileCallback()) return options->getReadFileCallback()->readHeightField(fileName,options);
239            else if (_readFileCallback.valid()) return _readFileCallback->readHeightField(fileName,options);
[3689]240            else return readHeightFieldImplementation(fileName,options);
[2877]241        }
[10171]242        ReaderWriter::ReadResult readHeightFieldImplementation(const std::string& fileName,const Options* options);
[2877]243
[10171]244        ReaderWriter::ReadResult readNode(const std::string& fileName,const Options* options, bool buildKdTreeIfRequired=true)
[2877]245        {
[10171]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
[9038]251            if (buildKdTreeIfRequired) _buildKdTreeIfRequired(result, options);
[10171]252
[8535]253            return result;
[2877]254        }
[10171]255        ReaderWriter::ReadResult readNodeImplementation(const std::string& fileName,const Options* options);
[2877]256
[10171]257        ReaderWriter::ReadResult readShader(const std::string& fileName,const Options* options)
[7908]258        {
[10171]259            if (options && options->getReadFileCallback()) return options->getReadFileCallback()->readShader(fileName,options);
[7908]260            if (_readFileCallback.valid()) return _readFileCallback->readShader(fileName,options);
261            else return readShaderImplementation(fileName,options);
262        }
[10171]263        ReaderWriter::ReadResult readShaderImplementation(const std::string& fileName,const Options* options);
[2877]264
[3580]265
[2877]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
[10171]276        ReaderWriter::WriteResult writeObject(const osg::Object& obj, const std::string& fileName,const Options* options)
[2877]277        {
[10171]278            if (options && options->getWriteFileCallback()) return options->getWriteFileCallback()->writeObject(obj,fileName,options);
279            else if (_writeFileCallback.valid()) return _writeFileCallback->writeObject(obj,fileName,options);
[7518]280            else return writeObjectImplementation(obj,fileName,options);
[2877]281        }
[10171]282        ReaderWriter::WriteResult writeObjectImplementation(const osg::Object& obj, const std::string& fileName,const Options* options);
[2877]283
[10171]284        ReaderWriter::WriteResult writeImage(const osg::Image& obj, const std::string& fileName,const Options* options)
[2877]285        {
[10171]286            if (options && options->getWriteFileCallback()) return options->getWriteFileCallback()->writeImage(obj,fileName,options);
287            else if (_writeFileCallback.valid()) return _writeFileCallback->writeImage(obj,fileName,options);
[7518]288            else return writeImageImplementation(obj,fileName,options);
[2877]289        }
[10171]290        ReaderWriter::WriteResult writeImageImplementation(const osg::Image& obj, const std::string& fileName,const Options* options);
[2877]291
[10171]292        ReaderWriter::WriteResult writeHeightField(const osg::HeightField& obj, const std::string& fileName,const Options* options)
[2877]293        {
[10171]294            if (options && options->getWriteFileCallback()) return options->getWriteFileCallback()->writeHeightField(obj,fileName,options);
295            else if (_writeFileCallback.valid()) return _writeFileCallback->writeHeightField(obj,fileName,options);
[7518]296            else return writeHeightFieldImplementation(obj,fileName,options);
[2877]297        }
[10171]298        ReaderWriter::WriteResult writeHeightFieldImplementation(const osg::HeightField& obj, const std::string& fileName,const Options* options);
[2877]299
[10171]300        ReaderWriter::WriteResult writeNode(const osg::Node& node, const std::string& fileName,const Options* options)
[2877]301        {
[10171]302            if (options && options->getWriteFileCallback()) return options->getWriteFileCallback()->writeNode(node,fileName,options);
303            else if (_writeFileCallback.valid()) return _writeFileCallback->writeNode(node,fileName,options);
[7518]304            else return writeNodeImplementation(node,fileName,options);
[2877]305        }
[10171]306        ReaderWriter::WriteResult writeNodeImplementation(const osg::Node& node, const std::string& fileName,const Options* options);
[2877]307       
[10171]308        ReaderWriter::WriteResult writeShader(const osg::Shader& obj, const std::string& fileName,const Options* options)
[7908]309        {
[10171]310            if (options && options->getWriteFileCallback()) return options->getWriteFileCallback()->writeShader(obj,fileName,options);
311            else if (_writeFileCallback.valid()) return _writeFileCallback->writeShader(obj,fileName,options);
[7908]312            else return writeShaderImplementation(obj,fileName,options);
313        }
[10171]314        ReaderWriter::WriteResult writeShaderImplementation(const osg::Shader& obj, const std::string& fileName,const Options* options);
[2877]315       
[3580]316
[10171]317        inline void _buildKdTreeIfRequired(ReaderWriter::ReadResult& result, const Options* options)
[8535]318        {
[10171]319            bool doKdTreeBuilder = (options && options->getBuildKdTreesHint()!=Options::NO_PREFERENCE) ?
320                options->getBuildKdTreesHint() == Options::BUILD_KDTREES :
321                _buildKdTreesHint == Options::BUILD_KDTREES;
[8535]322
323            if (doKdTreeBuilder && _kdTreeBuilder.valid() && result.validNode())
324            {
[8541]325                osg::ref_ptr<osg::KdTreeBuilder> builder = _kdTreeBuilder->clone();
326                result.getNode()->accept(*builder);
[8535]327            }
328        }
329
[10227]330        /** Set the callback to use inform the DatabasePager whether a file is located on local or remote file system.*/
[10174]331        void setFileLocationCallback( FileLocationCallback* cb) { _fileLocationCallback = cb; }
[8535]332
[10227]333        /** Get the callback to use inform the DatabasePager whether a file is located on local or remote file system.*/
[10174]334        FileLocationCallback* getFileLocationCallback() const { return _fileLocationCallback.get(); }
335
336
337
[8619]338        /** Set whether the KdTrees should be built for geometry in the loader model. */
[10171]339        void setBuildKdTreesHint(Options::BuildKdTreesHint hint) { _buildKdTreesHint = hint; }
[8619]340
341        /** Get whether the KdTrees should be built for geometry in the loader model. */
[10171]342        Options::BuildKdTreesHint getBuildKdTreesHint() const { return _buildKdTreesHint; }
[8535]343
[8619]344        /** Set the KdTreeBuilder visitor that is used to build KdTree on loaded models.*/
[8535]345        void setKdTreeBuilder(osg::KdTreeBuilder* builder) { _kdTreeBuilder = builder; }
[8619]346
347        /** Get the KdTreeBuilder visitor that is used to build KdTree on loaded models.*/
[8535]348        osg::KdTreeBuilder* getKdTreeBuilder() { return _kdTreeBuilder.get(); }
349
[10174]350
[9038]351        /** Set the FileCache that is used to manage local storage of files downloaded from the internet.*/
352        void setFileCache(FileCache* fileCache) { _fileCache = fileCache; }
[8535]353
[9038]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
[8619]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.*/
[8641]365        AuthenticationMap* getAuthenticationMap() { return _authenticationMap.get(); }
366
367        /** Get the password map to be used by plugins when access files from secure locations.*/
[8619]368        const AuthenticationMap* getAuthenticationMap() const { return _authenticationMap.get(); }
369
370
[8]371        void setCreateNodeFromImage(bool flag) { _createNodeFromImage = flag; }
372        bool getCreateNodeFromImage() const { return _createNodeFromImage; }
[8619]373       
[8]374
[10171]375        void setOptions(Options* opt) { _options = opt; }
376        Options* getOptions() { return _options.get(); }
377        const Options*  getOptions() const { return _options.get(); }
[93]378
379
[7648]380        /** initialize both the Data and Library FilePaths, by default called by the
[773]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       
[7648]385        /** initialize the Data FilePath by reading the OSG_FILE_PATH environmental variable.*/
[773]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
[7648]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.*/
[3330]392        void setDataFilePathList(const std::string& paths);
[773]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
[7648]400        /** initialize the Library FilePath by reading the OSG_LIBRARY_PATH
[773]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
[7648]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.*/
[3330]408        void setLibraryFilePathList(const std::string& paths);
[773]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
[1708]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.
[10520]421          * The time used is taken from the FrameStamp::getReferenceTime().*/
422        void updateTimeStampOfObjectsInCacheWithExternalReferences(const osg::FrameStamp& frameStamp);
[1708]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
[10520]427          * after the call to updateTimeStampOfObjectsInCacheWithExternalReferences(frameStamp).*/
428        void removeExpiredObjectsInCache(const osg::FrameStamp& frameStamp);
429
430        /** set hint to viewer code calling removeExpiredObjectsInCache to specify how long it should give before expiring objects in Registry cache,*/
431        void setExpiryDelay(double expiryDelay) { _expiryDelay = expiryDelay; }
432
433        double getExpiryDelay() const { return _expiryDelay; }
434
435
[1708]436        /** Remove all objects in the cache regardless of having external references or expiry times.*/
437        void clearObjectCache();
438
[7648]439        /** Add a filename,object,timestamp triple to the Registry::ObjectCache.*/
[2335]440        void addEntryToObjectCache(const std::string& filename, osg::Object* object, double timestamp = 0.0);
[3876]441
[4461]442        /** Get an object from the object cache*/
443        osg::Object* getFromObjectCache(const std::string& fileName);
[2340]444       
[3598]445        /** Add archive to archive cache so that future calls reference this archive.*/
446        void addToArchiveCache(const std::string& fileName, osgDB::Archive* archive);
447
448        /** Remove archive from cache.*/
449        void removeFromArchiveCache(const std::string& fileName);
450       
451        /** Get an archive from the archive cache*/
452        osgDB::Archive* getFromArchiveCache(const std::string& fileName);
453       
454        /** Remove all archives from the archive cache.*/
455        void clearArchiveCache();
[4170]456       
457         /** If State is non-zero, this function releases OpenGL objects for
458           * the specified graphics context. Otherwise, releases OpenGL objexts
459           * for all graphics contexts. */
460        void releaseGLObjects(osg::State* state=0);
[3598]461
[2166]462        /** get the attached library with specified name.*/
463        DynamicLibrary*              getLibrary(const std::string& fileName);
[8]464
[2631]465        /** Set the SharedStateManager.*/
466        void setSharedStateManager(SharedStateManager* SharedStateManager) { _sharedStateManager = SharedStateManager; }
467
468        /** Get the SharedStateManager, creating one if one is not already created.*/
469        SharedStateManager* getOrCreateSharedStateManager();
470       
471        /** Get the SharedStateManager. Return 0 if no SharedStateManager has been assigned.*/
472        SharedStateManager* getSharedStateManager() { return _sharedStateManager.get(); }
473
[6628]474        /** Add an Archive extension.*/
475        void addArchiveExtension(const std::string ext);
[9884]476       
477        /** registers a protocol */
478        void registerProtocol(const std::string& protocol);
479       
480        /** returns true, if named protocol is registered */
481        bool isProtocolRegistered(const std::string& protocol);
482       
[2534]483    protected:
[2522]484
[1463]485        virtual ~Registry();
486
[1708]487        typedef std::map< std::string, osg::ref_ptr<DotOsgWrapper> >    DotOsgWrapperMap;
488        typedef std::vector< osg::ref_ptr<DynamicLibrary> >             DynamicLibraryList;
489        typedef std::map< std::string, std::string>                     ExtensionAliasMap;
[10000]490        typedef std::map< std::string, std::string>                     MimeTypeExtensionMap;
[6628]491        typedef std::vector< std::string>                               ArchiveExtensionList;
[1708]492       
493        typedef std::pair<osg::ref_ptr<osg::Object>, double >           ObjectTimeStampPair;
494        typedef std::map<std::string, ObjectTimeStampPair >             ObjectCache;
[3598]495        typedef std::map<std::string, osg::ref_ptr<osgDB::Archive> >    ArchiveCache;
[9884]496       
497        typedef std::set<std::string>                                   RegisteredProtocolsSet;
[8]498
499        /** constructor is private, as its a singleton, preventing
500            construction other than via the instance() method and
501            therefore ensuring only one copy is ever constructed*/
502        Registry();
[10174]503
[8]504        /** get the attached library with specified name.*/
505        DynamicLibraryList::iterator getLibraryItr(const std::string& fileName);
506
[10171]507        Options::BuildKdTreesHint     _buildKdTreesHint;
[8535]508        osg::ref_ptr<osg::KdTreeBuilder>            _kdTreeBuilder;
509       
[9038]510        osg::ref_ptr<FileCache>                     _fileCache;
511       
[8619]512        osg::ref_ptr<AuthenticationMap>             _authenticationMap;
513       
[8535]514        bool                                        _createNodeFromImage;
[9884]515       
516        RegisteredProtocolsSet                      _registeredProtocols;
[8]517
518        osg::Object*       readObject(DotOsgWrapperMap& dowMap,Input& fr);
519
[742]520        void eraseWrapper(DotOsgWrapperMap& wrappermap,DotOsgWrapper* wrapper);
521
[3722]522    public:
[3596]523        /** Functor used in internal implementations.*/
524        struct ReadFunctor
525        {
[10171]526            ReadFunctor(const std::string& filename, const Options* options):
[3596]527                _filename(filename),
528                _options(options) {}
529
530            virtual ~ReadFunctor() {}
531            virtual ReaderWriter::ReadResult doRead(ReaderWriter& rw) const = 0;
532            virtual bool isValid(ReaderWriter::ReadResult& readResult) const = 0;
533            virtual bool isValid(osg::Object* object) const = 0;
534
535            std::string _filename;
[10171]536            const Options* _options;
[3596]537        };
538
[3722]539   protected:
[6881]540   
541        void destruct();
542   
[3598]543        // forward declare helper classes
[3596]544        struct ReadObjectFunctor;
545        struct ReadImageFunctor;
546        struct ReadHeightFieldFunctor;
547        struct ReadNodeFunctor;
548        struct ReadArchiveFunctor;
[7908]549        struct ReadShaderFunctor;
[3642]550       
551        // make helper classes friends to get round VS6.0 "issues"
[3646]552        friend struct ReadFunctor;
[3647]553        friend struct ReadObjectFunctor;
[3642]554        friend struct ReadImageFunctor;
555        friend struct ReadHeightFieldFunctor;
556        friend struct ReadNodeFunctor;
557        friend struct ReadArchiveFunctor;
[7908]558        friend struct ReadShaderFunctor;
[3596]559
[3581]560        ReaderWriter::ReadResult read(const ReadFunctor& readFunctor);
[10171]561        ReaderWriter::ReadResult readImplementation(const ReadFunctor& readFunctor,Options::CacheHintOptions cacheHint);
[742]562
[3598]563
[7648]564        // forward declare helper class
[3598]565        class AvailableReaderWriterIterator;
[3642]566        friend class AvailableReaderWriterIterator;
[3598]567
568
[10171]569        osg::ref_ptr<FindFileCallback>      _findFileCallback;
[2877]570        osg::ref_ptr<ReadFileCallback>      _readFileCallback;
571        osg::ref_ptr<WriteFileCallback>     _writeFileCallback;
[10174]572        osg::ref_ptr<FileLocationCallback>  _fileLocationCallback;
[2877]573
[8]574        DotOsgWrapperMap   _objectWrapperMap;
575        DotOsgWrapperMap   _imageWrapperMap;
576        DotOsgWrapperMap   _drawableWrapperMap;
577        DotOsgWrapperMap   _stateAttrWrapperMap;
[4061]578        DotOsgWrapperMap   _uniformWrapperMap;
[8]579        DotOsgWrapperMap   _nodeWrapperMap;
[7908]580        DotOsgWrapperMap   _shaderWrapperMap;
[8]581       
582        DotOsgWrapperMap   _classNameWrapperMap;
583
[8929]584        OpenThreads::ReentrantMutex _pluginMutex;
585        ReaderWriterList            _rwList;
586        DynamicLibraryList          _dlList;
[8]587
588        bool _openingLibrary;
[2877]589   
590        // map to alias to extensions to plugins.
591        ExtensionAliasMap  _extAliasMap;
[6089]592
[10000]593        // maps mime-types to extensions.
594        MimeTypeExtensionMap _mimeTypeExtMap;
595
[6089]596        // Utility: Removes whitespace from both ends of a string.
597        static std::string trim( const std::string& str );
[93]598       
599        // options to pass to reader writers.
[10171]600        osg::ref_ptr<Options>     _options;
[773]601       
[3689]602        FilePathList                            _dataFilePath;
603        FilePathList                            _libraryFilePath;
[2340]604
[10520]605        double                                  _expiryDelay;
[3689]606        ObjectCache                             _objectCache;
607        OpenThreads::Mutex                      _objectCacheMutex;
[2614]608       
[3689]609        ArchiveCache                            _archiveCache;
610        OpenThreads::Mutex                      _archiveCacheMutex;
[6628]611
612        ArchiveExtensionList                    _archiveExtList;
[3598]613       
[3689]614        osg::ref_ptr<SharedStateManager>        _sharedStateManager;
[8]615
616};
617
[1610]618/** read the command line arguments.*/
619inline void readCommandLine(osg::ArgumentParser& parser)
620{
621    Registry::instance()->readCommandLine(parser);
622}
623
[8]624/**  Proxy class for automatic registration of DotOsgWrappers with the Registry.*/
625class RegisterDotOsgWrapperProxy
626{
627    public:
628   
629        RegisterDotOsgWrapperProxy(osg::Object* proto,
630                                   const std::string& name,
631                                   const std::string& associates,
632                                   DotOsgWrapper::ReadFunc readFunc,
633                                   DotOsgWrapper::WriteFunc writeFunc,
634                                   DotOsgWrapper::ReadWriteMode readWriteMode=DotOsgWrapper::READ_AND_WRITE)
635        {
636            if (Registry::instance())
637            {
[1418]638                _wrapper = new DotOsgWrapper(proto,name,associates,readFunc,writeFunc,readWriteMode);
[8]639                Registry::instance()->addDotOsgWrapper(_wrapper.get());
640            }
641        }
642       
643        ~RegisterDotOsgWrapperProxy()
644        {
645            if (Registry::instance())
646            {
647                Registry::instance()->removeDotOsgWrapper(_wrapper.get());
648            }
649        }
650       
651    protected:
652        osg::ref_ptr<DotOsgWrapper> _wrapper;
653};
654
[7422]655template<class T>
656class TemplateRegisterDotOsgWrapperProxy : public RegisterDotOsgWrapperProxy, public T
657{
658    public:
659   
660        TemplateRegisterDotOsgWrapperProxy(osg::Object* proto,
661                                   const std::string& name,
662                                   const std::string& associates,
663                                   DotOsgWrapper::ReadFunc readFunc,
664                                   DotOsgWrapper::WriteFunc writeFunc,
665                                   DotOsgWrapper::ReadWriteMode readWriteMode=DotOsgWrapper::READ_AND_WRITE):
666            RegisterDotOsgWrapperProxy(proto, name, associates, readFunc, writeFunc, readWriteMode) {}
667       
668};
669
[8]670/** Proxy class for automatic registration of reader/writers with the Registry.*/
671template<class T>
672class RegisterReaderWriterProxy
673{
674    public:
675        RegisterReaderWriterProxy()
676        {
677            if (Registry::instance())
678            {
[1418]679                _rw = new T;
[8]680                Registry::instance()->addReaderWriter(_rw.get());
681            }
682        }
683
684        ~RegisterReaderWriterProxy()
685        {
686            if (Registry::instance())
687            {
688                Registry::instance()->removeReaderWriter(_rw.get());
689            }
690        }
691       
[2512]692        T* get() { return _rw.get(); }
693       
[8]694    protected:
695        osg::ref_ptr<T> _rw;
696};
697
698
[6927]699struct PluginFunctionProxy
[6753]700{
[6927]701    PluginFunctionProxy(CPluginFunction function) { (function)(); }
[6753]702};
703
704#define USE_OSGPLUGIN(ext) \
705    extern "C" void osgdb_##ext(void); \
706    static osgDB::PluginFunctionProxy proxy_##ext(osgdb_##ext);
707
708#define REGISTER_OSGPLUGIN(ext, classname) \
709    extern "C" void osgdb_##ext(void) {} \
[6927]710    static osgDB::RegisterReaderWriterProxy<classname> g_proxy_##classname;
[6753]711
[9334]712
713#define USE_DOTOSGWRAPPER(classname) \
714    extern "C" void dotosgwrapper_##classname(void); \
715    static osgDB::PluginFunctionProxy proxy_dotosgwrapper_##classname(dotosgwrapper_##classname);
716
717#define REGISTER_DOTOSGWRAPPER(classname) \
718    extern "C" void dotosgwrapper_##classname(void) {} \
719    static osgDB::RegisterDotOsgWrapperProxy dotosgwrapper_proxy_##classname
720
[6753]721}
722
[8]723#endif
Note: See TracBrowser for help on using the browser.