2002-07-17 04:07:32 +08:00
//C++ header - Open Scene Graph - Copyright (C) 1998-2002 Robert Osfield
2001-10-04 23:12:57 +08:00
//Distributed under the terms of the GNU Library General Public License (LGPL)
//as published by the Free Software Foundation.
2001-09-20 05:08:56 +08:00
#ifndef OSGDB_REGISTRY
#define OSGDB_REGISTRY 1
#include <osg/ref_ptr>
#include <osgDB/DynamicLibrary>
#include <osgDB/ReaderWriter>
#include <osgDB/DotOsgWrapper>
2002-06-18 05:50:37 +08:00
#include <vector>
#include <map>
#include <string>
#include <deque>
2001-09-20 05:08:56 +08:00
namespace osgDB {
2001-12-19 08:38:23 +08:00
2002-06-18 05:50:37 +08:00
/** list of directories to search through which searching for files. */
typedef std::deque<std::string> FilePathList;
2001-09-20 05:08:56 +08:00
/**
Registry is a singleton factory which stores
2001-09-28 20:36:40 +08:00
the reader/writers which are linked in
2001-09-20 05:08:56 +08:00
at runtime for reading non-native file formats.
2001-09-28 20:36:40 +08:00
The RegisterDotOsgWrapperProxy can be used to automatically register
DotOsgWrappers, at runtime with the Registry. A DotOsgWrapper encapsulates
the functions that can read and write to the .osg for each osg::Object.
2001-09-20 05:08:56 +08:00
The RegisterReaderWriterProxy can be used to automatically
register at runtime a reader/writer with the Registry.
*/
class OSGDB_EXPORT Registry
{
public:
static Registry* instance();
2002-02-28 08:11:31 +08:00
~Registry();
2001-09-20 05:08:56 +08:00
2001-12-19 08:38:23 +08:00
/** read the command line string list, removing any matched control sequences.*/
void readCommandLine(std::vector<std::string>& commandLine);
2001-09-20 05:08:56 +08:00
/** register an .fileextension alias to mapExt toExt, the later
* should the the extension name of the readerwriter plugin library.
* For example to map .tif files to the tiff loader, use
* addExtAlias("tif","tiff") which will enable .tif to be read
* by the libdb_tiff readerwriter plugin.*/
void addFileExtensionAlias(const std::string mapExt, const std::string toExt);
void addDotOsgWrapper(DotOsgWrapper* wrapper);
void removeDotOsgWrapper(DotOsgWrapper* wrapper);
void addReaderWriter(ReaderWriter* rw);
void removeReaderWriter(ReaderWriter* rw);
/** create the platform specific library name associated with file.*/
std::string createLibraryNameForFile(const std::string& fileName);
/** create the platform specific library name associated with file extension.*/
std::string createLibraryNameForExt(const std::string& ext);
2002-06-07 18:03:49 +08:00
/** create the platform specific library name associated with nodekit library name.*/
std::string createLibraryNameForNodeKit(const std::string& name);
2001-09-20 05:08:56 +08:00
/** find the library in the SG_LIBRARY_PATH and load it.*/
bool loadLibrary(const std::string& fileName);
/** close the attached library with specified name.*/
bool closeLibrary(const std::string& fileName);
osg::Object* readObjectOfType(const osg::Object& compObj,Input& fr);
osg::Object* readObject(Input& fr);
osg::Image* readImage(Input& fr);
osg::Drawable* readDrawable(Input& fr);
osg::StateAttribute* readStateAttribute(Input& fr);
osg::Node* readNode(Input& fr);
bool writeObject(const osg::Object& obj,Output& fw);
2001-10-31 03:23:24 +08:00
ReaderWriter::ReadResult readObject(const std::string& fileName);
ReaderWriter::WriteResult writeObject(const osg::Object& obj, const std::string& fileName);
2001-09-20 05:08:56 +08:00
2001-10-31 03:23:24 +08:00
ReaderWriter::ReadResult readImage(const std::string& fileName);
ReaderWriter::WriteResult writeImage(const osg::Image& obj, const std::string& fileName);
2001-09-20 05:08:56 +08:00
2001-10-31 03:23:24 +08:00
ReaderWriter::ReadResult readNode(const std::string& fileName);
ReaderWriter::WriteResult writeNode(const osg::Node& node, const std::string& fileName);
2001-09-20 05:08:56 +08:00
void setCreateNodeFromImage(bool flag) { _createNodeFromImage = flag; }
bool getCreateNodeFromImage() const { return _createNodeFromImage; }
2001-10-15 01:54:25 +08:00
void setOptions(ReaderWriter::Options* opt) { _options = opt; }
ReaderWriter::Options* getOptions() { return _options.get(); }
const ReaderWriter::Options* getOptions() const { return _options.get(); }
2002-06-18 05:50:37 +08:00
/** initilize both the Data and Library FilePaths, by default called by the
* constructor, so it should only be required if you want to force
* the re-reading of environmental variables.*/
void initFilePathLists() { initDataFilePathList(); initLibraryFilePathList(); }
/** initilize the Data FilePath by reading the OSG_FILE_PATH environmental variable.*/
void initDataFilePathList();
/** Set the data file path using a list of paths stored in a FilePath, which is used when search for data files.*/
void setDataFilePathList(const FilePathList& filepath) { _dataFilePath = filepath; }
/** 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.*/
void setDataFilePathList(const std::string& paths) { _dataFilePath.clear(); convertStringPathIntoFilePathList(paths,_dataFilePath); }
/** get the data file path which is used when search for data files.*/
FilePathList& getDataFilePathList() { return _dataFilePath; }
/** get the const data file path which is used when search for data files.*/
const FilePathList& getDataFilePathList() const { return _dataFilePath; }
/** initilize the Library FilePath by reading the OSG_LIBRARY_PATH
* and the appropriate system environmental variables*/
void initLibraryFilePathList();
/** Set the library file path using a list of paths stored in a FilePath, which is used when search for data files.*/
void setLibraryFilePathList(const FilePathList& filepath) { _libraryFilePath = filepath; }
/** 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.*/
void setLibraryFilePathList(const std::string& paths) { _libraryFilePath.clear(); convertStringPathIntoFilePathList(paths,_libraryFilePath); }
/** get the library file path which is used when search for library (dso/dll's) files.*/
FilePathList& getLibraryFilePathList() { return _libraryFilePath; }
/** get the const library file path which is used when search for library (dso/dll's) files.*/
const FilePathList& getLibraryFilePathList() const { return _libraryFilePath; }
/** convert a string containing a list of paths deliminated either with ';' (Windows) or ':' (All other platforms) into FilePath represetation.*/
static void convertStringPathIntoFilePathList(const std::string& paths,FilePathList& filepath);
2001-09-20 05:08:56 +08:00
private:
typedef std::map<std::string,osg::ref_ptr<DotOsgWrapper> > DotOsgWrapperMap;
typedef std::vector<osg::ref_ptr<ReaderWriter> > ReaderWriterList;
typedef std::vector<osg::ref_ptr<DynamicLibrary> > DynamicLibraryList;
typedef std::map<std::string,std::string> ExtensionAliasMap;
/** constructor is private, as its a singleton, preventing
construction other than via the instance() method and
therefore ensuring only one copy is ever constructed*/
Registry();
/** get the attached library with specified name.*/
DynamicLibraryList::iterator getLibraryItr(const std::string& fileName);
DynamicLibrary* getLibrary(const std::string& fileName);
bool _createNodeFromImage;
osg::Object* readObject(DotOsgWrapperMap& dowMap,Input& fr);
2002-06-07 18:03:49 +08:00
void eraseWrapper(DotOsgWrapperMap& wrappermap,DotOsgWrapper* wrapper);
2001-09-20 05:08:56 +08:00
DotOsgWrapperMap _objectWrapperMap;
DotOsgWrapperMap _imageWrapperMap;
DotOsgWrapperMap _drawableWrapperMap;
DotOsgWrapperMap _stateAttrWrapperMap;
DotOsgWrapperMap _nodeWrapperMap;
DotOsgWrapperMap _classNameWrapperMap;
ReaderWriterList _rwList;
DynamicLibraryList _dlList;
bool _openingLibrary;
// map to alias to extensions to plugins.
ExtensionAliasMap _extAliasMap;
2001-10-15 01:54:25 +08:00
// options to pass to reader writers.
osg::ref_ptr<ReaderWriter::Options> _options;
2002-06-18 05:50:37 +08:00
FilePathList _dataFilePath;
FilePathList _libraryFilePath;
2001-09-20 05:08:56 +08:00
};
2001-12-19 08:38:23 +08:00
/** read the command line string list into the osgDB::Registry(), removing any matched control sequences.*/
inline void readCommandLine(std::vector<std::string>& commandLine)
{
Registry::instance()->readCommandLine(commandLine);
}
2001-09-20 05:08:56 +08:00
/** Proxy class for automatic registration of DotOsgWrappers with the Registry.*/
class RegisterDotOsgWrapperProxy
{
public:
RegisterDotOsgWrapperProxy(osg::Object* proto,
const std::string& name,
const std::string& associates,
DotOsgWrapper::ReadFunc readFunc,
DotOsgWrapper::WriteFunc writeFunc,
DotOsgWrapper::ReadWriteMode readWriteMode=DotOsgWrapper::READ_AND_WRITE)
{
if (Registry::instance())
{
_wrapper = new DotOsgWrapper(proto,name,associates,readFunc,writeFunc,readWriteMode);
Registry::instance()->addDotOsgWrapper(_wrapper.get());
}
}
~RegisterDotOsgWrapperProxy()
{
if (Registry::instance())
{
Registry::instance()->removeDotOsgWrapper(_wrapper.get());
}
}
protected:
osg::ref_ptr<DotOsgWrapper> _wrapper;
};
/** Proxy class for automatic registration of reader/writers with the Registry.*/
template<class T>
class RegisterReaderWriterProxy
{
public:
RegisterReaderWriterProxy()
{
if (Registry::instance())
{
_rw = new T;
Registry::instance()->addReaderWriter(_rw.get());
}
}
~RegisterReaderWriterProxy()
{
if (Registry::instance())
{
Registry::instance()->removeReaderWriter(_rw.get());
}
}
protected:
osg::ref_ptr<T> _rw;
};
2002-02-03 20:33:41 +08:00
}
2001-09-20 05:08:56 +08:00
#endif