Qpid Proton C++  0.12.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
object.hpp
1 #ifndef OBJECT_HPP
2 #define OBJECT_HPP
3 
4 /*
5  * Licensed to the Apache Software Foundation (ASF) under one
6  * or more contributor license agreements. See the NOTICE file
7  * distributed with this work for additional information
8  * regarding copyright ownership. The ASF licenses this file
9  * to you under the Apache License, Version 2.0 (the
10  * "License"); you may not use this file except in compliance
11  * with the License. You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing,
16  * software distributed under the License is distributed on an
17  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18  * KIND, either express or implied. See the License for the
19  * specific language governing permissions and limitations
20  * under the License.
21  */
22 
24 
25 #include "proton/config.hpp"
26 #include "proton/export.hpp"
27 #include "proton/comparable.hpp"
28 #include <memory>
29 
30 namespace proton {
31 
32 class pn_ptr_base {
33  protected:
34  PN_CPP_EXTERN static void incref(void* p);
35  PN_CPP_EXTERN static void decref(void* p);
36 };
37 
38 template <class T> class pn_ptr : private pn_ptr_base, public comparable<pn_ptr<T> > {
39  public:
40  pn_ptr() : ptr_(0) {}
41  pn_ptr(T* p) : ptr_(p) { incref(ptr_); }
42  pn_ptr(const pn_ptr& o) : ptr_(o.ptr_) { incref(ptr_); }
43 
44 #if PN_HAS_CPP11
45  pn_ptr(pn_ptr&& o) : ptr_(0) { std::swap(ptr_, o.ptr_); }
46 #endif
47 
48  ~pn_ptr() { decref(ptr_); }
49 
50  pn_ptr& operator=(pn_ptr o) { std::swap(ptr_, o.ptr_); return *this; }
51 
52  T* get() const { return ptr_; }
53  T* release() { T *p = ptr_; ptr_ = 0; return p; }
54  bool operator!() const { return !ptr_; }
55 
56  static pn_ptr take_ownership(T* p) { return pn_ptr<T>(p, true); }
57 
58  private:
59  T *ptr_;
60 
61  // Note that it is the presence of the bool in the constructor signature that matters
62  // to get the "transfer ownership" constructor: The value of the bool isn't checked.
63  pn_ptr(T* p, bool) : ptr_(p) {}
64 
65  friend bool operator==(const pn_ptr& a, const pn_ptr& b) { return a.ptr_ == b.ptr_; }
66  friend bool operator<(const pn_ptr& a, const pn_ptr& b) { return a.ptr_ < b.ptr_; }
67 };
68 
69 template <class T> pn_ptr<T> take_ownership(T* p) { return pn_ptr<T>::take_ownership(p); }
70 
72 template <class T> class object : public comparable<object<T> > {
73  public:
74  bool operator!() const { return !object_; }
75 
76  protected:
77  object(pn_ptr<T> o) : object_(o) {}
78  T* pn_object() const { return object_.get(); }
79 
80  private:
81  pn_ptr<T> object_;
82 
83  friend bool operator==(const object& a, const object& b) { return a.object_ == b.object_; }
84  friend bool operator<(const object& a, const object& b) { return a.object_ < b.object_; }
85 };
86 
87 }
88 
90 
91 #endif // OBJECT_HPP