7 #include "fwServices/registry/ObjectService.hpp" 9 #include "fwServices/IService.hpp" 10 #include "fwServices/registry/AppConfig.hpp" 11 #include "fwServices/registry/ServiceConfig.hpp" 12 #include "fwServices/registry/ServiceFactory.hpp" 14 #include <fwCore/LogicStamp.hpp> 15 #include <fwCore/util/LazyInstantiator.hpp> 17 #include <fwTools/fwID.hpp> 19 #include <boost/filesystem.hpp> 35 ::fwServices::registry::ObjectService::sptr
get()
37 return ::fwCore::util::LazyInstantiator< ::fwServices::registry::ObjectService >::getInstance();
42 ::fwServices::registry::ObjectService::ObjectVectorType
getObjects()
44 return ::fwServices::OSR::get()->getObjects();
51 return ::fwServices::OSR::get()->getRegistryInformation();
56 ::fwServices::registry::ObjectService::ServiceVectorType
getServices(
const std::string& serviceType )
58 return ::fwServices::OSR::get()->getServices(serviceType);
63 ::fwServices::registry::ObjectService::ServiceVectorType
getServices( ::fwData::Object::sptr obj,
64 const std::string& serviceType )
66 return ::fwServices::OSR::get()->getServices(obj, serviceType);
71 ::fwServices::registry::ObjectService::ServiceVectorType
getServices( ::fwData::Object::sptr obj )
73 return ::fwServices::OSR::get()->getServices(obj);
78 bool has( ::fwData::Object::sptr obj,
const std::string& srvType)
80 return ::fwServices::OSR::get()->has(obj, srvType);
87 ::fwServices::OSR::get()->registerService(service);
92 void registerService( ::fwData::Object::sptr obj, ::fwServices::IService::sptr service )
94 ::fwServices::OSR::get()->registerService(obj, service);
99 void registerService( ::fwData::Object::sptr obj, const ::fwServices::IService::KeyType& objKey,
100 ::fwServices::IService::AccessType access, ::fwServices::IService::sptr service )
102 ::fwServices::OSR::get()->registerService(obj, objKey, access, service);
108 ::fwServices::IService::sptr service )
110 ::fwServices::OSR::get()->registerServiceInput(obj, objKey, service);
116 ::fwServices::IService::sptr service )
118 ::fwServices::OSR::get()->registerServiceOutput(obj, objKey, service);
123 void swapService( ::fwData::Object::sptr objDst, ::fwServices::IService::sptr service )
125 ::fwServices::OSR::get()->swapService(objDst, service);
132 ::fwServices::OSR::get()->unregisterService(service);
137 void unregisterService(const ::fwServices::IService::KeyType& objKey, ::fwServices::IService::AccessType access,
138 IService::sptr service)
140 ::fwServices::OSR::get()->unregisterService(objKey, access, service);
147 ::fwServices::OSR::get()->unregisterServiceOutput(objKey, service);
152 bool isRegistered(const ::fwServices::IService::KeyType& objKey,
153 ::fwServices::IService::AccessType access, IService::sptr service)
155 return ::fwServices::OSR::get()->isRegistered(objKey, access, service);
160 ::fwData::Object::csptr
getRegistered(const ::fwServices::IService::KeyType& objKey,
161 ::fwServices::IService::AccessType access, IService::sptr service)
163 return ::fwServices::OSR::get()->getRegistered(objKey, access, service);
176 const ::fwCom::Signals::SignalKeyType ObjectService::s_REGISTERED_SIG =
"registered";
177 const ::fwCom::Signals::SignalKeyType ObjectService::s_UNREGISTERED_SIG =
"unregistered";
181 ObjectService::ObjectService()
183 newSignal<RegisterSignalType>(s_REGISTERED_SIG);
184 newSignal<RegisterSignalType>(s_UNREGISTERED_SIG);
189 std::string ObjectService::getRegistryInformation()
const 191 std::stringstream info;
192 ::fwData::Object::csptr previousObj;
194 for(
const ServiceContainerType::left_map::value_type& objSrvMap : m_container.left)
196 ::fwData::Object::csptr obj = objSrvMap.first.lock();
198 if ( obj && previousObj != obj )
200 info <<
"Object ( id = "<<obj->getID()<<
" ) has " 201 << m_container.left.count(obj) <<
" services." << std::endl;
204 ::fwServices::IService::sptr service = objSrvMap.second;
205 info <<
" srv : uid = "<< service->getID() <<
" , classname = "<< service->getClassname()
206 <<
" , service is stopped = "<< ( service->isStopped() ?
"yes" :
"no" ) << std::endl;
213 void ObjectService::registerService( ::fwServices::IService::sptr service )
216 m_container.insert( ServiceContainerType::value_type( ::fwData::Object::wptr(), service ) );
221 void ObjectService::registerService( ::fwData::Object::sptr
object, ::fwServices::IService::sptr service )
224 FW_DEPRECATED_MSG(
"'ObjectService::registerSerice(object, service)' is deprecated. Use " 225 "'ObjectService::registerService(object, key, INOUT, service)' or " 226 "service->registerInOut(object, key)'.",
"20.0");
227 this->internalRegisterService(
object, service,
"", ::fwServices::IService::AccessType::INOUT);
232 void ObjectService::registerService( ::fwData::Object::sptr
object, const ::fwServices::IService::KeyType& objKey,
233 ::fwServices::IService::AccessType access, ::fwServices::IService::sptr service)
236 this->internalRegisterService(
object, service, objKey, access);
241 void ObjectService::registerServiceInput( const ::fwData::Object::csptr&
object,
242 const ::fwServices::IService::KeyType& objKey,
243 const ::fwServices::IService::sptr& service)
246 this->internalRegisterServiceInput(
object, service, objKey);
251 void ObjectService::registerServiceOutput(::fwData::Object::sptr
object, const ::fwServices::IService::KeyType& objKey,
252 ::fwServices::IService::sptr service)
256 this->internalRegisterService(
object, service, objKey, ::fwServices::IService::AccessType::OUTPUT);
258 const bool hasID = service->hasObjectId(objKey);
259 OSLM_DEBUG_IF(
"No output is defined for '" + objKey +
"', the object is not emitted to the configuration", !hasID);
262 const auto id = service->getObjectId(objKey);
263 auto sig = this->signal<RegisterSignalType>(s_REGISTERED_SIG);
264 sig->asyncEmit(
object,
id);
270 void ObjectService::swapService( ::fwData::Object::sptr objDst, ::fwServices::IService::sptr service )
272 FW_DEPRECATED_MSG(
"'ObjectService::swapService(object, service)' is deprecated. Use " 273 "'service->swapKey(key, object)'.",
"20.0");
275 OSLM_ASSERT(
"Object "<< service->getObject()->getID()<<
" is not registered in OSR",
276 m_container.left.find(service->getObject()) != m_container.left.end());
278 OSLM_ASSERT(
"Service "<< service->getID()<<
" is not registered in OSR",
279 m_container.right.find(service) != m_container.right.end());
282 m_container.right.erase(service);
283 this->internalRegisterService(objDst, service,
"", ::fwServices::IService::AccessType::INOUT);
288 void ObjectService::unregisterService( ::fwServices::IService::sptr service )
292 SLM_ASSERT(
"The service ( " + service->getID() +
" ) must be stopped before being unregistered.",
293 service->isStopped() );
295 this->removeFromContainer( service );
296 service->m_inputsMap.clear();
297 service->m_inOutsMap.clear();
298 service->m_outputsMap.clear();
303 void ObjectService::unregisterService(const ::fwServices::IService::KeyType& objKey,
304 ::fwServices::IService::AccessType access,
305 ::fwServices::IService::sptr service )
309 ::fwData::Object::cwptr obj;
311 if(access == ::fwServices::IService::AccessType::INPUT)
313 obj = service->m_inputsMap[objKey];
315 else if(access == ::fwServices::IService::AccessType::INOUT)
317 obj = service->m_inOutsMap[objKey];
321 obj = service->m_outputsMap[objKey];
324 SLM_ASSERT(
"Object key '" + objKey +
"' not found in service '" + service->getID() +
"'", !obj.expired());
328 auto range = m_container.left.equal_range(obj);
329 for(
auto it = range.first; it != range.second; )
331 if(it->second == service)
333 it = m_container.left.erase(it);
344 auto range = m_container.right.equal_range(service);
345 for(
auto it = range.first; it != range.second; )
347 if(it->second.lock() == obj.lock())
349 it = m_container.right.erase(it);
358 if(access == ::fwServices::IService::AccessType::INPUT)
360 service->m_inputsMap.erase(objKey);
362 else if(access == ::fwServices::IService::AccessType::INOUT)
364 service->m_inOutsMap.erase(objKey);
368 service->m_outputsMap.erase(objKey);
374 void ObjectService::unregisterServiceOutput( const ::fwServices::IService::KeyType& objKey,
375 ::fwServices::IService::sptr service )
377 ::fwData::Object::wptr obj = service->m_outputsMap[objKey];
379 if (service->hasObjectId(objKey))
381 const auto id = service->getObjectId(objKey);
382 auto sig = this->signal<RegisterSignalType>(s_UNREGISTERED_SIG);
383 sig->asyncEmit(obj.lock(), id);
385 service->m_outputsMap.erase(objKey);
390 bool ObjectService::isRegistered(const ::fwServices::IService::KeyType& objKey,
391 ::fwServices::IService::AccessType access, IService::sptr service)
const 395 if(access == ::fwServices::IService::AccessType::INPUT)
397 return service->m_inputsMap.find(objKey) != service->m_inputsMap.end();
399 else if(access == ::fwServices::IService::AccessType::INOUT)
401 return service->m_inOutsMap.find(objKey) != service->m_inOutsMap.end();
405 return service->m_outputsMap.find(objKey) != service->m_outputsMap.end();
411 ::fwData::Object::csptr ObjectService::getRegistered(const ::fwServices::IService::KeyType& objKey,
412 ::fwServices::IService::AccessType access,
413 IService::sptr service)
const 417 if(access == ::fwServices::IService::AccessType::INPUT)
419 auto it = service->m_inputsMap.find(objKey);
420 if(it != service->m_inputsMap.end())
422 return it->second.lock();
425 else if(access == ::fwServices::IService::AccessType::INOUT)
427 auto it = service->m_inOutsMap.find(objKey);
428 if(it != service->m_inOutsMap.end())
430 return it->second.lock();
435 auto it = service->m_outputsMap.find(objKey);
436 if(it != service->m_outputsMap.end())
446 void ObjectService::internalRegisterService(::fwData::Object::sptr
object, ::fwServices::IService::sptr service,
447 const ::fwServices::IService::KeyType& objKey,
448 ::fwServices::IService::AccessType access)
450 SLM_ASSERT(
"Can't register a null service in OSR.", service);
451 SLM_ASSERT(
"Can't register a null object in OSR.",
object);
455 OSLM_ASSERT(
"Service "<< service->getID()<<
" has already a valid associated object",
456 service->m_associatedObject.expired() ||
object != service->m_associatedObject.lock());
459 OSLM_ASSERT(
"This service "<< service->getClassname()
460 <<
" is not valid for the object " <<
object->getClassname(),
461 ServiceFactory::getDefault()->checkServiceValidity(object->getClassname(), service->getClassname())
464 OSLM_ASSERT(
"Service "<< service->getID()<<
" already registered",
465 m_container.right.find(service) == m_container.right.end());
467 service->m_associatedObject = object;
471 if(service->m_inputsMap.empty() && service->m_inOutsMap.empty() && service->m_outputsMap.empty())
475 service->m_associatedObject = object;
478 if(access == ::fwServices::IService::AccessType::INPUT)
480 service->m_inputsMap[objKey] = object;
482 else if(access == ::fwServices::IService::AccessType::INOUT)
484 service->m_inOutsMap[objKey] = object;
488 service->m_outputsMap[objKey] = object;
491 m_container.insert( ServiceContainerType::value_type(
object, service ) );
496 void ObjectService::internalRegisterServiceInput(
const fwData::Object::csptr&
object,
const IService::sptr& service,
497 const ::fwServices::IService::KeyType& objKey)
499 SLM_ASSERT(
"Can't register a null service in OSR.", service);
500 SLM_ASSERT(
"Can't register a null object in OSR.",
object);
501 SLM_ASSERT(
"Can't register an input object without key.", !objKey.empty());
503 service->m_inputsMap[objKey] = object;
505 m_container.insert( ServiceContainerType::value_type(
object, service ) );
510 void ObjectService::removeFromContainer( ::fwServices::IService::sptr service )
513 OSLM_ASSERT(
"Unknown service "<<service->getID()<<
" in OSR",
514 m_container.right.find(service) != m_container.right.end());
515 m_container.right.erase(service);
520 ObjectService::ObjectVectorType ObjectService::getObjects()
const 522 ObjectVectorType objects;
525 for(
const ServiceContainerType::right_map::value_type& elt : m_container.right)
527 auto srvObjects = elt.first->getObjects();
528 std::move(srvObjects.begin(), srvObjects.end(), std::inserter(objects, objects.begin()));
530 SLM_WARN_IF(
"No object registered for the requested type of service", objects.empty() );
536 ObjectService::ServiceVectorType ObjectService::getServices( ::fwData::Object::sptr obj,
537 const std::string& serviceType )
const 539 FW_DEPRECATED_MSG(
"'ObjectService::getServices(object, srvType)' is deprecated.",
"20.0");
541 ServiceVectorType allServices = this->
getServices(obj);
542 ServiceVectorType services;
545 for(::fwServices::IService::sptr srv : allServices)
547 if( srv->isA(serviceType) )
549 services.insert( srv );
557 ObjectService::ServiceVectorType ObjectService::getServices(
const std::string& serviceType )
const 559 ServiceVectorType services;
561 const ServiceContainerType::right_map right = m_container.right;
562 for( ServiceContainerType::right_map::value_type elt : right)
564 ::fwServices::IService::sptr service = elt.first;
565 if ( service->isA(serviceType) )
567 services.insert( service );
570 SLM_DEBUG_IF(
"No service registered", services.empty());
576 ObjectService::ServiceVectorType ObjectService::getServices( ::fwData::Object::sptr obj )
const 578 FW_DEPRECATED_MSG(
"'ObjectService::getServices(object)' is deprecated.",
"20.0");
579 ServiceVectorType services;
582 ServiceContainerType::left_map::const_iterator firstElement = m_container.left.find(obj);
583 if(firstElement != m_container.left.end())
585 ServiceContainerType::left_map::const_iterator lastElement = m_container.left.upper_bound(obj);
586 for (
auto iter = firstElement; iter != lastElement; ++iter)
588 services.insert( iter->second );
596 bool ObjectService::has( ::fwData::Object::sptr obj,
const std::string& srvType)
const 598 FW_DEPRECATED_MSG(
"'ObjectService::has(object, srvType)' is deprecated.",
"20.0");
599 bool hasServices =
false;
601 ServiceContainerType::left_map::const_iterator firstElement = m_container.left.find(obj);
602 if( firstElement != m_container.left.end())
604 ServiceContainerType::left_map::const_iterator lastElement = m_container.left.upper_bound(obj);
605 for (
auto iter = firstElement; iter != lastElement; ++iter)
607 if( iter->second->isA(srvType))
Contains fwAtomsFilter::registry details.
std::set< std::shared_ptr< SERVICE > > getServices()
Wraps ObjectService::getServices.
FWSERVICES_API void unregisterService(::fwServices::IService::sptr service)
Remove the service (service) from the m_container.
#define OSLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...
#define OSLM_DEBUG_IF(message, cond)
#define SLM_TRACE_FUNC()
Trace contextual function signature.
#define SLM_DEBUG_IF(message, cond)
Namespace fwServices is dedicated to (mimic) the dynamic affectation of methods to (pure data) object...
FWSERVICES_API void unregisterServiceOutput(const ::fwServices::IService::KeyType &objKey,::fwServices::IService::sptr service)
Emit the signal 'unregistered'.
FWSERVICES_API bool has(::fwData::Object::sptr obj, const std::string &srvType)
return true if the object has at least one service of type srvType
FWSERVICES_API void swapService(::fwData::Object::sptr objDst,::fwServices::IService::sptr service)
Wraps ObjectService::swapService.
FWSERVICES_API std::string getRegistryInformation()
Wraps ObjectService::getRegistryInformation.
::boost::unique_lock< ReadWriteMutex > WriteLock
Defines a lock of write type for read/write mutex.
#define FW_DEPRECATED_MSG(message, version)
Use this macro when deprecating a function to warn the developer.
FWSERVICES_API::fwServices::registry::ObjectService::ObjectVectorType getObjects()
Wraps ObjectService::getObjects.
FWSERVICES_API::fwData::Object::csptr getRegistered(const ::fwServices::IService::KeyType &objKey,::fwServices::IService::AccessType access,::fwServices::IService::sptr service)
Return the object pointer of a key of a given service.
FWSERVICES_API void registerService(::fwServices::IService::sptr service)
Register the service alone.
#define SLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...
::boost::shared_lock< ReadWriteMutex > ReadLock
Defines a lock of read type for read/write mutex.
FWSERVICES_API void registerServiceOutput(::fwData::Object::sptr obj, const ::fwServices::IService::KeyType &objKey,::fwServices::IService::sptr service)
Emit the signal 'registered'.
FWSERVICES_API void registerServiceInput(::fwData::Object::csptr obj, const ::fwServices::IService::KeyType &objKey,::fwServices::IService::sptr service)
Register the service (service) for the input object (obj) at the given service key. It also updates IService::m_associatedObject of service to point to obj removal at obj destruction.
FWSERVICES_API bool isRegistered(const ::fwServices::IService::KeyType &objKey,::fwServices::IService::AccessType access,::fwServices::IService::sptr service)
Return true if a key is registered for a given service.
#define SLM_WARN_IF(message, cond)