cmVariableWatch.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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;
  57. void* ClientData;
  58. DeleteData DeleteDataCall;
  59. Pair()
  60. : Method(nullptr)
  61. , ClientData(nullptr)
  62. , DeleteDataCall(nullptr)
  63. {
  64. }
  65. ~Pair()
  66. {
  67. if (this->DeleteDataCall && this->ClientData) {
  68. this->DeleteDataCall(this->ClientData);
  69. }
  70. }
  71. };
  72. typedef std::vector<std::shared_ptr<Pair>> VectorOfPairs;
  73. typedef std::map<std::string, VectorOfPairs> StringToVectorOfPairs;
  74. StringToVectorOfPairs WatchMap;
  75. };
  76. #endif