OSVR-Core
intrusive_ptr_COM.h
Go to the documentation of this file.
1 
19 // Excerpted from <https://github.com/rengeln/nyx/blob/master/src/Prefix.h>
20 //
21 // Original header, then accompanying LICENSE file (which matches the MIT
22 // license as found at http://opensource.org/licenses/mit-license) are
23 // included
24 
25 
26 #ifndef INCLUDED_intrusive_ptr_COM_h_GUID_BB97FC94_B320_4FB9_5379_940B0A631CA6
27 #define INCLUDED_intrusive_ptr_COM_h_GUID_BB97FC94_B320_4FB9_5379_940B0A631CA6
28 
29 // If we haven't yet included windows.h, don't make COM do it.
30 #if !defined(COM_NO_WINDOWS_H) && !defined(_WINDOWS_)
31 #define COM_NO_WINDOWS_H
32 #endif
33 #include <unknwn.h>
34 #include <boost/intrusive_ptr.hpp>
35 
36 
37 //
38 // Enable intrusive_ptr to work with COM objects.
39 //
40 inline void intrusive_ptr_add_ref(IUnknown* ptr)
41 {
42  ptr->AddRef();
43 }
44 
45 inline void intrusive_ptr_release(IUnknown* ptr)
46 {
47  ptr->Release();
48 }
49 
50 namespace detail {
51 //
52 // Enable intrusive_ptr objects to be passed as parameters to functions expecting
53 // a pointer-to-pointer which will be initialized by the function.
54 //
55 template <typename T>
57 {
58 public:
59  //
60  // Constructor.
61  //
62  // Parameters:
63  // [in] ref
64  // Intrusive_ptr object to wrap.
65  // [in] addRef
66  // If true then the ptr's refcount will be incremented on acquisition.
67  //
68  IntrusivePtrWrapper(boost::intrusive_ptr<T>& ref, bool addRef)
69  : m_ref(ref), m_ptr(nullptr), m_addRef(addRef)
70  {
71  }
72 
73  //
74  // Destructor.
75  //
77  {
78  m_ref = boost::intrusive_ptr<T>(m_ptr, m_addRef);
79  }
80 
81  //
82  // Assignment operator: deleted
83  //
84  IntrusivePtrWrapper & operator=(IntrusivePtrWrapper const &) = delete;
85 
86  //
87  // Implicit conversion to T**.
88  //
89  operator T**()
90  {
91  return &m_ptr;
92  }
93 
94  //
95  // Implicit conversion to T*.
96  //
97  operator T*()
98  {
99  return m_ptr;
100  }
101 
102  //
103  // Implicit conversion to void**.
104  //
105  operator void**()
106  {
107  return reinterpret_cast<void**>(&m_ptr);
108  }
109 
110 private:
111  //
112  // Properties.
113  //
114  boost::intrusive_ptr<T>& m_ref;
115  T* m_ptr;
116  bool m_addRef;
117 };
118 
119 
120 //
121 // Helper function for constructing an IntrusivePtrWrapper.
122 //
123 // Parameters:
124 // [in] ref
125 // Intrusive_ptr object to wrap.
126 // [in] addRef
127 // If true then the ptr's refcount will be incremented on acquisition.
128 // Default is false, which matches the behavior of COM functions.
129 //
130 template <typename T>
131 inline IntrusivePtrWrapper<T> AttachPtr(boost::intrusive_ptr<T>& ref, bool addRef = false)
132 {
133  return IntrusivePtrWrapper<T>(ref, addRef);
134 }
135 
136 } // namespace detail
137 
138 
139 using detail::AttachPtr;
140 
141 #endif // INCLUDED_intrusive_ptr_COM_h_GUID_BB97FC94_B320_4FB9_5379_940B0A631CA6
Definition: newuoa.h:1888
Definition: intrusive_ptr_COM.h:56