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