Imported Upstream version 2.8.9
[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     "ALLOWED_UNKNOWN_READ_ACCESS",
19     "MODIFIED_ACCESS",
20     "REMOVED_ACCESS",
21     "NO_ACCESS"
22 };
23
24 const char* cmVariableWatch::GetAccessAsString(int access_type)
25 {
26   if ( access_type < 0 || access_type >= cmVariableWatch::NO_ACCESS )
27     {
28     return "NO_ACCESS";
29     }
30   return cmVariableWatchAccessStrings[access_type];
31 }
32
33 cmVariableWatch::cmVariableWatch()
34 {
35 }
36
37 cmVariableWatch::~cmVariableWatch()
38 {
39 }
40
41 void cmVariableWatch::AddWatch(const std::string& variable, 
42                                WatchMethod method, void* client_data /*=0*/)
43 {
44   cmVariableWatch::Pair p;
45   p.Method = method;
46   p.ClientData = client_data;
47   cmVariableWatch::VectorOfPairs* vp = &this->WatchMap[variable];
48   cmVariableWatch::VectorOfPairs::size_type cc;
49   for ( cc = 0; cc < vp->size(); cc ++ )
50     {
51     cmVariableWatch::Pair* pair = &(*vp)[cc];
52     if ( pair->Method == method )
53       {
54       (*vp)[cc] = p;
55       return;
56       }
57     }
58   vp->push_back(p);
59 }
60
61 void cmVariableWatch::RemoveWatch(const std::string& variable, 
62                                   WatchMethod method)
63 {
64   cmVariableWatch::VectorOfPairs* vp = &this->WatchMap[variable];
65   cmVariableWatch::VectorOfPairs::iterator it;
66   for ( it = vp->begin(); it != vp->end(); ++it )
67     {
68     if ( it->Method == method )
69       {
70       vp->erase(it);
71       return;
72       }
73     }
74 }
75
76 void  cmVariableWatch::VariableAccessed(const std::string& variable, 
77                                         int access_type,
78                                         const char* newValue,
79                                         const cmMakefile* mf) const
80 {
81   cmVariableWatch::StringToVectorOfPairs::const_iterator mit = 
82     this->WatchMap.find(variable);
83   if ( mit  != this->WatchMap.end() )
84     {
85     const cmVariableWatch::VectorOfPairs* vp = &mit->second;
86     cmVariableWatch::VectorOfPairs::const_iterator it;
87     for ( it = vp->begin(); it != vp->end(); it ++ )
88       {
89       it->Method(variable, access_type, it->ClientData,
90         newValue, mf);
91       }
92     }
93 }