Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef __itkSmartPointerVectorContainer_h
00019 #define __itkSmartPointerVectorContainer_h
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "itkObject.h"
00034
00035 #include <utility>
00036 #include <vector>
00037
00038 namespace itk
00039 {
00040
00056 template <
00057 typename TElementIdentifier,
00058 typename TElement
00059 >
00060 class SmartPointerVectorContainer:
00061 public Object,
00062 public std::vector<itk::SmartPointer<TElement> >
00063 {
00064 public:
00066 typedef SmartPointerVectorContainer Self;
00067 typedef Object Superclass;
00068 typedef SmartPointer<Self> Pointer;
00069 typedef SmartPointer<const Self> ConstPointer;
00070
00072 typedef TElementIdentifier ElementIdentifier;
00073 typedef TElement Element;
00074 typedef const TElement ConstElement;
00075 typedef itk::SmartPointer<Element> ElementPointer;
00076 typedef const itk::SmartPointer<const Element> ConstElementPointer;
00077
00078 private:
00080 typedef std::vector<ElementPointer> VectorType;
00081 typedef typename VectorType::size_type size_type;
00082 typedef typename VectorType::iterator VectorIterator;
00083 typedef typename VectorType::const_iterator VectorConstIterator;
00084
00085 protected:
00089 SmartPointerVectorContainer():
00090 Object(), VectorType() {}
00091 SmartPointerVectorContainer(size_type n):
00092 Object(), VectorType(n) {}
00093 SmartPointerVectorContainer(size_type n, const Element& x):
00094 Object(), VectorType(n, x) {}
00095 SmartPointerVectorContainer(const Self& r):
00096 Object(), VectorType(r) {}
00097 template <typename InputIterator>
00098 SmartPointerVectorContainer(InputIterator first, InputIterator last):
00099 Object(), VectorType(first, last) {}
00100
00101 public:
00102
00104 typedef VectorType STLContainerType;
00105
00107 itkNewMacro(Self);
00108
00110 itkTypeMacro(SmartPointerVectorContainer, Object);
00111
00113 class Iterator;
00114 class ConstIterator;
00115
00117 STLContainerType & CastToSTLContainer() {
00118 return dynamic_cast<STLContainerType &>(*this); }
00119
00121 const STLContainerType & CastToSTLConstContainer() const {
00122 return dynamic_cast<const STLContainerType &>(*this); }
00123
00125 friend class Iterator;
00126 friend class ConstIterator;
00127
00128 class Iterator
00129 {
00130 public:
00131 Iterator() {}
00132 Iterator(size_type d, const VectorType* vec, const VectorIterator& i): m_Pos(d), m_Vector(vec), m_Iter(i) {}
00133
00134 ElementPointer& operator* () { return *m_Iter; }
00135 Element* operator-> () { return m_Iter == m_Vector->end() ? NULL : m_Iter->GetPointer(); }
00136 ElementPointer& operator++ () { ++m_Pos; ++m_Iter; return *m_Iter; }
00137 ElementPointer operator++ (int) { Iterator temp(*this); ++m_Pos; ++m_Iter; return *temp; }
00138 ElementPointer& operator-- () { --m_Pos; --m_Iter; return *this; }
00139 ElementPointer operator-- (int) { Iterator temp(*this); --m_Pos; --m_Iter; return *temp; }
00140
00141 bool operator == (const Iterator& r) const { return m_Iter == r.m_Iter; }
00142 bool operator != (const Iterator& r) const { return m_Iter != r.m_Iter; }
00143 bool operator == (const ConstIterator& r) const { return m_Iter == r.m_Iter; }
00144 bool operator != (const ConstIterator& r) const { return m_Iter != r.m_Iter; }
00145
00147 ElementIdentifier Index(void) const { return static_cast<ElementIdentifier>( m_Pos ); }
00148
00150 Element* Value(void) const { return m_Iter->GetPointer(); }
00151
00152 private:
00153 size_type m_Pos;
00154 const VectorType* m_Vector;
00155 VectorIterator m_Iter;
00156 friend class ConstIterator;
00157 };
00158
00159 class ConstIterator
00160 {
00161 public:
00162 ConstIterator() {}
00163 ConstIterator(size_type d, const VectorType* vec, const VectorConstIterator& i): m_Pos(d), m_Vector(vec), m_Iter(i) {}
00164 ConstIterator(const Iterator& r): m_Pos(r.m_Pos), m_Vector(r.m_Vector), m_Iter(r.m_Iter) {}
00165
00166 ConstElementPointer operator* () { return ConstElementPointer(m_Iter == m_Vector->end() ? NULL : m_Iter->GetPointer()); }
00167 ConstElement* operator-> () { return m_Iter == m_Vector->end() ? NULL : m_Iter->GetPointer(); }
00168 ConstElementPointer operator++ () { ++m_Pos; ++m_Iter; return ConstElementPointer(m_Iter == m_Vector->end() ? NULL : m_Iter->GetPointer()); }
00169 ConstElementPointer operator++ (int) { ConstIterator temp(*this); ++m_Pos; ++m_Iter; return *temp; }
00170 ConstElementPointer operator-- () { --m_Pos; --m_Iter; return ConstElementPointer(m_Iter == m_Vector->end() ? NULL : m_Iter->GetPointer()); }
00171 ConstElementPointer operator-- (int) { ConstIterator temp(*this); --m_Pos; --m_Iter; return *temp; }
00172
00173 ConstIterator& operator = (const Iterator& r) { m_Pos = r.m_Pos; m_Iter = r.m_Iter; return *this; }
00174
00175 bool operator == (const Iterator& r) const { return m_Iter == r.m_Iter; }
00176 bool operator != (const Iterator& r) const { return m_Iter != r.m_Iter; }
00177 bool operator == (const ConstIterator& r) const { return m_Iter == r.m_Iter; }
00178 bool operator != (const ConstIterator& r) const { return m_Iter != r.m_Iter; }
00179
00181 ElementIdentifier Index(void) const { return static_cast<ElementIdentifier>( m_Pos ); }
00182
00184 const Element* Value(void) const { return m_Iter->GetPointer(); }
00185
00186 private:
00187 size_type m_Pos;
00188 const VectorType* m_Vector;
00189 VectorConstIterator m_Iter;
00190 friend class Iterator;
00191 };
00192
00194 ElementPointer& ElementAt(ElementIdentifier);
00195 ConstElementPointer ElementAt(ElementIdentifier) const;
00196 ElementPointer& CreateElementAt(ElementIdentifier);
00197 ConstElementPointer GetElement(ElementIdentifier) const;
00198 void SetElement(ElementIdentifier, Element*);
00199 void InsertElement(ElementIdentifier, Element*);
00200 bool IndexExists(ElementIdentifier) const;
00201 bool GetElementIfIndexExists(ElementIdentifier, Element**) const;
00202 void CreateIndex(ElementIdentifier);
00203 void DeleteIndex(ElementIdentifier);
00204 ConstIterator Begin(void) const;
00205 ConstIterator End(void) const;
00206 Iterator Begin(void);
00207 Iterator End(void);
00208 unsigned long Size(void) const;
00209 void Reserve(ElementIdentifier);
00210 void Squeeze(void);
00211 void Initialize(void);
00212
00213 };
00214
00215 }
00216
00217 #ifndef ITK_MANUAL_INSTANTIATION
00218 #include "itkSmartPointerVectorContainer.txx"
00219 #endif
00220
00221 #endif
00222
00223