packaging: Initial packaging
[platform/upstream/cmake.git] / Source / cmVariableWatch.cxx
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 #include "cmVariableWatch.h"
13
14 static const char* const cmVariableWatchAccessStrings[] =
15 {
16     "READ_ACCESS",
17     "UNKNOWN_READ_ACCESS",
18     "UNKNOWN_DEFINED_ACCESS",
19     "ALLOWED_UNKNOWN_READ_ACCESS",
20     "MODIFIED_ACCESS",
21     "REMOVED_ACCESS",
22     "NO_ACCESS"
23 };
24
25 const char* cmVariableWatch::GetAccessAsString(int access_type)
26 {
27   if ( access_type < 0 || access_type >= cmVariableWatch::NO_ACCESS )
28     {
29     return "NO_ACCESS";
30     }
31   return cmVariableWatchAccessStrings[access_type];
32 }
33
34 cmVariableWatch::cmVariableWatch()
35 {
36 }
37
38 cmVariableWatch::~cmVariableWatch()
39 {
40   cmVariableWatch::StringToVectorOfPairs::iterator svp_it;
41
42   for ( svp_it = this->WatchMap.begin();
43         svp_it != this->WatchMap.end(); ++svp_it )
44     {
45     cmVariableWatch::VectorOfPairs::iterator p_it;
46
47     for ( p_it = svp_it->second.begin();
48           p_it != svp_it->second.end(); ++p_it )
49       {
50       delete *p_it;
51       }
52     }
53 }
54
55 bool cmVariableWatch::AddWatch(const std::string& variable,
56                                WatchMethod method, void* client_data /*=0*/,
57                                DeleteData delete_data /*=0*/)
58 {
59   cmVariableWatch::Pair* p = new cmVariableWatch::Pair;
60   p->Method = method;
61   p->ClientData = client_data;
62   p->DeleteDataCall = delete_data;
63   cmVariableWatch::VectorOfPairs* vp = &this->WatchMap[variable];
64   cmVariableWatch::VectorOfPairs::size_type cc;
65   for ( cc = 0; cc < vp->size(); cc ++ )
66     {
67     cmVariableWatch::Pair* pair = (*vp)[cc];
68     if ( pair->Method == method &&
69          client_data && client_data == pair->ClientData)
70       {
71       // Callback already exists
72       return false;
73       }
74     }
75   vp->push_back(p);
76   return true;
77 }
78
79 void cmVariableWatch::RemoveWatch(const std::string& variable,
80                                   WatchMethod method,
81                                   void* client_data /*=0*/)
82 {
83   if ( !this->WatchMap.count(variable) )
84     {
85     return;
86     }
87   cmVariableWatch::VectorOfPairs* vp = &this->WatchMap[variable];
88   cmVariableWatch::VectorOfPairs::iterator it;
89   for ( it = vp->begin(); it != vp->end(); ++it )
90     {
91     if ( (*it)->Method == method &&
92          // If client_data is NULL, we want to disconnect all watches against
93          // the given method; otherwise match ClientData as well.
94          (!client_data || (client_data == (*it)->ClientData)))
95       {
96       delete *it;
97       vp->erase(it);
98       return;
99       }
100     }
101 }
102
103 void  cmVariableWatch::VariableAccessed(const std::string& variable,
104                                         int access_type,
105                                         const char* newValue,
106                                         const cmMakefile* mf) const
107 {
108   cmVariableWatch::StringToVectorOfPairs::const_iterator mit =
109     this->WatchMap.find(variable);
110   if ( mit  != this->WatchMap.end() )
111     {
112     const cmVariableWatch::VectorOfPairs* vp = &mit->second;
113     cmVariableWatch::VectorOfPairs::const_iterator it;
114     for ( it = vp->begin(); it != vp->end(); it ++ )
115       {
116       (*it)->Method(variable, access_type, (*it)->ClientData,
117         newValue, mf);
118       }
119     }
120 }