Lumiera 0.pre.04
»edit your freedom«
Loading...
Searching...
No Matches
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
42#ifndef LIB_SCOPED_PTRVECT_H
43#define LIB_SCOPED_PTRVECT_H
44
45
46#include "include/logging.h"
48#include "lib/nocopy.hpp"
49#include "lib/error.hpp"
50#include "lib/util.hpp"
51
52#include <vector>
53#include <algorithm>
54
55
56namespace lib {
57
58
59
66 template<class T>
69 {
70 using _Vec = std::vector<T*>;
71 using VIter = _Vec::iterator;
72
75
76 using ConstIterType = IterType::ConstIterType;
77 using RcIter = IterType::WrappedConstIterType;
78
80
81 public:
82 typedef size_t size_type;
83 typedef T value_type;
84 typedef T & reference;
85 typedef T const& const_reference;
86
87
88
90 {
91 clear();
92 }
93
95 : vec_{}
96 { }
97
98 explicit
100 : vec_{}
101 {
102 vec_.reserve (capacity);
103 }
104
106 : vec_{std::move (src.vec_)}
107 { }
108
109 friend void
111 {
112 swap (left.vec_, right.vec_);
113 }
114
117 {
118 swap (*this, other);
119 return *this;
120 }
121
122
128 T&
129 manage (T* obj)
130 {
131 REQUIRE (obj);
132 try
133 {
134 vec_.push_back (obj);
135 return *obj;
136 }
137 catch(...)
138 {
139 delete obj;
140 throw;
141 } }
142
143
154 T*
155 detach (void* objAddress)
156 {
157 T* extracted = static_cast<T*> (objAddress);
158 VIter pos = std::find (vec_.begin(),vec_.end(), extracted);
159 if (pos != vec_.end() and *pos != nullptr)
160 {
161 extracted = *pos;
162 vec_.erase(pos); // EX_STRONG
163 return extracted;
164 }
165 return nullptr;
166 }
167
168
169 void
171 {
172 for (T*& p : vec_)
173 if (p)
174 try {
175 delete p;
176 p = nullptr;
177 }
178 ERROR_LOG_AND_IGNORE (library, "Clean-up of ScopedPtrVect");
179 vec_.clear();
180 }
181
182
183 /* === Element access and iteration === */
184
185 T&
187 {
188 return * get(i);
189 }
190
193
194 iterator begin() { return iterator (allPtrs()); }
195 iterator end() { return iterator ( RIter() ); }
196 const_iterator begin() const { return const_iterator::build_by_cast (allPtrs()); }
197 const_iterator end() const { return const_iterator::nil(); }
198
199
200
201
202 /* ====== proxied vector functions ==================== */
203
204 size_type size() const { return vec_.size(); }
205 size_type max_size() const { return vec_.max_size(); }
206 size_type capacity() const { return vec_.capacity(); }
207 bool empty() const { return vec_.empty(); }
208
209
210 private:
212 T*
214 {
215 T* p (vec_.at (i));
216 if (!p)
217 throw lumiera::error::Invalid("no valid object at this index");
218 else
219 return p;
220 }
221
223 RIter
225 {
226 return RIter{vec_.begin(), vec_.end()};
227 }
228 RIter
229 allPtrs() const
230 {
231 _Vec& elements = util::unConst(this)->vec_;
232 return RIter{elements.begin(), elements.end()};
233 }
234 };
235
236
237
238
239} // namespace lib
240#endif
wrapper for an existing Iterator type, automatically dereferencing the output of the former.
Accessing a STL element range through a Lumiera forward iterator, An instance of this iterator adapte...
Simple vector based collection of pointers, managing lifecycle of the pointed-to objects.
_Vec::iterator VIter
const_iterator begin() const
T & operator[](size_type i)
ScopedPtrVect(ScopedPtrVect &&src)
IterType::WrappedConstIterType RcIter
size_type size() const
ScopedPtrVect & operator=(ScopedPtrVect &&other)
ScopedPtrVect(size_type capacity)
friend void swap(ScopedPtrVect &left, ScopedPtrVect &right)
IterType::ConstIterType ConstIterType
size_type max_size() const
PtrDerefIter< RIter > IterType
std::vector< T * > _Vec
RangeIter< VIter > RIter
T * detach(void *objAddress)
withdraw responsibility for a specific object.
const_iterator end() const
size_type capacity() const
ConstIterType const_iterator
T * get(size_type i)
T & manage(T *obj)
take ownership of the given object, adding it at the end of the collection
Types marked with this mix-in may be moved and move-assigned.
Definition nocopy.hpp:64
Lumiera error handling (C++ interface).
#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:267
Extension adapter for Lumiera Forward Iterators to dereference any pointer values automatically.
This header is for including and configuring NoBug.
Implementation namespace for support and library code.
IterQueue< T > elements(T const &elm)
convenience free function to build an iterable sequence
LumieraError< LERR_(INVALID)> Invalid
Definition error.hpp:211
STL namespace.
constexpr IT find(IT, IT, V const &)
OBJ * unConst(const OBJ *)
shortcut to save some typing when having to define const and non-const variants of member functions
Definition util.hpp:358
Mix-Ins to allow or prohibit various degrees of copying and cloning.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...