Qpid Proton C++ API  0.38.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 "./export.hpp"
26 #include "./comparable.hpp"
27 
28 #include <memory>
29 #include <string>
30 
31 namespace proton {
32 
33 template <class T> class returned;
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  PN_CPP_EXTERN static std::string inspect(void* p);
42 };
43 
44 template <class T> class pn_ptr : private pn_ptr_base, private comparable<pn_ptr<T> > {
45  public:
46  pn_ptr() : ptr_(0) {}
47  pn_ptr(T* p) : ptr_(p) { incref(ptr_); }
48  pn_ptr(const pn_ptr& o) : ptr_(o.ptr_) { incref(ptr_); }
49  pn_ptr(pn_ptr&& o) : ptr_(0) { std::swap(ptr_, o.ptr_); }
50 
51  ~pn_ptr() { decref(ptr_); }
52 
53  pn_ptr& operator=(pn_ptr o) { std::swap(ptr_, o.ptr_); return *this; }
54 
55  T* get() const { return ptr_; }
56  T* release() { T *p = ptr_; ptr_ = 0; return p; }
57 
58  bool operator!() const { return !ptr_; }
59  explicit operator bool() const { return !!ptr_; }
60 
61  std::string inspect() const { return pn_ptr_base::inspect(ptr_); }
62 
63  static pn_ptr take_ownership(T* p) { return pn_ptr<T>(p, true); }
64 
65  private:
66  T *ptr_;
67 
68  // Note that it is the presence of the bool in the constructor signature that matters
69  // to get the "transfer ownership" constructor: The value of the bool isn't checked.
70  pn_ptr(T* p, bool) : ptr_(p) {}
71 
72  friend bool operator==(const pn_ptr& a, const pn_ptr& b) { return a.ptr_ == b.ptr_; }
73  friend bool operator<(const pn_ptr& a, const pn_ptr& b) { return a.ptr_ < b.ptr_; }
74 };
75 
76 template <class T> pn_ptr<T> take_ownership(T* p) { return pn_ptr<T>::take_ownership(p); }
77 
79 template <class T> class object : private comparable<object<T> > {
80  public:
81  bool operator!() const { return !object_; }
82  explicit operator bool() const { return object_.get(); }
83 
84  protected:
85  typedef T pn_type;
86  object(pn_ptr<T> o) : object_(o) {}
87  T* pn_object() const { return object_.get(); }
88 
89  private:
90  pn_ptr<T> object_;
91 
92  friend bool operator==(const object& a, const object& b) { return a.object_ == b.object_; }
93  friend bool operator<(const object& a, const object& b) { return a.object_ < b.object_; }
94  friend std::ostream& operator<<(std::ostream& o, const object& a) { o << a.object_.inspect(); return o; }
95 
96  template <class U> friend class proton::returned;
97 };
98 
100 template <class T> class factory;
101 
102 } // internal
103 } // proton
104 
105 #endif // PROTON_INTERNAL_OBJECT_HPP
A return type for container methods.
Definition: returned.hpp:51
The main Proton namespace.
Definition: annotation_key.hpp:33
T get(const scalar &s)
Get a contained value of type T.
Definition: scalar.hpp:60
void swap(map< K, T > &, map< K, T > &)
Swap proton::map instances.
std::ostream & operator<<(std::ostream &, const binary &)
Print a binary value.