23 #if __cplusplus < 201103 && defined( __GLIBCXX__ )
24 # if defined(__INTEL_COMPILER)
25 # include <boost/shared_ptr.hpp>
26 # include <boost/weak_ptr.hpp>
28 # include <tr1/memory>
30 #else // libc++ or other
54 #if __cplusplus < 201103 && defined( __GLIBCXX__ )
55 # if defined(__INTEL_COMPILER)
56 using boost::shared_ptr;
57 using boost::weak_ptr;
59 using std::tr1::shared_ptr;
60 using std::tr1::weak_ptr;
62 #else // libc++ or other
63 using std::shared_ptr;
74 #if __cplusplus >= 201103 || _MSC_VER >= 1600
75 using std::unique_ptr;
80 class unique_ptr :
public std::auto_ptr<T>
82 typedef std::auto_ptr<T> Base;
83 typedef std::auto_ptr_ref<T> unique_ptr_ref;
88 explicit unique_ptr(T* p = 0) throw() : Base(p) { }
90 unique_ptr(
const unique_ptr& r) : Base( const_cast<unique_ptr&>(r) ) { }
92 unique_ptr(unique_ptr_ref m)
throw() : Base(m) { }
95 unique_ptr(
const unique_ptr<U> & r
99 : Base(
const_cast<unique_ptr<U>&
>(r) ) { }
101 unique_ptr & operator=(
const unique_ptr& other)
throw()
103 Base::operator=(const_cast<unique_ptr&>(other));
108 unique_ptr & operator=(
const unique_ptr<U> & other)
throw()
110 Base::operator=(
const_cast<unique_ptr<U>&
>(other));
116 template<
class U>
operator shared_ptr<U>()
121 bool operator!()
const {
return Base::get() == NULL; }
129 typedef void (SafeBool::*bool_cast_type)();
133 operator bool_cast_type()
const
134 {
return !Base::get() ? 0 : &SafeBool::dummy; }
138 bool operator==(
const unique_ptr<T> & p1,
const unique_ptr<T> & p2)
139 {
return p1.get()==p2.get(); }
141 bool operator!=(
const unique_ptr<T> & p1,
const unique_ptr<T> & p2)
142 {
return p1.get()!=p2.get(); }
144 bool operator<(const unique_ptr<T> & p1,
const unique_ptr<T> & p2)
145 {
return p1.get()<p2.get(); }
147 bool operator>(
const unique_ptr<T> & p1,
const unique_ptr<T> & p2)
148 {
return p1.get()>p2.get(); }
150 bool operator<=(const unique_ptr<T> & p1,
const unique_ptr<T> & p2)
151 {
return p1.get()<=p2.get(); }
153 bool operator>=(
const unique_ptr<T> & p1,
const unique_ptr<T> & p2)
154 {
return p1.get()>=p2.get(); }
160 template<
typename T>
operator T*()
const {
return 0;}
162 template<
typename T,
typename U>
operator T U::*()
const {
return 0;}
164 operator void*()
const {
return 0;}
167 void operator&()
const;
180 template <
typename T>
181 inline shared_ptr<T>
make_shared(T *x) {
return shared_ptr<T>(x); }
188 template <
typename T>
190 {
return shared_ptr<T>(
const_cast<T*
>(x), null_deleter<T>); }
197 template <
typename T>
198 inline unique_ptr<T>
make_unique(T * x) {
return unique_ptr<T>(x); }
202 template<
class toC,
typename from>
204 {
return unique_ptr<toC>(
dynamic_cast<toC*
>(p.release()) ); }
207 template <
typename T>
208 inline std::vector<T*>
get_raw(
const std::vector< unique_ptr<T> >& cont)
210 std::vector<T*> result;
211 for (
typename std::vector< unique_ptr<T> >::const_iterator it = cont.begin(); it != cont.end(); ++it)
212 result.push_back(const_cast<T*>( (*it).get() ));
217 template <
typename T>
218 inline std::vector<T*>
get_raw(
const std::vector< shared_ptr<T> >& cont)
220 std::vector<T*> result;
221 for (
typename std::vector< shared_ptr<T> >::const_iterator it = cont.begin(); it != cont.end(); ++it)
222 result.push_back(const_cast<T*>( (*it).get() ));
227 template <
typename T>
228 inline std::vector<T*>
release(std::vector< unique_ptr<T> >& cont)
230 std::vector<T*> result;
231 for (
typename std::vector< unique_ptr<T> >::iterator it = cont.begin(); it != cont.end(); ++it)
232 result.push_back( (*it).release() );
239 #if __cplusplus >= 201103 || _MSC_VER >= 1900
253 template <
class T>
inline
254 auto give(T&& t) -> decltype(std::move(std::forward<T>(t)))
256 return std::move(std::forward<T>(t));
266 template <
typename S>
inline S
give(S & x)
267 { S t; t.swap(x);
return t; }
269 template <
typename T>
inline
270 memory::unique_ptr<T>
give(memory::unique_ptr<T> & x)
271 {
return memory::unique_ptr<T>(x.release()); }
273 template <
typename T>
inline
274 memory::shared_ptr<T>
give(memory::shared_ptr<T> & x)
275 { memory::shared_ptr<T> result = x; x.reset();
return result; }
282 #if defined(gsGmp_ENABLED) || defined(gsMpfr_ENABLED)
283 #define STACK_ARRAY( T, name, sz ) T name[sz];
288 #define STACK_ARRAY( T, name, sz ) T * name = (T*) alloca ( (sz) * sizeof(T) );
294 template <
typename It,
typename ItOut>
297 for (It i = start; i != end; ++i)
298 *out++ =
dynamic_cast<typename std::iterator_traits<ItOut>::value_type
>((*i)->clone().release());
303 template <
typename ContIn,
typename ContOut>
306 out.resize(in.size());
307 cloneAll(in.begin(), in.end(), out.begin());
311 template <
typename It>
314 for (It it = begin; it != end; ++it)
322 template <
typename Cont>
325 for (
typename Cont::iterator it = cont.begin(); it != cont.end(); ++it)
331 template<
typename obj>
inline
334 std::vector<obj*> result;
335 const size_t d = matv.size();
337 for (
size_t i = 0; i!=d; ++i)
338 result.push_back( const_cast<obj*>(&matv[i]) );
343 template <
typename Base,
typename Derived>
346 std::vector<Base*> result(pVec.size());
347 std::copy(pVec.begin(), pVec.end(), result.begin() );
352 template <
typename Derived,
typename Base>
355 for (
typename std::vector<Base*>::iterator it = pVec.begin(); it != pVec.end(); ++it)
356 if ( ! dynamic_cast<Derived*>(*it) )
367 template <
class T,
class U>
368 inline void copy_n(T begin,
const size_t n, U* result)
374 stdext::unchecked_array_iterator<U*>(result));
390 template <
class T,
class U>
391 inline void copy(T begin, T end, U* result)
397 stdext::unchecked_array_iterator<U*>(result));
407 #if __cplusplus < 201103L && _MSC_VER < 1600 && !defined(nullptr)
409 static const gismo::memory::nullptr_t
nullptr ={};
unique_ptr< T > make_unique(T *x)
Definition: gsMemory.h:198
shared_ptr< T > make_shared_not_owned(const T *x)
Creates a shared pointer which does not eventually delete the underlying raw pointer. Usefull to refer to objects which should not be destroyed.
Definition: gsMemory.h:189
std::vector< obj * > asVectorPtr(const std::vector< obj > &matv)
Constructs a vector of pointers from a vector of objects.
Definition: gsMemory.h:332
std::vector< T * > release(std::vector< unique_ptr< T > > &cont)
Takes a vector of smart pointers, releases them and returns the corresponding raw pointers...
Definition: gsMemory.h:228
unique_ptr< toC > convert_ptr(from p)
Converts an uPtr p to an uPtr of class toC and gives it back as return value.
Definition: gsMemory.h:203
std::vector< Base * > castVectorPtr(std::vector< Derived * > pVec)
Casts a vector of pointers.
Definition: gsMemory.h:344
S give(S &x)
Definition: gsMemory.h:266
std::vector< T * > get_raw(const std::vector< unique_ptr< T > > &cont)
Takes a vector of smart pointers and returns the corresponding raw pointers.
Definition: gsMemory.h:208
void cloneAll(It start, It end, ItOut out)
Clones all pointers in the range [start end) and stores new raw pointers in iterator out...
Definition: gsMemory.h:295
void copy_n(T begin, const size_t n, U *result)
Small wrapper for std::copy mimicking memcpy (or std::copy_n) for a raw pointer destination, copies n positions starting from begin into result. The latter is expected to have been allocated in advance.
Definition: gsMemory.h:368
void freeAll(It begin, It end)
Frees all pointers in the range [begin end)
Definition: gsMemory.h:312
void null_deleter(T *)
Deleter function that does not delete an object pointer.
Definition: gsMemory.h:172
bool checkVectorPtrCast(std::vector< Base * > pVec)
Returns true if all instances of Base cast to Derived.
Definition: gsMemory.h:353
void copy(T begin, T end, U *result)
Small wrapper for std::copy mimicking std::copy for a raw pointer destination, copies n positions sta...
Definition: gsMemory.h:391
shared_ptr< T > make_shared(T *x)
Definition: gsMemory.h:181