Renamed ObjectRegistry to ObjectWrapperManager, and replaced it's instance() method usage with assigning a ObjectWrapperManager

to the osgDB::Registry.  Added a osgDB::Registry::getObjectWrapperManager() for access of this object wrapper manager.  This
change centralises the singleton management in osgDB.

Merged the osgDB::GlobalLookUpTable functionality into ObjectWrapperManger to keep down the number of singletons in use.
This commit is contained in:
Robert Osfield 2010-01-22 15:16:22 +00:00
parent 8839736818
commit 7ab759c97f
6 changed files with 127 additions and 120 deletions

View File

@ -69,10 +69,11 @@ protected:
SerializerList _backupSerializers; SerializerList _backupSerializers;
}; };
class OSGDB_EXPORT ObjectRegistry : public osg::Referenced class Registry;
class OSGDB_EXPORT ObjectWrapperManager : public osg::Referenced
{ {
public: public:
static ObjectRegistry* instance();
// Wrapper handlers // Wrapper handlers
void addWrapper( ObjectWrapper* wrapper ); void addWrapper( ObjectWrapper* wrapper );
@ -92,12 +93,28 @@ public:
CompressorMap& getCompressorMap() { return _compressors; } CompressorMap& getCompressorMap() { return _compressors; }
const CompressorMap& getCompressorMap() const { return _compressors; } const CompressorMap& getCompressorMap() const { return _compressors; }
typedef std::map<std::string, IntLookup> IntLookupMap;
IntLookup::Value getValue( const std::string& group, const std::string& str ) { return findLookup(group).getValue(str.c_str()); }
const std::string& getString( const std::string& group, IntLookup::Value value ) { return findLookup(group).getString(value); }
protected: protected:
ObjectRegistry() {}
virtual ~ObjectRegistry() {} friend class osgDB::Registry;
ObjectWrapperManager();
virtual ~ObjectWrapperManager();
WrapperMap _wrappers; WrapperMap _wrappers;
CompressorMap _compressors; CompressorMap _compressors;
IntLookup& findLookup( const std::string& group )
{
IntLookupMap::iterator itr = _globalMap.find(group);
if ( itr!=_globalMap.end() ) return itr->second;
else return _globalMap["GL"];
}
IntLookupMap _globalMap;
}; };
@ -122,55 +139,18 @@ protected:
typedef CLASS MyClass; \ typedef CLASS MyClass; \
void wrapper_propfunc_##NAME(osgDB::ObjectWrapper* wrapper) void wrapper_propfunc_##NAME(osgDB::ObjectWrapper* wrapper)
template<typename T>
class RegisterCompressorProxy class RegisterCompressorProxy
{ {
public: public:
RegisterCompressorProxy( const std::string& name ) RegisterCompressorProxy( const std::string& name, BaseCompressor* compressor );
{ ~RegisterCompressorProxy();
_compressor = new T;
_compressor->setName( name );
ObjectRegistry::instance()->addCompressor( _compressor.get() );
}
virtual ~RegisterCompressorProxy()
{
ObjectRegistry::instance()->removeCompressor( _compressor.get() );
}
protected: protected:
osg::ref_ptr<BaseCompressor> _compressor; osg::ref_ptr<BaseCompressor> _compressor;
}; };
#define REGISTER_COMPRESSOR(NAME, CLASS) \ #define REGISTER_COMPRESSOR(NAME, CLASS) \
static osgDB::RegisterCompressorProxy<CLASS> compressor_proxy_##CLASS(NAME); static osgDB::RegisterCompressorProxy compressor_proxy_##CLASS(NAME, new CLASS);
class OSGDB_EXPORT GlobalLookupTable : public osg::Referenced
{
public:
typedef std::map<std::string, IntLookup> IntLookupMap;
static GlobalLookupTable* instance();
IntLookup::Value getValue( const std::string& group, const std::string& str )
{ return findLookup(group).getValue(str.c_str()); }
const std::string& getString( const std::string& group, IntLookup::Value value )
{ return findLookup(group).getString(value); }
protected:
GlobalLookupTable();
virtual ~GlobalLookupTable() {}
IntLookup& findLookup( const std::string& group )
{
IntLookupMap::iterator itr = _globalMap.find(group);
if ( itr!=_globalMap.end() ) return itr->second;
else return _globalMap["GL"];
}
IntLookupMap _globalMap;
};
} }

View File

