23#if __cplusplus < 201103 && defined( __GLIBCXX__ )
24# if defined(__INTEL_COMPILER)
25# include <boost/shared_ptr.hpp>
26# include <boost/weak_ptr.hpp>
54#if __cplusplus < 201103 && defined( __GLIBCXX__ )
55# if defined(__INTEL_COMPILER)
56using boost::shared_ptr;
59using std::tr1::shared_ptr;
60using std::tr1::weak_ptr;
74#if __cplusplus >= 201103 || _MSC_VER >= 1600
80class 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>()
119 {
return shared_ptr<U>(Base::release()); }
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; }
138bool operator==(
const unique_ptr<T> & p1,
const unique_ptr<T> & p2)
139{
return p1.get()==p2.get(); }
141bool operator!=(
const unique_ptr<T> & p1,
const unique_ptr<T> & p2)
142{
return p1.get()!=p2.get(); }
144bool operator<(
const unique_ptr<T> & p1,
const unique_ptr<T> & p2)
145{
return p1.get()<p2.get(); }
147bool operator>(
const unique_ptr<T> & p1,
const unique_ptr<T> & p2)
148{
return p1.get()>p2.get(); }
150bool operator<=(
const unique_ptr<T> & p1,
const unique_ptr<T> & p2)
151{
return p1.get()<=p2.get(); }
153bool 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;
181inline shared_ptr<T>
make_shared(T *x) {
return shared_ptr<T>(x); }
190{
return shared_ptr<T>(
const_cast<T*
>(x), null_deleter<T>); }
198inline unique_ptr<T>
make_unique(T * x) {
return unique_ptr<T>(x); }
202template<
class toC,
typename from>
204{
return unique_ptr<toC>(
dynamic_cast<toC*
>(p.release()) ); }
208inline 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() ));
218inline 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() ));
228inline 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
253template <
class T>
inline
254auto give(T&& t) ->
decltype(std::move(std::forward<T>(t)))
256 return std::move(std::forward<T>(t));
266template <
typename S>
inline S
give(S & x)
267{ S t; t.swap(x);
return t; }
269template <
typename T>
inline
270memory::unique_ptr<T>
give(memory::unique_ptr<T> & x)
271{
return memory::unique_ptr<T>(x.release()); }
273template <
typename T>
inline
274memory::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) );
294template <
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());
303template <
typename ContIn,
typename ContOut>
306 out.resize(in.size());
307 cloneAll(in.begin(), in.end(), out.begin());
311template <
typename It>
314 for (It it = begin; it != end; ++it)
322template <
typename Cont>
325 for (
typename Cont::iterator it = cont.begin(); it != cont.end(); ++it)
331template<
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]) );
343template <
typename Base,
typename Derived>
346 std::vector<Base*> result(pVec.size());
347 std::copy(pVec.begin(), pVec.end(), result.begin() );
352template <
typename Derived,
typename Base>
355 for (
typename std::vector<Base*>::iterator it = pVec.begin(); it != pVec.end(); ++it)
356 if ( !
dynamic_cast<Derived*
>(*it) )
367template <
class T,
class U>
368inline void copy_n(T begin,
const size_t n, U* result)
370 std::copy(begin, begin+n,
374 stdext::unchecked_array_iterator<U*>(result));
390template <
class T,
class U>
391inline void copy(T begin, T end, U* result)
393 std::copy(begin, end,
397 stdext::unchecked_array_iterator<U*>(result));
407#if __cplusplus < 201103L && _MSC_VER < 1600 && !defined(nullptr)
409static const gismo::memory::nullptr_t
nullptr ={};
void null_deleter(T *)
Deleter function that does not delete an object pointer.
Definition gsMemory.h:172
shared_ptr< T > make_shared_not_owned(const T *x)
Creates a shared pointer which does not eventually delete the underlying raw pointer....
Definition gsMemory.h:189
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
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
unique_ptr< T > make_unique(T *x)
Definition gsMemory.h:198
shared_ptr< T > make_shared(T *x)
Definition gsMemory.h:181
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
The G+Smo namespace, containing all definitions for the library.
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
std::vector< Base * > castVectorPtr(std::vector< Derived * > pVec)
Casts a vector of pointers.
Definition gsMemory.h:344
bool checkVectorPtrCast(std::vector< Base * > pVec)
Returns true if all instances of Base cast to Derived.
Definition gsMemory.h:353
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,...
Definition gsMemory.h:368
std::vector< obj * > asVectorPtr(const std::vector< obj > &matv)
Constructs a vector of pointers from a vector of objects.
Definition gsMemory.h:332
S give(S &x)
Definition gsMemory.h:266
void freeAll(It begin, It end)
Frees all pointers in the range [begin end)
Definition gsMemory.h:312
This namespace gathers several utility functions for miscellaneous tasks.