fw4spl
SrcLib/core/fwDataTools/src/fwDataTools/helper/Composite.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 #include "fwDataTools/helper/Composite.hpp"
8 
9 #include <fwCom/Signal.hxx>
10 
11 #include <fwData/Composite.hpp>
12 
13 namespace fwDataTools
14 {
15 namespace helper
16 {
17 
18 //-----------------------------------------------------------------------------
19 
20 Composite::Composite( ::fwData::Composite::wptr _composite ) :
21  m_composite( _composite )
22 {
23 }
24 
25 //-----------------------------------------------------------------------------
26 
28 {
29  if(!m_addedObjects.empty() || !m_removedObjects.empty() || !m_newChangedObjects.empty() ||
30  !m_oldChangedObjects.empty())
31  {
32  notify();
33  }
34 }
35 
36 //-----------------------------------------------------------------------------
37 
38 void Composite::add( std::string _compositeKey, ::fwData::Object::sptr _newObject )
39 {
40  OSLM_FATAL_IF( "The composite key " << _compositeKey << " does not exist in the composite, this is the key of the"
41  "object to be added.",
42  m_composite.lock()->find(_compositeKey) != m_composite.lock()->end() );
43 
44  // Modify composite
45  m_composite.lock()->getContainer()[ _compositeKey ] = _newObject;
46 
47  m_addedObjects[_compositeKey] = _newObject;
48 
49 }
50 
51 //-----------------------------------------------------------------------------
52 
53 void Composite::remove( std::string _compositeKey )
54 {
55  OSLM_FATAL_IF( "The composite key " << _compositeKey << " does not exist in the composite, this is the key of the"
56  "object to be removed.",
57  m_composite.lock()->find(_compositeKey) == m_composite.lock()->end() );
58 
59  // Get old object
60  ::fwData::Object::sptr objBackup = m_composite.lock()->getContainer()[ _compositeKey ];
61 
62  // Modify composite
63  m_composite.lock()->getContainer().erase( _compositeKey );
64 
65  m_removedObjects[_compositeKey] = objBackup;
66 
67 }
68 
69 //-----------------------------------------------------------------------------
70 
72 {
73  ::fwData::Composite::sptr composite = m_composite.lock();
74  std::vector<std::string> vectKey;
75  std::transform( composite->begin(), composite->end(),
76  std::back_inserter(vectKey),
77  std::bind(&::fwData::Composite::value_type::first, std::placeholders::_1) );
78 
79  for(std::string key : vectKey)
80  {
81  this->remove(key);
82  }
83 }
84 
85 //-----------------------------------------------------------------------------
86 
87 void Composite::swap( std::string _compositeKey, ::fwData::Object::sptr _newObject )
88 {
89  OSLM_FATAL_IF( "The composite key " << _compositeKey << " does not exist in the composite, this is the key of the"
90  "object to be swapped.",
91  m_composite.lock()->find(_compositeKey) == m_composite.lock()->end() );
92 
93  // Get old object
94  ::fwData::Object::sptr objBackup = m_composite.lock()->getContainer()[ _compositeKey ];
95 
96  if( objBackup != _newObject )
97  {
98  // Modify composite
99  m_composite.lock()->getContainer()[ _compositeKey ] = _newObject;
100 
101  m_newChangedObjects[_compositeKey] = _newObject;
102  m_oldChangedObjects[_compositeKey] = objBackup;
103  }
104  else
105  {
106  OSLM_INFO(
107  "Cannot swap this object ( "<< _compositeKey <<
108  " ) in composite because it is the same object. Do nothing (not notification)");
109  }
110 }
111 
112 //-----------------------------------------------------------------------------
113 
115 {
116  if ( !m_removedObjects.empty() )
117  {
118  auto sig = m_composite.lock()->signal< ::fwData::Composite::RemovedObjectsSignalType >(
120 
121  sig->asyncEmit(m_removedObjects);
122  }
123  if ( !m_newChangedObjects.empty() && !m_oldChangedObjects.empty() )
124  {
125  auto sig = m_composite.lock()->signal< ::fwData::Composite::ChangedObjectsSignalType >(
127 
128  sig->asyncEmit(m_newChangedObjects, m_oldChangedObjects);
129  }
130  if ( !m_addedObjects.empty() )
131  {
132  auto sig = m_composite.lock()->signal< ::fwData::Composite::AddedObjectsSignalType >(
134 
135  sig->asyncEmit(m_addedObjects);
136  }
137  OSLM_INFO_IF("No changes were found on the composite '" + m_composite.lock()->getID() + "', nothing to notify.",
138  m_addedObjects.empty() && m_newChangedObjects.empty() && m_removedObjects.empty());
139 
140  m_removedObjects.clear();
141  m_newChangedObjects.clear();
142  m_oldChangedObjects.clear();
143  m_addedObjects.clear();
144 }
145 
146 //-----------------------------------------------------------------------------
147 
148 } // namespace helper
149 } // namespace fwDataTools
FWDATATOOLS_API void remove(std::string _compositeKey)
Remove an object in the composite.
FWDATATOOLS_API void notify()
Send the message of modification.
FWDATATOOLS_API Composite(::fwData::Composite::wptr _composite)
Constructor. Initialize parameters.
The namespace fwDataTools contains classes which provide helpers to manipulate fwData::Object. *.
FWDATATOOLS_API void add(std::string _compositeKey,::fwData::Object::sptr _newObject)
Add an object in the composite.
FWDATATOOLS_API void swap(std::string _compositeKey,::fwData::Object::sptr _newObject)
Replace an object in the composite.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_CHANGED_OBJECTS_SIG
Type of signal when objects are added.
#define OSLM_INFO_IF(message, cond)
Definition: spyLog.hpp:256
#define OSLM_INFO(message)
Definition: spyLog.hpp:252
FWDATATOOLS_API void clear()
Clear all objects in the composite.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_REMOVED_OBJECTS_SIG
Type of signal when objects are added.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_ADDED_OBJECTS_SIG
Type of signal when objects are added.
#define OSLM_FATAL_IF(message, cond)
Definition: spyLog.hpp:289