CppCMS
hold_ptr.h
1 //
2 // Copyright (C) 2009-2012 Artyom Beilis (Tonkikh)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8 #ifndef BOOSTER_HOLD_PTR_H
9 #define BOOSTER_HOLD_PTR_H
10 
11 namespace booster {
12 
17  template<typename T>
18  class hold_ptr {
19  T *ptr_;
20  public:
21  hold_ptr(hold_ptr const &other) = delete; // non copyable
22  hold_ptr const &operator=(hold_ptr const &other) = delete; // non assignable
23  hold_ptr() : ptr_(0) {}
24  explicit hold_ptr(T *v) : ptr_(v) {}
25  hold_ptr(hold_ptr &&other) : ptr_(other.ptr_)
26  {
27  other.ptr_ = 0;
28  }
29  hold_ptr &operator=(hold_ptr &&other)
30  {
31  if(this!=&other) {
32  this->swap(other);
33  other.reset();
34  }
35  return *this;
36  }
37  ~hold_ptr()
38  {
39  if(ptr_) delete ptr_;
40  }
41 
42  T const *get() const { return ptr_; }
43  T *get() { return ptr_; }
44 
45  T const &operator *() const { return *ptr_; }
46  T &operator *() { return *ptr_; }
47  T const *operator->() const { return ptr_; }
48  T *operator->() { return ptr_; }
49  T *release() { T *tmp=ptr_; ptr_=0; return tmp; }
50  void reset(T *p=0)
51  {
52  if(ptr_) delete ptr_;
53  ptr_=p;
54  }
55  void swap(hold_ptr &other)
56  {
57  T *tmp=other.ptr_;
58  other.ptr_=ptr_;
59  ptr_=tmp;
60  }
61  };
62 } // booster
63 
64 #endif
a smart pointer similar to std::unique_ptr but it is non-copyable and underlying object has same cons...
Definition: hold_ptr.h:18
Booster library namespace. The library that implements Boost Like API in ABI backward compatible way...
Definition: application.h:23