blob: 2e7d82774ace9595280dddc330528081db6b0b9b [file] [log] [blame]
Yingdi Yu0b60e7a2015-07-16 21:05:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016 Regents of the University of California.
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
27#ifndef NDN_CXX_HAVE_OSX_SECURITY
28#error "This file should not be included ..."
29#endif
30
31#include <CoreFoundation/CoreFoundation.h>
32#include <Security/Security.h>
33
34namespace ndn {
35namespace security {
36namespace tpm {
37
38/**
39 * @brief Helper class to wrap CoreFoundation object pointers
40 *
41 * The class is similar in spirit to shared_ptr, but uses CoreFoundation
42 * mechanisms to retain/release object.
43 *
44 * Original implementation by Christopher Hunt and it was borrowed from
45 * http://www.cocoabuilder.com/archive/cocoa/130776-auto-cfrelease-and.html
46 */
47template<class T>
48class CFReleaser
49{
50public: // Construction/destruction
51 CFReleaser()
52 : m_typeRef(nullptr)
53 {
54 }
55
56 CFReleaser(const T& typeRef)
57 : m_typeRef(typeRef)
58 {
59 }
60
61 CFReleaser(const CFReleaser& inReleaser)
62 : m_typeRef(nullptr)
63 {
64 retain(inReleaser.m_typeRef);
65 }
66
67 CFReleaser&
68 operator=(const T& typeRef)
69 {
70 if (typeRef != m_typeRef) {
71 release();
72 m_typeRef = typeRef;
73 }
74 return *this;
75 }
76
77 CFReleaser&
78 operator=(const CFReleaser& inReleaser)
79 {
80 retain(inReleaser.m_typeRef);
81 return *this;
82 }
83
84 ~CFReleaser()
85 {
86 release();
87 }
88
89public: // Access
90 const T&
91 get() const
92 {
93 return m_typeRef;
94 }
95
96 T&
97 get()
98 {
99 return m_typeRef;
100 }
101
102 bool
103 operator==(const std::nullptr_t&)
104 {
105 return m_typeRef == nullptr;
106 }
107
108 bool
109 operator!=(const std::nullptr_t&)
110 {
111 return m_typeRef != nullptr;
112 }
113
114 ///////////////////
115 // Miscellaneous //
116
117 void
118 retain(const T& typeRef)
119 {
120 if (typeRef != nullptr) {
121 CFRetain(typeRef);
122 }
123 release();
124 m_typeRef = typeRef;
125 }
126
127 void
128 retain()
129 {
130 T typeRef = m_typeRef;
131 m_typeRef = nullptr;
132 retain(typeRef);
133 }
134
135 void
136 release()
137 {
138 if (m_typeRef != nullptr) {
139 CFRelease(m_typeRef);
140 m_typeRef = nullptr;
141 }
142 };
143
144private:
145 T m_typeRef;
146};
147
148typedef CFReleaser<SecKeyRef> KeyRefOsx;
149
150} // namespace tpm
151} // namespace security
152} // namespace ndn
153
154#endif // NDN_SECURITY_TPM_HELPER_OSX_HPP