fw4spl
ools/src/fwDataTools/helper/Vector.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/Vector.hpp"
8 
9 #include <fwCom/Signal.hpp>
10 #include <fwCom/Signal.hxx>
11 #include <fwCom/Signals.hpp>
12 
13 #include <fwData/Vector.hpp>
14 
15 namespace fwDataTools
16 {
17 namespace helper
18 {
19 
20 //-----------------------------------------------------------------------------
21 
22 Vector::Vector( ::fwData::Vector::wptr _vector ) :
23  m_vector( _vector )
24 {
25 }
26 
27 //-----------------------------------------------------------------------------
28 
30 {
31  if(!m_addedObjects.empty() || !m_removedObjects.empty())
32  {
33  notify();
34  }
35 }
36 
37 //-----------------------------------------------------------------------------
38 
39 void Vector::add( ::fwData::Object::sptr _newObject )
40 {
41  ::fwData::Vector::sptr vector = m_vector.lock();
42  OSLM_ASSERT( "The object " << _newObject->getID() << " must not exist in vector.",
43  std::find(vector->begin(), vector->end(), _newObject) == vector->end());
44 
45  // Modify vector
46  vector->getContainer().push_back(_newObject );
47 
48  m_addedObjects.push_back(_newObject);
49 }
50 
51 //-----------------------------------------------------------------------------
52 
53 void Vector::remove( ::fwData::Object::sptr _oldObject )
54 {
55  ::fwData::Vector::sptr vector = m_vector.lock();
56  ::fwData::Vector::iterator iter = std::find(vector->begin(), vector->end(), _oldObject);
57  OSLM_ASSERT( "The object " << _oldObject->getID() << " must exist in vector.",
58  iter != vector->end());
59 
60  // Modify vector
61  vector->getContainer().erase( iter );
62 
63  m_removedObjects.push_back(_oldObject);
64 
65 }
66 
67 //-----------------------------------------------------------------------------
68 
70 {
71  ::fwData::Vector::sptr vector = m_vector.lock();
72 
73  while (!vector->empty())
74  {
75  this->remove(vector->front());
76  }
77 }
78 
79 //-----------------------------------------------------------------------------
80 
82 {
83  if ( !m_removedObjects.empty() )
84  {
85  auto sig = m_vector.lock()->signal< ::fwData::Vector::RemovedObjectsSignalType >(
87  sig->asyncEmit(m_removedObjects);
88  }
89  if ( !m_addedObjects.empty() )
90  {
91  auto sig = m_vector.lock()->signal< ::fwData::Vector::AddedObjectsSignalType >(
93  sig->asyncEmit(m_addedObjects);
94  }
95  OSLM_INFO_IF("No changes were found on the vector '" + m_vector.lock()->getID() + "', nothing to notify.",
96  m_addedObjects.empty() && m_removedObjects.empty());
97 
98  m_removedObjects.clear();
99  m_addedObjects.clear();
100 }
101 
102 //-----------------------------------------------------------------------------
103 
104 } // namespace helper
105 } // namespace fwDataTools
FWDATATOOLS_API void clear()
Clear all objects in the vector.
ContainerType::iterator 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
The namespace fwDataTools contains classes which provide helpers to manipulate fwData::Object. *.
FWDATATOOLS_API Vector(::fwData::Vector::wptr vector)
Constructor. Initialize parameters.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_ADDED_OBJECTS_SIG
Type of signal when objects are added.
#define OSLM_INFO_IF(message, cond)
Definition: spyLog.hpp:256
FWDATATOOLS_API ~Vector()
Destrucotr. Do nothing.
FWDATATOOLS_API void remove(::fwData::Object::sptr _oldObject)
Remove an object in the vector.
FWDATATOOLS_API void add(::fwData::Object::sptr _newObject)
Add an object in the vector.
FWDATATOOLS_API void notify()
Send the signal of modification.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_REMOVED_OBJECTS_SIG
Type of signal when objects are added.