fw4spl
include/fwData/Vector.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_VECTOR_HPP__
8 #define __FWDATA_VECTOR_HPP__
9 
10 #include "fwData/config.hpp"
11 #include "fwData/factory/new.hpp"
12 #include "fwData/Object.hpp"
13 
14 #include <fwCom/Signal.hpp>
15 #include <fwCom/Signals.hpp>
16 
17 #include <vector>
18 
19 namespace fwData
20 {
21 class Vector;
22 }
23 
24 fwCampAutoDeclareDataMacro((fwData)(Vector), FWDATA_API);
25 
26 namespace fwData
27 {
28 
34 class FWDATA_CLASS_API Vector : public Object
35 {
36 
37 public:
38 
39  fwCoreClassDefinitionsWithFactoryMacro( (Vector)(::fwData::Object), (()), ::fwData::factory::New< Vector >);
40  fwCampMakeFriendDataMacro((fwData)(Vector));
41 
42  typedef std::vector< Object::sptr > ContainerType;
43 
44  typedef ContainerType::value_type ValueType;
45  typedef ContainerType::reference ReferenceType;
46  typedef ContainerType::const_reference ConstReferenceType;
47  typedef ContainerType::iterator IteratorType;
48  typedef ContainerType::const_iterator ConstIteratorType;
49  typedef ContainerType::reverse_iterator ReverseIteratorType;
50  typedef ContainerType::const_reverse_iterator ConstReverseIteratorType;
51  typedef ContainerType::size_type SizeType;
52 
57  FWDATA_API Vector(::fwData::Object::Key key);
58 
60  FWDATA_API virtual ~Vector();
61 
64  typedef ContainerType::value_type value_type;
65  typedef ContainerType::iterator iterator;
66  typedef ContainerType::const_iterator const_iterator;
67  typedef ContainerType::reverse_iterator reverse_iterator;
68  typedef ContainerType::const_reverse_iterator const_reverse_iterator;
69  typedef ContainerType::size_type size_type;
70 
71  typedef Vector Container;
72 
73  IteratorType begin();
74  IteratorType end();
75  ConstIteratorType begin() const;
76  ConstIteratorType end() const;
77 
78  ReverseIteratorType rbegin();
79  ReverseIteratorType rend();
80  ConstReverseIteratorType rbegin() const;
81  ConstReverseIteratorType rend() const;
82 
83  bool empty() const;
84  SizeType size() const;
85 
86  ValueType front() const;
87  ValueType back() const;
88 
89  ReferenceType operator[] ( size_type n );
90  ConstReferenceType operator[] ( size_type n ) const;
91 
92  ReferenceType at ( SizeType n );
93  ConstReferenceType at ( SizeType n ) const;
95 
98  ContainerType& getContainer();
99  const ContainerType& getContainer () const;
100  void setContainer (const ContainerType& val);
102 
104  FWDATA_API void shallowCopy( const Object::csptr& _source ) override;
105 
107  FWDATA_API void cachedDeepCopy(const Object::csptr& _source, DeepCopyCacheType& cache) override;
108 
110  template< class DATATYPE >
111  void setDataContainer( const std::vector< SPTR(DATATYPE) >& vec );
112 
114  template< class DATATYPE >
115  std::vector< SPTR(DATATYPE) > getDataContainer() const;
116 
121  typedef ::fwCom::Signal< void (ContainerType) > AddedObjectsSignalType;
123  FWDATA_API static const ::fwCom::Signals::SignalKeyType s_ADDED_OBJECTS_SIG;
124 
126  typedef ::fwCom::Signal< void (ContainerType) > RemovedObjectsSignalType;
127  FWDATA_API static const ::fwCom::Signals::SignalKeyType s_REMOVED_OBJECTS_SIG;
132 protected:
133 
134  ContainerType m_container;
135 };
136 
137 //-----------------------------------------------------------------------------
138 
139 inline Vector::IteratorType Vector::begin()
140 {
141  return m_container.begin();
142 }
143 
144 //-----------------------------------------------------------------------------
145 
146 inline Vector::IteratorType Vector::end()
147 {
148  return m_container.end();
149 }
150 
151 //-----------------------------------------------------------------------------
152 
153 inline Vector::ConstIteratorType Vector::begin() const
154 {
155  return m_container.begin();
156 }
157 
158 //-----------------------------------------------------------------------------
159 
160 inline Vector::ConstIteratorType Vector::end() const
161 {
162  return m_container.end();
163 }
164 
165 //-----------------------------------------------------------------------------
166 
167 inline Vector::ReverseIteratorType Vector::rbegin()
168 {
169  return m_container.rbegin();
170 }
171 
172 //-----------------------------------------------------------------------------
173 
174 inline Vector::ReverseIteratorType Vector::rend()
175 {
176  return m_container.rend();
177 }
178 
179 //-----------------------------------------------------------------------------
180 
181 inline Vector::ConstReverseIteratorType Vector::rbegin() const
182 {
183  return m_container.rbegin();
184 }
185 
186 //-----------------------------------------------------------------------------
187 
188 inline Vector::ConstReverseIteratorType Vector::rend() const
189 {
190  return m_container.rend();
191 }
192 
193 //-----------------------------------------------------------------------------
194 
195 inline bool Vector::empty() const
196 {
197  return m_container.empty();
198 }
199 
200 //-----------------------------------------------------------------------------
201 
202 inline Vector::SizeType Vector::size() const
203 {
204  return m_container.size();
205 }
206 
207 //-----------------------------------------------------------------------------
208 
209 inline Vector::ValueType Vector::front() const
210 {
211  return m_container.front();
212 }
213 
214 //-----------------------------------------------------------------------------
215 
216 inline Vector::ValueType Vector::back() const
217 {
218  return m_container.back();
219 }
220 
221 //-----------------------------------------------------------------------------
222 
223 inline Vector::ReferenceType Vector::operator[](Vector::size_type n)
224 {
225  return this->m_container[n];
226 }
227 
228 //-----------------------------------------------------------------------------
229 
230 inline Vector::ConstReferenceType Vector::operator[](Vector::size_type n) const
231 {
232  return this->m_container[n];
233 }
234 
235 //-----------------------------------------------------------------------------
236 
237 inline Vector::ReferenceType Vector::at(Vector::SizeType n)
238 {
239  return m_container.at(n);
240 }
241 
242 //-----------------------------------------------------------------------------
243 
244 inline Vector::ConstReferenceType Vector::at(Vector::SizeType n) const
245 {
246  return m_container.at(n);
247 }
248 
249 //-----------------------------------------------------------------------------
250 
251 inline Vector::ContainerType& Vector::getContainer()
252 {
253  return m_container;
254 }
255 
256 //-----------------------------------------------------------------------------
257 
258 inline const Vector::ContainerType& Vector::getContainer () const
259 {
260  return m_container;
261 }
262 
263 //-----------------------------------------------------------------------------
264 
265 inline void Vector::setContainer (const Vector::ContainerType& val)
266 {
267  m_container = val;
268 }
269 
270 //-----------------------------------------------------------------------------
271 
272 template< class DATATYPE >
273 inline void Vector::setDataContainer( const std::vector< SPTR(DATATYPE) >& vec )
274 {
275  m_container.clear();
276  std::copy( vec.begin(), vec.end(), std::back_inserter(this->getContainer()) );
277 }
278 
279 //-----------------------------------------------------------------------------
280 
281 template< class DATATYPE >
282 inline std::vector< SPTR(DATATYPE) > Vector::getDataContainer() const
283 {
284  std::vector< SPTR(DATATYPE) > vec;
285  vec.reserve( this->size() );
286  SPTR(DATATYPE) castedData;
287  for(const ::fwData::Object::sptr& data : this->getContainer() )
288  {
289  castedData = std::dynamic_pointer_cast<DATATYPE>( data );
290  OSLM_ASSERT("DynamicCast "<< ::fwCore::TypeDemangler<DATATYPE>().getClassname()<<" failed", castedData);
291  vec.push_back( castedData );
292  }
293 
294  return vec;
295 }
296 
297 //-----------------------------------------------------------------------------
298 
299 } //namespace fwData
300 
301 #endif /* __FWDATA_VECTOR_HPP__ */
ValueType back() const
#define SPTR(_cls_)
ContainerType::iterator iterator
void setContainer(const ContainerType &val)
get/set the vector of fwData::Object
std::vector< std::shared_ptr< DATATYPE > > getDataContainer() const
Method to get a std::vector from fwData::Vector.
#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
ContainerType::const_iterator const_iterator
ContainerType::value_type value_type
::fwCom::Signal< void(ContainerType) > RemovedObjectsSignalType
Type of signal when objects are removed.
void setDataContainer(const std::vector< std::shared_ptr< DATATYPE > > &vec)
Method to initialize a fwData::Vector from a std::vector.
ContainerType::size_type size_type
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_ADDED_OBJECTS_SIG
Type of signal when objects are added.
SizeType size() const
ReferenceType operator[](size_type n)
This class defines a vector of objects.
ContainerType::reverse_iterator reverse_iterator
ReverseIteratorType rbegin()
ReverseIteratorType rend()
#define fwCoreClassDefinitionsWithFactoryMacro(_classinfo_, _parameters_, _factory_)
Generate common construction methods for classes with one factory.
Type demangler. Use Demangler class to get a demangled string for the type T.
Definition: Demangler.hpp:95
Base class for each data object.
ReferenceType at(SizeType n)
ContainerType & getContainer()
get/set the vector of fwData::Object
Contains the representation of the data objects used in the framework.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_REMOVED_OBJECTS_SIG
Type of signal when objects are added.
ValueType front() const
ContainerType::const_reverse_iterator const_reverse_iterator