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