@ -24,6 +24,7 @@
#include <osgDB/ReaderWriter> #include <osgDB/ReaderWriter>
#include <osgDB/Options> #include <osgDB/Options>
#include <osgDB/DotOsgWrapper> #include <osgDB/DotOsgWrapper>
#include <osgDB/ObjectWrapper>
#include <osgDB/DatabasePager> #include <osgDB/DatabasePager>
#include <osgDB/FileCache> #include <osgDB/FileCache>
@ -480,6 +481,9 @@ class OSGDB_EXPORT Registry : public osg::Referenced
/** returns true, if named protocol is registered */ /** returns true, if named protocol is registered */
bool isProtocolRegistered(const std::string& protocol); bool isProtocolRegistered(const std::string& protocol);
/** Get the ObjectWrapperManager that is used to store all the ObjectWrappers. */
ObjectWrapperManager* getObjectWrapperManager() { return _objectWrapperManager.get(); }
protected: protected:
virtual ~Registry(); virtual ~Registry();
@ -613,6 +617,8 @@ class OSGDB_EXPORT Registry : public osg::Referenced
osg::ref_ptr<SharedStateManager> _sharedStateManager; osg::ref_ptr<SharedStateManager> _sharedStateManager;
osg::ref_ptr<ObjectWrapperManager> _objectWrapperManager;
}; };
/** read the command line arguments.*/ /** read the command line arguments.*/

View File

