Commit b5e52ce2 authored by Dominik Charousset's avatar Dominik Charousset

Improve opencl::smart_ptr and coding style nitpicks

parent 78f4e19d
......@@ -27,30 +27,30 @@
namespace caf {
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 {
public:
using element_type = typename std::remove_pointer<T>::type;
using pointer = element_type*;
using reference = element_type&;
using const_pointer = const element_type*;
using const_reference = const element_type&;
public:
smart_ptr(pointer ptr = nullptr) : m_ptr(ptr) {
if (m_ptr)
ref(m_ptr);
smart_ptr(pointer ptr = nullptr, bool inc_ref_count = true) : m_ptr(nullptr) {
reset(ptr, inc_ref_count);
}
~smart_ptr() { reset(); }
~smart_ptr() {
reset();
}
smart_ptr(const smart_ptr& other) : m_ptr(other.m_ptr) {
if (m_ptr)
ref(m_ptr);
smart_ptr(smart_ptr&& other) : m_ptr(nullptr) {
swap(other);
}
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) {
reset(ptr);
......@@ -58,39 +58,54 @@ class smart_ptr {
}
smart_ptr& operator=(smart_ptr&& other) {
std::swap(m_ptr, other.m_ptr);
swap(other);
return *this;
}
smart_ptr& operator=(const smart_ptr& other) {
smart_ptr tmp{other};
std::swap(m_ptr, tmp.m_ptr);
swap(tmp);
return *this;
}
inline void reset(pointer ptr = nullptr) {
if (m_ptr)
void swap(smart_ptr& other) {
std::swap(m_ptr, other.m_ptr);
}
void reset(pointer ptr = nullptr, bool inc_ref_count = true) {
if (m_ptr) {
deref(m_ptr);
}
m_ptr = ptr;
if (ptr)
if (ptr && inc_ref_count) {
ref(ptr);
}
}
// does not modify reference count of ptr
inline void adopt(pointer ptr) {
reset();
m_ptr = ptr;
void adopt(pointer ptr) {
reset(ptr, false);
}
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:
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