1
0

cmVariableWatch.cxx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmVariableWatch.h"
  11. #include "cmAlgorithms.h"
  12. #include <cm_auto_ptr.hxx>
  13. static const char* const cmVariableWatchAccessStrings[] = {
  14. "READ_ACCESS", "UNKNOWN_READ_ACCESS", "UNKNOWN_DEFINED_ACCESS",
  15. "MODIFIED_ACCESS", "REMOVED_ACCESS", "NO_ACCESS"
  16. };
  17. const char* cmVariableWatch::GetAccessAsString(int access_type)
  18. {
  19. if (access_type < 0 || access_type >= cmVariableWatch::NO_ACCESS) {
  20. return "NO_ACCESS";
  21. }
  22. return cmVariableWatchAccessStrings[access_type];
  23. }
  24. cmVariableWatch::cmVariableWatch()
  25. {
  26. }
  27. template <typename C>
  28. void deleteAllSecond(typename C::value_type it)
  29. {
  30. cmDeleteAll(it.second);
  31. }
  32. cmVariableWatch::~cmVariableWatch()
  33. {
  34. std::for_each(this->WatchMap.begin(), this->WatchMap.end(),
  35. deleteAllSecond<cmVariableWatch::StringToVectorOfPairs>);
  36. }
  37. bool cmVariableWatch::AddWatch(const std::string& variable, WatchMethod method,
  38. void* client_data /*=0*/,
  39. DeleteData delete_data /*=0*/)
  40. {
  41. CM_AUTO_PTR<cmVariableWatch::Pair> p(new cmVariableWatch::Pair);
  42. p->Method = method;
  43. p->ClientData = client_data;
  44. p->DeleteDataCall = delete_data;
  45. cmVariableWatch::VectorOfPairs* vp = &this->WatchMap[variable];
  46. cmVariableWatch::VectorOfPairs::size_type cc;
  47. for (cc = 0; cc < vp->size(); cc++) {
  48. cmVariableWatch::Pair* pair = (*vp)[cc];
  49. if (pair->Method == method && client_data &&
  50. client_data == pair->ClientData) {
  51. // Callback already exists
  52. return false;
  53. }
  54. }
  55. vp->push_back(p.release());
  56. return true;
  57. }
  58. void cmVariableWatch::RemoveWatch(const std::string& variable,
  59. WatchMethod method, void* client_data /*=0*/)
  60. {
  61. if (!this->WatchMap.count(variable)) {
  62. return;
  63. }
  64. cmVariableWatch::VectorOfPairs* vp = &this->WatchMap[variable];
  65. cmVariableWatch::VectorOfPairs::iterator it;
  66. for (it = vp->begin(); it != vp->end(); ++it) {
  67. if ((*it)->Method == method &&
  68. // If client_data is NULL, we want to disconnect all watches against
  69. // the given method; otherwise match ClientData as well.
  70. (!client_data || (client_data == (*it)->ClientData))) {
  71. delete *it;
  72. vp->erase(it);
  73. return;
  74. }
  75. }
  76. }
  77. bool cmVariableWatch::VariableAccessed(const std::string& variable,
  78. int access_type, const char* newValue,
  79. const cmMakefile* mf) const
  80. {
  81. cmVariableWatch::StringToVectorOfPairs::const_iterator mit =
  82. this->WatchMap.find(variable);
  83. if (mit != this->WatchMap.end()) {
  84. const cmVariableWatch::VectorOfPairs* vp = &mit->second;
  85. cmVariableWatch::VectorOfPairs::const_iterator it;
  86. for (it = vp->begin(); it != vp->end(); it++) {
  87. (*it)->Method(variable, access_type, (*it)->ClientData, newValue, mf);
  88. }
  89. return true;
  90. }
  91. return false;
  92. }