fw4spl
SrcLib/core/fwData/src/fwData/Object.cpp
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 //FIXME :This needs to be include first for GCC
8 #include <fwCom/Signal.hpp>
9 #include <fwCom/Signal.hxx>
10 //
11 #include "fwData/factory/new.hpp"
12 #include "fwData/Object.hpp"
13 
14 #include <functional>
15 
16 namespace fwData
17 {
18 
19 //------------------------------------------------------------------------------
20 
21 const ::fwCom::Signals::SignalKeyType Object::s_MODIFIED_SIG = "modified";
22 const ::fwCom::Signals::SignalKeyType Object::s_ADDED_FIELDS_SIG = "addedFields";
23 const ::fwCom::Signals::SignalKeyType Object::s_CHANGED_FIELDS_SIG = "changedFields";
24 const ::fwCom::Signals::SignalKeyType Object::s_REMOVED_FIELDS_SIG = "removedFields";
25 
26 //------------------------------------------------------------------------------
27 
28 Object::Object()
29 {
30  newSignal< ModifiedSignalType >(s_MODIFIED_SIG);
31  newSignal< AddedFieldsSignalType >(s_ADDED_FIELDS_SIG);
32  newSignal< ChangedFieldsSignalType >(s_CHANGED_FIELDS_SIG);
33  newSignal< RemovedFieldsSignalType >(s_REMOVED_FIELDS_SIG);
34 
35 }
36 
37 //------------------------------------------------------------------------------
38 
39 Object::~Object()
40 {
41 }
42 
43 //------------------------------------------------------------------------------
44 
45 ::fwData::Object::sptr Object::getField( const FieldNameType& name, ::fwData::Object::sptr defaultValue ) const
46 {
47  ::fwData::Object::sptr object = defaultValue;
48  FieldMapType::const_iterator iter = m_fields.find(name);
49  if(iter != m_fields.end())
50  {
51  object = iter->second;
52  }
53  return object;
54 }
55 
56 //------------------------------------------------------------------------------
57 
58 ::fwData::Object::csptr Object::getConstField( const FieldNameType& name ) const
59 {
60  return this->getField(name);
61 }
62 
63 //------------------------------------------------------------------------------
64 
65 const Object::FieldMapType& Object::getFields() const
66 {
67  return m_fields;
68 }
69 
70 //------------------------------------------------------------------------------
71 
72 Object::FieldNameVectorType Object::getFieldNames() const
73 {
74  FieldNameVectorType names;
75  std::transform( m_fields.begin(), m_fields.end(),
76  std::back_inserter(names),
77  std::bind(&FieldMapType::value_type::first, std::placeholders::_1) );
78  return names;
79 }
80 
81 //------------------------------------------------------------------------------
82 
83 void Object::setField( const FieldNameType& name, ::fwData::Object::sptr obj)
84 {
85  std::pair<FieldMapType::iterator, bool> res = m_fields.insert(FieldMapType::value_type(name, obj));
86  if( !res.second )
87  {
88  res.first->second = obj;
89  }
90 }
91 
92 //------------------------------------------------------------------------------
93 
94 void Object::setFields( const FieldMapType& fieldMap )
95 {
96  m_fields = fieldMap;
97 }
98 
99 //------------------------------------------------------------------------------
100 
101 void Object::removeField( const FieldNameType& name )
102 {
103  FieldMapType::const_iterator iter = m_fields.find(name);
104  OSLM_ASSERT("Field "<<name<<" not found.", iter != m_fields.end());
105  if(iter != m_fields.end())
106  {
107  m_fields.erase(iter);
108  }
109 }
110 
111 //-----------------------------------------------------------------------------
112 
113 void Object::fieldShallowCopy(const ::fwData::Object::csptr& source)
114 {
115  this->setFields(source->getFields());
116 }
117 
118 //-----------------------------------------------------------------------------
119 
120 void Object::deepCopy(const ::fwData::Object::csptr& source)
121 {
122  DeepCopyCacheType cache;
123  return this->cachedDeepCopy(source, cache);
124 }
125 //-----------------------------------------------------------------------------
126 
127 void Object::fieldDeepCopy(const ::fwData::Object::csptr& source)
128 {
129  DeepCopyCacheType cache;
130  return this->fieldDeepCopy(source, cache);
131 }
132 
133 //-----------------------------------------------------------------------------
134 
135 void Object::fieldDeepCopy(const ::fwData::Object::csptr& source, DeepCopyCacheType& cache)
136 {
137  m_fields.clear();
138  const ::fwData::Object::FieldMapType& sourceFields = source->getFields();
139  for(const ::fwData::Object::FieldMapType::value_type& elt : sourceFields)
140  {
141  this->setField(elt.first, ::fwData::Object::copy(elt.second, cache));
142  }
143 }
144 
145 //-----------------------------------------------------------------------------
146 
147 void Object::shallowCopy(const ::fwData::Object::csptr& source )
148 {
149  FwCoreNotUsedMacro(source);
150  SLM_FATAL("shallowCopy not implemented for : " + this->getClassname() );
151 }
152 
153 //-----------------------------------------------------------------------------
154 
155 ::fwData::Object::sptr Object::copy(const ::fwData::Object::csptr& source)
156 {
157  DeepCopyCacheType cache;
158  return Object::copy(source, cache);
159 }
160 
161 //-----------------------------------------------------------------------------
162 
163 ::fwData::Object::sptr Object::copy(const ::fwData::Object::csptr& source, Object::DeepCopyCacheType& cache)
164 {
165  ::fwData::Object::sptr obj;
166 
167  if( source )
168  {
169  DeepCopyCacheType::const_iterator cacheItem = cache.find(source);
170 
171  if (cacheItem == cache.end())
172  {
173  obj = ::fwData::factory::New(source->getClassname());
174  cache.insert( DeepCopyCacheType::value_type(source, obj) );
175  obj->cachedDeepCopy(source, cache);
176  }
177  else
178  {
179  obj = cacheItem->second;
180  }
181  }
182 
183  return obj;
184 }
185 
186 //-----------------------------------------------------------------------------
187 
188 } // namespace fwData
virtual FWDATA_API void shallowCopy(const ::fwData::Object::csptr &source)
A shallow copy of fields (objects in m_children)
#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
FWDATA_API const FieldMapType & getFields() const
Returns fields map.
FWDATA_API void removeField(const FieldNameType &name)
Removes field with specified name.
virtual const std::string & getClassname() const override
return full object&#39;s classname with its namespace, i.e. fwCore::BaseObject
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_CHANGED_FIELDS_SIG
Type of signal m_sigModified.
FWDATA_API::fwData::Object::sptr getField(const FieldNameType &name,::fwData::Object::sptr defaultValue=::fwData::Object::sptr()) const
Returns a pointer of corresponding field (null if non exist).
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_ADDED_FIELDS_SIG
Type of signal m_sigModified.
FWDATA_API::fwData::Object::csptr getConstField(const FieldNameType &name) const
Returns a pointer of corresponding field (null if non exist).
virtual FWDATA_API void cachedDeepCopy(const ::fwData::Object::csptr &source, DeepCopyCacheType &cache)=0
Internal-use methods to implement Object&#39;s deepCopy.
#define SLM_FATAL(message)
Definition: spyLog.hpp:283
FWDATA_API FieldNameVectorType getFieldNames() const
Returns vector of field names.
FWDATA_API void fieldDeepCopy(const ::fwData::Object::csptr &source)
A deep copy of fields (objects in m_children)
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.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_MODIFIED_SIG
Key in m_signals map of signal m_sigModified.
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.
FWDATA_API void fieldShallowCopy(const ::fwData::Object::csptr &source)
A shallow copy of fields (objects in m_children)
FWDATA_API void setField(const FieldNameType &name,::fwData::Object::sptr obj)
Register field with specified name. If the name does already exist, the matching field will be replac...
FWDATA_API void setFields(const FieldMapType &fieldMap)
Replace the field map content.
FWDATA_API void deepCopy(const ::fwData::Object::csptr &source)
Make a deep copy from the source Calling this method may invalidate any DumpLock, RescursiveLock or h...