fw4spl
IService.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 
11 namespace fwServices
12 {
13 
14 //------------------------------------------------------------------------------
15 
16 template< class DATATYPE >
17 SPTR(DATATYPE) IService::getObject()
18 {
19  FW_DEPRECATED("getObject()", "getInput() or getInOut()", "20.0");
20 
21  SPTR(DATATYPE) castData = std::dynamic_pointer_cast<DATATYPE>( m_associatedObject.lock() );
22  OSLM_ASSERT("DynamicCast " << ::fwCore::TypeDemangler<DATATYPE>().getClassname() << " failed", castData);
23 
24  return castData;
25 }
26 
27 //------------------------------------------------------------------------------
28 
29 template< class DATATYPE >
30 CSPTR(DATATYPE) IService::getInput(const KeyType& key) const
31 {
32  CSPTR(DATATYPE) input;
33 
34  auto iterator = m_inputsMap.find(key);
35  if(iterator != m_inputsMap.end())
36  {
37  input = std::dynamic_pointer_cast<const DATATYPE>( iterator->second.lock() );
38  OSLM_ASSERT("DynamicCast " << ::fwCore::TypeDemangler<DATATYPE>().getClassname() << " failed", input);
39  }
40 
41  return input;
42 }
43 
44 //------------------------------------------------------------------------------
45 
46 template< class DATATYPE >
47 SPTR(DATATYPE) IService::getInOut(const KeyType& key) const
48 {
49  SPTR(DATATYPE) inout;
50  auto iterator = m_inOutsMap.find(key);
51  if(iterator != m_inOutsMap.end())
52  {
53  inout = std::dynamic_pointer_cast<DATATYPE>( iterator->second.lock() );
54  OSLM_ASSERT("DynamicCast " << ::fwCore::TypeDemangler<DATATYPE>().getClassname() << " failed", inout);
55  }
56 
57  return inout;
58 }
59 
60 //------------------------------------------------------------------------------
61 
62 template< class DATATYPE >
63 SPTR(DATATYPE) IService::getOutput(const KeyType& key) const
64 {
65  SPTR(DATATYPE) output;
66  auto iterator = m_outputsMap.find(key);
67  if(iterator != m_outputsMap.end())
68  {
69  output = std::dynamic_pointer_cast<DATATYPE>( iterator->second );
70  OSLM_ASSERT("DynamicCast " << ::fwCore::TypeDemangler<DATATYPE>().getClassname() << " failed", output);
71  }
72 
73  return output;
74 }
75 
76 //------------------------------------------------------------------------------
77 
78 template< class DATATYPE >
79 CSPTR(DATATYPE) IService::getInput(const KeyType& keybase, size_t index) const
80 {
81 # ifdef _DEBUG
82  auto it = m_keyGroupSize.find(keybase);
83 #endif
84  SLM_ASSERT("Key group '" + keybase + "' not found", it != m_keyGroupSize.end());
85  OSLM_ASSERT("Index overflow '" << index << " >= " << it->second << "' in key group '" << keybase << ".",
86  index < it->second);
87  return this->getInput< DATATYPE >(KEY_GROUP_NAME(keybase, index));
88 }
89 
90 //------------------------------------------------------------------------------
91 
92 template< class DATATYPE >
93 SPTR(DATATYPE) IService::getInOut(const KeyType& keybase, size_t index) const
94 {
95 # ifdef _DEBUG
96  auto it = m_keyGroupSize.find(keybase);
97 #endif
98  SLM_ASSERT("Key group '" + keybase + "' not found", it != m_keyGroupSize.end());
99  OSLM_ASSERT("Index overflow '" << index << " >= " << it->second << "' in key group '" << keybase << ".",
100  index < it->second);
101  return this->getInOut< DATATYPE >(KEY_GROUP_NAME(keybase, index));
102 }
103 //------------------------------------------------------------------------------
104 
105 template< class DATATYPE >
106 SPTR(DATATYPE) IService::getOutput(const KeyType& keybase, size_t index) const
107 {
108 # ifdef _DEBUG
109  auto it = m_keyGroupSize.find(keybase);
110 #endif
111  SLM_ASSERT("Key group '" + keybase + "' not found", it != m_keyGroupSize.end());
112  OSLM_ASSERT("Index overflow '" << index << " >= " << it->second << "' in key group '" << keybase << ".",
113  index < it->second);
114  return this->getOutput< DATATYPE >(KEY_GROUP_NAME(keybase, index));
115 }
116 
117 //------------------------------------------------------------------------------
118 
119 inline size_t IService::getKeyGroupSize(const IService::KeyType& keybase) const
120 {
121  auto it = m_keyGroupSize.find(keybase);
122  if(it != m_keyGroupSize.end())
123  {
124  return it->second;
125  }
126  else
127  {
128  return 0;
129  }
130 }
131 
132 //------------------------------------------------------------------------------
133 
134 inline const IService::InputMapType& IService::getInputs() const
135 {
136  return m_inputsMap;
137 }
138 
139 //------------------------------------------------------------------------------
140 
141 inline const IService::InOutMapType& IService::getInOuts() const
142 {
143  return m_inOutsMap;
144 }
145 
146 //------------------------------------------------------------------------------
147 
148 inline const IService::OutputMapType& IService::getOutputs() const
149 {
150  return m_outputsMap;
151 }
152 
153 //------------------------------------------------------------------------------
154 
155 inline std::vector< ::fwData::Object::csptr > IService::getObjects() const
156 {
157  std::vector< ::fwData::Object::csptr > objectsVector;
158  if(!m_inputsMap.empty() || !m_inOutsMap.empty() || !m_outputsMap.empty())
159  {
160  for(auto itObj : m_inputsMap)
161  {
162  objectsVector.push_back(itObj.second.lock());
163  }
164  for(auto itObj : m_inOutsMap)
165  {
166  objectsVector.push_back(itObj.second.lock());
167  }
168  for(auto itObj : m_outputsMap)
169  {
170  objectsVector.push_back(itObj.second);
171  }
172  }
173  else
174  {
175  objectsVector.push_back(m_associatedObject.lock());
176  }
177 
178  return objectsVector;
179 }
180 
181 //------------------------------------------------------------------------------
182 
183 } // namespace fwServices
Base class for all services.
Definition: IService.hpp:61
#define SPTR(_cls_)
#define CSPTR(_cls_)
FWSERVICES_API std::vector< ::fwData::Object::csptr > getObjects() const
Return the objects associated to service.
Definition: IService.hxx:155
FWSERVICES_API const InOutMapType & getInOuts() const
Return the inouts map associated to service.
Definition: IService.hxx:141
FWSERVICES_API const OutputMapType & getOutputs() const
Return the outouts map associated to service.
Definition: IService.hxx:148
#define OSLM_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:310
Namespace fwServices is dedicated to (mimic) the dynamic affectation of methods to (pure data) object...
FWSERVICES_API const InputMapType & getInputs() const
Return the inputs map associated to service.
Definition: IService.hxx:134
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
size_t getKeyGroupSize(const KeyType &keybase) const
Return the number of key in a group of keys.
Definition: IService.hxx:119
::fwData::Object::wptr m_associatedObject
associated object of service
Definition: IService.hpp:676
#define FW_DEPRECATED(oldFnName, newFnName, version)
Use this macro when deprecating a function to warn the developer.
Definition: spyLog.hpp:344