CppCMS
copy_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_UTIL_COPY_PTR_H
9 #define BOOSTER_UTIL_COPY_PTR_H
10 
11 namespace booster {
12 
21  template<typename T>
22  class copy_ptr {
23  T *ptr_;
24  public:
25  copy_ptr() : ptr_(0) {}
26  explicit copy_ptr(T *v) : ptr_(v) {}
27  copy_ptr(copy_ptr const &other) :
28  ptr_(other.ptr_ ? new T(*other.ptr_) : 0)
29  {
30  }
31  copy_ptr(copy_ptr &&other) : ptr_(other.ptr_)
32  {
33  other.ptr_ = 0;
34  }
35  copy_ptr &operator=(copy_ptr &&other)
36  {
37  if(this!=&other) {
38  this->swap(other);
39  other.reset();
40  }
41  return *this;
42  }
43  copy_ptr const &operator=(copy_ptr const &other)
44  {
45  if(this != &other) {
46  copy_ptr tmp(other);
47  swap(tmp);
48  }
49  return *this;
50  }
51  ~copy_ptr() {
52  if(ptr_) delete ptr_;
53  }
54 
55  T const *get() const { return ptr_; }
56  T *get() { return ptr_; }
57 
58  T const &operator *() const { return *ptr_; }
59  T &operator *() { return *ptr_; }
60  T const *operator->() const { return ptr_; }
61  T *operator->() { return ptr_; }
62  T *release() { T *tmp=ptr_; ptr_=0; return tmp; }
63  void reset(T *p=0)
64  {
65  if(ptr_) delete ptr_;
66  ptr_=p;
67  }
68  void swap(copy_ptr &other)
69  {
70  T *tmp=other.ptr_;
71  other.ptr_=ptr_;
72  ptr_=tmp;
73  }
74  };
75 
76 } // booster
77 
78 #endif
a smart pointer similar to std::unique_ptr but it copies underlying object on pointer copy instead of...
Definition: copy_ptr.h:22
Booster library namespace. The library that implements Boost Like API in ABI backward compatible way...
Definition: application.h:23