Created
June 18, 2017 11:39
-
-
Save mntone/1af606730bb62ce3d2d56635a59a30bb to your computer and use it in GitHub Desktop.
MRC 環境でゴニョゴニョする何か。正直実用的でないし,ARC + C++ (Objective-C++) で綺麗に管理されるのでそれを使っておけ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma once | |
| template<typename T> | |
| class objc_ptr { | |
| protected: | |
| T *ptr; | |
| public: | |
| inline objc_ptr() : ptr(nil) {} | |
| inline explicit objc_ptr(T *p) : ptr(p) {if (ptr) [ptr retain];} | |
| objc_ptr(const objc_ptr &c) = delete; | |
| inline objc_ptr(objc_ptr &&c) : ptr(c.ptr) {c.ptr = nil;} | |
| inline ~objc_ptr() {[ptr release];} | |
| inline objc_ptr &operator=(T *p) noexcept | |
| { | |
| reset(p); | |
| return *this; | |
| } | |
| objc_ptr &operator=(const objc_ptr &c) = delete; | |
| inline objc_ptr &operator=(objc_ptr &&c) | |
| { | |
| if (this != &c) { | |
| reset(); | |
| ptr = c.ptr; | |
| c.ptr = nullptr; | |
| } | |
| return *this; | |
| } | |
| inline T *release() noexcept | |
| { | |
| T *p = ptr; | |
| ptr = nullptr; | |
| return p; | |
| } | |
| inline void reset(T *p = nil) noexcept | |
| { | |
| if (ptr != p) { | |
| [ptr release]; | |
| [p retain]; | |
| ptr = p; | |
| } | |
| } | |
| inline T *get() const noexcept {return ptr;} | |
| inline typename std::add_lvalue_reference<T>::type operator*() const | |
| {return ptr;} | |
| inline T *operator->() const noexcept {return ptr;} | |
| inline explicit operator bool() const noexcept {return !!ptr;} | |
| inline bool operator!() const noexcept {return !ptr;} | |
| }; | |
| template <class T1, class T2> | |
| bool operator==(const objc_ptr<T1> &a, const objc_ptr<T2> &b) | |
| {return a.ptr == b.ptr;} | |
| template <class T> | |
| bool operator==(const objc_ptr<T> &x, std::nullptr_t) noexcept | |
| {return x.ptr == nullptr;} | |
| template <class T> | |
| bool operator==(std::nullptr_t, const objc_ptr<T> &x) noexcept | |
| {return x.ptr == nullptr;} | |
| template <class T1, class T2> | |
| bool operator!=(const objc_ptr<T1> &a, const objc_ptr<T2> &b) | |
| {return a.ptr != b.ptr;} | |
| template <class T> | |
| bool operator!=(const objc_ptr<T> &x, std::nullptr_t) noexcept | |
| {return x.ptr != nullptr;} | |
| template <class T> | |
| bool operator!=(std::nullptr_t, const objc_ptr<T> &x) noexcept | |
| {return x.ptr != nullptr;} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment