IODA Bundle
SharedIterator.h
Go to the documentation of this file.
1 /// @file SharedIterator.h
2 /// @author Tomas Kral
3 
4 #ifndef ODBLIB_SHAREDITERATOR_H_
5 #define ODBLIB_SHAREDITERATOR_H_
6 
7 namespace odc {
8 
9 /*! @brief Represents a smart iterator.
10  *
11  * The SharedIterator class uses reference counting to manage shared ownership
12  * of an iterator object. Several instances of SharedIterator may refer to the
13  * same iterator object. The managed iterator object is destroyed only when
14  * the last SharedIterator pointing to it is destroyed.
15  *
16  * @see IteratorFacade
17  * @todo Write some test cases.
18  */
19 template <typename T>
21 {
22 public:
23  /// @brief Difference type of the managed iterator.
24  /// Provided for compatibility with C++ STL algorithms.
25 
26  typedef typename T::difference_type difference_type;
27 
28  /// @brief Value type of the managed iterator.
29  /// Provided for compatibility with C++ STL algorithms.
30 
31  typedef typename T::value_type value_type;
32 
33  /// @brief Reference type of the managed iterator.
34  /// Provided for compatibility with C++ STL algorithms.
35 
36  typedef typename T::reference reference;
37 
38  /// @brief Pointer type of the managed iterator.
39  /// Provided for compatibility with C++ STL algorithms.
40 
41  typedef typename T::pointer pointer;
42 
43  /// @brief Cagegory of the managed iterator.
44  /// Provided for compatibility with C++ STL algorithms.
45 
46  typedef typename T::iterator_category iterator_category;
47 
48  /*! @brief Creates a smart iterator.
49  *
50  * This templated constructor allows to create a new SharedIterator instance
51  * from any pointer to an iterator object @em it that is implicitly convertible
52  * to type @em T.
53  *
54  * @warning If the iterator object pointed to by @e it is already owned by
55  * some other SharedIterator instance, the usage of @c this SharedIterator
56  * will result in undefined behavior.
57  */
58  template <typename U>
59  explicit SharedIterator(U* it)
60  : it_(it),
61  useCount_(0)
62  {
63  if (it_) useCount_ = new long (1);
64  }
65 
66  /*! @brief Creates a smart iterator which shares ownership of the iterator
67  * object managed by the @em other smart iterator.
68  */
70  : it_(other.it_),
71  useCount_(other.useCount_)
72  {
73  if (it_) ++(*useCount_);
74  }
75 
76  /*! @brief Creates a smart iterator which shares ownership of the iterator
77  * object managed by the @em other smart iterator.
78  *
79  * This templated copy-constructor allows to create new SharedIterator
80  * from any @em other SharedIterator who's iterator type is implicitly
81  * convertible to @em T.
82  */
83  template <typename U>
85  : it_(other.it_),
86  useCount_(other.useCount_)
87  {
88  if (it_) ++(*useCount_);
89  }
90 
91  /// @brief Destroys the managed iterator if there are no more SharedIterator
92  /// instances referring to it.
94  {
95  destruct();
96  }
97 
98  /// @brief Replaces the wrapped iterator object with the one managed by @em other.
100  {
101  if (it_ == other.it_)
102  return *this;
103 
104  destruct();
105 
106  it_ = other.it_;
107  useCount_ = other.useCount_;
108  ++(*useCount_);
109 
110  return *this;
111  }
112 
113  /// @brief Compares two managed iterator objects.
114  bool operator==(const SharedIterator& other)
115  {
116  return (*it_ == *other.it_);
117  }
118 
119  /// @brief Compares two managed iterator objects.
120  bool operator!=(const SharedIterator& other)
121  {
122  return (*it_ != *other.it_);
123  }
124 
125  /// @brief Increments the managed iterator object.
127  {
128  ++(*it_);
129  return *this;
130  }
131 
132  /// @brief Dereferences the managed iterator object.
133  reference operator*() const { return (**it_); }
134 
135  /// @brief Dereferences the managed iterator object.
136  pointer operator->() const { return &(**it_); }
137 
138  /// @brief Returns pointer to the managed iterator object.
139  T* get() const { return it_; }
140 
141  /*! @brief Checks if @c this is the only SharedIterator instance managing
142  * the iterator object.
143  *
144  * Retuns @c true if @c this is the only SharedIterator instance managing
145  * the current iterator object (i.e. useCount() == 1), otherwise returns
146  * @c false.
147  */
148  bool unique() const
149  {
150  return useCount_ && (*useCount_ == 1);
151  }
152 
153  /*! @brief Returns the number of SharedIterator instances (including
154  * @c this) managing the same iterator object.
155  */
156  long useCount() const
157  {
158  return useCount_ ? *useCount_ : 0;
159  }
160 
161 private:
162  void destruct()
163  {
164  if (it_ && (--(*useCount_) == 0))
165  {
166  delete it_;
167  delete useCount_;
168  }
169  }
170 
171  T* it_;
172  long* useCount_;
173 };
174 
175 } // namespace odc
176 
177 #endif // ODBLIB_SHAREDITERATOR_H_
Represents a smart iterator.
bool operator!=(const SharedIterator &other)
Compares two managed iterator objects.
long useCount() const
Returns the number of SharedIterator instances (including this) managing the same iterator object.
T::reference reference
Reference type of the managed iterator. Provided for compatibility with C++ STL algorithms.
~SharedIterator()
Destroys the managed iterator if there are no more SharedIterator instances referring to it.
T * get() const
Returns pointer to the managed iterator object.
T::value_type value_type
Value type of the managed iterator. Provided for compatibility with C++ STL algorithms.
SharedIterator & operator++()
Increments the managed iterator object.
reference operator*() const
Dereferences the managed iterator object.
bool unique() const
Checks if this is the only SharedIterator instance managing the iterator object.
T::iterator_category iterator_category
Cagegory of the managed iterator. Provided for compatibility with C++ STL algorithms.
SharedIterator(const SharedIterator< U > &other)
Creates a smart iterator which shares ownership of the iterator object managed by the other smart ite...
T::difference_type difference_type
Difference type of the managed iterator. Provided for compatibility with C++ STL algorithms.
bool operator==(const SharedIterator &other)
Compares two managed iterator objects.
pointer operator->() const
Dereferences the managed iterator object.
SharedIterator & operator=(const SharedIterator &other)
Replaces the wrapped iterator object with the one managed by other.
SharedIterator(const SharedIterator &other)
Creates a smart iterator which shares ownership of the iterator object managed by the other smart ite...
SharedIterator(U *it)
Creates a smart iterator.
T::pointer pointer
Pointer type of the managed iterator. Provided for compatibility with C++ STL algorithms.
Definition: ColumnInfo.h:23