fw4spl
SrcLib/core/fwData/include/fwData/Composite.hpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
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 #ifndef __FWDATA_COMPOSITE_HPP__
8 #define __FWDATA_COMPOSITE_HPP__
9 
10 #include "fwData/config.hpp"
11 #include "fwData/factory/new.hpp"
12 #include "fwData/Object.hpp"
13 
14 #include <fwCom/Signal.hpp>
15 #include <fwCom/Signals.hpp>
16 
17 #include <map>
18 
19 namespace fwData
20 {
21 class Composite;
22 }
23 
24 fwCampAutoDeclareDataMacro((fwData)(Composite), FWDATA_API);
25 
26 namespace fwData
27 {
28 
35 class FWDATA_CLASS_API Composite : public Object
36 {
37 public:
38  fwCoreClassDefinitionsWithFactoryMacro( (Composite)(::fwData::Object), (()), ::fwData::factory::New< Composite >);
39  fwCampMakeFriendDataMacro((fwData)(Composite));
40 
41  typedef std::map< std::string, ::fwData::Object::sptr > ContainerType;
42 
43  typedef ContainerType::key_type KeyType;
44  typedef ContainerType::mapped_type MappedType;
45  typedef ContainerType::value_type ValueType;
46  typedef ContainerType::iterator IteratorType;
47  typedef ContainerType::const_iterator ConstIteratorType;
48  typedef ContainerType::reverse_iterator ReverseIteratorType;
49  typedef ContainerType::const_reverse_iterator ConstReverseIteratorType;
50  typedef ContainerType::size_type SizeType;
51 
54  typedef ContainerType::key_type key_type;
55  typedef ContainerType::mapped_type mapped_type;
56  typedef ContainerType::value_type value_type;
57  typedef ContainerType::iterator iterator;
58  typedef ContainerType::const_iterator const_iterator;
59  typedef ContainerType::reverse_iterator reverse_iterator;
60  typedef ContainerType::const_reverse_iterator const_reverse_iterator;
61  typedef ContainerType::size_type size_type;
62 
67  FWDATA_API Composite( ::fwData::Object::Key key );
68 
70  FWDATA_API virtual ~Composite();
71 
72  IteratorType begin();
73  IteratorType end();
74  ConstIteratorType begin() const;
75  ConstIteratorType end() const;
76 
77  ReverseIteratorType rbegin();
78  ReverseIteratorType rend();
79  ConstReverseIteratorType rbegin() const;
80  ConstReverseIteratorType rend() const;
81 
82  bool empty() const;
83  SizeType size() const;
84 
85  mapped_type& operator[] ( KeyType n );
86 
87  IteratorType find ( const KeyType& x );
88  ConstIteratorType find ( const KeyType& x ) const;
89 
90  SizeType count ( const KeyType& x ) const;
92 
95  ContainerType& getContainer();
96  const ContainerType& getContainer () const;
97  void setContainer (const ContainerType& val);
99 
101  FWDATA_API void shallowCopy( const Object::csptr& _source ) override;
102 
104  FWDATA_API void cachedDeepCopy(const Object::csptr& _source, DeepCopyCacheType& cache) override;
105 
107  template< class DATATYPE >
108  void setDataContainer( const std::map< std::string, SPTR(DATATYPE) >& map );
109 
111  template< class DATATYPE >
112  std::map< std::string, SPTR(DATATYPE) > getDataContainer() const;
113 
122  template< class DATATYPE >
123  SPTR(DATATYPE) at(const std::string& key);
124 
133  template< class DATATYPE >
134  CSPTR(DATATYPE) at(const std::string& key) const;
135 
140  typedef ::fwCom::Signal< void (ContainerType) > AddedObjectsSignalType;
142  FWDATA_API static const ::fwCom::Signals::SignalKeyType s_ADDED_OBJECTS_SIG;
143 
145  typedef ::fwCom::Signal< void (ContainerType, ContainerType) > ChangedObjectsSignalType;
146  FWDATA_API static const ::fwCom::Signals::SignalKeyType s_CHANGED_OBJECTS_SIG;
147 
149  typedef ::fwCom::Signal< void (ContainerType) > RemovedObjectsSignalType;
150  FWDATA_API static const ::fwCom::Signals::SignalKeyType s_REMOVED_OBJECTS_SIG;
155 protected:
156  ContainerType m_container;
157 };
158 
159 //-----------------------------------------------------------------------------
160 
161 inline Composite::IteratorType Composite::begin()
162 {
163  return m_container.begin();
164 }
165 
166 //-----------------------------------------------------------------------------
167 
168 inline Composite::IteratorType Composite::end()
169 {
170  return m_container.end();
171 }
172 
173 //-----------------------------------------------------------------------------
174 
175 inline Composite::ConstIteratorType Composite::begin() const
176 {
177  return m_container.begin();
178 }
179 
180 //-----------------------------------------------------------------------------
181 
182 inline Composite::ConstIteratorType Composite::end() const
183 {
184  return m_container.end();
185 }
186 
187 //-----------------------------------------------------------------------------
188 
189 inline Composite::ReverseIteratorType Composite::rbegin()
190 {
191  return m_container.rbegin();
192 }
193 
194 //-----------------------------------------------------------------------------
195 
196 inline Composite::ReverseIteratorType Composite::rend()
197 {
198  return m_container.rend();
199 }
200 
201 //-----------------------------------------------------------------------------
202 
203 inline Composite::ConstReverseIteratorType Composite::rbegin() const
204 {
205  return m_container.rbegin();
206 }
207 
208 //-----------------------------------------------------------------------------
209 
210 inline Composite::ConstReverseIteratorType Composite::rend() const
211 {
212  return m_container.rend();
213 }
214 
215 //-----------------------------------------------------------------------------
216 
217 inline bool Composite::empty() const
218 {
219  return m_container.empty();
220 }
221 
222 //-----------------------------------------------------------------------------
223 
224 inline Composite::SizeType Composite::size() const
225 {
226  return m_container.size();
227 }
228 
229 //-----------------------------------------------------------------------------
230 
231 inline Composite::mapped_type& Composite::operator[](Composite::KeyType n)
232 {
233  return this->m_container[n];
234 }
235 
236 //-----------------------------------------------------------------------------
237 
238 inline Composite::IteratorType Composite::find(const Composite::KeyType& x)
239 {
240  return m_container.find(x);
241 }
242 
243 //-----------------------------------------------------------------------------
244 
245 inline Composite::ConstIteratorType Composite::find(const Composite::KeyType& x) const
246 {
247  return m_container.find(x);
248 }
249 
250 //-----------------------------------------------------------------------------
251 
252 inline Composite::SizeType Composite::count(const Composite::KeyType& x) const
253 {
254  return m_container.count(x);
255 }
256 
257 //-----------------------------------------------------------------------------
258 
259 inline Composite::ContainerType& Composite::getContainer()
260 {
261  return m_container;
262 }
263 
264 //-----------------------------------------------------------------------------
265 
266 inline const Composite::ContainerType& Composite::getContainer () const
267 {
268  return m_container;
269 }
270 
271 //-----------------------------------------------------------------------------
272 
273 inline void Composite::setContainer (const Composite::ContainerType& val)
274 {
275  m_container = val;
276 }
277 
278 //-----------------------------------------------------------------------------
279 
280 template< class DATATYPE >
281 inline void Composite::setDataContainer( const std::map< std::string, SPTR(DATATYPE) >& map )
282 {
283  this->getContainer().clear();
284  this->getContainer().insert( map.begin(), map.end() );
285 }
286 
287 //-----------------------------------------------------------------------------
288 
289 template< class DATATYPE >
290 inline std::map< std::string, SPTR(DATATYPE) > Composite::getDataContainer() const
291 {
292  std::map< std::string, SPTR(DATATYPE) > map;
293  SPTR(DATATYPE) castData;
294  for( ::fwData::Composite::value_type elem : *this )
295  {
296  castData = std::dynamic_pointer_cast<DATATYPE>( elem.second );
297  OSLM_ASSERT("DynamicCast "<< ::fwCore::TypeDemangler<DATATYPE>().getClassname()<<" failed", castData);
298  map[elem.first] = castData;
299  }
300 
301  return map;
302 }
303 
304 //-----------------------------------------------------------------------------
305 
306 template< class DATATYPE >
307 SPTR(DATATYPE) Composite::at(const std::string& key)
308 {
309  SPTR(DATATYPE) castData;
310  ::fwData::Composite::iterator iter = this->find(key);
311  if(iter != this->end())
312  {
313  castData = std::dynamic_pointer_cast<DATATYPE>(iter->second);
314  SLM_TRACE_IF("DynamicCast "+ ::fwCore::TypeDemangler<DATATYPE>().getClassname()+" failed", !castData);
315  }
316  else
317  {
318  SLM_TRACE( "Object '" + key + "' not found.");
319  }
320  return castData;
321 }
322 
323 //-----------------------------------------------------------------------------
324 
325 template< class DATATYPE >
326 CSPTR(DATATYPE) Composite::at(const std::string& key) const
327 {
328  CSPTR(DATATYPE) castData;
329  ::fwData::Composite::const_iterator iter = this->find(key);
330  if(iter != this->end())
331  {
332  castData = std::dynamic_pointer_cast<DATATYPE>(iter->second);
333  SLM_TRACE_IF("DynamicCast "+ ::fwCore::TypeDemangler<DATATYPE>().getClassname()+" failed", !castData);
334  }
335  else
336  {
337  SLM_TRACE( "Object '" + key + "' not found.");
338  }
339  return castData;
340 }
341 
342 //-----------------------------------------------------------------------------
343 
344 } //namespace fwData
345 
346 #endif /* __FWDATA_COMPOSITE_HPP__ */
347 
#define SPTR(_cls_)
#define CSPTR(_cls_)
#define SLM_TRACE_IF(message, cond)
Definition: spyLog.hpp:232
void setDataContainer(const std::map< std::string, std::shared_ptr< DATATYPE > > &map)
Method to initialize a fwData::Composite from a std::map< string, X >
ContainerType::const_reverse_iterator const_reverse_iterator
#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 containing fw4spl communication tools.
Definition: DumpEditor.hpp:30
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
STL namespace.
SizeType count(const KeyType &x) const
std::map< std::string, std::shared_ptr< DATATYPE > > getDataContainer() const
Method to get a std::map< string, X > from fwData::Composite.
#define fwCoreClassDefinitionsWithFactoryMacro(_classinfo_, _parameters_, _factory_)
Generate common construction methods for classes with one factory.
Type demangler. Use Demangler class to get a demangled string for the type T.
Definition: Demangler.hpp:95
void setContainer(const ContainerType &val)
get/set the map of std::string/fwData::Object
ContainerType::const_iterator const_iterator
Base class for each data object.
#define SLM_TRACE(message)
Definition: spyLog.hpp:228
Contains the representation of the data objects used in the framework.
This class defines a composite object.
ContainerType::reverse_iterator reverse_iterator
ContainerType & getContainer()
get/set the map of std::string/fwData::Object