fw4spl
SManage.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2015-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 #include "ctrlSelection/SManage.hpp"
8 
9 #include <fwCom/Slot.hpp>
10 #include <fwCom/Slot.hxx>
11 #include <fwCom/Slots.hpp>
12 #include <fwCom/Slots.hxx>
13 
14 #include <fwData/Composite.hpp>
15 #include <fwData/Exception.hpp>
16 #include <fwData/Vector.hpp>
17 
18 #include <fwDataTools/helper/Composite.hpp>
19 #include <fwDataTools/helper/Field.hpp>
20 #include <fwDataTools/helper/Vector.hpp>
21 
22 #include <fwMedData/Series.hpp>
23 #include <fwMedData/SeriesDB.hpp>
24 
25 #include <fwMedDataTools/helper/SeriesDB.hpp>
26 
27 #include <fwServices/macros.hpp>
28 
29 namespace ctrlSelection
30 {
31 
32 const ::fwCom::Slots::SlotKeyType SManage::s_ADD_SLOT = "add";
33 const ::fwCom::Slots::SlotKeyType SManage::s_ADD_COPY_SLOT = "addCopy";
34 const ::fwCom::Slots::SlotKeyType SManage::s_ADD_OR_SWAP_SLOT = "addOrSwap";
35 const ::fwCom::Slots::SlotKeyType SManage::s_SWAP_OBJ_SLOT = "swapObj";
36 const ::fwCom::Slots::SlotKeyType SManage::s_REMOVE_SLOT = "remove";
37 const ::fwCom::Slots::SlotKeyType SManage::s_REMOVE_IF_PRESENT_SLOT = "removeIfPresent";
38 const ::fwCom::Slots::SlotKeyType SManage::s_CLEAR_SLOT = "clear";
39 
40 const ::fwServices::IService::KeyType s_COMPOSITE_INOUT = "composite";
41 const ::fwServices::IService::KeyType s_VECTOR_INOUT = "vector";
42 const ::fwServices::IService::KeyType s_SERIESDB_INOUT = "seriesDB";
43 const ::fwServices::IService::KeyType s_FIELD_HOLDER_INOUT = "fieldHolder";
44 const ::fwServices::IService::KeyType s_OBJECT_INOUT = "object";
45 
46 //-----------------------------------------------------------------------------
47 
48 SManage::SManage() noexcept
49 {
50  newSlot(s_ADD_SLOT, &SManage::add, this);
51  newSlot(s_ADD_COPY_SLOT, &SManage::addCopy, this);
52  newSlot(s_ADD_OR_SWAP_SLOT, &SManage::addOrSwap, this);
53  newSlot(s_SWAP_OBJ_SLOT, &SManage::swap, this);
54  newSlot(s_REMOVE_SLOT, &SManage::remove, this);
56  newSlot(s_CLEAR_SLOT, &SManage::clear, this);
57 }
58 
59 //-----------------------------------------------------------------------------
60 
62 {
63 }
64 
65 //-----------------------------------------------------------------------------
66 
68 {
69  ::fwServices::IService::ConfigType config = this->getConfigTree();
70 
71  m_compositeKey = config.get("compositeKey", "");
72  m_fieldName = config.get("field", "");
73 }
74 
75 //-----------------------------------------------------------------------------
76 
78 {
79 }
80 
81 //-----------------------------------------------------------------------------
82 
84 {
85 }
86 
87 //-----------------------------------------------------------------------------
88 
90 {
91 }
92 
93 //-----------------------------------------------------------------------------
94 
96 {
97  internalAdd(false);
98 }
99 
100 //------------------------------------------------------------------------------
101 
103 {
104  internalAdd(true);
105 }
106 
107 //-----------------------------------------------------------------------------
108 
110 {
111  SLM_ASSERT("Service is not started", this->isStarted());
112 
113  ::fwData::Object::sptr obj = this->getInOut< ::fwData::Object >(s_OBJECT_INOUT);
114  SLM_ASSERT("Object '" + s_OBJECT_INOUT + "' is missing.", obj);
115 
116  ::fwData::Composite::sptr composite = this->getInOut< ::fwData::Composite >(s_COMPOSITE_INOUT);
117  ::fwData::Vector::sptr vector = this->getInOut< ::fwData::Vector >(s_VECTOR_INOUT);
118  ::fwMedData::SeriesDB::sptr seriesDB = this->getInOut< ::fwMedData::SeriesDB >(s_SERIESDB_INOUT);
119  ::fwData::Object::sptr fieldHolder = this->getInOut< ::fwData::Object >(s_FIELD_HOLDER_INOUT);
120 
121  SLM_ASSERT("Target object is missing, required one of 'composite', 'vector', 'seriesDB', or 'fieldHolder'",
122  vector || composite || seriesDB || fieldHolder);
123  if (composite)
124  {
125  SLM_ASSERT("Only one target object is managed", !vector && !seriesDB && !fieldHolder);
126  ::fwDataTools::helper::Composite helper( composite );
127  if (composite->find(m_compositeKey) == composite->end())
128  {
129  helper.add(m_compositeKey, obj);
130  }
131  else
132  {
133  helper.swap(m_compositeKey, obj);
134  }
135 
136  helper.notify();
137  }
138  else if (vector)
139  {
140  SLM_ASSERT("Only one target object is managed", !composite && !seriesDB && !fieldHolder);
141  auto iter = std::find(vector->begin(), vector->end(), obj);
142  if (iter == vector->end())
143  {
144  ::fwDataTools::helper::Vector helper( vector );
145  helper.add(obj);
146  helper.notify();
147  }
148  SLM_WARN_IF("Object already exists in the Vector, does nothing.", iter != vector->end());
149  }
150  else if (seriesDB)
151  {
152  ::fwMedData::Series::sptr series = ::fwMedData::Series::dynamicCast(obj);
153  SLM_ASSERT("Target object is a SeriesDB, so object must be a Series.", series);
154  SLM_ASSERT("Only one target object is managed", !composite && !vector && !fieldHolder);
155 
156  auto iter = std::find(seriesDB->begin(), seriesDB->end(), series);
157  if (iter == seriesDB->end())
158  {
159  ::fwMedDataTools::helper::SeriesDB helper( seriesDB );
160  helper.add(series);
161  helper.notify();
162  }
163  SLM_WARN_IF("Object already exists in the SeriesDB, does nothing.", iter != seriesDB->end());
164  }
165  else if (fieldHolder)
166  {
167  SLM_ASSERT("Only one target object is managed", !vector && !composite && !seriesDB);
168  ::fwDataTools::helper::Field helper( fieldHolder );
169  helper.addOrSwap(m_fieldName, obj);
170  helper.notify();
171  }
172 }
173 
174 //-----------------------------------------------------------------------------
175 
177 {
178  SLM_ASSERT("Service is not started", this->isStarted());
179 
180  ::fwData::Object::sptr obj = this->getInOut< ::fwData::Object >(s_OBJECT_INOUT);
181  SLM_ASSERT("Object '" + s_OBJECT_INOUT + "' is missing.", obj);
182 
183  ::fwData::Composite::sptr composite = this->getInOut< ::fwData::Composite >(s_COMPOSITE_INOUT);
184  ::fwData::Object::sptr fieldHolder = this->getInOut< ::fwData::Object >(s_FIELD_HOLDER_INOUT);
185 
186  SLM_ASSERT("'swap' slot is only managed for 'composite' or 'fieldHolder'",
187  composite || fieldHolder);
188  if (composite)
189  {
190  SLM_ASSERT("Only one target object is managed", !fieldHolder);
191  ::fwDataTools::helper::Composite helper( composite );
192  helper.swap(m_compositeKey, obj);
193  helper.notify();
194  }
195  else if (fieldHolder)
196  {
197  SLM_ASSERT("Only one target object is managed", !composite);
198  ::fwDataTools::helper::Field helper( fieldHolder );
199  helper.swap(m_fieldName, obj);
200  helper.notify();
201  }
202 }
203 
204 //-----------------------------------------------------------------------------
205 
207 {
208  SLM_ASSERT("Service is not started", this->isStarted());
209 
210  ::fwData::Object::sptr obj = this->getInOut< ::fwData::Object >(s_OBJECT_INOUT);
211 
212  ::fwData::Composite::sptr composite = this->getInOut< ::fwData::Composite >(s_COMPOSITE_INOUT);
213  ::fwData::Vector::sptr vector = this->getInOut< ::fwData::Vector >(s_VECTOR_INOUT);
214  ::fwMedData::SeriesDB::sptr seriesDB = this->getInOut< ::fwMedData::SeriesDB >(s_SERIESDB_INOUT);
215  ::fwData::Object::sptr fieldHolder = this->getInOut< ::fwData::Object >(s_FIELD_HOLDER_INOUT);
216 
217  SLM_ASSERT("Target object is missing, required one of 'composite', 'vector', 'seriesDB', or 'fieldHolder'",
218  vector || composite || seriesDB || fieldHolder);
219  if (composite)
220  {
221  SLM_ASSERT("Only one target object is managed", !vector && !seriesDB && !fieldHolder);
222  ::fwDataTools::helper::Composite helper( composite );
223  helper.remove(m_compositeKey);
224  helper.notify();
225  }
226  else if (vector)
227  {
228  SLM_ASSERT("Only one target object is managed", !composite && !seriesDB && !fieldHolder);
229  SLM_ASSERT("Object '" + s_OBJECT_INOUT + "' is missing.", obj);
230  ::fwDataTools::helper::Vector helper( vector );
231  helper.remove(obj);
232  helper.notify();
233  }
234  else if (seriesDB)
235  {
236  SLM_ASSERT("Object '" + s_OBJECT_INOUT + "' is missing.", obj);
237  ::fwMedData::Series::sptr series = ::fwMedData::Series::dynamicCast(obj);
238  SLM_ASSERT("Target object is a SeriesDB, so object must be a Series.", series);
239  SLM_ASSERT("Only one target object is managed", !composite && !vector && !fieldHolder);
240 
241  ::fwMedDataTools::helper::SeriesDB helper( seriesDB );
242  helper.remove(series);
243  helper.notify();
244  }
245  else if (fieldHolder)
246  {
247  SLM_ASSERT("Only one target object is managed", !vector && !composite && !seriesDB);
248  ::fwDataTools::helper::Field helper( fieldHolder );
249  helper.remove(m_fieldName);
250  helper.notify();
251  }
252 }
253 
254 //-----------------------------------------------------------------------------
255 
257 {
258  SLM_ASSERT("Service is not started", this->isStarted());
259 
260  ::fwData::Object::sptr obj = this->getInOut< ::fwData::Object >(s_OBJECT_INOUT);
261 
262  ::fwData::Composite::sptr composite = this->getInOut< ::fwData::Composite >(s_COMPOSITE_INOUT);
263  ::fwData::Vector::sptr vector = this->getInOut< ::fwData::Vector >(s_VECTOR_INOUT);
264  ::fwMedData::SeriesDB::sptr seriesDB = this->getInOut< ::fwMedData::SeriesDB >(s_SERIESDB_INOUT);
265  ::fwData::Object::sptr fieldHolder = this->getInOut< ::fwData::Object >(s_FIELD_HOLDER_INOUT);
266 
267  SLM_ASSERT("Target object is missing, required one of 'composite', 'vector', 'seriesDB', or 'fieldHolder'",
268  vector || composite || seriesDB || fieldHolder);
269  if (composite)
270  {
271  SLM_ASSERT("Only one target object is managed", !vector && !seriesDB && !fieldHolder);
272  ::fwDataTools::helper::Composite helper( composite );
273  if (composite->find(m_compositeKey) != composite->end())
274  {
275  helper.remove(m_compositeKey);
276  helper.notify();
277  }
278  }
279  else if (vector)
280  {
281  SLM_ASSERT("Only one target object is managed", !composite && !seriesDB && !fieldHolder);
282  SLM_ASSERT("Object '" + s_OBJECT_INOUT + "' is missing.", obj);
283  auto iter = std::find(vector->begin(), vector->end(), obj);
284  if (iter != vector->end())
285  {
286  ::fwDataTools::helper::Vector helper( vector );
287  helper.remove(obj);
288  helper.notify();
289  }
290  SLM_WARN_IF("Object does not exist in the Vector, does nothing.", iter == vector->end());
291  }
292  else if (seriesDB)
293  {
294  SLM_ASSERT("Object '" + s_OBJECT_INOUT + "' is missing.", obj);
295  ::fwMedData::Series::sptr series = ::fwMedData::Series::dynamicCast(obj);
296  SLM_ASSERT("Target object is a SeriesDB, so object must be a Series.", series);
297  SLM_ASSERT("Only one target object is managed", !composite && !vector && !fieldHolder);
298 
299  auto iter = std::find(seriesDB->begin(), seriesDB->end(), series);
300  if (iter != seriesDB->end())
301  {
302  ::fwMedDataTools::helper::SeriesDB helper( seriesDB );
303  helper.remove(series);
304  helper.notify();
305  }
306  SLM_WARN_IF("Object does not exist in the SeriesDB, does nothing.", iter == seriesDB->end());
307  }
308  else if (fieldHolder)
309  {
310  SLM_ASSERT("Only one target object is managed", !vector && !composite && !seriesDB);
311  ::fwDataTools::helper::Field helper( fieldHolder );
312  try
313  {
314  helper.remove(m_fieldName);
315  helper.notify();
316  }
317  catch(::fwData::Exception&)
318  {
319  // Silently ignore the exception which means the field was not present
320  }
321  }
322 }
323 
324 //-----------------------------------------------------------------------------
325 
327 {
328  SLM_ASSERT("Service is not started", this->isStarted());
329 
330  ::fwData::Composite::sptr composite = this->getInOut< ::fwData::Composite >(s_COMPOSITE_INOUT);
331  ::fwData::Vector::sptr vector = this->getInOut< ::fwData::Vector >(s_VECTOR_INOUT);
332  ::fwMedData::SeriesDB::sptr seriesDB = this->getInOut< ::fwMedData::SeriesDB >(s_SERIESDB_INOUT);
333  ::fwData::Object::sptr fieldHolder = this->getInOut< ::fwData::Object >(s_FIELD_HOLDER_INOUT);
334 
335  SLM_ASSERT("Target object is missing, required one of 'composite', 'vector', 'seriesDB', or 'fieldHolder'",
336  vector || composite || seriesDB || fieldHolder);
337  if (composite)
338  {
339  SLM_ASSERT("Only one target object is managed", !vector && !seriesDB && !fieldHolder);
340  ::fwDataTools::helper::Composite helper( composite );
341  helper.clear();
342  helper.notify();
343  }
344  else if (vector)
345  {
346  SLM_ASSERT("Only one target object is managed", !composite && !seriesDB && !fieldHolder);
347  ::fwDataTools::helper::Vector helper( vector );
348  helper.clear();
349  helper.notify();
350  }
351  else if (seriesDB)
352  {
353  ::fwMedDataTools::helper::SeriesDB helper( seriesDB );
354  helper.clear();
355  helper.notify();
356  }
357  else if (fieldHolder)
358  {
359  SLM_ASSERT("Only one target object is managed", !vector && !composite && !seriesDB);
360  ::fwDataTools::helper::Field helper( fieldHolder );
361  helper.clear();
362  helper.notify();
363  }
364 }
365 
366 //------------------------------------------------------------------------------
367 
368 void SManage::internalAdd(bool _copy)
369 {
370  SLM_ASSERT("Service is not started", this->isStarted());
371 
372  ::fwData::Object::sptr obj = this->getInOut< ::fwData::Object >(s_OBJECT_INOUT);
373  SLM_ASSERT("Object '" + s_OBJECT_INOUT + "' is missing.", obj);
374 
375  if(_copy)
376  {
377  obj = ::fwData::Object::copy(obj);
378  }
379 
380  ::fwData::Composite::sptr composite = this->getInOut< ::fwData::Composite >(s_COMPOSITE_INOUT);
381  ::fwData::Vector::sptr vector = this->getInOut< ::fwData::Vector >(s_VECTOR_INOUT);
382  ::fwMedData::SeriesDB::sptr seriesDB = this->getInOut< ::fwMedData::SeriesDB >(s_SERIESDB_INOUT);
383  ::fwData::Object::sptr fieldHolder = this->getInOut< ::fwData::Object >(s_FIELD_HOLDER_INOUT);
384 
385  SLM_ASSERT("Target object is missing, required one of 'composite', 'vector', 'seriesDB', or 'fieldHolder'",
386  vector || composite || seriesDB || fieldHolder);
387  if (composite)
388  {
389  SLM_ASSERT("Only one target object is managed", !vector && !seriesDB && !fieldHolder);
390  ::fwDataTools::helper::Composite helper( composite );
391  helper.add(m_compositeKey, obj);
392  helper.notify();
393  }
394  else if (vector)
395  {
396  SLM_ASSERT("Only one target object is managed", !composite && !seriesDB && !fieldHolder);
397  ::fwDataTools::helper::Vector helper( vector );
398  helper.add(obj);
399  helper.notify();
400  }
401  else if (seriesDB)
402  {
403  ::fwMedData::Series::sptr series = ::fwMedData::Series::dynamicCast(obj);
404  SLM_ASSERT("Target object is a SeriesDB, so object must be a Series.", series);
405  SLM_ASSERT("Only one target object is managed", !composite && !vector && !fieldHolder);
406  ::fwMedDataTools::helper::SeriesDB helper( seriesDB );
407  helper.add(series);
408  helper.notify();
409  }
410  else if (fieldHolder)
411  {
412  SLM_ASSERT("Only one target object is managed", !vector && !composite && !seriesDB);
413  ::fwDataTools::helper::Field helper( fieldHolder );
414  helper.add(m_fieldName, obj);
415  helper.notify();
416  }
417 }
418 
419 //-----------------------------------------------------------------------------
420 
421 } // ctrlSelection
FWDATATOOLS_API void remove(std::string _compositeKey)
Remove an object in the composite.
void addCopy()
Adds a copy of object into the composite with the key given by config.
Definition: SManage.cpp:102
FWDATATOOLS_API void clear()
Clear all objects in the vector.
void clear()
Removes all objects.
Definition: SManage.cpp:326
Defines a helper to modify field on a fwData::Object and create a message notifying this modification...
Definition: Field.hpp:22
FWDATATOOLS_API void notify()
Send the message of modification.
FWDATATOOLS_API void add(const ::fwData::Object::FieldNameType &_name,::fwData::Object::sptr _obj)
Add a field in the object.
Definition: Field.cpp:66
FWDATATOOLS_API void addOrSwap(const ::fwData::Object::FieldNameType &_name,::fwData::Object::sptr _obj)
Add or replace a field in the object.
Definition: Field.cpp:97
virtual CTRLSELECTION_API void starting() override
Implements starting method derived from IService. Do nothing.
Definition: SManage.cpp:77
virtual CTRLSELECTION_API void configuring() override
Configures the service.
Definition: SManage.cpp:67
FWDATATOOLS_API void add(std::string _compositeKey,::fwData::Object::sptr _newObject)
Add an object in the composite.
static const ::fwCom::Slots::SlotKeyType s_CLEAR_SLOT
Adds the object into the composite with the key given by config.
Definition: SManage.hpp:114
FWDATATOOLS_API void swap(std::string _compositeKey,::fwData::Object::sptr _newObject)
Replace an object in the composite.
Defines an helper to modify an fwData::Composite and create in parallel the message to announce this ...
Implements data exception class.
static const ::fwCom::Slots::SlotKeyType s_ADD_OR_SWAP_SLOT
Adds the object into the composite with the key given by config.
Definition: SManage.hpp:110
FWMEDDATATOOLS_API void add(::fwMedData::Series::sptr newSeries)
Add a Series in the SeriesDB.
FWDATATOOLS_API void notify()
Send the built message and clear the internal maps.
Definition: Field.cpp:148
static const ::fwCom::Slots::SlotKeyType s_REMOVE_SLOT
Adds the object into the composite with the key given by config.
Definition: SManage.hpp:112
Defines an helper to modify an fwMedData::SeriesDB and create in parallel the message to announce thi...
virtual CTRLSELECTION_API void updating() override
Implements updating method derived from IService. Do nothing.
Definition: SManage.cpp:89
void swap()
Swaps the object into the composite with the key given by config.
Definition: SManage.cpp:176
FWDATATOOLS_API void clear()
Clear all fields in the object.
Definition: Field.cpp:132
CTRLSELECTION_API SManage() noexcept
Constructor. Do nothing.
Definition: SManage.cpp:48
FWDATATOOLS_API void remove(const ::fwData::Object::FieldNameType &_name)
Remove a field from the object.
Definition: Field.cpp:118
FWMEDDATATOOLS_API void clear()
Clear all series in the SeriesDB.
static const ::fwCom::Slots::SlotKeyType s_ADD_SLOT
Adds the object into the composite with the key given by config.
Definition: SManage.hpp:108
FWDATATOOLS_API void clear()
Clear all objects in the composite.
FWDATATOOLS_API void remove(::fwData::Object::sptr _oldObject)
Remove an object in the vector.
#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
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 const ::fwCom::Slots::SlotKeyType s_REMOVE_IF_PRESENT_SLOT
Adds the object into the composite with the key given by config.
Definition: SManage.hpp:113
FWDATATOOLS_API void add(::fwData::Object::sptr _newObject)
Add an object in the vector.
FWMEDDATATOOLS_API void remove(::fwMedData::Series::sptr oldSeries)
Remove a Series in the SeriesDB.
static const ::fwCom::Slots::SlotKeyType s_SWAP_OBJ_SLOT
Adds the object into the composite with the key given by config.
Definition: SManage.hpp:111
virtual CTRLSELECTION_API ~SManage() noexcept
Destructor. Do nothing.
Definition: SManage.cpp:61
FWDATATOOLS_API void swap(const ::fwData::Object::FieldNameType &_name,::fwData::Object::sptr _obj)
Replace a field in the object.
Definition: Field.cpp:81
void remove()
Removes the object from the composite at the key given by config.
Definition: SManage.cpp:206
void addOrSwap()
Adds or swap the object into the composite with the key given by config.
Definition: SManage.cpp:109
FWMEDDATATOOLS_API void notify()
Send the signal of modification.
static const ::fwCom::Slots::SlotKeyType s_ADD_COPY_SLOT
Adds the object into the composite with the key given by config.
Definition: SManage.hpp:109
void removeIfPresent()
Removes the object from the composite at the key given by config if it is present.
Definition: SManage.cpp:256
FWDATATOOLS_API void notify()
Send the signal of modification.
The namespace ctrlSelection contains several interfaces for manager, updater and wrapper.
Definition: BookmarkSrv.hpp:15
Defines an helper to modify an fwData::Vector and create in parallel the message to announce this mod...
void add()
Adds the object into the composite with the key given by config.
Definition: SManage.cpp:95
virtual CTRLSELECTION_API void stopping() override
Implements stopping method derived from IService. Do nothing.
Definition: SManage.cpp:83
#define SLM_WARN_IF(message, cond)
Definition: spyLog.hpp:265
FWSERVICES_API bool isStarted() const noexcept
Test if the service is started or not.
Definition: IService.cpp:383
FWSERVICES_API ConfigType getConfigTree() const
Return the configuration, in an boost property tree.
Definition: IService.cpp:247