added iOS source code
[wl-app.git] / iOS / Pods / Realm / include / core / realm / util / cf_ptr.hpp
1 /*************************************************************************
2  *
3  * Copyright 2016 Realm Inc.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  **************************************************************************/
18
19 #ifndef REALM_UTIL_CF_PTR_HPP
20 #define REALM_UTIL_CF_PTR_HPP
21
22 #include <realm/util/assert.hpp>
23
24 #if REALM_PLATFORM_APPLE
25
26 #include <CoreFoundation/CoreFoundation.h>
27
28 namespace realm {
29 namespace util {
30
31 template <class Ref>
32 class CFPtr {
33 public:
34     explicit CFPtr(Ref ref = nullptr) noexcept
35         : m_ref(ref)
36     {
37     }
38
39     CFPtr(CFPtr&& rg) noexcept
40         : m_ref(rg.m_ref)
41     {
42         rg.m_ref = nullptr;
43     }
44
45     ~CFPtr() noexcept
46     {
47         if (m_ref)
48             CFRelease(m_ref);
49     }
50
51     CFPtr& operator=(CFPtr&& rg) noexcept
52     {
53         REALM_ASSERT(!m_ref || m_ref != rg.m_ref);
54         if (m_ref)
55             CFRelease(m_ref);
56         m_ref = rg.m_ref;
57         rg.m_ref = nullptr;
58         return *this;
59     }
60
61     explicit operator bool() const noexcept
62     {
63         return bool(m_ref);
64     }
65
66     Ref get() const noexcept
67     {
68         return m_ref;
69     }
70
71     Ref release() noexcept
72     {
73         Ref ref = m_ref;
74         m_ref = nullptr;
75         return ref;
76     }
77
78     void reset(Ref ref = nullptr) noexcept
79     {
80         REALM_ASSERT(!m_ref || m_ref != ref);
81         if (m_ref)
82             CFRelease(m_ref);
83         m_ref = ref;
84     }
85
86 private:
87     Ref m_ref;
88 };
89
90 template <class Ref>
91 CFPtr<Ref> adoptCF(Ref ptr)
92 {
93     return CFPtr<Ref>(ptr);
94 }
95
96 template <class Ref>
97 CFPtr<Ref> retainCF(Ref ptr)
98 {
99     CFRetain(ptr);
100     return CFPtr<Ref>(ptr);
101 }
102 }
103 }
104
105
106 #endif // REALM_PLATFORM_APPLE
107
108 #endif // REALM_UTIL_CF_PTR_HPP