ExclusiveHolder.tli 1.9 KB

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