Coverage Report

Created: 2026-02-09 06:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmVariableWatch.h
Line
Count
Source
1
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2
   file LICENSE.rst or https://cmake.org/licensing for details.  */
3
#pragma once
4
5
#include "cmConfigure.h" // IWYU pragma: keep
6
7
#include <map>
8
#include <memory>
9
#include <string>
10
#include <vector>
11
12
class cmMakefile;
13
14
/** \class cmVariableWatch
15
 * \brief Helper class for watching of variable accesses.
16
 *
17
 * Calls function when variable is accessed
18
 */
19
class cmVariableWatch
20
{
21
public:
22
  using WatchMethod = void (*)(std::string const&, int, void*, char const*,
23
                               cmMakefile const*);
24
  using DeleteData = void (*)(void*);
25
26
  cmVariableWatch();
27
  ~cmVariableWatch();
28
29
  /**
30
   * Add watch to the variable
31
   */
32
  bool AddWatch(std::string const& variable, WatchMethod method,
33
                void* client_data = nullptr, DeleteData delete_data = nullptr);
34
  void RemoveWatch(std::string const& variable, WatchMethod method,
35
                   void* client_data = nullptr);
36
37
  /**
38
   * This method is called when variable is accessed
39
   */
40
  bool VariableAccessed(std::string const& variable, int access_type,
41
                        char const* newValue, cmMakefile const* mf) const;
42
43
  /**
44
   * Different access types.
45
   */
46
  enum
47
  {
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
54
  };
55
56
  /**
57
   * Return the access as string
58
   */
59
  static std::string const& GetAccessAsString(int access_type);
60
61
protected:
62
  struct Pair
63
  {
64
    WatchMethod Method = nullptr;
65
    void* ClientData = nullptr;
66
    DeleteData DeleteDataCall = nullptr;
67
    ~Pair()
68
0
    {
69
0
      if (this->DeleteDataCall && this->ClientData) {
70
0
        this->DeleteDataCall(this->ClientData);
71
0
      }
72
0
    }
73
0
    Pair() = default;
74
    Pair(Pair const&) = delete;
75
    Pair& operator=(Pair const&) = delete;
76
  };
77
78
  using VectorOfPairs = std::vector<std::shared_ptr<Pair>>;
79
  using StringToVectorOfPairs = std::map<std::string, VectorOfPairs>;
80
81
  StringToVectorOfPairs WatchMap;
82
};