cmVariableWatch.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmVariableWatch_h
  4. #define cmVariableWatch_h
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. class cmMakefile;
  11. /** \class cmVariableWatch
  12. * \brief Helper class for watching of variable accesses.
  13. *
  14. * Calls function when variable is accessed
  15. */
  16. class cmVariableWatch
  17. {
  18. public:
  19. using WatchMethod = void (*)(const std::string&, int, void*, const char*,
  20. const cmMakefile*);
  21. using DeleteData = void (*)(void*);
  22. cmVariableWatch();
  23. ~cmVariableWatch();
  24. /**
  25. * Add watch to the variable
  26. */
  27. bool AddWatch(const std::string& variable, WatchMethod method,
  28. void* client_data = nullptr, DeleteData delete_data = nullptr);
  29. void RemoveWatch(const std::string& variable, WatchMethod method,
  30. void* client_data = nullptr);
  31. /**
  32. * This method is called when variable is accessed
  33. */
  34. bool VariableAccessed(const std::string& variable, int access_type,
  35. const char* newValue, const cmMakefile* mf) const;
  36. /**
  37. * Different access types.
  38. */
  39. enum
  40. {
  41. VARIABLE_READ_ACCESS = 0,
  42. UNKNOWN_VARIABLE_READ_ACCESS,
  43. UNKNOWN_VARIABLE_DEFINED_ACCESS,
  44. VARIABLE_MODIFIED_ACCESS,
  45. VARIABLE_REMOVED_ACCESS,
  46. NO_ACCESS
  47. };
  48. /**
  49. * Return the access as string
  50. */
  51. static const char* GetAccessAsString(int access_type);
  52. protected:
  53. struct Pair
  54. {
  55. WatchMethod Method = nullptr;
  56. void* ClientData = nullptr;
  57. DeleteData DeleteDataCall = nullptr;
  58. ~Pair()
  59. {
  60. if (this->DeleteDataCall && this->ClientData) {
  61. this->DeleteDataCall(this->ClientData);
  62. }
  63. }
  64. Pair() = default;
  65. Pair(const Pair&) = delete;
  66. Pair& operator=(const Pair&) = delete;
  67. };
  68. using VectorOfPairs = std::vector<std::shared_ptr<Pair>>;
  69. using StringToVectorOfPairs = std::map<std::string, VectorOfPairs>;
  70. StringToVectorOfPairs WatchMap;
  71. };
  72. #endif