fw4spl
SrcLib/core/fwData/include/fwData/Object.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_OBJECT_HPP__
8 #define __FWDATA_OBJECT_HPP__
9 
10 #include "fwData/config.hpp"
11 #include "fwData/factory/new.hpp"
12 #include "fwData/registry/detail.hpp"
13 
14 #include <fwCamp/macros.hpp>
15 #ifdef CAMP_COMPILATION
16 #include <fwCamp/Mapper/ArrayMapper.hpp>
17 #include <fwCamp/camp/ExtendedClassVisitor.hpp>
18 #include <fwCamp/camp/MapMapper.hpp>
19 #include <fwCamp/camp/MapProperty.hpp>
20 #include <fwCamp/camp/MapValueMapper.hpp>
21 #include <fwCamp/camp/customtype.hpp>
22 #include <fwCamp/camp/detail/MapPropertyImpl.hpp>
23 #endif
24 
25 #include <fwCom/HasSignals.hpp>
26 #include <fwCom/Signal.hpp>
27 
28 #include <fwCore/base.hpp>
29 #include <fwCore/mt/types.hpp>
30 
31 #include <fwTools/Object.hpp>
32 
33 #include <string>
34 #include <unordered_map>
35 
36 fwCampAutoDeclareDataMacro((fwData)(Object), FWDATA_API);
37 
38 namespace fwData
39 {
40 
48 class FWDATA_CLASS_API Object : public ::fwTools::Object,
49  public ::fwCom::HasSignals
50 {
51 public:
52 
53  typedef ::fwData::factory::Key Key;
54 
57  fwCampMakeFriendDataMacro((fwData)(Object));
58 
65  template <typename T>
66  class Registrar
67  {
68  public:
69  Registrar()
70  {
71  ::fwData::registry::get()->addFactory(T::classname(), &::fwData::factory::New<T>);
72  }
73  };
74 
79  typedef ::fwCom::Signal< void () > ModifiedSignalType;
81 
83  FWDATA_API static const ::fwCom::Signals::SignalKeyType s_MODIFIED_SIG;
84 
85  typedef std::map<std::string, ::fwData::Object::sptr> FieldsContainerType;
87  typedef ::fwCom::Signal< void (FieldsContainerType) > AddedFieldsSignalType;
88  FWDATA_API static const ::fwCom::Signals::SignalKeyType s_ADDED_FIELDS_SIG;
89 
91  typedef ::fwCom::Signal< void (FieldsContainerType, FieldsContainerType) > ChangedFieldsSignalType;
92  FWDATA_API static const ::fwCom::Signals::SignalKeyType s_CHANGED_FIELDS_SIG;
93 
95  typedef ::fwCom::Signal< void (FieldsContainerType) > RemovedFieldsSignalType;
96  FWDATA_API static const ::fwCom::Signals::SignalKeyType s_REMOVED_FIELDS_SIG;
101  typedef std::string FieldNameType;
102  typedef std::vector<FieldNameType> FieldNameVectorType;
103  typedef ::std::unordered_map< FieldNameType, ::fwData::Object::sptr > FieldMapType;
104 
105  typedef ::std::unordered_map< ::fwData::Object::csptr, ::fwData::Object::sptr > DeepCopyCacheType;
106 
113  FWDATA_API ::fwData::Object::sptr getField( const FieldNameType& name,
114  ::fwData::Object::sptr defaultValue = ::fwData::Object::sptr() ) const;
115 
121  template< typename DATA_TYPE >
122  SPTR(DATA_TYPE) getField( const FieldNameType &name ) const;
123 
130  template< typename DATA_TYPE >
131  SPTR(DATA_TYPE) getField( const FieldNameType &name, SPTR(DATA_TYPE) defaultValue ) const;
132 
140  template< typename DATA_TYPE >
141  SPTR(DATA_TYPE) setDefaultField( const FieldNameType &name, SPTR(DATA_TYPE) defaultValue );
142 
148  FWDATA_API ::fwData::Object::csptr getConstField( const FieldNameType& name ) const;
149 
153  FWDATA_API const FieldMapType& getFields() const;
154 
158  FWDATA_API FieldNameVectorType getFieldNames() const;
159 
165  FWDATA_API void setField( const FieldNameType& name, ::fwData::Object::sptr obj );
166 
170  FWDATA_API void setFields( const FieldMapType& fieldMap );
171 
176  FWDATA_API void removeField( const FieldNameType& name );
177 
182  FWDATA_API virtual void shallowCopy( const ::fwData::Object::csptr& source );
183 
189  FWDATA_API void deepCopy( const ::fwData::Object::csptr& source );
190 
195  FWDATA_API static ::fwData::Object::sptr copy(const ::fwData::Object::csptr& source);
196  template <typename DATA_TYPE>
197  static SPTR(DATA_TYPE) copy(const CSPTR(DATA_TYPE) &source);
198  template <typename DATA_TYPE>
199  static SPTR(DATA_TYPE) copy(const SPTR(DATA_TYPE) &source);
206  FWDATA_API void fieldShallowCopy( const ::fwData::Object::csptr& source );
207 
212  FWDATA_API void fieldDeepCopy( const ::fwData::Object::csptr& source );
213 
214  //-----------------------------------------------------------------------------
215 
217  ::fwCore::mt::ReadWriteMutex &getMutex() const
218  {
219  return m_mutex;
220  }
221 
222  FWDATA_API virtual ~Object();
223 
224 protected:
225 
226  FWDATA_API Object();
227 
232  FWDATA_API static ::fwData::Object::sptr copy(const ::fwData::Object::csptr& source, DeepCopyCacheType& cache);
233  FWDATA_API void fieldDeepCopy( const ::fwData::Object::csptr& source, DeepCopyCacheType& cache );
234  FWDATA_API virtual void cachedDeepCopy(const ::fwData::Object::csptr& source, DeepCopyCacheType& cache) = 0;
235  template <typename DATA_TYPE>
236  static SPTR(DATA_TYPE) copy(const CSPTR(DATA_TYPE) &source, DeepCopyCacheType &cache);
237  template <typename DATA_TYPE>
238  static SPTR(DATA_TYPE) copy(const SPTR(DATA_TYPE) &source, DeepCopyCacheType &cache);
241  FieldMapType m_fields;
243 
245  mutable ::fwCore::mt::ReadWriteMutex m_mutex;
246 };
247 
248 template <typename DATA_TYPE>
249 SPTR(DATA_TYPE) Object::copy(const CSPTR(DATA_TYPE) &source, DeepCopyCacheType &cache)
250 {
251  return DATA_TYPE::dynamicCast( ::fwData::Object::copy(::fwData::Object::csptr(source), cache) );
252 }
253 
254 template <typename DATA_TYPE>
255 SPTR(DATA_TYPE) Object::copy(const SPTR(DATA_TYPE) &source, DeepCopyCacheType &cache)
256 {
257  return DATA_TYPE::dynamicCast( ::fwData::Object::copy(::fwData::Object::csptr(source), cache) );
258 }
259 
260 template <typename DATA_TYPE>
261 SPTR(DATA_TYPE) Object::copy(const CSPTR(DATA_TYPE) &source)
262 {
263  return DATA_TYPE::dynamicCast( ::fwData::Object::copy(::fwData::Object::csptr(source)) );
264 }
265 
266 template <typename DATA_TYPE>
267 SPTR(DATA_TYPE) Object::copy(const SPTR(DATA_TYPE) &source)
268 {
269  return DATA_TYPE::dynamicCast( ::fwData::Object::copy(::fwData::Object::csptr(source)) );
270 }
271 
272 //-----------------------------------------------------------------------------
273 
274 template< typename DATA_TYPE >
275 SPTR(DATA_TYPE) Object::getField( const FieldNameType &name ) const
276 {
277  ::fwData::Object::sptr field;
278  field = this->getField( name, field );
279  SPTR(DATA_TYPE) result = DATA_TYPE::dynamicCast( field );
280  return result;
281 }
282 
283 //-----------------------------------------------------------------------------
284 
285 template< typename DATA_TYPE >
286 SPTR(DATA_TYPE) Object::getField( const FieldNameType &name, SPTR(DATA_TYPE) defaultValue ) const
287 {
288  ::fwData::Object::sptr field = defaultValue;
289  field = this->getField( name, field );
290  SPTR(DATA_TYPE) result = DATA_TYPE::dynamicCast( field );
291  return result;
292 }
293 
294 //-----------------------------------------------------------------------------
295 
296 template< typename DATA_TYPE >
297 SPTR(DATA_TYPE) Object::setDefaultField( const FieldNameType &name, SPTR(DATA_TYPE) defaultValue )
298 {
299  SPTR(DATA_TYPE) result = getField< DATA_TYPE >(name);
300  if( !result && defaultValue)
301  {
302  result = defaultValue;
303  this->setField(name, defaultValue);
304  }
305  return result;
306 }
307 
308 } // namespace fwData
309 
310 #endif //__FWDATA_OBJECT_HPP__
#define SPTR(_cls_)
#define CSPTR(_cls_)
#define fwCoreNonInstanciableClassDefinitionsMacro(_classinfo_)
Generate common code for Non Instanciable classes (Interfaces, Abstract classes, ...)
Define Base class for FW4SPL objects and services.
::fwCom::Signal< void(FieldsContainerType) > AddedFieldsSignalType
Type of signal when objects are added.
mutable::fwCore::mt::ReadWriteMutex m_mutex
Mutex to protect object access.
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
This namespace fwCore provides common foundations for FW4SPL.
Definition: BaseObject.hpp:16
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_CHANGED_FIELDS_SIG
Type of signal m_sigModified.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_ADDED_FIELDS_SIG
Type of signal m_sigModified.
std::map< std::string,::fwData::Object::sptr > FieldsContainerType
Type of signal m_sigModified.
Class used to register a class factory in factory registry. This class defines also the object factor...
::fwCom::Signal< void(FieldsContainerType, FieldsContainerType) > ChangedFieldsSignalType
Type of signal when objects are changed (newObjects, oldObjects)
static FWDATA_API::fwData::Object::sptr copy(const ::fwData::Object::csptr &source)
return a copy of the source. if source is a null pointer, return a null pointer.
Base class for each data object.
#define fwCoreAllowSharedFromThis()
Generate getSptr and getConstSptr methods.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_MODIFIED_SIG
Key in m_signals map of signal m_sigModified.
::fwCom::Signal< void(FieldsContainerType) > RemovedFieldsSignalType
Type of signal when objects are removed.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_REMOVED_FIELDS_SIG
Type of signal m_sigModified.
Contains the representation of the data objects used in the framework.
This class proposes a mapping between a SignalKeyType and a SignalBase.
Definition: HasSignals.hpp:21