@ -169,7 +169,7 @@ InputStream& InputStream::operator>>( ObjectGLenum& value )
{ {
std::string enumString; std::string enumString;
*this >> enumString; *this >> enumString;
e = GlobalLookupTable::instance()->getValue("GL", enumString); e = osgDB::Registry::instance()->getObjectWrapperManager()->getValue("GL", enumString);
} }
value.set( e ); value.set( e );
return *this; return *this;
@ -192,8 +192,7 @@ InputStream& InputStream::operator>>( ObjectProperty& prop )
*this >> enumString; *this >> enumString;
if ( prop._mapProperty ) if ( prop._mapProperty )
{ {
value = value = osgDB::Registry::instance()->getObjectWrapperManager()->getValue(prop._name, enumString);
GlobalLookupTable::instance()->getValue(prop._name, enumString);
} }
else else
{ {
@ -587,7 +586,7 @@ osg::Object* InputStream::readObject( osg::Object* existingObj )
return itr->second.get(); return itr->second.get();
} }
ObjectWrapper* wrapper = ObjectRegistry::instance()->findWrapper( className ); ObjectWrapper* wrapper = Registry::instance()->getObjectWrapperManager()->findWrapper( className );
if ( !wrapper ) if ( !wrapper )
{ {
osg::notify(osg::WARN) << "InputStream::readObject(): Unsupported wrapper class " osg::notify(osg::WARN) << "InputStream::readObject(): Unsupported wrapper class "
@ -603,7 +602,7 @@ osg::Object* InputStream::readObject( osg::Object* existingObj )
const StringList& associates = wrapper->getAssociates(); const StringList& associates = wrapper->getAssociates();
for ( StringList::const_iterator itr=associates.begin(); itr!=associates.end(); ++itr ) for ( StringList::const_iterator itr=associates.begin(); itr!=associates.end(); ++itr )
{ {
ObjectWrapper* assocWrapper = ObjectRegistry::instance()->findWrapper(*itr); ObjectWrapper* assocWrapper = Registry::instance()->getObjectWrapperManager()->findWrapper(*itr);
if ( !assocWrapper ) if ( !assocWrapper )
{ {
osg::notify(osg::WARN) << "InputStream::readObject(): Unsupported associated class " osg::notify(osg::WARN) << "InputStream::readObject(): Unsupported associated class "
@ -695,8 +694,7 @@ void InputStream::decompress()
std::string compressorName; *this >> compressorName; std::string compressorName; *this >> compressorName;
if ( compressorName=="0" ) return; if ( compressorName=="0" ) return;
BaseCompressor* compressor = BaseCompressor* compressor = Registry::instance()->getObjectWrapperManager()->findCompressor(compressorName);
ObjectRegistry::instance()->findCompressor(compressorName);
if ( !compressor ) if ( !compressor )
{ {
osg::notify(osg::WARN) << "InputStream::decompress(): No such compressor " osg::notify(osg::WARN) << "InputStream::decompress(): No such compressor "
@ -713,7 +711,7 @@ void InputStream::decompress()
void InputStream::setWrapperSchema( const std::string& name, const std::string& properties ) void InputStream::setWrapperSchema( const std::string& name, const std::string& properties )
{ {
ObjectWrapper* wrapper = ObjectRegistry::instance()->findWrapper(name); ObjectWrapper* wrapper = Registry::instance()->getObjectWrapperManager()->findWrapper(name);
if ( !wrapper ) if ( !wrapper )
{ {
osg::notify(osg::WARN) << "InputStream::setSchema(): Unsupported wrapper class " osg::notify(osg::WARN) << "InputStream::setSchema(): Unsupported wrapper class "
@ -728,8 +726,8 @@ void InputStream::setWrapperSchema( const std::string& name, const std::string&
void InputStream::resetSchema() void InputStream::resetSchema()
{ {
const ObjectRegistry::WrapperMap& wrappers = ObjectRegistry::instance()->getWrapperMap(); const ObjectWrapperManager::WrapperMap& wrappers = Registry::instance()->getObjectWrapperManager()->getWrapperMap();
for ( ObjectRegistry::WrapperMap::const_iterator itr=wrappers.begin(); for ( ObjectWrapperManager::WrapperMap::const_iterator itr=wrappers.begin();
itr!=wrappers.end(); ++itr ) itr!=wrappers.end(); ++itr )
{ {
ObjectWrapper* wrapper = itr->second.get(); ObjectWrapper* wrapper = itr->second.get();

View File

@ -65,6 +65,10 @@ void osgDB::split( const std::string& src, StringList& list, char separator )
} }
} }
////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// ObjectWrapper
//
ObjectWrapper::ObjectWrapper( osg::Object* proto, const std::string& name, ObjectWrapper::ObjectWrapper( osg::Object* proto, const std::string& name,
const std::string& associates ) const std::string& associates )
: osg::Referenced(), : osg::Referenced(),
@ -152,13 +156,32 @@ void ObjectWrapper::writeSchema( StringList& properties )
} }
} }
GlobalLookupTable* GlobalLookupTable::instance() ////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// RegisterWrapperProxy
//
RegisterWrapperProxy::RegisterWrapperProxy( osg::Object* proto, const std::string& name,
const std::string& associates, AddPropFunc func )
{ {
static osg::ref_ptr<GlobalLookupTable> s_lookup = new GlobalLookupTable; _wrapper = new ObjectWrapper( proto, name, associates );
return s_lookup.get(); if ( func ) (*func)( _wrapper.get() );
Registry::instance()->getObjectWrapperManager()->addWrapper( _wrapper.get() );
} }
GlobalLookupTable::GlobalLookupTable() RegisterWrapperProxy::~RegisterWrapperProxy()
{
Registry::instance()->getObjectWrapperManager()->removeWrapper( _wrapper.get() );
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// ObjectWrapperManager
//
////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// GlobalLookupTable
//
ObjectWrapperManager::ObjectWrapperManager()
{ {
IntLookup& glTable = _globalMap["GL"]; IntLookup& glTable = _globalMap["GL"];
@ -384,39 +407,25 @@ GlobalLookupTable::GlobalLookupTable()
primitiveTable.add( "GL_TRIANGLE_STRIP_ADJACENCY_EXT", GL_TRIANGLE_STRIP_ADJACENCY_EXT ); primitiveTable.add( "GL_TRIANGLE_STRIP_ADJACENCY_EXT", GL_TRIANGLE_STRIP_ADJACENCY_EXT );
} }
RegisterWrapperProxy::RegisterWrapperProxy( osg::Object* proto, const std::string& name, ObjectWrapperManager::~ObjectWrapperManager()
const std::string& associates, AddPropFunc func )
{ {
_wrapper = new ObjectWrapper( proto, name, associates );
if ( func ) (*func)( _wrapper.get() );
ObjectRegistry::instance()->addWrapper( _wrapper.get() );
} }
RegisterWrapperProxy::~RegisterWrapperProxy()
{
ObjectRegistry::instance()->removeWrapper( _wrapper.get() );
}
ObjectRegistry* ObjectRegistry::instance() void ObjectWrapperManager::addWrapper( ObjectWrapper* wrapper )
{
static osg::ref_ptr<ObjectRegistry> s_registry = new ObjectRegistry;
return s_registry.get();
}
void ObjectRegistry::addWrapper( ObjectWrapper* wrapper )
{ {
if ( !wrapper ) return; if ( !wrapper ) return;
WrapperMap::iterator itr = _wrappers.find( wrapper->getName() ); WrapperMap::iterator itr = _wrappers.find( wrapper->getName() );
if ( itr!=_wrappers.end() ) if ( itr!=_wrappers.end() )
{ {
osg::notify(osg::WARN) << "ObjectRegistry::addWrapper(): '" << wrapper->getName() osg::notify(osg::WARN) << "ObjectWrapperManager::addWrapper(): '" << wrapper->getName()
<< "' already exists." << std::endl; << "' already exists." << std::endl;
} }
_wrappers[wrapper->getName()] = wrapper; _wrappers[wrapper->getName()] = wrapper;
} }
void ObjectRegistry::removeWrapper( ObjectWrapper* wrapper ) void ObjectWrapperManager::removeWrapper( ObjectWrapper* wrapper )
{ {
if ( !wrapper ) return; if ( !wrapper ) return;
@ -424,7 +433,7 @@ void ObjectRegistry::removeWrapper( ObjectWrapper* wrapper )
if ( itr!=_wrappers.end() ) _wrappers.erase( itr ); if ( itr!=_wrappers.end() ) _wrappers.erase( itr );
} }
ObjectWrapper* ObjectRegistry::findWrapper( const std::string& name ) ObjectWrapper* ObjectWrapperManager::findWrapper( const std::string& name )
{ {
WrapperMap::iterator itr = _wrappers.find( name ); WrapperMap::iterator itr = _wrappers.find( name );
if ( itr!=_wrappers.end() ) return itr->second.get(); if ( itr!=_wrappers.end() ) return itr->second.get();
@ -446,20 +455,20 @@ ObjectWrapper* ObjectRegistry::findWrapper( const std::string& name )
return NULL; return NULL;
} }
void ObjectRegistry::addCompressor( BaseCompressor* compressor ) void ObjectWrapperManager::addCompressor( BaseCompressor* compressor )
{ {
if ( !compressor ) return; if ( !compressor ) return;
CompressorMap::iterator itr = _compressors.find( compressor->getName() ); CompressorMap::iterator itr = _compressors.find( compressor->getName() );
if ( itr!=_compressors.end() ) if ( itr!=_compressors.end() )
{ {
osg::notify(osg::WARN) << "ObjectRegistry::addCompressor(): '" << compressor->getName() osg::notify(osg::WARN) << "ObjectWrapperManager::addCompressor(): '" << compressor->getName()
<< "' already exists." << std::endl; << "' already exists." << std::endl;
} }
_compressors[compressor->getName()] = compressor; _compressors[compressor->getName()] = compressor;
} }
void ObjectRegistry::removeCompressor( BaseCompressor* compressor ) void ObjectWrapperManager::removeCompressor( BaseCompressor* compressor )
{ {
if ( !compressor ) return; if ( !compressor ) return;
@ -467,7 +476,7 @@ void ObjectRegistry::removeCompressor( BaseCompressor* compressor )
if ( itr!=_compressors.end() ) _compressors.erase( itr ); if ( itr!=_compressors.end() ) _compressors.erase( itr );
} }
BaseCompressor* ObjectRegistry::findCompressor( const std::string& name ) BaseCompressor* ObjectWrapperManager::findCompressor( const std::string& name )
{ {
CompressorMap::iterator itr = _compressors.find( name ); CompressorMap::iterator itr = _compressors.find( name );
if ( itr!=_compressors.end() ) return itr->second.get(); if ( itr!=_compressors.end() ) return itr->second.get();
@ -488,3 +497,19 @@ BaseCompressor* ObjectRegistry::findCompressor( const std::string& name )
} }
return NULL; return NULL;
} }
////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// RegisytrCompressorProxy
//
RegisterCompressorProxy::RegisterCompressorProxy( const std::string& name, BaseCompressor* compressor ):
_compressor(compressor)
{
_compressor->setName( name );
Registry::instance()->getObjectWrapperManager()->addCompressor( _compressor.get() );
}
RegisterCompressorProxy::~RegisterCompressorProxy()
{
Registry::instance()->getObjectWrapperManager()->removeCompressor( _compressor.get() );
}

View File

@ -146,8 +146,7 @@ OutputStream& OutputStream::operator<<( const ObjectGLenum& value )
} }
else else
{ {
const std::string& enumString = const std::string& enumString = osgDB::Registry::instance()->getObjectWrapperManager()->getString("GL", e);
GlobalLookupTable::instance()->getString("GL", e);
*_out << enumString << ' '; *_out << enumString << ' ';
} }
return *this; return *this;
@ -165,8 +164,7 @@ OutputStream& OutputStream::operator<<( const ObjectProperty& prop )
std::string enumString = prop._name; std::string enumString = prop._name;
if ( prop._mapProperty ) if ( prop._mapProperty )
{ {
enumString = enumString = osgDB::Registry::instance()->getObjectWrapperManager()->getString(prop._name, prop._value);
GlobalLookupTable::instance()->getString(prop._name, prop._value);
} }
*_out << enumString << ' '; *_out << enumString << ' ';
} }
@ -460,7 +458,7 @@ void OutputStream::writeObject( const osg::Object* obj )
// Check whether this is a shared object or not // Check whether this is a shared object or not
if ( id>=_objectMap.size() ) if ( id>=_objectMap.size() )
{ {
ObjectWrapper* wrapper = ObjectRegistry::instance()->findWrapper( name ); ObjectWrapper* wrapper = Registry::instance()->getObjectWrapperManager()->findWrapper( name );
if ( !wrapper ) if ( !wrapper )
{ {
osg::notify(osg::WARN) << "OutputStream::writeObject(): Unsupported wrapper class " osg::notify(osg::WARN) << "OutputStream::writeObject(): Unsupported wrapper class "
@ -472,7 +470,7 @@ void OutputStream::writeObject( const osg::Object* obj )
const StringList& associates = wrapper->getAssociates(); const StringList& associates = wrapper->getAssociates();
for ( StringList::const_iterator itr=associates.begin(); itr!=associates.end(); ++itr ) for ( StringList::const_iterator itr=associates.begin(); itr!=associates.end(); ++itr )
{ {
ObjectWrapper* assocWrapper = ObjectRegistry::instance()->findWrapper(*itr); ObjectWrapper* assocWrapper = Registry::instance()->getObjectWrapperManager()->findWrapper(*itr);
if ( !assocWrapper ) if ( !assocWrapper )
{ {
osg::notify(osg::WARN) << "OutputStream::writeObject(): Unsupported associated class " osg::notify(osg::WARN) << "OutputStream::writeObject(): Unsupported associated class "
@ -501,8 +499,7 @@ void OutputStream::start( OutputStream::WriteType type )
if ( !_compressorName.empty() ) if ( !_compressorName.empty() )
{ {
BaseCompressor* compressor = BaseCompressor* compressor = Registry::instance()->getObjectWrapperManager()->findCompressor(_compressorName);
ObjectRegistry::instance()->findCompressor(_compressorName);
if ( !compressor ) if ( !compressor )
{ {
osg::notify(osg::WARN) << "OutputStream::start(): No such compressor " osg::notify(osg::WARN) << "OutputStream::start(): No such compressor "
@ -541,8 +538,7 @@ void OutputStream::compress( std::ostream* ostream )
_currentField = "Compression"; _currentField = "Compression";
if ( _compressorName.empty() || !isBinary() ) return; if ( _compressorName.empty() || !isBinary() ) return;
BaseCompressor* compressor = BaseCompressor* compressor = Registry::instance()->getObjectWrapperManager()->findCompressor(_compressorName);
ObjectRegistry::instance()->findCompressor(_compressorName);
if ( !compressor || !ostream ) return; if ( !compressor || !ostream ) return;
if ( !compressor->compress(*ostream, _compressSource.str()) ) if ( !compressor->compress(*ostream, _compressSource.str()) )
@ -554,8 +550,8 @@ void OutputStream::compress( std::ostream* ostream )
void OutputStream::writeSchema( std::ostream& fout ) void OutputStream::writeSchema( std::ostream& fout )
{ {
// Write to external ascii stream // Write to external ascii stream
const ObjectRegistry::WrapperMap& wrappers = ObjectRegistry::instance()->getWrapperMap(); const ObjectWrapperManager::WrapperMap& wrappers = Registry::instance()->getObjectWrapperManager()->getWrapperMap();
for ( ObjectRegistry::WrapperMap::const_iterator itr=wrappers.begin(); for ( ObjectWrapperManager::WrapperMap::const_iterator itr=wrappers.begin();
itr!=wrappers.end(); ++itr ) itr!=wrappers.end(); ++itr )
{ {
ObjectWrapper* wrapper = itr->second.get(); ObjectWrapper* wrapper = itr->second.get();

View File

@ -368,6 +368,8 @@ Registry::Registry()
// register http-protocol, so the curl can handle it, if necessary // register http-protocol, so the curl can handle it, if necessary
registerProtocol("http"); registerProtocol("http");
_objectWrapperManager = new ObjectWrapperManager;
} }