Skip to content

Commit 61e0418

Browse files
committed
Add move constructor and move assignment operator to ref_ptr
Use conditional compilation to make it work only with C++11 support.
1 parent 4dad4af commit 61e0418

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

include/osg/ref_ptr

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
#include <string>
2323
#endif
2424

25+
#if __cplusplus >= 201103L
26+
#include <utility>
27+
#endif
28+
2529
namespace osg {
2630

2731
template<typename T> class observer_ptr;
@@ -36,6 +40,9 @@ class ref_ptr
3640
ref_ptr() : _ptr(0) {}
3741
ref_ptr(T* ptr) : _ptr(ptr) { if (_ptr) _ptr->ref(); }
3842
ref_ptr(const ref_ptr& rp) : _ptr(rp._ptr) { if (_ptr) _ptr->ref(); }
43+
#if __cplusplus >= 201103L
44+
ref_ptr(ref_ptr&& rp) noexcept : _ptr(rp._ptr) { rp._ptr = 0; }
45+
#endif
3946
template<class Other> ref_ptr(const ref_ptr<Other>& rp) : _ptr(rp._ptr) { if (_ptr) _ptr->ref(); }
4047
ref_ptr(observer_ptr<T>& optr) : _ptr(0) { optr.lock(*this); }
4148
~ref_ptr() { if (_ptr) _ptr->unref(); _ptr = 0; }
@@ -52,6 +59,17 @@ class ref_ptr
5259
return *this;
5360
}
5461

62+
#if __cplusplus >= 201103L
63+
template<class Other> ref_ptr& operator = (ref_ptr<Other>&& rp)
64+
{
65+
if (_ptr == rp._ptr) return *this;
66+
if (_ptr != nullptr) _ptr->unref();
67+
_ptr = rp._ptr;
68+
rp._ptr = nullptr;
69+
return *this;
70+
}
71+
#endif
72+
5573
inline ref_ptr& operator = (T* ptr)
5674
{
5775
if (_ptr==ptr) return *this;

0 commit comments

Comments
 (0)