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