PROTON-1068: Slightly nicer syntax at point of use for take_ownership()
Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/595a8540 Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/595a8540 Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/595a8540 Branch: refs/heads/go1 Commit: 595a854048093c49c5c1401202b64d957e23a7b3 Parents: bd9a41b Author: Andrew Stitcher <[email protected]> Authored: Thu Dec 10 18:05:27 2015 -0500 Committer: Andrew Stitcher <[email protected]> Committed: Thu Dec 10 18:05:27 2015 -0500 ---------------------------------------------------------------------- proton-c/bindings/cpp/include/proton/object.hpp | 12 +++++++++--- proton-c/bindings/cpp/src/container_impl.cpp | 2 +- proton-c/bindings/cpp/src/data.cpp | 2 +- proton-c/bindings/cpp/src/reactor.cpp | 2 +- 4 files changed, 12 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/595a8540/proton-c/bindings/cpp/include/proton/object.hpp ---------------------------------------------------------------------- diff --git a/proton-c/bindings/cpp/include/proton/object.hpp b/proton-c/bindings/cpp/include/proton/object.hpp index 6e5cef5..34fc5a0 100644 --- a/proton-c/bindings/cpp/include/proton/object.hpp +++ b/proton-c/bindings/cpp/include/proton/object.hpp @@ -46,8 +46,6 @@ template <class T> class pn_ptr : public comparable<pn_ptr<T> >, private pn_ptr_ ~pn_ptr() { decref(ptr_); }; - static pn_ptr<T> take(T* p) { return pn_ptr<T>(p, true); } - pn_ptr& operator=(pn_ptr o) { std::swap(ptr_, o.ptr_); return *this; } T* get() const { return ptr_; } @@ -57,9 +55,17 @@ template <class T> class pn_ptr : public comparable<pn_ptr<T> >, private pn_ptr_ friend bool operator<(const pn_ptr& a, const pn_ptr& b) { return a.ptr_ < b.ptr_; } private: - pn_ptr(T* p, bool) : ptr_(p) {} T *ptr_; + + // Note that it is the presence of the bool in the constructor signature that matters + // to get the "transfer ownership" constructor: The value of the bool isn't checked. + pn_ptr(T* p, bool) : ptr_(p) {} + template <class U> pn_ptr<U> take_ownership(U* p); + friend pn_ptr take_ownership<T>(T* p); }; + +template <class T> pn_ptr<T> take_ownership(T* p) { return pn_ptr<T>(p, true); } + ///@endcond INTERNAL /** http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/595a8540/proton-c/bindings/cpp/src/container_impl.cpp ---------------------------------------------------------------------- diff --git a/proton-c/bindings/cpp/src/container_impl.cpp b/proton-c/bindings/cpp/src/container_impl.cpp index f83e0a6..648aa56 100644 --- a/proton-c/bindings/cpp/src/container_impl.cpp +++ b/proton-c/bindings/cpp/src/container_impl.cpp @@ -115,7 +115,7 @@ class override_handler : public handler pn_ptr<pn_handler_t> container_impl::cpp_handler(handler *h) { if (!h->pn_handler_) { - h->pn_handler_ = pn_ptr<pn_handler_t>::take( + h->pn_handler_ = take_ownership( pn_handler_new(&handler_context::dispatch, sizeof(struct handler_context), &handler_context::cleanup)); http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/595a8540/proton-c/bindings/cpp/src/data.cpp ---------------------------------------------------------------------- diff --git a/proton-c/bindings/cpp/src/data.cpp b/proton-c/bindings/cpp/src/data.cpp index 1eb6ab8..70f3e8f 100644 --- a/proton-c/bindings/cpp/src/data.cpp +++ b/proton-c/bindings/cpp/src/data.cpp @@ -62,7 +62,7 @@ std::ostream& operator<<(std::ostream& o, const data& d) { return o << inspectable(d.pn_object()); } -data data::create() { return pn_ptr<pn_data_t>::take(pn_data(0)); } +data data::create() { return take_ownership(pn_data(0)); } encoder data::encoder() { return proton::encoder(pn_object()); } decoder data::decoder() { return proton::decoder(pn_object()); } http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/595a8540/proton-c/bindings/cpp/src/reactor.cpp ---------------------------------------------------------------------- diff --git a/proton-c/bindings/cpp/src/reactor.cpp b/proton-c/bindings/cpp/src/reactor.cpp index 2a740e8..59db5e0 100644 --- a/proton-c/bindings/cpp/src/reactor.cpp +++ b/proton-c/bindings/cpp/src/reactor.cpp @@ -30,7 +30,7 @@ namespace proton { reactor reactor::create() { - return pn_ptr<pn_reactor_t>::take(pn_reactor()).get(); + return take_ownership(pn_reactor()).get(); } void reactor::run() { pn_reactor_run(pn_object()); } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
