cmVariableWatch.cxx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. static const char* const cmVariableWatchAccessStrings[] =
  12. {
  13. "READ_ACCESS",
  14. "UNKNOWN_READ_ACCESS",
  15. "UNKNOWN_DEFINED_ACCESS",
  16. "ALLOWED_UNKNOWN_READ_ACCESS",
  17. "MODIFIED_ACCESS",
  18. "REMOVED_ACCESS",
  19. "NO_ACCESS"
  20. };
  21. const char* cmVariableWatch::GetAccessAsString(int access_type)
  22. {
  23. if ( access_type < 0 || access_type >= cmVariableWatch::NO_ACCESS )
  24. {
  25. return "NO_ACCESS";
  26. }
  27. return cmVariableWatchAccessStrings[access_type];
  28. }
  29. cmVariableWatch::cmVariableWatch()
  30. {
  31. }
  32. cmVariableWatch::~cmVariableWatch()
  33. {
  34. cmVariableWatch::StringToVectorOfPairs::iterator svp_it;
  35. for ( svp_it = this->WatchMap.begin();
  36. svp_it != this->WatchMap.end(); ++svp_it )
  37. {
  38. cmVariableWatch::VectorOfPairs::iterator p_it;
  39. for ( p_it = svp_it->second.begin();
  40. p_it != svp_it->second.end(); ++p_it )
  41. {
  42. delete *p_it;
  43. }
  44. }
  45. }
  46. bool cmVariableWatch::AddWatch(const std::string& variable,
  47. WatchMethod method, void* client_data /*=0*/,
  48. DeleteData delete_data /*=0*/)
  49. {
  50. cmVariableWatch::Pair* p = new cmVariableWatch::Pair;
  51. p->Method = method;
  52. p->ClientData = client_data;
  53. p->DeleteDataCall = delete_data;
  54. cmVariableWatch::VectorOfPairs* vp = &this->WatchMap[variable];
  55. cmVariableWatch::VectorOfPairs::size_type cc;
  56. for ( cc = 0; cc < vp->size(); cc ++ )
  57. {
  58. cmVariableWatch::Pair* pair = (*vp)[cc];
  59. if ( pair->Method == method &&
  60. client_data && client_data == pair->ClientData)
  61. {
  62. // Callback already exists
  63. return false;
  64. }
  65. }
  66. vp->push_back(p);
  67. return true;
  68. }
  69. void cmVariableWatch::RemoveWatch(const std::string& variable,
  70. WatchMethod method,
  71. void* client_data /*=0*/)
  72. {
  73. if ( !this->WatchMap.count(variable) )
  74. {
  75. return;
  76. }
  77. cmVariableWatch::VectorOfPairs* vp = &this->WatchMap[variable];
  78. cmVariableWatch::VectorOfPairs::iterator it;
  79. for ( it = vp->begin(); it != vp->end(); ++it )
  80. {
  81. if ( (*it)->Method == method &&
  82. // If client_data is NULL, we want to disconnect all watches against
  83. // the given method; otherwise match ClientData as well.
  84. (!client_data || (client_data == (*it)->ClientData)))
  85. {
  86. delete *it;
  87. vp->erase(it);
  88. return;
  89. }
  90. }
  91. }
  92. void cmVariableWatch::VariableAccessed(const std::string& variable,
  93. int access_type,
  94. const char* newValue,
  95. const cmMakefile* mf) const
  96. {
  97. cmVariableWatch::StringToVectorOfPairs::const_iterator mit =
  98. this->WatchMap.find(variable);
  99. if ( mit != this->WatchMap.end() )
  100. {
  101. const cmVariableWatch::VectorOfPairs* vp = &mit->second;
  102. cmVariableWatch::VectorOfPairs::const_iterator it;
  103. for ( it = vp->begin(); it != vp->end(); it ++ )
  104. {
  105. (*it)->Method(variable, access_type, (*it)->ClientData,
  106. newValue, mf);
  107. }
  108. }
  109. }