All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
helper-osx.hpp
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
23 #ifndef NDN_HELPER_OSX_HPP
24 #define NDN_HELPER_OSX_HPP
25 
26 // Only compile if ndn-cpp-config.h defines NDN_CPP_HAVE_OSX_SECURITY 1.
27 #include "../../ndn-cpp-config.h"
28 #if NDN_CPP_HAVE_OSX_SECURITY
29 
30 #include <CoreFoundation/CoreFoundation.h>
31 #include <Security/Security.h>
32 #include <CoreServices/CoreServices.h>
33 
34 namespace ndn {
35 
45 template<class T>
46 class CFReleaser
47 {
48 public:
50  // Construction/destruction //
51 
52  CFReleaser()
53  : m_typeRef(0)
54  {
55  }
56 
57  CFReleaser(const T& typeRef)
58  : m_typeRef(typeRef)
59  {
60  }
61 
62  CFReleaser(const CFReleaser& inReleaser)
63  : m_typeRef(0)
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 
91  // Access //
92 
93  // operator const T&() const
94  // {
95  // return m_typeRef;
96  // }
97 
98  // operator T&()
99  // {
100  // return m_typeRef;
101  // }
102 
103  const T&
104  get() const
105  {
106  return m_typeRef;
107  }
108 
109  T&
110  get()
111  {
112  return m_typeRef;
113  }
114 
116  // Miscellaneous //
117 
118  void
119  retain(const T& typeRef)
120  {
121  if (typeRef != 0) {
122  CFRetain(typeRef);
123  }
124  release();
125  m_typeRef = typeRef;
126  }
127 
128  void
129  retain()
130  {
131  T typeRef = m_typeRef;
132  m_typeRef = nullptr;
133  retain(typeRef);
134  }
135 
136  void release()
137  {
138  if (m_typeRef != 0) {
139  CFRelease(m_typeRef);
140  m_typeRef = 0;
141  }
142  };
143 
144 private:
145  T m_typeRef;
146 };
147 
148 typedef CFReleaser<SecKeyRef> KeyRefOsx;
149 
150 }
151 
152 #endif // NDN_CPP_HAVE_OSX_SECURITY
153 
154 #endif