cmake/Source/cmVariableWatch.h

83 lines
2.0 KiB
C
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2021-09-14 00:13:48 +02:00
#pragma once
2017-07-20 19:35:53 +02:00
#include "cmConfigure.h" // IWYU pragma: keep
2016-10-30 18:24:19 +01:00
#include <map>
2020-02-01 23:06:01 +01:00
#include <memory>
2016-10-30 18:24:19 +01:00
#include <string>
#include <vector>
class cmMakefile;
/** \class cmVariableWatch
* \brief Helper class for watching of variable accesses.
*
* Calls function when variable is accessed
*/
class cmVariableWatch
{
public:
2020-02-01 23:06:01 +01:00
using WatchMethod = void (*)(const std::string&, int, void*, const char*,
const cmMakefile*);
using DeleteData = void (*)(void*);
cmVariableWatch();
~cmVariableWatch();
/**
* Add watch to the variable
*/
2013-11-03 12:27:13 +02:00
bool AddWatch(const std::string& variable, WatchMethod method,
2018-01-26 17:06:56 +01:00
void* client_data = nullptr, DeleteData delete_data = nullptr);
2013-11-03 12:27:13 +02:00
void RemoveWatch(const std::string& variable, WatchMethod method,
2018-01-26 17:06:56 +01:00
void* client_data = nullptr);
2013-03-16 19:13:01 +02:00
/**
* This method is called when variable is accessed
*/
2016-03-29 18:52:50 +02:00
bool VariableAccessed(const std::string& variable, int access_type,
2016-07-09 11:21:54 +02:00
const char* newValue, const cmMakefile* mf) const;
/**
* Different access types.
*/
enum
2016-07-09 11:21:54 +02:00
{
2020-08-30 11:54:41 +02:00
VARIABLE_READ_ACCESS,
UNKNOWN_VARIABLE_READ_ACCESS,
UNKNOWN_VARIABLE_DEFINED_ACCESS,
VARIABLE_MODIFIED_ACCESS,
VARIABLE_REMOVED_ACCESS,
NO_ACCESS
2016-07-09 11:21:54 +02:00
};
/**
* Return the access as string
*/
2020-08-30 11:54:41 +02:00
static const std::string& GetAccessAsString(int access_type);
2013-03-16 19:13:01 +02:00
protected:
struct Pair
{
2019-11-11 23:01:05 +01:00
WatchMethod Method = nullptr;
void* ClientData = nullptr;
DeleteData DeleteDataCall = nullptr;
2013-11-03 12:27:13 +02:00
~Pair()
2016-07-09 11:21:54 +02:00
{
if (this->DeleteDataCall && this->ClientData) {
2013-11-03 12:27:13 +02:00
this->DeleteDataCall(this->ClientData);
}
2016-07-09 11:21:54 +02:00
}
2019-11-11 23:01:05 +01:00
Pair() = default;
Pair(const Pair&) = delete;
Pair& operator=(const Pair&) = delete;
};
2020-02-01 23:06:01 +01:00
using VectorOfPairs = std::vector<std::shared_ptr<Pair>>;
using StringToVectorOfPairs = std::map<std::string, VectorOfPairs>;
StringToVectorOfPairs WatchMap;
};