Smart pointer cleanup

This commit is contained in:
Jack Andersen 2017-05-07 11:31:56 -10:00
parent 62443b59e7
commit aa272fe7b9
3 changed files with 39 additions and 99 deletions

View File

@ -3,37 +3,7 @@
#include <stdint.h> #include <stdint.h>
#include <type_traits> #include <type_traits>
#include "DeviceBase.hpp" #include "DeviceBase.hpp"
#include "../System.hpp"
#ifndef ENABLE_BITWISE_ENUM
#define ENABLE_BITWISE_ENUM(type)\
constexpr type operator|(type a, type b)\
{\
using T = std::underlying_type_t<type>;\
return type(static_cast<T>(a) | static_cast<T>(b));\
}\
constexpr type operator&(type a, type b)\
{\
using T = std::underlying_type_t<type>;\
return type(static_cast<T>(a) & static_cast<T>(b));\
}\
inline type& operator|=(type& a, const type& b)\
{\
using T = std::underlying_type_t<type>;\
a = type(static_cast<T>(a) | static_cast<T>(b));\
return a;\
}\
inline type& operator&=(type& a, const type& b)\
{\
using T = std::underlying_type_t<type>;\
a = type(static_cast<T>(a) & static_cast<T>(b));\
return a;\
}\
inline type operator~(const type& key)\
{\
using T = std::underlying_type_t<type>;\
return type(~static_cast<T>(key));\
}
#endif
namespace boo namespace boo
{ {

View File

@ -31,10 +31,19 @@ public:
} }
} }
static inline CFPointer<T> adopt(T CF_RELEASES_ARGUMENT ptr); 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;
}
T get() const;
CFPointer &operator=(CFPointer);
CFTypeRef* operator&() CFTypeRef* operator&()
{ {
if (CFTypeRef pointer = storage) { if (CFTypeRef pointer = storage) {
@ -58,30 +67,11 @@ private:
return (T)pointer; return (T)pointer;
} }
void swap(CFPointer &); void swap(CFPointer &other) {
std::swap(storage, other.storage);
}
}; };
template<typename T>
CFPointer<T> CFPointer<T>::adopt(T CF_RELEASES_ARGUMENT ptr) {
return CFPointer<T>(ptr, CFPointer<T>::Adopt);
}
template<typename T>
T CFPointer<T>::get() const {
return fromStorageType(storage);
}
template<typename T>
inline CFPointer<T>& CFPointer<T>::operator=(CFPointer other) {
swap(other);
return *this;
}
template<typename T>
inline void CFPointer<T>::swap(CFPointer &other) {
std::swap(storage, other.storage);
}
/// A smart pointer that can manage the lifecycle of CoreFoundation IUnknown objects. /// A smart pointer that can manage the lifecycle of CoreFoundation IUnknown objects.
template<typename T> template<typename T>
class IUnknownPointer { class IUnknownPointer {
@ -118,13 +108,17 @@ public:
} }
} }
static inline IUnknownPointer<T> adopt(T** ptr); static inline IUnknownPointer<T> adopt(T** ptr) {
return IUnknownPointer<T>(ptr, IUnknownPointer<T>::Adopt);
}
T* get() const {
return fromStorageType(_storage);
}
T* get() const;
T* operator->() const { return get(); } T* operator->() const { return get(); }
T** storage() const { return (T**)_storage; } T** storage() const { return (T**)_storage; }
LPVOID* operator&() LPVOID* operator&() {
{
if (IUnknownVTbl** pointer = _storage) { if (IUnknownVTbl** pointer = _storage) {
printf("%p RELEASE %d\n", pointer, (*pointer)->Release(pointer)); printf("%p RELEASE %d\n", pointer, (*pointer)->Release(pointer));
} }
@ -146,22 +140,9 @@ private:
return *(T**)pointer; return *(T**)pointer;
} }
void swap(IUnknownPointer &); void swap(IUnknownPointer &other) {
std::swap(_storage, other._storage);
}
}; };
template<typename T>
IUnknownPointer<T> IUnknownPointer<T>::adopt(T** ptr) {
return IUnknownPointer<T>(ptr, IUnknownPointer<T>::Adopt);
}
template<typename T>
T* IUnknownPointer<T>::get() const {
return fromStorageType(_storage);
}
template<typename T>
inline void IUnknownPointer<T>::swap(IUnknownPointer &other) {
std::swap(_storage, other._storage);
}
#endif // __CFPOINTER_HPP__ #endif // __CFPOINTER_HPP__

View File

@ -43,9 +43,13 @@ public:
} }
} }
static inline IOObjectPointer<T> adopt(T ptr); static inline IOObjectPointer<T> adopt(T ptr) {
return IOObjectPointer<T>(ptr, IOObjectPointer<T>::Adopt);
}
T get() const; T get() const {
return fromStorageType(storage);
}
io_object_t* operator&() io_object_t* operator&()
{ {
if (io_object_t pointer = storage) { if (io_object_t pointer = storage) {
@ -69,24 +73,11 @@ private:
return (T)pointer; return (T)pointer;
} }
void swap(IOObjectPointer &); void swap(IOObjectPointer &other) {
std::swap(storage, other.storage);
}
}; };
template<typename T>
IOObjectPointer<T> IOObjectPointer<T>::adopt(T ptr) {
return IOObjectPointer<T>(ptr, IOObjectPointer<T>::Adopt);
}
template<typename T>
T IOObjectPointer<T>::get() const {
return fromStorageType(storage);
}
template<typename T>
inline void IOObjectPointer<T>::swap(IOObjectPointer &other) {
std::swap(storage, other.storage);
}
/// A smart pointer that can manage the lifecycle of IOKit plugin objects. /// A smart pointer that can manage the lifecycle of IOKit plugin objects.
class IOCFPluginPointer { class IOCFPluginPointer {
public: public:
@ -122,11 +113,9 @@ public:
private: private:
IOCFPlugInInterface** _storage; IOCFPlugInInterface** _storage;
void swap(IOCFPluginPointer &); void swap(IOCFPluginPointer &other) {
std::swap(_storage, other._storage);
}
}; };
inline void IOCFPluginPointer::swap(IOCFPluginPointer &other) {
std::swap(_storage, other._storage);
}
#endif // __IOKITPOINTER_HPP__ #endif // __IOKITPOINTER_HPP__