Lumiera  0.pre.03
»edit your freedom«
scoped-ptrvect.hpp
Go to the documentation of this file.
1 /*
2  SCOPED-PTRVECT.hpp - simple noncopyable lifecycle managing collection of pointers
3 
4  Copyright (C) Lumiera.org
5  2009, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 */
22 
46 #ifndef LIB_SCOPED_PTRVECT_H
47 #define LIB_SCOPED_PTRVECT_H
48 
49 
50 #include "include/logging.h"
52 #include "lib/nocopy.hpp"
53 #include "lib/error.hpp"
54 #include "lib/util.hpp"
55 
56 #include <vector>
57 #include <algorithm>
58 
59 
60 namespace lib {
61 
62 
63 
70  template<class T>
72  : std::vector<T*>,
74  {
75  typedef std::vector<T*> _Vec;
76  typedef typename _Vec::iterator VIter;
77 
78  typedef RangeIter<VIter> RIter;
80 
81  typedef typename IterType::ConstIterType ConstIterType;
82  typedef typename IterType::WrappedConstIterType RcIter;
83 
84 
85  public:
86  typedef size_t size_type;
87  typedef T value_type;
88  typedef T & reference;
89  typedef T const& const_reference;
90 
91 
92 
93  ScopedPtrVect ()
94  : _Vec()
95  { }
96 
97  explicit
98  ScopedPtrVect (size_type capacity)
99  : _Vec()
100  {
101  _Vec::reserve (capacity);
102  }
103 
104  ~ScopedPtrVect ()
105  {
106  clear();
107  }
108 
109 
115  T&
116  manage (T* obj)
117  {
118  REQUIRE (obj);
119  try
120  {
121  this->push_back (obj);
122  return *obj;
123  }
124  catch(...)
125  {
126  delete obj;
127  throw;
128  } }
129 
130 
142  T*
143  detach (void* objAddress)
144  {
145  T* extracted = static_cast<T*> (objAddress);
146  VIter pos = std::find (_Vec::begin(),_Vec::end(), extracted);
147  if (pos != _Vec::end() && bool(*pos))
148  {
149  extracted = *pos;
150  _Vec::erase(pos); // EX_STRONG
151  return extracted;
152  }
153  return NULL;
154  }
155 
156 
157  void
158  clear()
159  {
160  VIter e = _Vec::end();
161  for (VIter i = _Vec::begin(); i!=e; ++i)
162  if (*i)
163  try {
164  delete *i;
165  *i = 0;
166  }
167  ERROR_LOG_AND_IGNORE (library, "Clean-up of ScopedPtrVect");
168  _Vec::clear();
169  }
170 
171 
172  /* === Element access and iteration === */
173 
174  T&
175  operator[] (size_type i)
176  {
177  return *get(i);
178  }
179 
180  typedef IterType iterator;
181  typedef ConstIterType const_iterator;
182 
183  iterator begin() { return iterator (allPtrs()); }
184  iterator end() { return iterator ( RIter() ); }
185  const_iterator begin() const { return const_iterator::build_by_cast (allPtrs()); }
186  const_iterator end() const { return const_iterator::nil(); }
187 
188 
189 
190 
191  /* ====== proxied vector functions ==================== */
192 
193  size_type size () const { return _Vec::size(); }
194  size_type max_size () const { return _Vec::max_size(); }
195  size_type capacity () const { return _Vec::capacity(); }
196  bool empty () const { return _Vec::empty(); }
197 
198 
199  private:
201  T*
202  get (size_type i)
203  {
204  T* p (_Vec::at (i));
205  if (!p)
206  throw lumiera::error::Invalid("no valid object at this index");
207  else
208  return p;
209  }
210 
212  RIter
214  {
215  return RIter (_Vec::begin(), _Vec::end());
216  }
217  RIter
218  allPtrs () const
219  {
220  _Vec& elements = util::unConst(*this);
221  return RIter (elements.begin(), elements.end());
222  }
223  };
224 
225 
226 
227 
228 } // namespace lib
229 #endif
wrapper for an existing Iterator type, automatically dereferencing the output of the former...
IterQueue< T > elements(T const &elm)
convenience free function to build an iterable sequence
Definition: iter-stack.hpp:301
Extension adapter for Lumiera Forward Iterators to dereference any pointer values automatically...
#define ERROR_LOG_AND_IGNORE(_FLAG_, _OP_DESCR_)
convenience shortcut for a sequence of catch blocks just logging and consuming an error...
Definition: error.hpp:275
Any copy and copy construction prohibited.
Definition: nocopy.hpp:46
This header is for including and configuring NoBug.
Implementation namespace for support and library code.
Simple vector based collection of pointers, managing lifecycle of the pointed-to objects.
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:199
Mix-Ins to allow or prohibit various degrees of copying and cloning.
static PtrDerefIter build_by_cast(WrappedIterType const &srcIter)
explicit builder to allow creating a const variant from the basic srcIter type.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
T * detach(void *objAddress)
withdraw responsibility for a specific object.
Lumiera error handling (C++ interface).
Accessing a STL element range through a Lumiera forward iterator, An instance of this iterator adapte...
T & manage(T *obj)
take ownership of the given object, adding it at the end of the collection