Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 0cf887d | 2017-03-26 16:58:59 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
Junxiao Shi | 0b1b467 | 2017-07-02 22:02:31 -0700 | [diff] [blame] | 22 | #ifndef NDN_UTIL_CF_RELEASER_OSX_HPP |
| 23 | #define NDN_UTIL_CF_RELEASER_OSX_HPP |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 24 | |
Junxiao Shi | 0b1b467 | 2017-07-02 22:02:31 -0700 | [diff] [blame] | 25 | #include "../common.hpp" |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 26 | |
Alexander Afanasyev | 0cf887d | 2017-03-26 16:58:59 -0500 | [diff] [blame] | 27 | #ifndef NDN_CXX_HAVE_OSX_FRAMEWORKS |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 28 | #error "This file should not be included ..." |
| 29 | #endif |
| 30 | |
| 31 | #include <CoreFoundation/CoreFoundation.h> |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 32 | |
| 33 | namespace ndn { |
Junxiao Shi | 0b1b467 | 2017-07-02 22:02:31 -0700 | [diff] [blame] | 34 | namespace util { |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * @brief Helper class to wrap CoreFoundation object pointers |
| 38 | * |
| 39 | * The class is similar in spirit to shared_ptr, but uses CoreFoundation |
| 40 | * mechanisms to retain/release object. |
| 41 | * |
| 42 | * Original implementation by Christopher Hunt and it was borrowed from |
| 43 | * http://www.cocoabuilder.com/archive/cocoa/130776-auto-cfrelease-and.html |
Junxiao Shi | 0b1b467 | 2017-07-02 22:02:31 -0700 | [diff] [blame] | 44 | * |
| 45 | * @note The filename cf-releaser-osx.hpp is an intentional violation of code-style rule 2.1. |
| 46 | * Having '-osx' suffix is necessary to prevent installation on non-macOS platforms. |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 47 | */ |
| 48 | template<class T> |
| 49 | class CFReleaser |
| 50 | { |
| 51 | public: // Construction/destruction |
| 52 | CFReleaser() |
| 53 | : m_typeRef(nullptr) |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | CFReleaser(const T& typeRef) |
| 58 | : m_typeRef(typeRef) |
| 59 | { |
| 60 | } |
| 61 | |
| 62 | CFReleaser(const CFReleaser& inReleaser) |
| 63 | : m_typeRef(nullptr) |
| 64 | { |
| 65 | retain(inReleaser.m_typeRef); |
| 66 | } |
| 67 | |
| 68 | CFReleaser& |
| 69 | operator=(const T& typeRef) |
| 70 | { |
| 71 | if (typeRef != m_typeRef) { |
| 72 | release(); |
| 73 | m_typeRef = typeRef; |
| 74 | } |
| 75 | return *this; |
| 76 | } |
| 77 | |
| 78 | CFReleaser& |
| 79 | operator=(const CFReleaser& inReleaser) |
| 80 | { |
| 81 | retain(inReleaser.m_typeRef); |
| 82 | return *this; |
| 83 | } |
| 84 | |
| 85 | ~CFReleaser() |
| 86 | { |
| 87 | release(); |
| 88 | } |
| 89 | |
| 90 | public: // Access |
| 91 | const T& |
| 92 | get() const |
| 93 | { |
| 94 | return m_typeRef; |
| 95 | } |
| 96 | |
| 97 | T& |
| 98 | get() |
| 99 | { |
| 100 | return m_typeRef; |
| 101 | } |
| 102 | |
| 103 | bool |
| 104 | operator==(const std::nullptr_t&) |
| 105 | { |
| 106 | return m_typeRef == nullptr; |
| 107 | } |
| 108 | |
| 109 | bool |
| 110 | operator!=(const std::nullptr_t&) |
| 111 | { |
| 112 | return m_typeRef != nullptr; |
| 113 | } |
| 114 | |
Junxiao Shi | 0b1b467 | 2017-07-02 22:02:31 -0700 | [diff] [blame] | 115 | public: // Miscellaneous |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 116 | void |
| 117 | retain(const T& typeRef) |
| 118 | { |
| 119 | if (typeRef != nullptr) { |
| 120 | CFRetain(typeRef); |
| 121 | } |
| 122 | release(); |
| 123 | m_typeRef = typeRef; |
| 124 | } |
| 125 | |
| 126 | void |
| 127 | retain() |
| 128 | { |
| 129 | T typeRef = m_typeRef; |
| 130 | m_typeRef = nullptr; |
| 131 | retain(typeRef); |
| 132 | } |
| 133 | |
| 134 | void |
| 135 | release() |
| 136 | { |
| 137 | if (m_typeRef != nullptr) { |
| 138 | CFRelease(m_typeRef); |
| 139 | m_typeRef = nullptr; |
| 140 | } |
| 141 | }; |
| 142 | |
| 143 | private: |
| 144 | T m_typeRef; |
| 145 | }; |
| 146 | |
Junxiao Shi | 0b1b467 | 2017-07-02 22:02:31 -0700 | [diff] [blame] | 147 | } // namespace util |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 148 | } // namespace ndn |
| 149 | |
Junxiao Shi | 0b1b467 | 2017-07-02 22:02:31 -0700 | [diff] [blame] | 150 | #endif // NDN_UTIL_CF_RELEASER_OSX_HPP |