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;
};
class OSGDB_EXPORT ObjectRegistry : public osg::Referenced
class Registry;
class OSGDB_EXPORT ObjectWrapperManager : public osg::Referenced
{
public:
static ObjectRegistry* instance();
// Wrapper handlers
void addWrapper( ObjectWrapper* wrapper );
@ -92,12 +93,28 @@ public:
CompressorMap& getCompressorMap() { 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:
ObjectRegistry() {}
virtual ~ObjectRegistry() {}
friend class osgDB::Registry;
ObjectWrapperManager();
virtual ~ObjectWrapperManager();
WrapperMap _wrappers;
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; \
void wrapper_propfunc_##NAME(osgDB::ObjectWrapper* wrapper)
template<typename T>
class RegisterCompressorProxy
{
public:
RegisterCompressorProxy( const std::string& name )
{
_compressor = new T;
_compressor->setName( name );
ObjectRegistry::instance()->addCompressor( _compressor.get() );
}
virtual ~RegisterCompressorProxy()
{
ObjectRegistry::instance()->removeCompressor( _compressor.get() );
}
RegisterCompressorProxy( const std::string& name, BaseCompressor* compressor );
~RegisterCompressorProxy();
protected:
osg::ref_ptr<BaseCompressor> _compressor;
};
#define REGISTER_COMPRESSOR(NAME, CLASS) \
static osgDB::RegisterCompressorProxy<CLASS> compressor_proxy_##CLASS(NAME);
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;
};
static osgDB::RegisterCompressorProxy compressor_proxy_##CLASS(NAME, new CLASS);
}

View File

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

View File

