ExclusiveArray.tlh 2.5 KB

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