Imported Upstream version 2.8.12.2
[platform/upstream/cmake.git] / Source / cmVariableWatch.h
1 /*============================================================================
2   CMake - Cross Platform Makefile Generator
3   Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
4
5   Distributed under the OSI-approved BSD License (the "License");
6   see accompanying file Copyright.txt for details.
7
8   This software is distributed WITHOUT ANY WARRANTY; without even the
9   implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10   See the License for more information.
11 ============================================================================*/
12 #ifndef cmVariableWatch_h
13 #define cmVariableWatch_h
14
15 #include "cmStandardIncludes.h"
16
17 class cmMakefile;
18
19 /** \class cmVariableWatch
20  * \brief Helper class for watching of variable accesses.
21  *
22  * Calls function when variable is accessed
23  */
24 class cmVariableWatch
25 {
26 public:
27   typedef void (*WatchMethod)(const std::string& variable, int access_type,
28     void* client_data, const char* newValue, const cmMakefile* mf);
29   typedef void (*DeleteData)(void* client_data);
30
31   cmVariableWatch();
32   ~cmVariableWatch();
33
34   /**
35    * Add watch to the variable
36    */
37   bool AddWatch(const std::string& variable, WatchMethod method,
38                 void* client_data=0, DeleteData delete_data=0);
39   void RemoveWatch(const std::string& variable, WatchMethod method,
40                    void* client_data=0);
41
42   /**
43    * This method is called when variable is accessed
44    */
45   void VariableAccessed(const std::string& variable, int access_type,
46     const char* newValue, const cmMakefile* mf) const;
47
48   /**
49    * Different access types.
50    */
51   enum
52     {
53     VARIABLE_READ_ACCESS = 0,
54     UNKNOWN_VARIABLE_READ_ACCESS,
55     UNKNOWN_VARIABLE_DEFINED_ACCESS,
56     ALLOWED_UNKNOWN_VARIABLE_READ_ACCESS,
57     VARIABLE_MODIFIED_ACCESS,
58     VARIABLE_REMOVED_ACCESS,
59     NO_ACCESS
60     };
61
62   /**
63    * Return the access as string
64    */
65   static const char* GetAccessAsString(int access_type);
66
67 protected:
68   struct Pair
69   {
70     WatchMethod Method;
71     void*        ClientData;
72     DeleteData   DeleteDataCall;
73     Pair() : Method(0), ClientData(0), DeleteDataCall(0) {}
74     ~Pair()
75       {
76       if (this->DeleteDataCall && this->ClientData)
77         {
78         this->DeleteDataCall(this->ClientData);
79         }
80       }
81   };
82
83   typedef std::vector< Pair* > VectorOfPairs;
84   typedef std::map<cmStdString, VectorOfPairs > StringToVectorOfPairs;
85
86   StringToVectorOfPairs WatchMap;
87 };
88
89
90 #endif