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