Source code of Windows XP (NT5)
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.

111 lines
2.2 KiB

  1. // CounterMgr.cpp: implementation of the CCounterMgr class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "CounterMgr.h"
  6. #include <algorithm>
  7. #ifdef _DEBUG
  8. #undef THIS_FILE
  9. static char THIS_FILE[]=__FILE__;
  10. #define new DEBUG_NEW
  11. #endif
  12. // Using Singleton to assure initialization
  13. CCounterMgr* Get_g_CounterMgr()
  14. {
  15. static CCounterMgr g_CounterMgr;
  16. return &g_CounterMgr;
  17. }
  18. //////////////////////////////////////////////////////////////////////
  19. // CCounterMgr class implementation
  20. //////////////////////////////////////////////////////////////////////
  21. CAbstractCounter* CCounterMgr::Exists(const CCounterLocation& location) const
  22. {
  23. CAbstractCounter* ret = NULL;
  24. for (vector<CAbstractCounter*>::const_iterator i = m_arrCounterPool.begin(); i != m_arrCounterPool.end(); i++)
  25. {
  26. if ((CCounterLocation&)location == **i) {
  27. ret = *i;
  28. break;
  29. }
  30. }
  31. return ret;
  32. }
  33. bool CCounterMgr::RemoveLocation(const CCounterLocation& location)
  34. {
  35. bool ret = false;
  36. CAbstractCounter* found = NULL;
  37. LOCKOBJECT();
  38. if (NULL != (found = Exists(location)))
  39. {
  40. vector<CAbstractCounter*>::iterator i = find(m_arrCounterPool.begin(), m_arrCounterPool.end(), found);
  41. if (i != m_arrCounterPool.end())
  42. {
  43. m_arrCounterPool.erase(i);
  44. ret = true;
  45. }
  46. }
  47. UNLOCKOBJECT();
  48. return ret;
  49. }
  50. bool CCounterMgr::Add(const CAbstractCounter& counter)
  51. {
  52. bool ret = false;
  53. LOCKOBJECT();
  54. if (!Exists(counter)) {
  55. m_arrCounterPool.push_back((CAbstractCounter*)&counter);
  56. ret = true;
  57. }
  58. UNLOCKOBJECT();
  59. return ret;
  60. }
  61. void CCounterMgr::AddSubstitute(const CAbstractCounter& counter)
  62. {
  63. LOCKOBJECT();
  64. RemoveLocation(counter);
  65. m_arrCounterPool.push_back((CAbstractCounter*)&counter);
  66. UNLOCKOBJECT();
  67. }
  68. bool CCounterMgr::Remove(const CAbstractCounter& counter)
  69. {
  70. bool ret = false;
  71. LOCKOBJECT();
  72. vector<CAbstractCounter*>::iterator i = find(m_arrCounterPool.begin(), m_arrCounterPool.end(), &counter);
  73. if (i != m_arrCounterPool.end())
  74. {
  75. m_arrCounterPool.erase(i);
  76. ret = true;
  77. }
  78. UNLOCKOBJECT();
  79. return ret;
  80. }
  81. CAbstractCounter* CCounterMgr::Get(const CCounterLocation& location) const
  82. {
  83. CAbstractCounter* ret = NULL;
  84. LOCKOBJECT();
  85. ret = Exists(location);
  86. UNLOCKOBJECT();
  87. return ret;
  88. }