@ -169,7 +169,7 @@ InputStream& InputStream::operator>>( ObjectGLenum& value )
{
std::string enumString;
*this >> enumString;
e = GlobalLookupTable::instance()->getValue("GL", enumString);
e = osgDB::Registry::instance()->getObjectWrapperManager()->getValue("GL", enumString);
}
value.set( e );
return *this;
@ -192,8 +192,7 @@ InputStream& InputStream::operator>>( ObjectProperty& prop )
*this >> enumString;
if ( prop._mapProperty )
{
value =
GlobalLookupTable::instance()->getValue(prop._name, enumString);
value = osgDB::Registry::instance()->getObjectWrapperManager()->getValue(prop._name, enumString);
}
else
{
@ -587,7 +586,7 @@ osg::Object* InputStream::readObject( osg::Object* existingObj )
return itr->second.get();
}
ObjectWrapper* wrapper = ObjectRegistry::instance()->findWrapper( className );
ObjectWrapper* wrapper = Registry::instance()->getObjectWrapperManager()->findWrapper( className );
if ( !wrapper )
{
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();
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 )
{
osg::notify(osg::WARN) << "InputStream::readObject(): Unsupported associated class "
@ -695,8 +694,7 @@ void InputStream::decompress()
std::string compressorName; *this >> compressorName;
if ( compressorName=="0" ) return;
BaseCompressor* compressor =
ObjectRegistry::instance()->findCompressor(compressorName);
BaseCompressor* compressor = Registry::instance()->getObjectWrapperManager()->findCompressor(compressorName);
if ( !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 )
{
ObjectWrapper* wrapper = ObjectRegistry::instance()->findWrapper(name);
ObjectWrapper* wrapper = Registry::instance()->getObjectWrapperManager()->findWrapper(name);
if ( !wrapper )
{
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()
{
const ObjectRegistry::WrapperMap& wrappers = ObjectRegistry::instance()->getWrapperMap();
for ( ObjectRegistry::WrapperMap::const_iterator itr=wrappers.begin();
const ObjectWrapperManager::WrapperMap& wrappers = Registry::instance()->getObjectWrapperManager()->getWrapperMap();
for ( ObjectWrapperManager::WrapperMap::const_iterator itr=wrappers.begin();
itr!=wrappers.end(); ++itr )
{
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,
const std::string& associates )
: 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;
return s_lookup.get();
_wrapper = new ObjectWrapper( proto, name, associates );
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"];
@ -384,39 +407,25 @@ GlobalLookupTable::GlobalLookupTable()
primitiveTable.add( "GL_TRIANGLE_STRIP_ADJACENCY_EXT", GL_TRIANGLE_STRIP_ADJACENCY_EXT );
}
RegisterWrapperProxy::RegisterWrapperProxy( osg::Object* proto, const std::string& name,
const std::string& associates, AddPropFunc func )
ObjectWrapperManager::~ObjectWrapperManager()
{
_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()
{
static osg::ref_ptr<ObjectRegistry> s_registry = new ObjectRegistry;
return s_registry.get();
}
void ObjectRegistry::addWrapper( ObjectWrapper* wrapper )
void ObjectWrapperManager::addWrapper( ObjectWrapper* wrapper )
{
if ( !wrapper ) return;
WrapperMap::iterator itr = _wrappers.find( wrapper->getName() );
if ( itr!=_wrappers.end() )
{
osg::notify(osg::WARN) << "ObjectRegistry::addWrapper(): '" << wrapper->getName()
osg::notify(osg::WARN) << "ObjectWrapperManager::addWrapper(): '" << wrapper->getName()
<< "' already exists." << std::endl;
}
_wrappers[wrapper->getName()] = wrapper;
}
void ObjectRegistry::removeWrapper( ObjectWrapper* wrapper )
void ObjectWrapperManager::removeWrapper( ObjectWrapper* wrapper )
{
if ( !wrapper ) return;
@ -424,7 +433,7 @@ void ObjectRegistry::removeWrapper( ObjectWrapper* wrapper )
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 );
if ( itr!=_wrappers.end() ) return itr->second.get();
@ -446,20 +455,20 @@ ObjectWrapper* ObjectRegistry::findWrapper( const std::string& name )
return NULL;
}
void ObjectRegistry::addCompressor( BaseCompressor* compressor )
void ObjectWrapperManager::addCompressor( BaseCompressor* compressor )
{
if ( !compressor ) return;
CompressorMap::iterator itr = _compressors.find( compressor->getName() );
if ( itr!=_compressors.end() )
{
osg::notify(osg::WARN) << "ObjectRegistry::addCompressor(): '" << compressor->getName()
osg::notify(osg::WARN) << "ObjectWrapperManager::addCompressor(): '" << compressor->getName()
<< "' already exists." << std::endl;
}
_compressors[compressor->getName()] = compressor;
}
void ObjectRegistry::removeCompressor( BaseCompressor* compressor )
void ObjectWrapperManager::removeCompressor( BaseCompressor* compressor )
{
if ( !compressor ) return;
@ -467,7 +476,7 @@ void ObjectRegistry::removeCompressor( BaseCompressor* compressor )
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 );
if ( itr!=_compressors.end() ) return itr->second.get();
@ -488,3 +497,19 @@ BaseCompressor* ObjectRegistry::findCompressor( const std::string& name )
}
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
{
const std::string& enumString =
GlobalLookupTable::instance()->getString("GL", e);
const std::string& enumString = osgDB::Registry::instance()->getObjectWrapperManager()->getString("GL", e);
*_out << enumString << ' ';
}
return *this;
@ -165,8 +164,7 @@ OutputStream& OutputStream::operator<<( const ObjectProperty& prop )
std::string enumString = prop._name;
if ( prop._mapProperty )
{
enumString =
GlobalLookupTable::instance()->getString(prop._name, prop._value);
enumString = osgDB::Registry::instance()->getObjectWrapperManager()->getString(prop._name, prop._value);
}
*_out << enumString << ' ';
}
@ -460,7 +458,7 @@ void OutputStream::writeObject( const osg::Object* obj )
// Check whether this is a shared object or not
if ( id>=_objectMap.size() )
{
ObjectWrapper* wrapper = ObjectRegistry::instance()->findWrapper( name );
ObjectWrapper* wrapper = Registry::instance()->getObjectWrapperManager()->findWrapper( name );
if ( !wrapper )
{
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();
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 )
{
osg::notify(osg::WARN) << "OutputStream::writeObject(): Unsupported associated class "
@ -501,8 +499,7 @@ void OutputStream::start( OutputStream::WriteType type )
if ( !_compressorName.empty() )
{
BaseCompressor* compressor =
ObjectRegistry::instance()->findCompressor(_compressorName);
BaseCompressor* compressor = Registry::instance()->getObjectWrapperManager()->findCompressor(_compressorName);
if ( !compressor )
{
osg::notify(osg::WARN) << "OutputStream::start(): No such compressor "
@ -541,8 +538,7 @@ void OutputStream::compress( std::ostream* ostream )
_currentField = "Compression";
if ( _compressorName.empty() || !isBinary() ) return;
BaseCompressor* compressor =
ObjectRegistry::instance()->findCompressor(_compressorName);
BaseCompressor* compressor = Registry::instance()->getObjectWrapperManager()->findCompressor(_compressorName);
if ( !compressor || !ostream ) return;
if ( !compressor->compress(*ostream, _compressSource.str()) )
@ -554,8 +550,8 @@ void OutputStream::compress( std::ostream* ostream )
void OutputStream::writeSchema( std::ostream& fout )
{
// Write to external ascii stream
const ObjectRegistry::WrapperMap& wrappers = ObjectRegistry::instance()->getWrapperMap();
for ( ObjectRegistry::WrapperMap::const_iterator itr=wrappers.begin();
const ObjectWrapperManager::WrapperMap& wrappers = Registry::instance()->getObjectWrapperManager()->getWrapperMap();
for ( ObjectWrapperManager::WrapperMap::const_iterator itr=wrappers.begin();
itr!=wrappers.end(); ++itr )
{
ObjectWrapper* wrapper = itr->second.get();

View File

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