boo/lib/CFPointer.hpp

156 lines
3.8 KiB
C++
Raw Normal View History

2018-10-07 03:36:44 +00:00
#pragma once
2017-05-07 21:24:00 +00:00
#include <CoreFoundation/CoreFoundation.h>
#include <CoreFoundation/CFPlugInCOM.h>
#include <utility>
/// A smart pointer that can manage the lifecycle of Core Foundation objects.
template<typename T>
class CFPointer {
public:
CFPointer() : storage(nullptr) { }
CFPointer(T pointer) : storage(toStorageType(pointer)) {
if (storage) {
CFRetain(storage);
}
}
CFPointer(const CFPointer & other) : storage(other.storage) {
if (CFTypeRef ptr = storage) {
CFRetain(ptr);
}
}
CFPointer(CFPointer && other) : storage(std::exchange(other.storage, nullptr)) { }
~CFPointer() {
if (CFTypeRef pointer = storage) {
CFRelease(pointer);
}
}
2017-05-07 21:31:56 +00:00
static inline CFPointer<T> adopt(T CF_RELEASES_ARGUMENT ptr) {
return CFPointer<T>(ptr, CFPointer<T>::Adopt);
}
T get() const {
return fromStorageType(storage);
}
CFPointer &operator=(CFPointer other) {
swap(other);
return *this;
}
2017-05-07 21:24:00 +00:00
2018-08-28 03:46:33 +00:00
T* operator&()
2017-05-07 21:24:00 +00:00
{
if (CFTypeRef pointer = storage) {
CFRelease(pointer);
}
2018-08-28 03:46:33 +00:00
return (T*)&storage;
2017-05-07 21:24:00 +00:00
}
operator bool() const { return storage != nullptr; }
void reset()
{
if (storage)
{
CFRelease(storage);
storage = nullptr;
}
}
2017-05-07 21:24:00 +00:00
private:
CFTypeRef storage;
enum AdoptTag { Adopt };
CFPointer(T ptr, AdoptTag) : storage(toStorageType(ptr)) { }
inline CFTypeRef toStorageType(CFTypeRef ptr) const {
return (CFTypeRef)ptr;
}
inline T fromStorageType(CFTypeRef pointer) const {
return (T)pointer;
}
2017-05-07 21:31:56 +00:00
void swap(CFPointer &other) {
std::swap(storage, other.storage);
}
2017-05-07 21:24:00 +00:00
};
/// A smart pointer that can manage the lifecycle of CoreFoundation IUnknown objects.
template<typename T>
class IUnknownPointer {
public:
IUnknownPointer() : _storage(nullptr) { }
IUnknownPointer(T** pointer) : _storage(toStorageType(pointer)) {
if (_storage) {
(*pointer)->AddRef(pointer);
}
}
IUnknownPointer(const IUnknownPointer & other) : _storage(other._storage) {
if (IUnknownVTbl** ptr = _storage) {
(*ptr)->AddRef(ptr);
}
}
IUnknownPointer& operator=(const IUnknownPointer & other) {
if (IUnknownVTbl** pointer = _storage) {
(*pointer)->Release(pointer);
}
_storage = other._storage;
if (IUnknownVTbl** ptr = _storage) {
(*ptr)->AddRef(ptr);
}
return *this;
}
IUnknownPointer(IUnknownPointer && other) : _storage(std::exchange(other._storage, nullptr)) { }
~IUnknownPointer() {
if (IUnknownVTbl** pointer = _storage) {
(*pointer)->Release(pointer);
}
}
2017-05-07 21:31:56 +00:00
static inline IUnknownPointer<T> adopt(T** ptr) {
return IUnknownPointer<T>(ptr, IUnknownPointer<T>::Adopt);
}
T* get() const {
return fromStorageType(_storage);
}
2017-05-07 21:24:00 +00:00
T* operator->() const { return get(); }
T** storage() const { return (T**)_storage; }
2017-05-07 21:31:56 +00:00
LPVOID* operator&() {
2017-05-07 21:24:00 +00:00
if (IUnknownVTbl** pointer = _storage) {
printf("%p RELEASE %d\n", pointer, (*pointer)->Release(pointer));
}
return (LPVOID*)&_storage;
}
operator bool() const { return _storage != nullptr; }
private:
IUnknownVTbl** _storage;
enum AdoptTag { Adopt };
IUnknownPointer(T** ptr, AdoptTag) : _storage(toStorageType(ptr)) { }
inline IUnknownVTbl** toStorageType(T** ptr) const {
return (IUnknownVTbl**)ptr;
}
inline T* fromStorageType(IUnknownVTbl** pointer) const {
return *(T**)pointer;
}
2017-05-07 21:31:56 +00:00
void swap(IUnknownPointer &other) {
std::swap(_storage, other._storage);
}
2017-05-07 21:24:00 +00:00
};