Skip to content

Commit c423b95

Browse files
committed
Add cf_ptr and free_deleter
1 parent 5c70a49 commit c423b95

File tree

4 files changed

+145
-1
lines changed

4 files changed

+145
-1
lines changed

Sources/OpenGraphCxx/include/OpenGraphCxx/Data/zone.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class zone {
4343
return info(zone_id(), deleted);
4444
}
4545
private:
46-
enum {
46+
enum : uint32_t {
4747
zone_id_mask = 0x7fffffff,
4848
deleted = 0x80000000,
4949
};
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
//
2+
// cf_ptr.hpp
3+
// OpenGraphCxx
4+
//
5+
// Status: Complete
6+
// Modified based Compute code
7+
8+
#ifndef OPENGRAPH_CXX_UTIL_CF_PTR_HPP
9+
#define OPENGRAPH_CXX_UTIL_CF_PTR_HPP
10+
11+
#include <OpenGraph/OGBase.h>
12+
#include <CoreFoundation/CoreFoundation.h>
13+
14+
OG_ASSUME_NONNULL_BEGIN
15+
16+
namespace util {
17+
18+
template <typename T> class cf_ptr {
19+
private:
20+
CFTypeRef _storage;
21+
22+
static OG_INLINE CFTypeRef to_storage(T ref) { return (CFTypeRef)(ref); }
23+
static OG_INLINE T from_storage(CFTypeRef storage) { return (T)storage; }
24+
25+
public:
26+
OG_CONSTEXPR cf_ptr() OG_NOEXCEPT : _storage(nullptr) {}
27+
OG_CONSTEXPR cf_ptr(std::nullptr_t) OG_NOEXCEPT : _storage(nullptr) {}
28+
29+
explicit cf_ptr(T ref) : _storage(to_storage(ref)) {
30+
if (_storage) {
31+
CFRetain(_storage);
32+
}
33+
}
34+
35+
~cf_ptr() {
36+
if (_storage) {
37+
CFRelease(_storage);
38+
}
39+
}
40+
41+
// Copy and move constructors
42+
43+
cf_ptr(const cf_ptr &other) OG_NOEXCEPT : _storage(other._storage) {
44+
if (_storage) {
45+
CFRetain(_storage);
46+
}
47+
};
48+
49+
cf_ptr(cf_ptr &&other) OG_NOEXCEPT : _storage(std::exchange(other._storage, nullptr)) {};
50+
51+
// Copy and move assignment operators
52+
53+
cf_ptr &operator=(const cf_ptr &other) OG_NOEXCEPT {
54+
if (this != &other) {
55+
if (_storage) {
56+
CFRelease(_storage);
57+
}
58+
_storage = other._storage;
59+
if (_storage) {
60+
CFRetain(_storage);
61+
}
62+
}
63+
return *this;
64+
};
65+
66+
cf_ptr &operator=(cf_ptr &&other) OG_NOEXCEPT {
67+
if (this != &other) {
68+
if (_storage) {
69+
CFRelease(_storage);
70+
}
71+
_storage = other._storage;
72+
other._storage = nullptr;
73+
}
74+
return *this;
75+
}
76+
77+
// Modifiers
78+
79+
void reset() OG_NOEXCEPT { reset(nullptr); }
80+
81+
void reset(T ref = nullptr) OG_NOEXCEPT {
82+
if (_storage != ref) {
83+
if (_storage) {
84+
CFRelease(_storage);
85+
}
86+
_storage = to_storage(ref);
87+
if (_storage) {
88+
CFRetain(_storage);
89+
}
90+
}
91+
}
92+
93+
// Observers
94+
95+
T get() const OG_NOEXCEPT { return from_storage(_storage); };
96+
97+
explicit operator bool() const OG_NOEXCEPT { return _storage != nullptr; }
98+
}; /* class cf_ptr */
99+
100+
using cf_data_ptr = cf_ptr<CFDataRef>;
101+
using cf_mutable_data_ptr = cf_ptr<CFMutableDataRef>;
102+
103+
} /* namespace util */
104+
105+
OG_ASSUME_NONNULL_END
106+
107+
#endif /* OPENGRAPH_CXX_UTIL_CF_PTR_HPP */
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// free_deleter.hpp
3+
// OpenGraphCxx
4+
//
5+
// Status: Complete
6+
// Modified based Compute code
7+
8+
#ifndef OPENGRAPH_CXX_UTIL_FREE_DELETER_HPP
9+
#define OPENGRAPH_CXX_UTIL_FREE_DELETER_HPP
10+
11+
#include <OpenGraph/OGBase.h>
12+
13+
OG_ASSUME_NONNULL_BEGIN
14+
15+
namespace util {
16+
17+
class free_deleter {
18+
public:
19+
template <typename T> void operator()(T *_Nullable ptr) {
20+
if (ptr) {
21+
free((void *)ptr);
22+
}
23+
}
24+
}; /* class free_deleter */
25+
26+
} /* namespace util */
27+
28+
OG_ASSUME_NONNULL_END
29+
30+
#endif /* OPENGRAPH_CXX_UTIL_FREE_DELETER_HPP */

Sources/OpenGraphCxx/include/module.private.modulemap

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ module OpenGraphCxx_Private.Runtime {
4444
export *
4545
}
4646

47+
module OpenGraphCxx_Private.Util {
48+
requires cplusplus
49+
umbrella "OpenGraphCxx/Util"
50+
export *
51+
module * { export * }
52+
}
53+
4754
module OpenGraphCxx_Private.Vector {
4855
requires cplusplus
4956
umbrella "OpenGraphCxx/Vector"

0 commit comments

Comments
 (0)