CMake Nightly Date Stamp
[kiteware-cmake.git] / Source / cmVariableWatch.h
blob349ce0ef49aff72c824c5b3d9972f9cc26322da9
1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
3 #pragma once
5 #include "cmConfigure.h" // IWYU pragma: keep
7 #include <map>
8 #include <memory>
9 #include <string>
10 #include <vector>
12 class cmMakefile;
14 /** \class cmVariableWatch
15 * \brief Helper class for watching of variable accesses.
17 * Calls function when variable is accessed
19 class cmVariableWatch
21 public:
22 using WatchMethod = void (*)(const std::string&, int, void*, const char*,
23 const cmMakefile*);
24 using DeleteData = void (*)(void*);
26 cmVariableWatch();
27 ~cmVariableWatch();
29 /**
30 * Add watch to the variable
32 bool AddWatch(const std::string& variable, WatchMethod method,
33 void* client_data = nullptr, DeleteData delete_data = nullptr);
34 void RemoveWatch(const std::string& variable, WatchMethod method,
35 void* client_data = nullptr);
37 /**
38 * This method is called when variable is accessed
40 bool VariableAccessed(const std::string& variable, int access_type,
41 const char* newValue, const cmMakefile* mf) const;
43 /**
44 * Different access types.
46 enum
48 VARIABLE_READ_ACCESS,
49 UNKNOWN_VARIABLE_READ_ACCESS,
50 UNKNOWN_VARIABLE_DEFINED_ACCESS,
51 VARIABLE_MODIFIED_ACCESS,
52 VARIABLE_REMOVED_ACCESS,
53 NO_ACCESS
56 /**
57 * Return the access as string
59 static const std::string& GetAccessAsString(int access_type);
61 protected:
62 struct Pair
64 WatchMethod Method = nullptr;
65 void* ClientData = nullptr;
66 DeleteData DeleteDataCall = nullptr;
67 ~Pair()
69 if (this->DeleteDataCall && this->ClientData) {
70 this->DeleteDataCall(this->ClientData);
73 Pair() = default;
74 Pair(const Pair&) = delete;
75 Pair& operator=(const Pair&) = delete;
78 using VectorOfPairs = std::vector<std::shared_ptr<Pair>>;
79 using StringToVectorOfPairs = std::map<std::string, VectorOfPairs>;
81 StringToVectorOfPairs WatchMap;