From 10eb7233f2ab52bf16a29ac8889bd35a11b0b40a Mon Sep 17 00:00:00 2001 From: James Yonan Date: Wed, 12 Apr 2017 12:20:02 -0600 Subject: [PATCH] Apple CF wrapper: renamed OWN/BORROW. In keeping with Apple terminology, do the following renames: CF::BORROW -> CF::GET CF::OWN -> CF::CREATE This more clearly ties into Apple's "Get" and "Create" rules for object allocation and wrapping. Signed-off-by: James Yonan --- openvpn/apple/maclife.hpp | 2 +- openvpn/applecrypto/cf/cf.hpp | 38 ++++++++++++++++------------- openvpn/tun/mac/macdns_watchdog.hpp | 2 +- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/openvpn/apple/maclife.hpp b/openvpn/apple/maclife.hpp index 0c23731f..d2836999 100644 --- a/openvpn/apple/maclife.hpp +++ b/openvpn/apple/maclife.hpp @@ -126,7 +126,7 @@ namespace openvpn { void thread_func() { - runloop.reset(CFRunLoopGetCurrent(), CF::BORROW); + runloop.reset(CFRunLoopGetCurrent(), CF::GET); Log::Context logctx(logwrap); try { // set up dynamic store query object diff --git a/openvpn/applecrypto/cf/cf.hpp b/openvpn/applecrypto/cf/cf.hpp index 4eb44f03..cbef799f 100644 --- a/openvpn/applecrypto/cf/cf.hpp +++ b/openvpn/applecrypto/cf/cf.hpp @@ -50,7 +50,7 @@ { \ CFTypeRef o = Type::cast(obj); \ if (o) \ - return cls(cftype(o), BORROW); \ + return cls(cftype(o), GET); \ else \ return cls(); \ } @@ -58,9 +58,9 @@ namespace openvpn { namespace CF { - enum Own { - OWN, - BORROW + enum Rule { + CREATE, // create rule + GET // get rule }; template struct Type {}; @@ -71,10 +71,9 @@ namespace openvpn { public: Wrap() : obj_(nullptr) {} - // Set own=BORROW if we don't currently own the object - explicit Wrap(T obj, const Own own=OWN) + explicit Wrap(T obj, const Rule rule=CREATE) { - if (own == BORROW && obj) + if (rule == GET && obj) CFRetain(obj); obj_ = obj; } @@ -116,9 +115,9 @@ namespace openvpn { std::swap(obj_, other.obj_); } - void reset(T obj=nullptr, const Own own=OWN) + void reset(T obj=nullptr, const Rule rule=CREATE) { - if (own == BORROW && obj) + if (rule == GET && obj) CFRetain(obj); if (obj_) CFRelease(obj_); @@ -127,15 +126,20 @@ namespace openvpn { bool defined() const { return obj_ != nullptr; } + explicit operator bool() const noexcept + { + return defined(); + } + T operator()() const { return obj_; } CFTypeRef generic() const { return (CFTypeRef)obj_; } static T cast(CFTypeRef obj) { return T(Type::cast(obj)); } - static Wrap from_generic(CFTypeRef obj, const Own own=OWN) + static Wrap from_generic(CFTypeRef obj, const Rule rule=CREATE) { - return Wrap(cast(obj), own); + return Wrap(cast(obj), rule); } T release() @@ -153,7 +157,7 @@ namespace openvpn { } // Intended for use with Core Foundation methods that require - // a T* for saving a (non-borrowed) return value + // a T* for saving a create-rule return value T* mod_ref() { if (obj_) @@ -179,7 +183,7 @@ namespace openvpn { } private: - Wrap& operator=(T obj); // prevent use because no way to pass ownership parameter + Wrap& operator=(T obj) = delete; // prevent use because no way to pass rule parameter T obj_; }; @@ -202,7 +206,7 @@ namespace openvpn { inline Generic generic_cast(CFTypeRef obj) { - return Generic(obj, BORROW); + return Generic(obj, GET); } // constructors @@ -214,7 +218,7 @@ namespace openvpn { inline String string(CFStringRef str) { - return String(str, BORROW); + return String(str, GET); } inline String string(const String& str) @@ -274,12 +278,12 @@ namespace openvpn { inline Dict const_dict(MutableDict& mdict) { - return Dict(mdict(), CF::BORROW); + return Dict(mdict(), CF::GET); } inline Array const_array(MutableArray& marray) { - return Array(marray(), CF::BORROW); + return Array(marray(), CF::GET); } inline Dict empty_dict() diff --git a/openvpn/tun/mac/macdns_watchdog.hpp b/openvpn/tun/mac/macdns_watchdog.hpp index 419a232c..e9288d8e 100644 --- a/openvpn/tun/mac/macdns_watchdog.hpp +++ b/openvpn/tun/mac/macdns_watchdog.hpp @@ -183,7 +183,7 @@ namespace openvpn { // as well. void thread_func() { - runloop.reset(CFRunLoopGetCurrent(), CF::BORROW); + runloop.reset(CFRunLoopGetCurrent(), CF::GET); Log::Context logctx(logwrap); try {