You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

449 lines
13 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: comstl/util/acyclic_connector.hpp
  3. *
  4. * Purpose: A component for relating two COM objects without cycles.
  5. *
  6. * Created: 25th March 2006
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2006-2009, Matthew Wilson and Synesis Software
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice, this
  18. * list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  23. * any contributors may be used to endorse or promote products derived from
  24. * this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * ////////////////////////////////////////////////////////////////////// */
  39. /** \file comstl/util/acyclic_connector.hpp
  40. *
  41. * \brief [C++ only; requires COM] Definition of the
  42. * comstl::acyclic_connector class template
  43. * (\ref group__library__utility__com "COM Utility" Library).
  44. */
  45. #ifndef COMSTL_INCL_COMSTL_UTIL_HPP_ACYCLIC_CONNECTOR
  46. #define COMSTL_INCL_COMSTL_UTIL_HPP_ACYCLIC_CONNECTOR
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. # define COMSTL_VER_COMSTL_UTIL_HPP_ACYCLIC_CONNECTOR_MAJOR 1
  49. # define COMSTL_VER_COMSTL_UTIL_HPP_ACYCLIC_CONNECTOR_MINOR 2
  50. # define COMSTL_VER_COMSTL_UTIL_HPP_ACYCLIC_CONNECTOR_REVISION 7
  51. # define COMSTL_VER_COMSTL_UTIL_HPP_ACYCLIC_CONNECTOR_EDIT 21
  52. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  53. /* /////////////////////////////////////////////////////////////////////////
  54. * Includes
  55. */
  56. #ifndef COMSTL_INCL_COMSTL_H_COMSTL
  57. # include <comstl/comstl.h>
  58. #endif /* !COMSTL_INCL_COMSTL_H_COMSTL */
  59. #ifndef COMSTL_INCL_COMSTL_UTIL_HPP_OBJECT_FUNCTIONS
  60. # include <comstl/util/object_functions.h>
  61. #endif /* !COMSTL_INCL_COMSTL_UTIL_HPP_OBJECT_FUNCTIONS */
  62. #ifndef STLSOFT_INCL_STLSOFT_SYNCH_HPP_LOCK_SCOPE
  63. # include <stlsoft/synch/lock_scope.hpp>
  64. #endif /* !STLSOFT_INCL_STLSOFT_SYNCH_HPP_LOCK_SCOPE */
  65. /* /////////////////////////////////////////////////////////////////////////
  66. * Namespace
  67. */
  68. #ifndef _COMSTL_NO_NAMESPACE
  69. # if defined(_STLSOFT_NO_NAMESPACE) || \
  70. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  71. /* There is no stlsoft namespace, so must define ::comstl */
  72. namespace comstl
  73. {
  74. # else
  75. /* Define stlsoft::comstl_project */
  76. namespace stlsoft
  77. {
  78. namespace comstl_project
  79. {
  80. # endif /* _STLSOFT_NO_NAMESPACE */
  81. #endif /* !_COMSTL_NO_NAMESPACE */
  82. /* /////////////////////////////////////////////////////////////////////////
  83. * Classes
  84. */
  85. /** \brief Interface for acyclic communication.
  86. */
  87. #ifdef STLSOFT_DOCUMENTATION_SKIP_SECTION
  88. struct IAcyclicSide
  89. : public IUnknown
  90. #else /* ? STLSOFT_DOCUMENTATION_SKIP_SECTION */
  91. DECLARE_INTERFACE_(IAcyclicSide, IUnknown)
  92. #endif /* STLSOFT_DOCUMENTATION_SKIP_SECTION */
  93. {
  94. /** \brief Causes knowledge of the presence of the peer to be discarded
  95. */
  96. STDMETHOD_(void, Clear)() PURE;
  97. /** \brief Queries for the given interface on the peer.
  98. *
  99. * \param riid The interface identifier of the requested interface.
  100. * \param ppv Address of the interface pointer.
  101. *
  102. * \return A standard HRESULT status code indicating success/failure.
  103. */
  104. STDMETHOD(QueryPeer)(THIS_ REFIID riid, void **ppv) PURE;
  105. /** \brief returns the IID for the IAcyclicSide interface. */
  106. static REFIID iid()
  107. {
  108. static IID s_iid = { 0x8D5D0B0A, 0x4429, 0x4be1, { 0x8C, 0x00, 0xDE, 0xE0, 0xA8, 0xFF, 0xD0, 0xAF } };
  109. return s_iid;
  110. }
  111. };
  112. /** \brief A component that allows two objects to be connected without
  113. * creating cyclic dependencies.
  114. *
  115. * Reference counting architectures, such as COM, rely on there being no
  116. * dependency cycles. A cyclic dependency is the situation where, say,
  117. * object <b>a</b> holds a reference to object <b>b</b>, and object
  118. * <b>b</b> holds a reference to object <b>a</b>. Since neither will
  119. * release the references it holds (held on its efferent dependents)
  120. * until all references to itself (held by its afferent dependents) the
  121. * circle will never be broken.
  122. *
  123. * COM lore thus proscribes the use of mutual references, or prescribes
  124. * rigid protocols (e.g. Connection Points) for their management.
  125. *
  126. * acyclic_connector is designed to help in the case where two objects
  127. * need to have knowledge of each other, but not hold references on each
  128. * other. It works by acting as an intermediary that each of two objects
  129. * in the relationship keep informed as to their existance.
  130. *
  131. * \param MX The type of the mutex that will be used to provide exclusive
  132. * access to the instance state during the Clear() and QueryPeer() methods.
  133. */
  134. template <ss_typename_param_k MX>
  135. class acyclic_connector
  136. {
  137. /// \name Member Types
  138. /// @{
  139. public:
  140. typedef MX mutex_type;
  141. private:
  142. typedef acyclic_connector<MX> connector_type;
  143. public:
  144. typedef acyclic_connector<MX> class_type;
  145. private:
  146. class side
  147. : public IAcyclicSide
  148. {
  149. /// \name Member Types
  150. /// @{
  151. public:
  152. typedef side class_type;
  153. /// @}
  154. /// \name Construction
  155. /// @{
  156. public:
  157. ss_explicit_k side(connector_type &connector, LPUNKNOWN peer, IAcyclicSide **side)
  158. : m_connector(connector)
  159. , m_peer(peer)
  160. , m_refCount(1)
  161. {
  162. COMSTL_ASSERT(NULL != side);
  163. *side = this;
  164. }
  165. /// @}
  166. /// \name IUnknown
  167. /// @{
  168. public:
  169. STDMETHOD_(ULONG, AddRef)()
  170. {
  171. return ++m_refCount;
  172. }
  173. STDMETHOD_(ULONG, Release)()
  174. {
  175. class_type &other = (this == &m_connector.m_left) ? m_connector.m_right : m_connector.m_left;
  176. if(0 == --m_refCount)
  177. {
  178. m_peer = NULL;
  179. if(0 == other.m_refCount)
  180. {
  181. delete &m_connector;
  182. return 0;
  183. }
  184. }
  185. return m_refCount;
  186. }
  187. STDMETHOD(QueryInterface)(REFIID riid, void **ppv)
  188. {
  189. if( IID_IUnknown == riid ||
  190. IAcyclicSide::iid() == riid)
  191. {
  192. *ppv = static_cast<LPUNKNOWN>(this);
  193. static_cast<LPUNKNOWN>(*ppv)->AddRef();
  194. return S_OK;
  195. }
  196. return E_NOINTERFACE;
  197. }
  198. /// @}
  199. /// \name IAcyclicSide
  200. /// @{
  201. public:
  202. STDMETHOD_(void, Clear)()
  203. {
  204. stlsoft::lock_scope<mutex_type> lock(m_connector.m_mx);
  205. m_peer = NULL;
  206. }
  207. STDMETHOD(QueryPeer)(THIS_ REFIID riid, void **ppv)
  208. {
  209. COMSTL_ASSERT(NULL != ppv);
  210. stlsoft::lock_scope<mutex_type> lock(m_connector.m_mx);
  211. class_type &other = (this == &m_connector.m_left) ? m_connector.m_right : m_connector.m_left;
  212. if(NULL == other.m_peer)
  213. {
  214. return E_POINTER;
  215. }
  216. else
  217. {
  218. return other.m_peer->QueryInterface(riid, ppv);
  219. }
  220. }
  221. /// @}
  222. /// \name Members
  223. /// @{
  224. private:
  225. connector_type &m_connector;
  226. LPUNKNOWN m_peer;
  227. LONG m_refCount;
  228. /// @}
  229. };
  230. friend class side;
  231. /// @}
  232. /// \name Construction
  233. /// @{
  234. public:
  235. acyclic_connector( LPUNKNOWN leftPeer
  236. , IAcyclicSide **leftSide
  237. , LPUNKNOWN rightPeer
  238. , IAcyclicSide **rightSide);
  239. private:
  240. ~acyclic_connector() stlsoft_throw_0();
  241. /// @}
  242. /// \name Implementation
  243. /// @{
  244. private:
  245. class_type& get_this_();
  246. /// @}
  247. /// \name Construction
  248. /// @{
  249. private:
  250. side m_left;
  251. side m_right;
  252. mutex_type m_mx;
  253. /// @}
  254. /// \name Not to be implemented
  255. /// @{
  256. private:
  257. acyclic_connector(class_type const& rhs);
  258. class_type& operator =(class_type const& rhs);
  259. /// @}
  260. };
  261. /* /////////////////////////////////////////////////////////////////////////
  262. * Implementation
  263. */
  264. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  265. // acyclic_connector::side
  266. #if 0
  267. template <ss_typename_param_k MX>
  268. inline acyclic_connector<MX>::side::side(connector_type &connector, LPUNKNOWN peer, IAcyclicSide **side)
  269. : m_connector(connector)
  270. , m_peer(peer)
  271. , m_refCount(1)
  272. {
  273. COMSTL_ASSERT(NULL != side);
  274. *side = this;
  275. }
  276. template <ss_typename_param_k MX>
  277. inline STDMETHODIMP_(ULONG) acyclic_connector<MX>::side::AddRef()
  278. {
  279. return ++m_refCount;
  280. }
  281. template <ss_typename_param_k MX>
  282. inline STDMETHODIMP_(ULONG) acyclic_connector<MX>::side::Release()
  283. {
  284. class_type &other = (this == &m_connector.m_left) ? m_connector.m_right : m_connector.m_left;
  285. if( 0 == --m_refCount &&
  286. 0 == other.m_refCount)
  287. {
  288. delete &m_connector;
  289. return 0;
  290. }
  291. return m_refCount;
  292. }
  293. template <ss_typename_param_k MX>
  294. inline STDMETHODIMP acyclic_connector<MX>::side::QueryInterface(REFIID riid, void **ppv)
  295. {
  296. if( IID_IUnknown == riid ||
  297. IAcyclicSide::iid() == riid)
  298. {
  299. *ppv = static_cast<LPUNKNOWN>(this);
  300. static_cast<LPUNKNOWN>(*ppv)->AddRef();
  301. return S_OK;
  302. }
  303. return E_INTERFACE;
  304. }
  305. #endif /* 0 */
  306. #if 0
  307. template <ss_typename_param_k MX>
  308. inline STDMETHODIMP_(void) acyclic_connector<MX>::side::Clear()
  309. {
  310. stlsoft::lock_scope<mutex_type> lock(m_mx);
  311. m_peer = NULL;
  312. }
  313. template <ss_typename_param_k MX>
  314. inline STDMETHODIMP acyclic_connector<MX>::side::QueryPeer(THIS_ REFIID riid, void **ppv)
  315. {
  316. COMSTL_ASSERT(NULL != ppv);
  317. stlsoft::lock_scope<mutex_type> lock(m_mx);
  318. if(NULL == m_peer)
  319. {
  320. return E_POINTER;
  321. }
  322. else
  323. {
  324. return m_peer->QueryInterface(riid, ppv);
  325. }
  326. }
  327. #endif /* 0 */
  328. // acyclic_connector
  329. template <ss_typename_param_k MX>
  330. inline acyclic_connector<MX>::acyclic_connector(LPUNKNOWN leftPeer
  331. , IAcyclicSide **leftSide
  332. , LPUNKNOWN rightPeer
  333. , IAcyclicSide **rightSide)
  334. : m_left(get_this_(), leftPeer, leftSide)
  335. , m_right(get_this_(), rightPeer, rightSide)
  336. , m_mx()
  337. {
  338. COMSTL_MESSAGE_ASSERT("Acyclic connector cannot be initialised with null pointers", NULL != leftPeer);
  339. COMSTL_MESSAGE_ASSERT("Acyclic connector cannot be initialised with null pointers", NULL != rightPeer);
  340. COMSTL_MESSAGE_ASSERT("Acyclic connector cannot be initialised with null pointers", NULL != leftSide);
  341. COMSTL_MESSAGE_ASSERT("Acyclic connector cannot be initialised with null pointers", NULL != rightSide);
  342. #if 0
  343. HRESULT hr;
  344. hr = get_object_identity(left, &m_left);
  345. if(FAILED(hr))
  346. {
  347. #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
  348. STLSOFT_THROW_X(com_exception("Could not acquire left-side identity", hr));
  349. #endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
  350. }
  351. else
  352. {
  353. hr = get_object_identity(right, &m_right);
  354. if(FAILED(hr))
  355. {
  356. m_left->Release();
  357. #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
  358. STLSOFT_THROW_X(com_exception("Could not acquire right-side identity", hr));
  359. #else /* ? STLSOFT_CF_EXCEPTION_SUPPORT */
  360. m_left = NULL;
  361. #endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
  362. }
  363. }
  364. #endif /* 0 */
  365. }
  366. template <ss_typename_param_k MX>
  367. inline acyclic_connector<MX>::~acyclic_connector() stlsoft_throw_0()
  368. {}
  369. template <ss_typename_param_k MX>
  370. inline ss_typename_type_ret_k acyclic_connector<MX>::class_type& acyclic_connector<MX>::get_this_()
  371. {
  372. return *this;
  373. }
  374. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  375. /* ////////////////////////////////////////////////////////////////////// */
  376. #ifndef _COMSTL_NO_NAMESPACE
  377. # if defined(_STLSOFT_NO_NAMESPACE) || \
  378. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  379. } // namespace comstl
  380. # else
  381. } // namespace comstl_project
  382. } // namespace stlsoft
  383. # endif /* _STLSOFT_NO_NAMESPACE */
  384. #endif /* !_COMSTL_NO_NAMESPACE */
  385. /* ////////////////////////////////////////////////////////////////////// */
  386. #endif /* !COMSTL_INCL_COMSTL_UTIL_HPP_ACYCLIC_CONNECTOR */
  387. /* ///////////////////////////// end of file //////////////////////////// */