/usr/include/dune/common/shared_ptr.hh is in libdune-common-dev 2.2.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 | // -*- tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
// vi: set et ts=8 sw=4 sts=4:
// $Id: smartpointer.hh 5504 2009-04-08 13:35:31Z christi $
#ifndef DUNE_SHARED_PTR_HH
#define DUNE_SHARED_PTR_HH
#if defined SHARED_PTR_HEADER
# include SHARED_PTR_HEADER
#endif
#if defined HAVE_BOOST_SHARED_PTR_HPP
#if defined HAVE_BOOST_MAKE_SHARED_HPP
# include <boost/make_shared.hpp>
#endif
#endif
#include<dune/common/nullptr.hh>
#include<dune/common/typetraits.hh>
/**
* @file
* @brief This file implements the class shared_ptr (a reference counting
* pointer), for those systems that don't have it in the standard library.
* @author Markus Blatt
*/
namespace Dune
{
// A shared_ptr implementation has been found if SHARED_PTR_NAMESPACE is set at all
#ifdef SHARED_PTR_NAMESPACE
using SHARED_PTR_NAMESPACE :: shared_ptr;
#else
/** @addtogroup Common
*
* @{
*/
/** @brief The object we reference. */
class SharedCount
{
template<class T1>
friend class shared_ptr;
protected:
/** @brief The number of references. */
int count_;
/** @brief Constructor from existing Pointer. */
SharedCount() : count_(1) {}
/** @brief Copy constructor with type conversion. */
SharedCount(const SharedCount& rep)
: count_(rep.count_) {}
/** @brief Destructor, deletes element_type* rep_. */
virtual ~SharedCount() {};
};
/**
* @brief A reference counting smart pointer.
*
* It is designed such that it is usable within a std::vector.
* The contained object is destroyed only if there are no more
* references to it.
*/
template<class T>
class shared_ptr
{
template<class T1> friend class shared_ptr;
public:
/**
* @brief The data type we are a pointer for.
*
* This has to have a parameterless constructor.
*/
typedef T element_type;
/**
* @brief Constructs a new smart pointer and allocates the referenced Object.
*/
inline shared_ptr();
inline shared_ptr(nullptr_t null);
/**
* @brief Constructs a new smart pointer from a preallocated Object.
*
* \param pointer Raw pointer to the shared data
*
* note: the object must be allocated on the heap and after handing the pointer to
* shared_ptr the ownership of the pointer is also handed to the shared_ptr.
*/
template<class T1>
inline shared_ptr(T1 * pointer);
/**
* @brief Constructs a new smart pointer from a preallocated Object.
*
* \tparam Deleter This class must by copyconstructable, the copy constructor must not throw an exception
* and it must implement void operator() (T*) const
*
* \param pointer Raw pointer to the shared data
* \param deleter A copy of this deleter is stored
*
* note: the object must be allocated on the heap and after handing the pointer to
* shared_ptr the ownership of the pointer is also handed to the shared_ptr.
*/
template<class T1, class Deleter>
inline shared_ptr(T1 * pointer, Deleter deleter);
/**
* @brief Copy constructor.
* @param pointer The object to copy.
*/
template<class T1>
inline shared_ptr(const shared_ptr<T1>& pointer);
/**
* @brief Copy constructor.
* @param pointer The object to copy.
*/
inline shared_ptr(const shared_ptr& pointer);
/**
* @brief Destructor.
*/
inline ~shared_ptr();
/** \brief Assignment operator */
template<class T1>
inline shared_ptr& operator=(const shared_ptr<T1>& pointer);
/** \brief Assignment operator */
inline shared_ptr& operator=(const shared_ptr& pointer);
/** \brief Dereference as object */
inline element_type& operator*();
/** \brief Dereference as pointer */
inline element_type* operator->();
/** \brief Dereference as const object */
inline const element_type& operator*() const;
/** \brief Dereference as const pointer */
inline const element_type* operator->() const;
/** \brief Access to the raw pointer, if you really want it */
element_type* get() const {
return rep_;
}
/** \brief Checks if shared_ptr manages an object, i.e. whether get() != 0. */
operator bool() const {
return count_ != 0 && rep_ != 0;
}
/** \brief Swap content of this shared_ptr and another */
inline void swap(shared_ptr& other);
/** \brief Decrease the reference count by one and free the memory if the
reference count has reached 0
*/
inline void reset();
/** \brief Detach shared pointer and set it anew for the given pointer */
template<class T1>
inline void reset(T1* pointer);
//** \brief Same as shared_ptr(pointer,deleter).swap(*this)
template<class T1, class Deleter>
inline void reset(T1* pointer, Deleter deleter);
/** \brief The number of shared_ptrs pointing to the object we point to */
int use_count() const;
private:
/** \brief Assignment operator */
template<class T1>
inline shared_ptr& assign(const shared_ptr<T1>& pointer);
/** @brief Adds call to deleter to SharedCount. */
template<class Deleter>
class SharedCountImpl :
public SharedCount
{
template<class T1>
friend class shared_ptr;
/** @brief Constructor from existing Pointer with custom deleter. */
SharedCountImpl(T* elem,const Deleter& deleter) :
SharedCount(),
rep_(elem),
deleter_(deleter)
{}
/** @brief Copy constructor with type conversion. */
SharedCountImpl(const SharedCountImpl& rep)
: SharedCount(rep), deleter_(rep.deleter_) {}
/** @brief Destructor, deletes element_type* rep_ using deleter. */
~SharedCountImpl()
{ deleter_(rep_); }
// store a copy of the deleter
Deleter deleter_;
T* rep_;
};
/** \brief A default deleter that just calls delete */
struct DefaultDeleter
{
void operator() (element_type* p) const
{ delete p; }
};
SharedCount *count_;
T *rep_;
// Needed for the implicit conversion to "bool"
typedef T* *__unspecified_bool_type;
public:
/** \brief Implicit conversion to "bool" */
operator __unspecified_bool_type() const // never throws
{
return rep_ == 0 ? 0 : &shared_ptr::rep_;
}
};
template<class T>
template<class T1>
inline shared_ptr<T>::shared_ptr(T1 * p)
{
rep_ = p;
count_ = new SharedCountImpl<DefaultDeleter>(p, DefaultDeleter());
}
template<class T>
inline shared_ptr<T>::shared_ptr(nullptr_t n)
{
rep_ = 0;
count_ = 0;
}
template<class T>
template<class T1, class Deleter>
inline shared_ptr<T>::shared_ptr(T1 * p, Deleter deleter)
{
rep_ = p;
count_ = new SharedCountImpl<Deleter>(p, deleter);
}
template<class T>
inline shared_ptr<T>::shared_ptr()
{
rep_ = 0;
count_=0;
}
template<class T>
template<class T1>
inline shared_ptr<T>::shared_ptr(const shared_ptr<T1>& other)
: rep_(other.rep_), count_(other.count_)
{
if (rep_)
++(count_->count_);
}
template<class T>
inline shared_ptr<T>::shared_ptr(const shared_ptr& other)
: rep_(other.rep_), count_(other.count_)
{
if (rep_)
++(count_->count_);
}
template<class T>
template<class T1>
inline shared_ptr<T>& shared_ptr<T>::operator=(const shared_ptr<T1>& other)
{
return assign(other);
}
template<class T>
inline shared_ptr<T>& shared_ptr<T>::operator=(const shared_ptr& other)
{
return assign(other);
}
template<class T>
template<class T1>
inline shared_ptr<T>& shared_ptr<T>::assign(const shared_ptr<T1>& other)
{
if (other.count_)
(other.count_->count_)++;
if(rep_!=0 && --(count_->count_)<=0){
delete count_;
}
rep_ = other.rep_;
count_ = other.count_;
return *this;
}
template<class T>
inline shared_ptr<T>::~shared_ptr()
{
if(rep_!=0 && --(count_->count_)==0){
delete count_;
rep_=0;
}
}
template<class T>
inline T& shared_ptr<T>::operator*()
{
return *(rep_);
}
template<class T>
inline T *shared_ptr<T>::operator->()
{
return rep_;
}
template<class T>
inline const T& shared_ptr<T>::operator*() const
{
return *(rep_);
}
template<class T>
inline const T *shared_ptr<T>::operator->() const
{
return rep_;
}
template<class T>
inline int shared_ptr<T>::use_count() const
{
return count_->count_;
}
template<class T>
inline void shared_ptr<T>::swap(shared_ptr<T>& other)
{
SharedCount* dummy = count_;
count_=other.count_;
other.count_ = dummy;
T* tdummy=rep_;
rep_ = other.rep_;
other.rep_ = tdummy;
}
template<class T>
inline void shared_ptr<T>::reset()
{
shared_ptr<T>().swap(*this);
}
template<class T>
template<class T1>
inline void shared_ptr<T>::reset(T1* pointer)
{
shared_ptr<T>(pointer).swap(*this);
}
template<class T>
template<class T1, class Deleter>
inline void shared_ptr<T>::reset(T1* pointer, Deleter deleter)
{
shared_ptr<T>(pointer, deleter).swap(*this);
}
/** @} */
#endif // #ifdef SHARED_PTR_NAMESPACE
// C++0x and Boost have a make_shared implementation, TR1 does not.
// Unfortunately, TR1 gets picked over Boost if present.
// Moreover, boost::make_shared() only exists for (remotely) recent versions of Boost.
#if HAVE_MAKE_SHARED
#ifdef SHARED_PTR_NAMESPACE
using SHARED_PTR_NAMESPACE :: make_shared;
#endif
#else
template<typename T>
shared_ptr<T> make_shared()
{
return shared_ptr<T>(new T());
}
template<typename T, typename Arg1>
shared_ptr<T> make_shared(const Arg1& arg1)
{
return shared_ptr<T>(new T(arg1));
}
template<typename T, typename Arg1, typename Arg2>
shared_ptr<T> make_shared(const Arg1& arg1, const Arg2& arg2)
{
return shared_ptr<T>(new T(arg1,arg2));
}
template<typename T, typename Arg1, typename Arg2, typename Arg3>
shared_ptr<T> make_shared(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3)
{
return shared_ptr<T>(new T(arg1,arg2,arg3));
}
template<typename T, typename Arg1, typename Arg2, typename Arg3, typename Arg4>
shared_ptr<T> make_shared(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3, const Arg4& arg4)
{
return shared_ptr<T>(new T(arg1,arg2,arg3,arg4));
}
template<typename T, typename Arg1, typename Arg2, typename Arg3, typename Arg4,
typename Arg5>
shared_ptr<T> make_shared(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3, const Arg4& arg4,
const Arg5& arg5)
{
return shared_ptr<T>(new T(arg1,arg2,arg3,arg4,arg5));
}
template<typename T, typename Arg1, typename Arg2, typename Arg3, typename Arg4,
typename Arg5, typename Arg6>
shared_ptr<T> make_shared(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3, const Arg4& arg4,
const Arg5& arg5, const Arg6& arg6)
{
return shared_ptr<T>(new T(arg1,arg2,arg3,arg4,arg5,arg6));
}
template<typename T, typename Arg1, typename Arg2, typename Arg3, typename Arg4,
typename Arg5, typename Arg6, typename Arg7>
shared_ptr<T> make_shared(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3, const Arg4& arg4,
const Arg5& arg5, const Arg6& arg6, const Arg7& arg7)
{
return shared_ptr<T>(new T(arg1,arg2,arg3,arg4,arg5,arg6,arg7));
}
template<typename T, typename Arg1, typename Arg2, typename Arg3, typename Arg4,
typename Arg5, typename Arg6, typename Arg7, typename Arg8>
shared_ptr<T> make_shared(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3, const Arg4& arg4,
const Arg5& arg5, const Arg6& arg6, const Arg7& arg7, const Arg8& arg8)
{
return shared_ptr<T>(new T(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8));
}
template<typename T, typename Arg1, typename Arg2, typename Arg3, typename Arg4,
typename Arg5, typename Arg6, typename Arg7, typename Arg8, typename Arg9>
shared_ptr<T> make_shared(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3, const Arg4& arg4,
const Arg5& arg5, const Arg6& arg6, const Arg7& arg7, const Arg8& arg8,
const Arg9& arg9)
{
return shared_ptr<T>(new T(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9));
}
#endif // custom make_shared
/**
@brief implements the Deleter concept of shared_ptr without deleting anything
@relates shared_ptr
If you allocate an object on the stack, but want to pass it to a class or function as a shared_ptr,
you can use this deleter to avoid accidental deletion of the stack-allocated object.
For convenience we provide two free functions to create a shared_ptr from a stack-allocated object
(\see stackobject_to_shared_ptr):
1) Convert a stack-allocated object to a shared_ptr:
@code
int i = 10;
shared_ptr<int> pi = stackobject_to_shared_ptr(i);
@endcode
2) Convert a stack-allocated object to a shared_ptr of a base class
@code
class A {};
class B : public A {};
...
B b;
shared_ptr<A> pa = stackobject_to_shared_ptr<A>(b);
@endcode
@tparam T type of the stack-allocated object
*/
template<class T>
struct null_deleter
{
void operator() (T* p) const {}
};
/**
@brief Convert a stack-allocated object to a shared_ptr:
@relates shared_ptr
@code
int i = 10;
shared_ptr<int> pi = stackobject_to_shared_ptr(i);
@endcode
*/
template<typename T>
inline shared_ptr<T> stackobject_to_shared_ptr(T & t)
{
return shared_ptr<T>(&t, null_deleter<T>());
}
/**
@brief Convert a stack object to a shared_ptr of a base class
@relates shared_ptr
@code
class A {};
class B : public A {};
...
B b;
shared_ptr<A> pa = stackobject_to_shared_ptr<A>(b);
@endcode
*/
template<typename T, typename T2>
inline shared_ptr<T2> stackobject_to_shared_ptr(T & t)
{
return shared_ptr<T2>(dynamic_cast<T2*>(&t), null_deleter<T2>());
}
}
#endif
|