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 | |
| 22 | #ifndef NDN_SECURITY_TPM_HELPER_OSX_HPP |
| 23 | #define NDN_SECURITY_TPM_HELPER_OSX_HPP |
| 24 | |
| 25 | #include "../../common.hpp" |
| 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> |
| 32 | #include <Security/Security.h> |
| 33 | |
| 34 | namespace ndn { |
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 |
| 44 | */ |
| 45 | template<class T> |
| 46 | class CFReleaser |
| 47 | { |
| 48 | public: // Construction/destruction |
| 49 | CFReleaser() |
| 50 | : m_typeRef(nullptr) |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | CFReleaser(const T& typeRef) |
| 55 | : m_typeRef(typeRef) |
| 56 | { |
| 57 | } |
| 58 | |
| 59 | CFReleaser(const CFReleaser& inReleaser) |
| 60 | : m_typeRef(nullptr) |
| 61 | { |
| 62 | retain(inReleaser.m_typeRef); |
| 63 | } |
| 64 | |
| 65 | CFReleaser& |
| 66 | operator=(const T& typeRef) |
| 67 | { |
| 68 | if (typeRef != m_typeRef) { |
| 69 | release(); |
| 70 | m_typeRef = typeRef; |
| 71 | } |
| 72 | return *this; |
| 73 | } |
| 74 | |
| 75 | CFReleaser& |
| 76 | operator=(const CFReleaser& inReleaser) |
| 77 | { |
| 78 | retain(inReleaser.m_typeRef); |
| 79 | return *this; |
| 80 | } |
| 81 | |
| 82 | ~CFReleaser() |
| 83 | { |
| 84 | release(); |
| 85 | } |
| 86 | |
| 87 | public: // Access |
| 88 | const T& |
| 89 | get() const |
| 90 | { |
| 91 | return m_typeRef; |
| 92 | } |
| 93 | |
| 94 | T& |
| 95 | get() |
| 96 | { |
| 97 | return m_typeRef; |
| 98 | } |
| 99 | |
| 100 | bool |
| 101 | operator==(const std::nullptr_t&) |
| 102 | { |
| 103 | return m_typeRef == nullptr; |
| 104 | } |
| 105 | |
| 106 | bool |
| 107 | operator!=(const std::nullptr_t&) |
| 108 | { |
| 109 | return m_typeRef != nullptr; |
| 110 | } |
| 111 | |
| 112 | /////////////////// |
| 113 | // Miscellaneous // |
| 114 | |
| 115 | void |
| 116 | retain(const T& typeRef) |
| 117 | { |
| 118 | if (typeRef != nullptr) { |
| 119 | CFRetain(typeRef); |
| 120 | } |
| 121 | release(); |
| 122 | m_typeRef = typeRef; |
| 123 | } |
| 124 | |
| 125 | void |
| 126 | retain() |
| 127 | { |
| 128 | T typeRef = m_typeRef; |
| 129 | m_typeRef = nullptr; |
| 130 | retain(typeRef); |
| 131 | } |
| 132 | |
| 133 | void |
| 134 | release() |
| 135 | { |
| 136 | if (m_typeRef != nullptr) { |
| 137 | CFRelease(m_typeRef); |
| 138 | m_typeRef = nullptr; |
| 139 | } |
| 140 | }; |
| 141 | |
| 142 | private: |
| 143 | T m_typeRef; |
| 144 | }; |
| 145 | |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 146 | namespace security { |
| 147 | namespace tpm { |
| 148 | |
| 149 | using KeyRefOsx = CFReleaser<SecKeyRef>; |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 150 | |
| 151 | } // namespace tpm |
| 152 | } // namespace security |
Alexander Afanasyev | 3b3355c | 2017-03-26 11:57:13 -0500 | [diff] [blame] | 153 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 154 | } // namespace ndn |
| 155 | |
| 156 | #endif // NDN_SECURITY_TPM_HELPER_OSX_HPP |