XDWExclusiveHolder.tli 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // XDWExclusiveHolder.tli
  2. //
  3. #pragma once
  4. #include "XDWExclusiveHolder.tlh"
  5. template <typename T>
  6. XDWExclusiveHolder <T>::XDWExclusiveHolder ()
  7. {
  8. m_pObject = NULL;
  9. }
  10. template <typename T>
  11. XDWExclusiveHolder <T>::~XDWExclusiveHolder ()
  12. {
  13. LockHolder Lock (this);
  14. delete m_pObject;
  15. }
  16. template <typename T>
  17. XDWExclusiveHolder <T>::XDWExclusiveHolder (T * object)
  18. {
  19. m_pObject = object;
  20. }
  21. template <typename T>
  22. void XDWExclusiveHolder <T>::Attach (T * object)
  23. {
  24. LockHolder Lock (this);
  25. delete m_pObject;
  26. m_pObject = object;
  27. }
  28. template <typename T>
  29. T * XDWExclusiveHolder <T>::Detach ()
  30. {
  31. LockHolder Lock (this);
  32. T * over = m_pObject;
  33. m_pObject = NULL;
  34. Release ();
  35. return over;
  36. }
  37. template <typename T>
  38. void XDWExclusiveHolder <T>::Release ()
  39. {
  40. LockHolder Lock (this);
  41. delete m_pObject;
  42. m_pObject = NULL;
  43. }
  44. template <typename T>
  45. XDWExclusiveHolder <T> & XDWExclusiveHolder <T>::operator = (T * object)
  46. {
  47. Attach (object);
  48. return *this;
  49. }
  50. template <typename T>
  51. bool XDWExclusiveHolder <T>::IsEmpty () const
  52. {
  53. LockHolder Lock (this);
  54. return (m_pObject == NULL);
  55. }
  56. template <typename T>
  57. XDWExclusiveHolder <T>::LockHolder::LockHolder (const XDWExclusiveHolder <T> *holder)
  58. {
  59. auto lock = new std::lock_guard < std::recursive_mutex > (holder->m_Mutex);
  60. m_Lock.Attach (lock);
  61. m_Holder = holder;
  62. }
  63. template <typename T>
  64. XDWExclusiveHolder <T>::LockHolder::LockHolder (const LockHolder & h)
  65. {
  66. m_Lock = h.m_Lock;
  67. m_Holder = h.m_Holder;
  68. }
  69. template <typename T>
  70. XDWExclusiveHolder <T>::LockHolder::~LockHolder ()
  71. {
  72. // m_Lock->Unlock ();
  73. }
  74. /*
  75. template <typename T>
  76. void XDWExclusiveHolder <T>::LockHolder::Unlock ()
  77. {
  78. m_Lock->Unlock ();
  79. }
  80. */
  81. template <typename T>
  82. T * XDWExclusiveHolder <T>::LockHolder::As ()
  83. {
  84. return m_Holder->m_pObject;
  85. }
  86. template <typename T>
  87. const T * XDWExclusiveHolder <T>::LockHolder::As () const
  88. {
  89. return m_Holder->m_pObject;
  90. }
  91. template <typename T>
  92. template <typename C> C * XDWExclusiveHolder <T>::LockHolder::AS ()
  93. {
  94. return m_Holder->m_pObject;
  95. }
  96. template <typename T>
  97. template <typename C> const C * XDWExclusiveHolder <T>::LockHolder::AS () const
  98. {
  99. return m_Holder->m_pObject;
  100. }