00001
00002
00003
00004 #include "GaudiKernel/IFactory.h"
00005 #include "GaudiKernel/IObjManager.h"
00006 #include "GaudiKernel/ISvcLocator.h"
00007 #include "GaudiKernel/MsgStream.h"
00008 #include "ObjectManager.h"
00009
00010
00011 #include <iostream>
00012
00013
00014 ObjectManager::ObjectManager(ISvcLocator* svclocator) {
00015 m_objects = new Container();
00016 m_svclocator = svclocator;
00017 m_refcount = 1;
00018 m_msgsvc = 0;
00019 }
00020
00021
00022 ObjectManager::~ObjectManager() {
00023 delete m_objects;
00024 }
00025
00026
00027 unsigned long ObjectManager::addRef() {
00028 m_refcount++;
00029 return m_refcount;
00030 }
00031
00032
00033 unsigned long ObjectManager::release() {
00034 m_refcount--;
00035 if( m_refcount <= 0) {
00036 delete this;
00037 }
00038 return m_refcount;
00039 }
00040
00041
00042 StatusCode ObjectManager::queryInterface(const IID& iid, void** pinterface) {
00043 if( iid == IID_IInterface ) {
00044 *pinterface = (IInterface*)this;
00045 addRef();
00046 return StatusCode::SUCCESS;
00047 }
00048 else if ( iid == IID_IObjManager ) {
00049 *pinterface = (IObjManager*)this;
00050 addRef();
00051 return StatusCode::SUCCESS;
00052 }
00053 else {
00054 return StatusCode::FAILURE;
00055 }
00056 return StatusCode::SUCCESS;
00057 }
00058
00059
00060 StatusCode ObjectManager::declareObjFactory( const IFactory& factory ) {
00061
00062 if( m_msgsvc == 0 ) {
00063 m_svclocator->getService( "MessageSvc", IID_IMessageSvc, (IInterface*&)m_msgsvc );
00064 }
00065 MsgStream log(m_msgsvc, "ObjectManager");
00066
00067 const std::string& objtype = factory.ident();
00068 for ( Container::iterator i = m_objects->begin(); i != m_objects->end(); i++ ) {
00069 if ( objtype == (*i)->ident() ) {
00070 log << MSG::WARNING << "Factory for class:" << factory.ident()
00071 << " already exists." << endreq;
00072 m_objects->erase(i);
00073 break;
00074 }
00075 }
00076
00077 log << MSG::DEBUG << "Added object factory for class:" << factory.ident() << endreq;
00078
00079 m_objects->push_back( & factory );
00080 return StatusCode::SUCCESS;
00081 }
00082
00083
00084 bool ObjectManager::existsObjFactory(const std::string& objtype
00085 ) const {
00086 for ( Container::const_iterator i = m_objects->begin(); i != m_objects->end(); i++ ) {
00087 if ( objtype == (*i)->ident() ) {
00088 return true;
00089 }
00090 }
00091 return false;
00092 }
00093
00094
00095 const IFactory* ObjectManager::objFactory(const std::string& objtype
00096 ) const
00097 {
00098 for ( Container::const_iterator i = m_objects->begin(); i != m_objects->end(); i++ ) {
00099 if ( objtype == (*i)->ident() ) {
00100 return (*i);
00101 }
00102 }
00103 return 0;
00104 }