| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 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 <osg/ref_ptr> |
|---|
| 18 | #include <osg/ArgumentParser> |
|---|
| 19 | |
|---|
| 20 | #include <osgDB/DynamicLibrary> |
|---|
| 21 | #include <osgDB/ReaderWriter> |
|---|
| 22 | #include <osgDB/DotOsgWrapper> |
|---|
| 23 | #include <osgDB/DatabasePager> |
|---|
| 24 | |
|---|
| 25 | #include <vector> |
|---|
| 26 | #include <map> |
|---|
| 27 | #include <string> |
|---|
| 28 | #include <deque> |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | namespace osgDB { |
|---|
| 32 | |
|---|
| 33 | /** basic structure for custom runtime inheritance checking */ |
|---|
| 34 | struct basic_type_wrapper { |
|---|
| 35 | virtual bool matches(const osg::Object *proto) const = 0; |
|---|
| 36 | }; |
|---|
| 37 | |
|---|
| 38 | /** a class template that checks inheritance between a given |
|---|
| 39 | Object's class and a class defined at compile time through |
|---|
| 40 | the template parameter T. |
|---|
| 41 | This is used in conjunction with readObjectOfType() to |
|---|
| 42 | specify an abstract class as reference type. |
|---|
| 43 | **/ |
|---|
| 44 | template<class T> |
|---|
| 45 | struct type_wrapper: basic_type_wrapper { |
|---|
| 46 | bool matches(const osg::Object *proto) const |
|---|
| 47 | { |
|---|
| 48 | return dynamic_cast<const T*>(proto) != 0; |
|---|
| 49 | } |
|---|
| 50 | }; |
|---|
| 51 | |
|---|
| 52 | /** list of directories to search through which searching for files. */ |
|---|
| 53 | typedef std::deque<std::string> FilePathList; |
|---|
| 54 | |
|---|
| 55 | /** |
|---|
| 56 | Registry is a singleton factory which stores |
|---|
| 57 | the reader/writers which are linked in |
|---|
| 58 | at runtime for reading non-native file formats. |
|---|
| 59 | |
|---|
| 60 | The RegisterDotOsgWrapperProxy can be used to automatically register |
|---|
| 61 | DotOsgWrappers, at runtime with the Registry. A DotOsgWrapper encapsulates |
|---|
| 62 | the functions that can read and write to the .osg for each osg::Object. |
|---|
| 63 | |
|---|
| 64 | The RegisterReaderWriterProxy can be used to automatically |
|---|
| 65 | register at runtime a reader/writer with the Registry. |
|---|
| 66 | */ |
|---|
| 67 | class OSGDB_EXPORT Registry : public osg::Referenced |
|---|
| 68 | { |
|---|
| 69 | public: |
|---|
| 70 | |
|---|
| 71 | /// bit mask for setting up which object types get cached by readObject/Image/HeightField/Node(filename) calls |
|---|
| 72 | enum CacheHintOptions |
|---|
| 73 | { /// do not cache objects of any type |
|---|
| 74 | CACHE_NONE = 0, |
|---|
| 75 | |
|---|
| 76 | /// cache nodes loaded via readNode(filename) |
|---|
| 77 | CACHE_NODES = 1, |
|---|
| 78 | |
|---|
| 79 | /// cache images loaded via readImage(filename) |
|---|
| 80 | CACHE_IMAGES = 2, |
|---|
| 81 | |
|---|
| 82 | /// cache heightfield loaded via readHeightField(filename) |
|---|
| 83 | CACHE_HEIGHTFIELDS = 4, |
|---|
| 84 | |
|---|
| 85 | /// cache objects loaded via readObject(filename) |
|---|
| 86 | CACHE_OBJECTS = 8, |
|---|
| 87 | |
|---|
| 88 | /// cache on all read*(filename) calls |
|---|
| 89 | CACHE_ALL = CACHE_NODES | |
|---|
| 90 | CACHE_IMAGES | |
|---|
| 91 | CACHE_HEIGHTFIELDS | |
|---|
| 92 | CACHE_OBJECTS |
|---|
| 93 | }; |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | static Registry* instance(bool erase = false); |
|---|
| 98 | |
|---|
| 99 | /** read the command line arguments.*/ |
|---|
| 100 | void readCommandLine(osg::ArgumentParser& commandLine); |
|---|
| 101 | |
|---|
| 102 | /** register an .fileextension alias to mapExt toExt, the later |
|---|
| 103 | * should the the extension name of the readerwriter plugin library. |
|---|
| 104 | * For example to map .tif files to the tiff loader, use |
|---|
| 105 | * addExtAlias("tif","tiff") which will enable .tif to be read |
|---|
| 106 | * by the libdb_tiff readerwriter plugin.*/ |
|---|
| 107 | void addFileExtensionAlias(const std::string mapExt, const std::string toExt); |
|---|
| 108 | |
|---|
| 109 | void addDotOsgWrapper(DotOsgWrapper* wrapper); |
|---|
| 110 | void removeDotOsgWrapper(DotOsgWrapper* wrapper); |
|---|
| 111 | |
|---|
| 112 | void addReaderWriter(ReaderWriter* rw); |
|---|
| 113 | void removeReaderWriter(ReaderWriter* rw); |
|---|
| 114 | |
|---|
| 115 | /** create the platform specific library name associated with file.*/ |
|---|
| 116 | std::string createLibraryNameForFile(const std::string& fileName); |
|---|
| 117 | |
|---|
| 118 | /** create the platform specific library name associated with file extension.*/ |
|---|
| 119 | std::string createLibraryNameForExtension(const std::string& ext); |
|---|
| 120 | |
|---|
| 121 | /** create the platform specific library name associated with nodekit library name.*/ |
|---|
| 122 | std::string createLibraryNameForNodeKit(const std::string& name); |
|---|
| 123 | |
|---|
| 124 | /** find the library in the SG_LIBRARY_PATH and load it.*/ |
|---|
| 125 | bool loadLibrary(const std::string& fileName); |
|---|
| 126 | /** close the attached library with specified name.*/ |
|---|
| 127 | bool closeLibrary(const std::string& fileName); |
|---|
| 128 | /** close all libraries.*/ |
|---|
| 129 | void closeAllLibraries(); |
|---|
| 130 | |
|---|
| 131 | /** get a reader writer which handles specified extension.*/ |
|---|
| 132 | ReaderWriter* getReaderWriterForExtension(const std::string& ext); |
|---|
| 133 | |
|---|
| 134 | osg::Object* readObjectOfType(const osg::Object& compObj,Input& fr); |
|---|
| 135 | osg::Object* readObjectOfType(const basic_type_wrapper &btw, Input& fr); |
|---|
| 136 | |
|---|
| 137 | osg::Object* readObject(Input& fr); |
|---|
| 138 | osg::Image* readImage(Input& fr); |
|---|
| 139 | osg::Drawable* readDrawable(Input& fr); |
|---|
| 140 | osg::StateAttribute* readStateAttribute(Input& fr); |
|---|
| 141 | osg::Node* readNode(Input& fr); |
|---|
| 142 | |
|---|
| 143 | bool writeObject(const osg::Object& obj,Output& fw); |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | class ReadFileCallback : public osg::Referenced |
|---|
| 147 | { |
|---|
| 148 | public: |
|---|
| 149 | |
|---|
| 150 | virtual ReaderWriter::ReadResult readObject(const std::string& filename, CacheHintOptions options) |
|---|
| 151 | { |
|---|
| 152 | return osgDB::Registry::instance()->readObjectImplementation(filename,options); |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | virtual ReaderWriter::ReadResult readImage(const std::string& filename, CacheHintOptions options) |
|---|
| 156 | { |
|---|
| 157 | return osgDB::Registry::instance()->readImageImplementation(filename,options); |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | virtual ReaderWriter::ReadResult readHeightField(const std::string& filename, CacheHintOptions options) |
|---|
| 161 | { |
|---|
| 162 | return osgDB::Registry::instance()->readHeightFieldImplementation(filename,options); |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | virtual ReaderWriter::ReadResult readNode(const std::string& filename, CacheHintOptions options) |
|---|
| 166 | { |
|---|
| 167 | return osgDB::Registry::instance()->readNodeImplementation(filename,options); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | protected: |
|---|
| 171 | virtual ~ReadFileCallback() {} |
|---|
| 172 | }; |
|---|
| 173 | |
|---|
| 174 | /** Set the Registry callback to use in place of the default readFile calls.*/ |
|---|
| 175 | void setReadFileCallback( ReadFileCallback* cb) { _readFileCallback = cb; } |
|---|
| 176 | |
|---|
| 177 | /** Get the readFile callback.*/ |
|---|
| 178 | ReadFileCallback* getReadFileCallback() { return _readFileCallback.get(); } |
|---|
| 179 | |
|---|
| 180 | /** Get the const readFile callback.*/ |
|---|
| 181 | const ReadFileCallback* getReadFileCallback() const { return _readFileCallback.get(); } |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | ReaderWriter::ReadResult readObject(const std::string& fileName,CacheHintOptions useObjectCache) |
|---|
| 185 | { |
|---|
| 186 | if (_readFileCallback.valid()) return _readFileCallback->readObject(fileName,useObjectCache); |
|---|
| 187 | else return readObjectImplementation(fileName,useObjectCache); |
|---|
| 188 | } |
|---|
| 189 | ReaderWriter::ReadResult readObjectImplementation(const std::string& fileName,CacheHintOptions useObjectCache); |
|---|
| 190 | |
|---|
| 191 | ReaderWriter::ReadResult readImage(const std::string& fileName,CacheHintOptions useObjectCache) |
|---|
| 192 | { |
|---|
| 193 | if (_readFileCallback.valid()) return _readFileCallback->readImage(fileName,useObjectCache); |
|---|
| 194 | else return readImageImplementation(fileName,useObjectCache); |
|---|
| 195 | } |
|---|
| 196 | ReaderWriter::ReadResult readImageImplementation(const std::string& fileName,CacheHintOptions useObjectCache); |
|---|
| 197 | |
|---|
| 198 | ReaderWriter::ReadResult readHeightField(const std::string& fileName,CacheHintOptions useObjectCache) |
|---|
| 199 | { |
|---|
| 200 | if (_readFileCallback.valid()) return _readFileCallback->readHeightField(fileName,useObjectCache); |
|---|
| 201 | else return readHeightFieldImplementation(fileName,useObjectCache); |
|---|
| 202 | } |
|---|
| 203 | ReaderWriter::ReadResult readHeightFieldImplementation(const std::string& fileName,CacheHintOptions useObjectCache); |
|---|
| 204 | |
|---|
| 205 | ReaderWriter::ReadResult readNode(const std::string& fileName,CacheHintOptions useObjectCache) |
|---|
| 206 | { |
|---|
| 207 | if (_readFileCallback.valid()) return _readFileCallback->readNode(fileName,useObjectCache); |
|---|
| 208 | else return readNodeImplementation(fileName,useObjectCache); |
|---|
| 209 | } |
|---|
| 210 | ReaderWriter::ReadResult readNodeImplementation(const std::string& fileName,CacheHintOptions useObjectCache); |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | class WriteFileCallback : public osg::Referenced |
|---|
| 214 | { |
|---|
| 215 | public: |
|---|
| 216 | |
|---|
| 217 | virtual ReaderWriter::WriteResult writeObject(const osg::Object& obj, const std::string& fileName) |
|---|
| 218 | { |
|---|
| 219 | return osgDB::Registry::instance()->writeObjectImplementation(obj,fileName); |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | virtual ReaderWriter::WriteResult writeImage(const osg::Image& obj, const std::string& fileName) |
|---|
| 223 | { |
|---|
| 224 | return osgDB::Registry::instance()->writeImageImplementation(obj,fileName); |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | virtual ReaderWriter::WriteResult writeHeightField(const osg::HeightField& obj, const std::string& fileName) |
|---|
| 228 | { |
|---|
| 229 | return osgDB::Registry::instance()->writeHeightFieldImplementation(obj,fileName); |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | virtual ReaderWriter::WriteResult writeNode(const osg::Node& obj, const std::string& fileName) |
|---|
| 233 | { |
|---|
| 234 | return osgDB::Registry::instance()->writeNodeImplementation(obj,fileName); |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | protected: |
|---|
| 238 | virtual ~WriteFileCallback() {} |
|---|
| 239 | }; |
|---|
| 240 | |
|---|
| 241 | /** Set the Registry callback to use in place of the default writeFile calls.*/ |
|---|
| 242 | void setWriteFileCallback( WriteFileCallback* cb) { _writeFileCallback = cb; } |
|---|
| 243 | |
|---|
| 244 | /** Get the writeFile callback.*/ |
|---|
| 245 | WriteFileCallback* getWriteFileCallback() { return _writeFileCallback.get(); } |
|---|
| 246 | |
|---|
| 247 | /** Get the const writeFile callback.*/ |
|---|
| 248 | const WriteFileCallback* getWriteFileCallback() const { return _writeFileCallback.get(); } |
|---|
| 249 | |
|---|
| 250 | |
|---|
| 251 | ReaderWriter::WriteResult writeObject(const osg::Object& obj, const std::string& fileName) |
|---|
| 252 | { |
|---|
| 253 | if (_writeFileCallback.valid()) return _writeFileCallback->writeObject(obj,fileName); |
|---|
| 254 | else return writeObjectImplementation(obj,fileName); |
|---|
| 255 | } |
|---|
| 256 | ReaderWriter::WriteResult writeObjectImplementation(const osg::Object& obj, const std::string& fileName); |
|---|
| 257 | |
|---|
| 258 | ReaderWriter::WriteResult writeImage(const osg::Image& obj, const std::string& fileName) |
|---|
| 259 | { |
|---|
| 260 | if (_writeFileCallback.valid()) return _writeFileCallback->writeImage(obj,fileName); |
|---|
| 261 | else return writeImageImplementation(obj,fileName); |
|---|
| 262 | } |
|---|
| 263 | ReaderWriter::WriteResult writeImageImplementation(const osg::Image& obj, const std::string& fileName); |
|---|
| 264 | |
|---|
| 265 | ReaderWriter::WriteResult writeHeightField(const osg::HeightField& obj, const std::string& fileName) |
|---|
| 266 | { |
|---|
| 267 | if (_writeFileCallback.valid()) return _writeFileCallback->writeHeightField(obj,fileName); |
|---|
| 268 | else return writeHeightFieldImplementation(obj,fileName); |
|---|
| 269 | } |
|---|
| 270 | ReaderWriter::WriteResult writeHeightFieldImplementation(const osg::HeightField& obj, const std::string& fileName); |
|---|
| 271 | |
|---|
| 272 | ReaderWriter::WriteResult writeNode(const osg::Node& node, const std::string& fileName) |
|---|
| 273 | { |
|---|
| 274 | if (_writeFileCallback.valid()) return _writeFileCallback->writeNode(node,fileName); |
|---|
| 275 | else return writeNodeImplementation(node,fileName); |
|---|
| 276 | } |
|---|
| 277 | ReaderWriter::WriteResult writeNodeImplementation(const osg::Node& node, const std::string& fileName); |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | void setCreateNodeFromImage(bool flag) { _createNodeFromImage = flag; } |
|---|
| 281 | bool getCreateNodeFromImage() const { return _createNodeFromImage; } |
|---|
| 282 | |
|---|
| 283 | |
|---|
| 284 | void setOptions(ReaderWriter::Options* opt) { _options = opt; } |
|---|
| 285 | ReaderWriter::Options* getOptions() { return _options.get(); } |
|---|
| 286 | const ReaderWriter::Options* getOptions() const { return _options.get(); } |
|---|
| 287 | |
|---|
| 288 | |
|---|
| 289 | /** initilize both the Data and Library FilePaths, by default called by the |
|---|
| 290 | * constructor, so it should only be required if you want to force |
|---|
| 291 | * the re-reading of environmental variables.*/ |
|---|
| 292 | void initFilePathLists() { initDataFilePathList(); initLibraryFilePathList(); } |
|---|
| 293 | |
|---|
| 294 | /** initilize the Data FilePath by reading the OSG_FILE_PATH environmental variable.*/ |
|---|
| 295 | void initDataFilePathList(); |
|---|
| 296 | |
|---|
| 297 | /** Set the data file path using a list of paths stored in a FilePath, which is used when search for data files.*/ |
|---|
| 298 | void setDataFilePathList(const FilePathList& filepath) { _dataFilePath = filepath; } |
|---|
| 299 | |
|---|
| 300 | /** Set the data file path using a single string deliminated either with ';' (Windows) or ':' (All other platforms), which is used when search for data files.*/ |
|---|
| 301 | void setDataFilePathList(const std::string& paths); |
|---|
| 302 | |
|---|
| 303 | /** get the data file path which is used when search for data files.*/ |
|---|
| 304 | FilePathList& getDataFilePathList() { return _dataFilePath; } |
|---|
| 305 | |
|---|
| 306 | /** get the const data file path which is used when search for data files.*/ |
|---|
| 307 | const FilePathList& getDataFilePathList() const { return _dataFilePath; } |
|---|
| 308 | |
|---|
| 309 | /** initilize the Library FilePath by reading the OSG_LIBRARY_PATH |
|---|
| 310 | * and the appropriate system environmental variables*/ |
|---|
| 311 | void initLibraryFilePathList(); |
|---|
| 312 | |
|---|
| 313 | /** Set the library file path using a list of paths stored in a FilePath, which is used when search for data files.*/ |
|---|
| 314 | void setLibraryFilePathList(const FilePathList& filepath) { _libraryFilePath = filepath; } |
|---|
| 315 | |
|---|
| 316 | /** Set the library file path using a single string deliminated either with ';' (Windows) or ':' (All other platforms), which is used when search for data files.*/ |
|---|
| 317 | void setLibraryFilePathList(const std::string& paths); |
|---|
| 318 | |
|---|
| 319 | /** get the library file path which is used when search for library (dso/dll's) files.*/ |
|---|
| 320 | FilePathList& getLibraryFilePathList() { return _libraryFilePath; } |
|---|
| 321 | |
|---|
| 322 | /** get the const library file path which is used when search for library (dso/dll's) files.*/ |
|---|
| 323 | const FilePathList& getLibraryFilePathList() const { return _libraryFilePath; } |
|---|
| 324 | |
|---|
| 325 | /** For each object in the cache which has an reference count greater than 1 |
|---|
| 326 | * (and therefore referenced by elsewhere in the application) set the time stamp |
|---|
| 327 | * for that object in the cache to specified time. |
|---|
| 328 | * This would typically be called once per frame by applications which are doing database paging, |
|---|
| 329 | * and need to prune objects that are no longer required. |
|---|
| 330 | * Time value is time in sceonds.*/ |
|---|
| 331 | void updateTimeStampOfObjectsInCacheWithExtenalReferences(double currentTime); |
|---|
| 332 | |
|---|
| 333 | /** Removed object in the cache which have a time stamp at or before the specified expiry time. |
|---|
| 334 | * This would typically be called once per frame by applications which are doing database paging, |
|---|
| 335 | * and need to prune objects that are no longer required, and called after the a called |
|---|
| 336 | * after the call to updateTimeStampOfObjectsInCacheWithExtenalReferences(currentTime). |
|---|
| 337 | * Note, the currentTime is not the expiryTime, one would typically set the expiry time |
|---|
| 338 | * to a fixed amount of time before currentTime, such as expiryTime = currentTime-10.0. |
|---|
| 339 | * Time value is time in sceonds.*/ |
|---|
| 340 | void removeExpiredObjectsInCache(double expiryTime); |
|---|
| 341 | |
|---|
| 342 | /** Remove all objects in the cache regardless of having external references or expiry times.*/ |
|---|
| 343 | void clearObjectCache(); |
|---|
| 344 | |
|---|
| 345 | /** Add a filename,object,timestamp tripple to the Registry::ObjectCache.*/ |
|---|
| 346 | void addEntryToObjectCache(const std::string& filename, osg::Object* object, double timestamp = 0.0); |
|---|
| 347 | |
|---|
| 348 | /** Set whether the Registry::ObjectCache should be used by default.*/ |
|---|
| 349 | void setUseObjectCacheHint(CacheHintOptions useObjectCache) { _useObjectCacheHint = useObjectCache; } |
|---|
| 350 | |
|---|
| 351 | /** Get whether the Registry::ObjectCache should be used by default.*/ |
|---|
| 352 | CacheHintOptions getUseObjectCacheHint() const { return _useObjectCacheHint; } |
|---|
| 353 | |
|---|
| 354 | /** get the attached library with specified name.*/ |
|---|
| 355 | DynamicLibrary* getLibrary(const std::string& fileName); |
|---|
| 356 | |
|---|
| 357 | typedef std::vector< osg::ref_ptr<ReaderWriter> > ReaderWriterList; |
|---|
| 358 | |
|---|
| 359 | |
|---|
| 360 | /** Set the DatabasePager.*/ |
|---|
| 361 | void setDatabasePager(DatabasePager* databasePager) { _databasePager = databasePager; } |
|---|
| 362 | |
|---|
| 363 | /** Get the DatabasePager, creating one if one is not already created.*/ |
|---|
| 364 | DatabasePager* getOrCreateDatabasePager(); |
|---|
| 365 | |
|---|
| 366 | /** Get the DatabasePager. Return 0 if no DatabasePager has been assigned.*/ |
|---|
| 367 | DatabasePager* getDatabasePager() { return _databasePager.get(); } |
|---|
| 368 | |
|---|
| 369 | |
|---|
| 370 | /** Set the SharedStateManager.*/ |
|---|
| 371 | void setSharedStateManager(SharedStateManager* SharedStateManager) { _sharedStateManager = SharedStateManager; } |
|---|
| 372 | |
|---|
| 373 | /** Get the SharedStateManager, creating one if one is not already created.*/ |
|---|
| 374 | SharedStateManager* getOrCreateSharedStateManager(); |
|---|
| 375 | |
|---|
| 376 | /** Get the SharedStateManager. Return 0 if no SharedStateManager has been assigned.*/ |
|---|
| 377 | SharedStateManager* getSharedStateManager() { return _sharedStateManager.get(); } |
|---|
| 378 | |
|---|
| 379 | protected: |
|---|
| 380 | |
|---|
| 381 | virtual ~Registry(); |
|---|
| 382 | |
|---|
| 383 | typedef std::map< std::string, osg::ref_ptr<DotOsgWrapper> > DotOsgWrapperMap; |
|---|
| 384 | typedef std::vector< osg::ref_ptr<DynamicLibrary> > DynamicLibraryList; |
|---|
| 385 | typedef std::map< std::string, std::string> ExtensionAliasMap; |
|---|
| 386 | |
|---|
| 387 | typedef std::pair<osg::ref_ptr<osg::Object>, double > ObjectTimeStampPair; |
|---|
| 388 | typedef std::map<std::string, ObjectTimeStampPair > ObjectCache; |
|---|
| 389 | |
|---|
| 390 | /** constructor is private, as its a singleton, preventing |
|---|
| 391 | construction other than via the instance() method and |
|---|
| 392 | therefore ensuring only one copy is ever constructed*/ |
|---|
| 393 | Registry(); |
|---|
| 394 | |
|---|
| 395 | /** get the attached library with specified name.*/ |
|---|
| 396 | DynamicLibraryList::iterator getLibraryItr(const std::string& fileName); |
|---|
| 397 | |
|---|
| 398 | bool _createNodeFromImage; |
|---|
| 399 | |
|---|
| 400 | osg::Object* readObject(DotOsgWrapperMap& dowMap,Input& fr); |
|---|
| 401 | |
|---|
| 402 | void eraseWrapper(DotOsgWrapperMap& wrappermap,DotOsgWrapper* wrapper); |
|---|
| 403 | |
|---|
| 404 | ReaderWriter::ReadResult readObject(const std::string& fileName); |
|---|
| 405 | ReaderWriter::ReadResult readImage(const std::string& fileName); |
|---|
| 406 | ReaderWriter::ReadResult readHeightField(const std::string& fileName); |
|---|
| 407 | ReaderWriter::ReadResult readNode(const std::string& fileName); |
|---|
| 408 | |
|---|
| 409 | osg::ref_ptr<ReadFileCallback> _readFileCallback; |
|---|
| 410 | osg::ref_ptr<WriteFileCallback> _writeFileCallback; |
|---|
| 411 | |
|---|
| 412 | DotOsgWrapperMap _objectWrapperMap; |
|---|
| 413 | DotOsgWrapperMap _imageWrapperMap; |
|---|
| 414 | DotOsgWrapperMap _drawableWrapperMap; |
|---|
| 415 | DotOsgWrapperMap _stateAttrWrapperMap; |
|---|
| 416 | DotOsgWrapperMap _nodeWrapperMap; |
|---|
| 417 | |
|---|
| 418 | DotOsgWrapperMap _classNameWrapperMap; |
|---|
| 419 | |
|---|
| 420 | ReaderWriterList _rwList; |
|---|
| 421 | DynamicLibraryList _dlList; |
|---|
| 422 | |
|---|
| 423 | bool _openingLibrary; |
|---|
| 424 | |
|---|
| 425 | // map to alias to extensions to plugins. |
|---|
| 426 | ExtensionAliasMap _extAliasMap; |
|---|
| 427 | |
|---|
| 428 | // options to pass to reader writers. |
|---|
| 429 | osg::ref_ptr<ReaderWriter::Options> _options; |
|---|
| 430 | |
|---|
| 431 | FilePathList _dataFilePath; |
|---|
| 432 | FilePathList _libraryFilePath; |
|---|
| 433 | |
|---|
| 434 | CacheHintOptions _useObjectCacheHint; |
|---|
| 435 | ObjectCache _objectCache; |
|---|
| 436 | |
|---|
| 437 | osg::ref_ptr<DatabasePager> _databasePager; |
|---|
| 438 | osg::ref_ptr<SharedStateManager> _sharedStateManager; |
|---|
| 439 | |
|---|
| 440 | }; |
|---|
| 441 | |
|---|
| 442 | /** read the command line arguments.*/ |
|---|
| 443 | inline void readCommandLine(osg::ArgumentParser& parser) |
|---|
| 444 | { |
|---|
| 445 | Registry::instance()->readCommandLine(parser); |
|---|
| 446 | } |
|---|
| 447 | |
|---|
| 448 | /** Proxy class for automatic registration of DotOsgWrappers with the Registry.*/ |
|---|
| 449 | class RegisterDotOsgWrapperProxy |
|---|
| 450 | { |
|---|
| 451 | public: |
|---|
| 452 | |
|---|
| 453 | RegisterDotOsgWrapperProxy(osg::Object* proto, |
|---|
| 454 | const std::string& name, |
|---|
| 455 | const std::string& associates, |
|---|
| 456 | DotOsgWrapper::ReadFunc readFunc, |
|---|
| 457 | DotOsgWrapper::WriteFunc writeFunc, |
|---|
| 458 | DotOsgWrapper::ReadWriteMode readWriteMode=DotOsgWrapper::READ_AND_WRITE) |
|---|
| 459 | { |
|---|
| 460 | if (Registry::instance()) |
|---|
| 461 | { |
|---|
| 462 | _wrapper = new DotOsgWrapper(proto,name,associates,readFunc,writeFunc,readWriteMode); |
|---|
| 463 | Registry::instance()->addDotOsgWrapper(_wrapper.get()); |
|---|
| 464 | } |
|---|
| 465 | } |
|---|
| 466 | |
|---|
| 467 | ~RegisterDotOsgWrapperProxy() |
|---|
| 468 | { |
|---|
| 469 | if (Registry::instance()) |
|---|
| 470 | { |
|---|
| 471 | Registry::instance()->removeDotOsgWrapper(_wrapper.get()); |
|---|
| 472 | } |
|---|
| 473 | } |
|---|
| 474 | |
|---|
| 475 | protected: |
|---|
| 476 | osg::ref_ptr<DotOsgWrapper> _wrapper; |
|---|
| 477 | }; |
|---|
| 478 | |
|---|
| 479 | /** Proxy class for automatic registration of reader/writers with the Registry.*/ |
|---|
| 480 | template<class T> |
|---|
| 481 | class RegisterReaderWriterProxy |
|---|
| 482 | { |
|---|
| 483 | public: |
|---|
| 484 | RegisterReaderWriterProxy() |
|---|
| 485 | { |
|---|
| 486 | if (Registry::instance()) |
|---|
| 487 | { |
|---|
| 488 | _rw = new T; |
|---|
| 489 | Registry::instance()->addReaderWriter(_rw.get()); |
|---|
| 490 | } |
|---|
| 491 | } |
|---|
| 492 | |
|---|
| 493 | ~RegisterReaderWriterProxy() |
|---|
| 494 | { |
|---|
| 495 | if (Registry::instance()) |
|---|
| 496 | { |
|---|
| 497 | Registry::instance()->removeReaderWriter(_rw.get()); |
|---|
| 498 | } |
|---|
| 499 | } |
|---|
| 500 | |
|---|
| 501 | T* get() { return _rw.get(); } |
|---|
| 502 | |
|---|
| 503 | protected: |
|---|
| 504 | osg::ref_ptr<T> _rw; |
|---|
| 505 | }; |
|---|
| 506 | |
|---|
| 507 | } |
|---|
| 508 | |
|---|
| 509 | #endif |
|---|