Coin: SoRefPtr.h Source File

| Coin3D is Free Software, published under the BSD 3-clause license. | https://coin3d.github.io https://www.kongsberg.com/en/kogt/

SoRefPtr.h
1#ifndef COIN_SOREFPTR_H
2#define COIN_SOREFPTR_H
3
4#include <utility>
5
6template <typename T>
7 class SoRefPtr {
8public:
9 SoRefPtr(void) noexcept : ptr(NULL) { }
10
11 explicit SoRefPtr(T * p) : ptr(p)
12 {
13 if (this->ptr) this->ptr->ref();
14 }
15
16 SoRefPtr(const SoRefPtr & other) : ptr(other.ptr)
17 {
18 if (this->ptr) this->ptr->ref();
19 }
20
21 SoRefPtr(SoRefPtr && other) noexcept : ptr(other.ptr)
22 {
23 other.ptr = NULL;
24 }
25
26 ~SoRefPtr(void)
27 {
28 if (this->ptr) this->ptr->unref();
29 }
30
31 SoRefPtr & operator=(SoRefPtr other) noexcept
32 {
33 this->swap(other);
34 return *this;
35 }
36
37 void reset(T * p = NULL)
38 {
39 SoRefPtr tmp(p);
40 this->swap(tmp);
41 }
42
43 T * get(void) const noexcept { return this->ptr; }
44 T & operator*(void) const { return *this->ptr; }
45 T * operator->(void) const noexcept { return this->ptr; }
46 explicit operator bool(void) const noexcept { return this->ptr != NULL; }
47
48 void swap(SoRefPtr & other) noexcept
49 {
50 using std::swap;
51 swap(this->ptr, other.ptr);
52 }
53
54private:
55 T * ptr;
56};
57
58#endif // !COIN_SOREFPTR_H
59