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)
5  2009, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 */
13 
37 #ifndef LIB_SCOPED_PTRVECT_H
38 #define LIB_SCOPED_PTRVECT_H
39 
40 
41 #include "include/logging.h"
43 #include "lib/nocopy.hpp"
44 #include "lib/error.hpp"
45 #include "lib/util.hpp"
46 
47 #include <vector>
48 #include <algorithm>
49 
50 
51 namespace lib {
52 
53 
54 
61  template<class T>
63  : std::vector<T*>,
65  {
66  typedef std::vector<T*> _Vec;
67  typedef typename _Vec::iterator VIter;
68 
69  typedef RangeIter<VIter> RIter;
71 
72  typedef typename IterType::ConstIterType ConstIterType;
73  typedef typename IterType::WrappedConstIterType RcIter;
74 
75 
76  public:
77  typedef size_t size_type;
78  typedef T value_type;
79  typedef T & reference;
80  typedef T const& const_reference;
81 
82 
83 
84  ScopedPtrVect ()
85  : _Vec()
86  { }
87 
88  explicit
89  ScopedPtrVect (size_type capacity)
90  : _Vec()
91  {
92  _Vec::reserve (capacity);
93  }
94 
95  ~ScopedPtrVect ()
96  {
97  clear();
98  }
99 
100 
106  T&
107  manage (T* obj)
108  {
109  REQUIRE (obj);
110  try
111  {
112  this->push_back (obj);
113  return *obj;
114  }
115  catch(...)
116  {
117  delete obj;
118  throw;
119  } }
120 
121 
133  T*
134  detach (void* objAddress)
135  {
136  T* extracted = static_cast<T*> (objAddress);
137  VIter pos = std::find (_Vec::begin(),_Vec::end(), extracted);
138  if (pos != _Vec::end() && bool(*pos))
139  {
140  extracted = *pos;
141  _Vec::erase(pos); // EX_STRONG
142  return extracted;
143  }
144  return NULL;
145  }
146 
147 
148  void
149  clear()
150  {
151  VIter e = _Vec::end();
152  for (VIter i = _Vec::begin(); i!=e; ++i)
153  if (*i)
154  try {
155  delete *i;
156  *i = 0;
157  }
158  ERROR_LOG_AND_IGNORE (library, "Clean-up of ScopedPtrVect");
159  _Vec::clear();
160  }
161 
162 
163  /* === Element access and iteration === */
164 
165  T&
166  operator[] (size_type i)
167  {
168  return *get(i);
169  }
170 
171  typedef IterType iterator;
172  typedef ConstIterType const_iterator;
173 
174  iterator begin() { return iterator (allPtrs()); }
175  iterator end() { return iterator ( RIter() ); }
176  const_iterator begin() const { return const_iterator::build_by_cast (allPtrs()); }
177  const_iterator end() const { return const_iterator::nil(); }
178 
179 
180 
181 
182  /* ====== proxied vector functions ==================== */
183 
184  size_type size () const { return _Vec::size(); }
185  size_type max_size () const { return _Vec::max_size(); }
186  size_type capacity () const { return _Vec::capacity(); }
187  bool empty () const { return _Vec::empty(); }
188 
189 
190  private:
192  T*
193  get (size_type i)
194  {
195  T* p (_Vec::at (i));
196  if (!p)
197  throw lumiera::error::Invalid("no valid object at this index");
198  else
199  return p;
200  }
201 
203  RIter
205  {
206  return RIter (_Vec::begin(), _Vec::end());
207  }
208  RIter
209  allPtrs () const
210  {
211  _Vec& elements = util::unConst(*this);
212  return RIter (elements.begin(), elements.end());
213  }
214  };
215 
216 
217 
218 
219 } // namespace lib
220 #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:292
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:266
Any copy and copy construction prohibited.
Definition: nocopy.hpp:37
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:190
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