Qpid Proton C++  0.17.0
pn_unique_ptr.hpp
1 #ifndef PROTON_INTERNAL_UNIQUE_PTR_HPP
2 #define PROTON_INTERNAL_UNIQUE_PTR_HPP
3 
4 /*
5  *
6  * Licensed to the Apache Software Foundation (ASF) under one
7  * or more contributor license agreements. See the NOTICE file
8  * distributed with this work for additional information
9  * regarding copyright ownership. The ASF licenses this file
10  * to you under the Apache License, Version 2.0 (the
11  * "License"); you may not use this file except in compliance
12  * with the License. You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing,
17  * software distributed under the License is distributed on an
18  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19  * KIND, either express or implied. See the License for the
20  * specific language governing permissions and limitations
21  * under the License.
22  *
23  */
24 
25 #include "./config.hpp"
26 
27 #include <memory>
28 
29 namespace proton {
30 namespace internal {
31 
40 template <class T> class pn_unique_ptr {
41  public:
42  pn_unique_ptr(T* p=0) : ptr_(p) {}
43 #if PN_CPP_HAS_RVALUE_REFERENCES
44  pn_unique_ptr(pn_unique_ptr&& x) : ptr_(0) { std::swap(ptr_, x.ptr_); }
45 #else
46  pn_unique_ptr(const pn_unique_ptr& x) : ptr_() { std::swap(ptr_, const_cast<pn_unique_ptr&>(x).ptr_); }
47 #endif
48  ~pn_unique_ptr() { delete(ptr_); }
49  T& operator*() const { return *ptr_; }
50  T* operator->() const { return ptr_; }
51  T* get() const { return ptr_; }
52  void reset(T* p = 0) { pn_unique_ptr<T> tmp(p); std::swap(ptr_, tmp.ptr_); }
53  T* release() { T *p = ptr_; ptr_ = 0; return p; }
54 #if PN_CPP_HAS_EXPLICIT_CONVERSIONS
55  explicit operator bool() const { return get(); }
56 #endif
57  bool operator !() const { return !get(); }
58 
59 #if PN_CPP_HAS_UNIQUE_PTR
60  operator std::unique_ptr<T>() { T *p = ptr_; ptr_ = 0; return std::unique_ptr<T>(p); }
61 #endif
62 
63  private:
64  T* ptr_;
65 };
66 
67 } // internal
68 } // proton
69 
70 #endif // PROTON_INTERNAL_UNIQUE_PTR_HPP
The main Proton namespace.
Definition: annotation_key.hpp:30