cmVariableWatch.cxx 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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() = default;
  19. cmVariableWatch::~cmVariableWatch() = default;
  20. bool cmVariableWatch::AddWatch(const std::string& variable, WatchMethod method,
  21. void* client_data /*=0*/,
  22. DeleteData delete_data /*=0*/)
  23. {
  24. auto p = std::make_shared<cmVariableWatch::Pair>();
  25. p->Method = method;
  26. p->ClientData = client_data;
  27. p->DeleteDataCall = delete_data;
  28. cmVariableWatch::VectorOfPairs& vp = this->WatchMap[variable];
  29. for (auto& pair : vp) {
  30. if (pair->Method == method && client_data &&
  31. client_data == pair->ClientData) {
  32. // Callback already exists
  33. return false;
  34. }
  35. }
  36. vp.push_back(std::move(p));
  37. return true;
  38. }
  39. void cmVariableWatch::RemoveWatch(const std::string& variable,
  40. WatchMethod method, void* client_data /*=0*/)
  41. {
  42. if (!this->WatchMap.count(variable)) {
  43. return;
  44. }
  45. cmVariableWatch::VectorOfPairs* vp = &this->WatchMap[variable];
  46. cmVariableWatch::VectorOfPairs::iterator it;
  47. for (it = vp->begin(); it != vp->end(); ++it) {
  48. if ((*it)->Method == method &&
  49. // If client_data is NULL, we want to disconnect all watches against
  50. // the given method; otherwise match ClientData as well.
  51. (!client_data || (client_data == (*it)->ClientData))) {
  52. vp->erase(it);
  53. return;
  54. }
  55. }
  56. }
  57. bool cmVariableWatch::VariableAccessed(const std::string& variable,
  58. int access_type, const char* newValue,
  59. const cmMakefile* mf) const
  60. {
  61. auto mit = this->WatchMap.find(variable);
  62. if (mit != this->WatchMap.end()) {
  63. // The strategy here is to copy the list of callbacks, and ignore
  64. // new callbacks that existing ones may add.
  65. std::vector<std::weak_ptr<Pair>> vp(mit->second.begin(),
  66. mit->second.end());
  67. for (auto& weak_it : vp) {
  68. // In the case where a callback was removed, the weak_ptr will not be
  69. // lockable, and so this ensures we don't attempt to call into freed
  70. // memory
  71. if (auto it = weak_it.lock()) {
  72. it->Method(variable, access_type, it->ClientData, newValue, mf);
  73. }
  74. }
  75. return true;
  76. }
  77. return false;
  78. }