Commit b5e52ce2 authored by Dominik Charousset's avatar Dominik Charousset

Improve opencl::smart_ptr and coding style nitpicks

parent 78f4e19d
...@@ -27,30 +27,30 @@ ...@@ -27,30 +27,30 @@
namespace caf { namespace caf {
namespace opencl { namespace opencl {
template <typename T, cl_int (*ref)(T), cl_int (*deref)(T)> template <class T, cl_int (*ref)(T), cl_int (*deref)(T)>
class smart_ptr { class smart_ptr {
public:
using element_type = typename std::remove_pointer<T>::type; using element_type = typename std::remove_pointer<T>::type;
using pointer = element_type*; using pointer = element_type*;
using reference = element_type&; using reference = element_type&;
using const_pointer = const element_type*; using const_pointer = const element_type*;
using const_reference = const element_type&; using const_reference = const element_type&;
public: smart_ptr(pointer ptr = nullptr, bool inc_ref_count = true) : m_ptr(nullptr) {
smart_ptr(pointer ptr = nullptr) : m_ptr(ptr) { reset(ptr, inc_ref_count);
if (m_ptr)
ref(m_ptr);
} }
~smart_ptr() { reset(); } ~smart_ptr() {
reset();
}
smart_ptr(const smart_ptr& other) : m_ptr(other.m_ptr) { smart_ptr(smart_ptr&& other) : m_ptr(nullptr) {
if (m_ptr) swap(other);
ref(m_ptr);
} }
smart_ptr(smart_ptr&& other) : m_ptr(other.m_ptr) { other.m_ptr = nullptr; } smart_ptr(const smart_ptr& other) : m_ptr(nullptr) {
reset(other.m_ptr);
}
smart_ptr& operator=(pointer ptr) { smart_ptr& operator=(pointer ptr) {
reset(ptr); reset(ptr);
...@@ -58,39 +58,54 @@ class smart_ptr { ...@@ -58,39 +58,54 @@ class smart_ptr {
} }
smart_ptr& operator=(smart_ptr&& other) { smart_ptr& operator=(smart_ptr&& other) {
std::swap(m_ptr, other.m_ptr); swap(other);
return *this; return *this;
} }
smart_ptr& operator=(const smart_ptr& other) { smart_ptr& operator=(const smart_ptr& other) {
smart_ptr tmp{other}; smart_ptr tmp{other};
std::swap(m_ptr, tmp.m_ptr); swap(tmp);
return *this; return *this;
} }
inline void reset(pointer ptr = nullptr) { void swap(smart_ptr& other) {
if (m_ptr) std::swap(m_ptr, other.m_ptr);
}
void reset(pointer ptr = nullptr, bool inc_ref_count = true) {
if (m_ptr) {
deref(m_ptr); deref(m_ptr);
}
m_ptr = ptr; m_ptr = ptr;
if (ptr) if (ptr && inc_ref_count) {
ref(ptr); ref(ptr);
} }
}
// does not modify reference count of ptr // does not modify reference count of ptr
inline void adopt(pointer ptr) { void adopt(pointer ptr) {
reset(); reset(ptr, false);
m_ptr = ptr;
} }
inline pointer get() const { return m_ptr; } pointer get() const {
return m_ptr;
}
inline pointer operator->() const { return m_ptr; } pointer operator->() const {
return m_ptr;
}
inline reference operator*() const { return *m_ptr; } reference operator*() const {
return *m_ptr;
}
inline bool operator!() const { return m_ptr == nullptr; } bool operator!() const {
return m_ptr == nullptr;
}
inline explicit operator bool() const { return m_ptr != nullptr; } explicit operator bool() const {
return m_ptr != nullptr;
}
private: private:
pointer m_ptr; pointer m_ptr;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment