ExclusiveArrayOfPtr.tlh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #pragma once
  2. #include <vector>
  3. #include <list>
  4. #include "XDWExclusiveHolder.tlh"
  5. #include "XDWExclusiveHolder.tli"
  6. #include "VectorOfPtr.tlh"
  7. #include "ListOfPtr.tlh"
  8. #include "Iterator.tlh"
  9. //-----------------------------------------------------------------------------
  10. // ExclusiveListOfPtr
  11. //-----------------------------------------------------------------------------
  12. template <class T>
  13. class ExclusiveListOfPtr : public XDWExclusiveHolder <ListOfPtr <T> >
  14. {
  15. typedef XDWExclusiveHolder <ListOfPtr <T> > inherited;
  16. public:
  17. using container = ListOfPtr <T>;
  18. using reference = typename ListOfPtr <T>::reference;
  19. using const_reference = typename ListOfPtr <T>::const_reference;
  20. using const_iterator = typename ListOfPtr <T>::const_iterator;
  21. public:
  22. ExclusiveListOfPtr ()
  23. {
  24. Attach (new ListOfPtr <T> ());
  25. }
  26. public:
  27. bool IsEmpty () const
  28. {
  29. LockHolder Lock (this);
  30. if (m_pObject == NULL) return true;
  31. return m_pObject->empty ();
  32. }
  33. template <class Action> void LockForEach (Action action)
  34. {
  35. LockHolder holder (this);
  36. for (auto Iter = Iterator::From (holder.As ()); Iter; Iter ++)
  37. {
  38. action (Iter ());
  39. }
  40. }
  41. template <class Action> int LockForEachIf (Action action)
  42. {
  43. LockHolder holder (this);
  44. for (auto Iter = Iterator::From (holder.As ()); Iter; Iter ++)
  45. {
  46. if (! action (Iter ()))
  47. return Iter.Index ();
  48. }
  49. return -1;
  50. }
  51. };
  52. //-----------------------------------------------------------------------------
  53. // ExclusiveVectorOfPtr
  54. //-----------------------------------------------------------------------------
  55. template <class T>
  56. class ExclusiveVectorOfPtr : public XDWExclusiveHolder <VectorOfPtr <T> >
  57. {
  58. typedef XDWExclusiveHolder <VectorOfPtr <T> > inherited;
  59. public:
  60. using container = ListOfPtr <T>;
  61. using reference = typename VectorOfPtr <T>::reference;
  62. using const_reference = typename VectorOfPtr <T>::const_reference;
  63. using const_iterator = typename VectorOfPtr <T>::const_iterator;
  64. public:
  65. ExclusiveVectorOfPtr ()
  66. {
  67. Attach (new VectorOfPtr <T> ());
  68. }
  69. public:
  70. bool IsEmpty () const
  71. {
  72. LockHolder Lock (this);
  73. if (m_pObject == NULL) return true;
  74. return m_pObject->empty ();
  75. }
  76. template <class Action> void LockForEach (Action action)
  77. {
  78. LockHolder holder (this);
  79. for (auto Iter = Iterator::From (holder.As ()); Iter; Iter ++)
  80. {
  81. action (Iter ());
  82. }
  83. }
  84. template <class Action> int LockForEachIf (Action action)
  85. {
  86. LockHolder holder (this);
  87. for (auto Iter = Iterator::From (holder.As ()); Iter; Iter ++)
  88. {
  89. if (! action (Iter ()))
  90. return Iter.Index ();
  91. }
  92. return -1;
  93. }
  94. };