cmVariableWatch.cxx 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmVariableWatch.h"
  4. #include <memory>
  5. #include <utility>
  6. #include <vector>
  7. static const char* const cmVariableWatchAccessStrings[] = {
  8. "READ_ACCESS", "UNKNOWN_READ_ACCESS", "UNKNOWN_DEFINED_ACCESS",
  9. "MODIFIED_ACCESS", "REMOVED_ACCESS", "NO_ACCESS"
  10. };
  11. const char* cmVariableWatch::GetAccessAsString(int access_type)
  12. {
  13. if (access_type < 0 || access_type >= cmVariableWatch::NO_ACCESS) {
  14. return "NO_ACCESS";
  15. }
  16. return cmVariableWatchAccessStrings[access_type];
  17. }
  18. cmVariableWatch::cmVariableWatch()
  19. {
  20. }
  21. cmVariableWatch::~cmVariableWatch()
  22. {
  23. }
  24. bool cmVariableWatch::AddWatch(const std::string& variable, WatchMethod method,
  25. void* client_data /*=0*/,
  26. DeleteData delete_data /*=0*/)
  27. {
  28. auto p = std::make_shared<cmVariableWatch::Pair>();
  29. p->Method = method;
  30. p->ClientData = client_data;
  31. p->DeleteDataCall = delete_data;
  32. cmVariableWatch::VectorOfPairs& vp = this->WatchMap[variable];
  33. for (auto& pair : vp) {
  34. if (pair->Method == method && client_data &&
  35. client_data == pair->ClientData) {
  36. // Callback already exists
  37. return false;
  38. }
  39. }
  40. vp.push_back(std::move(p));
  41. return true;
  42. }
  43. void cmVariableWatch::RemoveWatch(const std::string& variable,
  44. WatchMethod method, void* client_data /*=0*/)
  45. {
  46. if (!this->WatchMap.count(variable)) {
  47. return;
  48. }
  49. cmVariableWatch::VectorOfPairs* vp = &this->WatchMap[variable];
  50. cmVariableWatch::VectorOfPairs::iterator it;
  51. for (it = vp->begin(); it != vp->end(); ++it) {
  52. if ((*it)->Method == method &&
  53. // If client_data is NULL, we want to disconnect all watches against
  54. // the given method; otherwise match ClientData as well.
  55. (!client_data || (client_data == (*it)->ClientData))) {
  56. vp->erase(it);
  57. return;
  58. }
  59. }
  60. }
  61. bool cmVariableWatch::VariableAccessed(const std::string& variable,
  62. int access_type, const char* newValue,
  63. const cmMakefile* mf) const
  64. {
  65. cmVariableWatch::StringToVectorOfPairs::const_iterator mit =
  66. this->WatchMap.find(variable);
  67. if (mit != this->WatchMap.end()) {
  68. // The strategy here is to copy the list of callbacks, and ignore
  69. // new callbacks that existing ones may add.
  70. std::vector<std::weak_ptr<Pair>> vp(mit->second.begin(),
  71. mit->second.end());
  72. for (auto& weak_it : vp) {
  73. // In the case where a callback was removed, the weak_ptr will not be
  74. // lockable, and so this ensures we don't attempt to call into freed
  75. // memory
  76. if (auto it = weak_it.lock()) {
  77. it->Method(variable, access_type, it->ClientData, newValue, mf);
  78. }
  79. }
  80. return true;
  81. }
  82. return false;
  83. }