Qpid Proton C++  0.13.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
object.hpp
1 #ifndef PROTON_INTERNAL_OBJECT_HPP
2 #define PROTON_INTERNAL_OBJECT_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 #include "./export.hpp"
27 #include "./comparable.hpp"
28 
29 #include <memory>
30 
31 namespace proton {
32 
33 template <class T> class thread_safe;
34 
35 namespace internal {
36 
37 class pn_ptr_base {
38  protected:
39  PN_CPP_EXTERN static void incref(void* p);
40  PN_CPP_EXTERN static void decref(void* p);
41 };
42 
43 template <class T> class pn_ptr : private pn_ptr_base, private comparable<pn_ptr<T> > {
44  public:
45  pn_ptr() : ptr_(0) {}
46  pn_ptr(T* p) : ptr_(p) { incref(ptr_); }
47  pn_ptr(const pn_ptr& o) : ptr_(o.ptr_) { incref(ptr_); }
48 
49 #if PN_CPP_HAS_RVALUE_REFERENCES
50  pn_ptr(pn_ptr&& o) : ptr_(0) { std::swap(ptr_, o.ptr_); }
51 #endif
52 
53  ~pn_ptr() { decref(ptr_); }
54 
55  pn_ptr& operator=(pn_ptr o) { std::swap(ptr_, o.ptr_); return *this; }
56 
57  T* get() const { return ptr_; }
58  T* release() { T *p = ptr_; ptr_ = 0; return p; }
59 
60  bool operator!() const { return !ptr_; }
61 
62 #if PN_CPP_HAS_EXPLICIT_CONVERSIONS
63  explicit operator bool() const { return !!ptr_; }
64 #endif
65 
66  static pn_ptr take_ownership(T* p) { return pn_ptr<T>(p, true); }
67 
68  private:
69  T *ptr_;
70 
71  // Note that it is the presence of the bool in the constructor signature that matters
72  // to get the "transfer ownership" constructor: The value of the bool isn't checked.
73  pn_ptr(T* p, bool) : ptr_(p) {}
74 
75  friend bool operator==(const pn_ptr& a, const pn_ptr& b) { return a.ptr_ == b.ptr_; }
76  friend bool operator<(const pn_ptr& a, const pn_ptr& b) { return a.ptr_ < b.ptr_; }
77 };
78 
79 template <class T> pn_ptr<T> take_ownership(T* p) { return pn_ptr<T>::take_ownership(p); }
80 
82 template <class T> class object : private comparable<object<T> > {
83  public:
84  bool operator!() const { return !object_; }
85 #if PN_CPP_HAS_EXPLICIT_CONVERSIONS
86  explicit operator bool() const { return object_; }
87 #endif
88 
89  protected:
90  typedef T pn_type;
91  object(pn_ptr<T> o) : object_(o) {}
92  T* pn_object() const { return object_.get(); }
93 
94  private:
95  pn_ptr<T> object_;
96 
97  friend bool operator==(const object& a, const object& b) { return a.object_ == b.object_; }
98  friend bool operator<(const object& a, const object& b) { return a.object_ < b.object_; }
99  template <class U> friend class proton::thread_safe;
100 };
101 
103 template <class T> class factory;
104 
105 } // internal
106 } // proton
107 
108 #endif // PROTON_INTERNAL_OBJECT_HPP
Experimental - A thread-safe object wrapper.
Definition: connection.hpp:45