cmVariableWatch.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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> // IWYU pragma: keep
  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. typedef void (*WatchMethod)(const std::string& variable, int access_type,
  20. void* client_data, const char* newValue,
  21. const cmMakefile* mf);
  22. typedef void (*DeleteData)(void* client_data);
  23. cmVariableWatch();
  24. ~cmVariableWatch();
  25. /**
  26. * Add watch to the variable
  27. */
  28. bool AddWatch(const std::string& variable, WatchMethod method,
  29. void* client_data = nullptr, DeleteData delete_data = nullptr);
  30. void RemoveWatch(const std::string& variable, WatchMethod method,
  31. void* client_data = nullptr);
  32. /**
  33. * This method is called when variable is accessed
  34. */
  35. bool VariableAccessed(const std::string& variable, int access_type,
  36. const char* newValue, const cmMakefile* mf) const;
  37. /**
  38. * Different access types.
  39. */
  40. enum
  41. {
  42. VARIABLE_READ_ACCESS = 0,
  43. UNKNOWN_VARIABLE_READ_ACCESS,
  44. UNKNOWN_VARIABLE_DEFINED_ACCESS,
  45. VARIABLE_MODIFIED_ACCESS,
  46. VARIABLE_REMOVED_ACCESS,
  47. NO_ACCESS
  48. };
  49. /**
  50. * Return the access as string
  51. */
  52. static const char* GetAccessAsString(int access_type);
  53. protected:
  54. struct Pair
  55. {
  56. WatchMethod Method = nullptr;
  57. void* ClientData = nullptr;
  58. DeleteData DeleteDataCall = nullptr;
  59. Pair() {}
  60. ~Pair()
  61. {
  62. if (this->DeleteDataCall && this->ClientData) {
  63. this->DeleteDataCall(this->ClientData);
  64. }
  65. }
  66. };
  67. typedef std::vector<std::shared_ptr<Pair>> VectorOfPairs;
  68. typedef std::map<std::string, VectorOfPairs> StringToVectorOfPairs;
  69. StringToVectorOfPairs WatchMap;
  70. };
  71. #endif