fw4spl
Add.hxx
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #pragma once
8 
9 #include "fwServices/IService.hpp"
10 #include "fwServices/registry/ObjectService.hpp"
11 #include "fwServices/registry/ServiceFactory.hpp"
12 
13 namespace fwServices
14 {
15 
16 //------------------------------------------------------------------------------
17 
18 template<class SERVICE>
19 SPTR(SERVICE) add( ::fwData::Object::csptr _obj, const std::string& _implType, const std::string& _id)
20 {
21  FW_DEPRECATED_MSG("'fwServices::add(object, srvImpl[,uid])' is deprecated.", "20.0");
22 
23  std::string serviceType = ::fwCore::TypeDemangler< SERVICE >().getClassname();
24  // TODO: Remove this ConstCast ?
25  auto const notConstObj = ::fwData::Object::constCast(_obj);
26  ::fwServices::IService::sptr service = ::fwServices::add( notConstObj, serviceType, _implType, _id );
27  SLM_ASSERT("Failed to add " + _implType, service );
28  SPTR(SERVICE) castedService = std::dynamic_pointer_cast< SERVICE >( service );
29  SLM_ASSERT("DynamicCast failed", castedService );
30  return castedService;
31 }
32 
33 //------------------------------------------------------------------------------
34 
35 template<class SERVICE>
36 SPTR(SERVICE) add(const std::string& _implType, const std::string& _id)
37 {
38  ::fwServices::IService::sptr genericSrv = ::fwServices::add( _implType, _id );
39  auto srv = std::dynamic_pointer_cast< SERVICE >(genericSrv);
40  FW_RAISE_IF("Failed to cast service from factory type '" + _implType + "' into '" +
41  ::fwCore::TypeDemangler<SERVICE>().getClassname() + "'", !srv );
42 
43  return srv;
44 }
45 
46 //------------------------------------------------------------------------------
47 
48 }
#define SPTR(_cls_)
STL namespace.
Namespace fwServices is dedicated to (mimic) the dynamic affectation of methods to (pure data) object...
#define FW_DEPRECATED_MSG(message, version)
Use this macro when deprecating a function to warn the developer.
Definition: spyLog.hpp:360
Type demangler. Use Demangler class to get a demangled string for the type T.
Definition: Demangler.hpp:95
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
Contains the representation of the data objects used in the framework.
FWSERVICES_API::fwServices::IService::sptr add(::fwData::Object::sptr obj, const std::string &serviceType, const std::string &_implType, const std::string &_id="")
Create and attach to the object obj a service of type serviceType, implementation _implementationId w...
Definition: Add.cpp:24