|
1
|
1 #pragma once
|
|
|
2
|
|
|
3 #ifdef _WIN32
|
|
|
4 namespace pfc {
|
|
|
5
|
|
|
6 template<typename what> static void _COM_AddRef(what * ptr) {
|
|
|
7 if (ptr != NULL) ptr->AddRef();
|
|
|
8 }
|
|
|
9 template<typename what> static void _COM_Release(what * ptr) {
|
|
|
10 if (ptr != NULL) ptr->Release();
|
|
|
11 }
|
|
|
12
|
|
|
13 template<class T>
|
|
|
14 class com_ptr_t {
|
|
|
15 public:
|
|
|
16 typedef com_ptr_t<T> t_self;
|
|
|
17
|
|
|
18 com_ptr_t( nullptr_t ) throw() : m_ptr() {}
|
|
|
19
|
|
|
20 com_ptr_t() throw() : m_ptr() {}
|
|
|
21 template<typename source> inline com_ptr_t(source * p_ptr) throw() : m_ptr(p_ptr) {_COM_AddRef(m_ptr);}
|
|
|
22 com_ptr_t(const t_self & p_source) throw() : m_ptr(p_source.m_ptr) {_COM_AddRef(m_ptr);}
|
|
|
23 template<typename source> inline com_ptr_t(const com_ptr_t<source> & p_source) throw() : m_ptr(p_source.get_ptr()) {_COM_AddRef(m_ptr);}
|
|
|
24
|
|
|
25 inline ~com_ptr_t() throw() {_COM_Release(m_ptr);}
|
|
|
26
|
|
|
27 inline void copy(T * p_ptr) throw() {
|
|
|
28 _COM_Release(m_ptr);
|
|
|
29 m_ptr = p_ptr;
|
|
|
30 _COM_AddRef(m_ptr);
|
|
|
31 }
|
|
|
32
|
|
|
33 template<typename source> inline void copy(const com_ptr_t<source> & p_source) throw() {copy(p_source.get_ptr());}
|
|
|
34
|
|
|
35 inline void attach(T * p_ptr) throw() {
|
|
|
36 _COM_Release(m_ptr);
|
|
|
37 m_ptr = p_ptr;
|
|
|
38 }
|
|
|
39
|
|
|
40 inline const t_self & operator=(const t_self & p_source) throw() {copy(p_source); return *this;}
|
|
|
41 inline const t_self & operator=(T* p_source) throw() {copy(p_source); return *this;}
|
|
|
42 template<typename source> inline const t_self & operator=(const com_ptr_t<source> & p_source) throw() {copy(p_source); return *this;}
|
|
|
43 template<typename source> inline const t_self & operator=(source * p_ptr) throw() {copy(p_ptr); return *this;}
|
|
|
44
|
|
|
45 inline void release() throw() {
|
|
|
46 _COM_Release(m_ptr);
|
|
|
47 m_ptr = NULL;
|
|
|
48 }
|
|
|
49
|
|
|
50
|
|
|
51 inline T* operator->() const throw() {PFC_ASSERT(m_ptr);return m_ptr;}
|
|
|
52
|
|
|
53 inline T* get_ptr() const throw() {return m_ptr;}
|
|
|
54
|
|
|
55 inline T* duplicate_ptr() const throw() //should not be used ! temporary !
|
|
|
56 {
|
|
|
57 _COM_AddRef(m_ptr);
|
|
|
58 return m_ptr;
|
|
|
59 }
|
|
|
60
|
|
|
61 inline T* detach() throw() {
|
|
|
62 return replace_null_t(m_ptr);
|
|
|
63 }
|
|
|
64
|
|
|
65 inline bool is_valid() const throw() {return m_ptr != 0;}
|
|
|
66 inline bool is_empty() const throw() {return m_ptr == 0;}
|
|
|
67
|
|
|
68 inline bool operator==(const com_ptr_t<T> & p_item) const throw() {return m_ptr == p_item.m_ptr;}
|
|
|
69 inline bool operator!=(const com_ptr_t<T> & p_item) const throw() {return m_ptr != p_item.m_ptr;}
|
|
|
70 inline bool operator>(const com_ptr_t<T> & p_item) const throw() {return m_ptr > p_item.m_ptr;}
|
|
|
71 inline bool operator<(const com_ptr_t<T> & p_item) const throw() {return m_ptr < p_item.m_ptr;}
|
|
|
72
|
|
|
73 inline static void g_swap(com_ptr_t<T> & item1, com_ptr_t<T> & item2) throw() {
|
|
|
74 pfc::swap_t(item1.m_ptr,item2.m_ptr);
|
|
|
75 }
|
|
|
76
|
|
|
77 inline T** receive_ptr() throw() {release();return &m_ptr;}
|
|
|
78 inline void** receive_void_ptr() throw() {return (void**) receive_ptr();}
|
|
|
79
|
|
|
80 inline t_self & operator<<(t_self & p_source) throw() {attach(p_source.detach());return *this;}
|
|
|
81 inline t_self & operator>>(t_self & p_dest) throw() {p_dest.attach(detach());return *this;}
|
|
|
82 private:
|
|
|
83 T* m_ptr;
|
|
|
84 };
|
|
|
85
|
|
|
86 }
|
|
|
87 #endif
|