Commit f5ee5fa0 authored by neverlord's avatar neverlord

cow_ptr requires a copy() member function

parent a939cb1e
...@@ -36,35 +36,6 @@ ...@@ -36,35 +36,6 @@
#include "cppa/intrusive_ptr.hpp" #include "cppa/intrusive_ptr.hpp"
#include "cppa/util/is_copyable.hpp"
#include "cppa/util/has_copy_member_fun.hpp"
namespace cppa { namespace detail {
template<typename T>
constexpr int copy_method()
{
return util::has_copy_member_fun<T>::value
? 2
: (util::is_copyable<T>::value ? 1 : 0);
}
// is_copyable
template<typename T>
T* copy_of(T const* what, std::integral_constant<int, 1>)
{
return new T(*what);
}
// has_copy_member_fun
template<typename T>
T* copy_of(T const* what, std::integral_constant<int, 2>)
{
return what->copy();
}
} } // namespace cppa::detail
namespace cppa { namespace cppa {
/** /**
...@@ -75,27 +46,6 @@ template<typename T> ...@@ -75,27 +46,6 @@ template<typename T>
class cow_ptr class cow_ptr
{ {
static_assert(detail::copy_method<T>() != 0, "T is not copyable");
typedef std::integral_constant<int, detail::copy_method<T>()> copy_of_token;
intrusive_ptr<T> m_ptr;
T* detached_ptr()
{
T* ptr = m_ptr.get();
if (!ptr->unique())
{
T* new_ptr = detail::copy_of(ptr, copy_of_token());
cow_ptr tmp(new_ptr);
swap(tmp);
return new_ptr;
}
return ptr;
}
inline T const* ptr() const { return m_ptr.get(); }
public: public:
template<typename Y> template<typename Y>
...@@ -123,7 +73,7 @@ class cow_ptr ...@@ -123,7 +73,7 @@ class cow_ptr
cow_ptr& operator=(cow_ptr const& other) cow_ptr& operator=(cow_ptr const& other)
{ {
cow_ptr tmp(other); cow_ptr tmp{other};
swap(tmp); swap(tmp);
return *this; return *this;
} }
...@@ -131,7 +81,7 @@ class cow_ptr ...@@ -131,7 +81,7 @@ class cow_ptr
template<typename Y> template<typename Y>
cow_ptr& operator=(cow_ptr<Y> const& other) cow_ptr& operator=(cow_ptr<Y> const& other)
{ {
cow_ptr tmp(other); cow_ptr tmp{other};
swap(tmp); swap(tmp);
return *this; return *this;
} }
...@@ -150,6 +100,26 @@ class cow_ptr ...@@ -150,6 +100,26 @@ class cow_ptr
inline explicit operator bool() const { return static_cast<bool>(m_ptr); } inline explicit operator bool() const { return static_cast<bool>(m_ptr); }
private:
intrusive_ptr<T> m_ptr;
T* detached_ptr()
{
T* ptr = m_ptr.get();
if (!ptr->unique())
{
//T* new_ptr = detail::copy_of(ptr, copy_of_token());
T* new_ptr = ptr->copy();
cow_ptr tmp(new_ptr);
swap(tmp);
return new_ptr;
}
return ptr;
}
inline T const* ptr() const { return m_ptr.get(); }
}; };
} // namespace cppa } // namespace cppa
......
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