|
1
|
1 #pragma once
|
|
|
2 #include <memory>
|
|
|
3 #include "ptrholder.h"
|
|
|
4
|
|
|
5 namespace pfc {
|
|
|
6
|
|
|
7 // autoref<> : turn arbitrary ptr that needs to be delete'd into a shared_ptr<> alike
|
|
|
8 template<typename obj_t> class autoref {
|
|
|
9 public:
|
|
|
10 autoref() {}
|
|
|
11 autoref(std::nullptr_t) {}
|
|
|
12 autoref(obj_t * source) {
|
|
|
13 attach(source);
|
|
|
14 }
|
|
|
15 void attach(obj_t * source) {
|
|
|
16 PFC_ASSERT( source != nullptr );
|
|
|
17 m_obj = std::make_shared<holder_t>(source);
|
|
|
18 }
|
|
|
19 void reset() {
|
|
|
20 m_obj.reset();
|
|
|
21 }
|
|
|
22
|
|
|
23 obj_t * operator->() const {
|
|
|
24 return m_obj->get_ptr();
|
|
|
25 }
|
|
|
26
|
|
|
27 obj_t * get() const {
|
|
|
28 if (! m_obj ) return nullptr;
|
|
|
29 return m_obj->get_ptr();
|
|
|
30 }
|
|
|
31
|
|
|
32 operator bool() const {
|
|
|
33 return !!m_obj;
|
|
|
34 }
|
|
|
35 private:
|
|
|
36 typedef pfc::ptrholder_t< obj_t > holder_t;
|
|
|
37 std::shared_ptr< holder_t > m_obj;
|
|
|
38 };
|
|
|
39 }
|