KWSys Nightly Date Stamp
[cmake.git] / Source / cmDefinitions.cxx
blob98d27774a18506c1db808d7f7dfeaac5c0c6766f
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmDefinitions.cxx,v $
5 Language: C++
6 Date: $Date: 2009-07-22 18:22:45 $
7 Version: $Revision: 1.1 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 #include "cmDefinitions.h"
19 //----------------------------------------------------------------------------
20 cmDefinitions::Def cmDefinitions::NoDef;
22 //----------------------------------------------------------------------------
23 cmDefinitions::cmDefinitions(cmDefinitions* parent): Up(parent)
27 //----------------------------------------------------------------------------
28 void cmDefinitions::Reset(cmDefinitions* parent)
30 this->Up = parent;
31 this->Map.clear();
34 //----------------------------------------------------------------------------
35 cmDefinitions::Def const&
36 cmDefinitions::GetInternal(const char* key)
38 MapType::const_iterator i = this->Map.find(key);
39 if(i != this->Map.end())
41 return i->second;
43 else if(cmDefinitions* up = this->Up)
45 // Query the parent scope and store the result locally.
46 Def def = up->GetInternal(key);
47 return this->Map.insert(MapType::value_type(key, def)).first->second;
49 return this->NoDef;
52 //----------------------------------------------------------------------------
53 cmDefinitions::Def const&
54 cmDefinitions::SetInternal(const char* key, Def const& def)
56 if(this->Up || def.Exists)
58 // In lower scopes we store keys, defined or not.
59 MapType::iterator i = this->Map.find(key);
60 if(i == this->Map.end())
62 i = this->Map.insert(MapType::value_type(key, def)).first;
64 else
66 i->second = def;
68 return i->second;
70 else
72 // In the top-most scope we need not store undefined keys.
73 this->Map.erase(key);
74 return this->NoDef;
78 //----------------------------------------------------------------------------
79 const char* cmDefinitions::Get(const char* key)
81 Def const& def = this->GetInternal(key);
82 return def.Exists? def.c_str() : 0;
85 //----------------------------------------------------------------------------
86 const char* cmDefinitions::Set(const char* key, const char* value)
88 Def const& def = this->SetInternal(key, Def(value));
89 return def.Exists? def.c_str() : 0;
92 //----------------------------------------------------------------------------
93 cmDefinitions cmDefinitions::Closure() const
95 return cmDefinitions(ClosureTag(), this);
98 //----------------------------------------------------------------------------
99 cmDefinitions::cmDefinitions(ClosureTag const&, cmDefinitions const* root):
100 Up(0)
102 std::set<cmStdString> undefined;
103 this->ClosureImpl(undefined, root);
106 //----------------------------------------------------------------------------
107 void cmDefinitions::ClosureImpl(std::set<cmStdString>& undefined,
108 cmDefinitions const* defs)
110 // Consider local definitions.
111 for(MapType::const_iterator mi = defs->Map.begin();
112 mi != defs->Map.end(); ++mi)
114 // Use this key if it is not already set or unset.
115 if(this->Map.find(mi->first) == this->Map.end() &&
116 undefined.find(mi->first) == undefined.end())
118 if(mi->second.Exists)
120 this->Map.insert(*mi);
122 else
124 undefined.insert(mi->first);
129 // Traverse parents.
130 if(cmDefinitions const* up = defs->Up)
132 this->ClosureImpl(undefined, up);
136 //----------------------------------------------------------------------------
137 std::set<cmStdString> cmDefinitions::ClosureKeys() const
139 std::set<cmStdString> defined;
140 std::set<cmStdString> undefined;
141 this->ClosureKeys(defined, undefined);
142 return defined;
145 //----------------------------------------------------------------------------
146 void cmDefinitions::ClosureKeys(std::set<cmStdString>& defined,
147 std::set<cmStdString>& undefined) const
149 // Consider local definitions.
150 for(MapType::const_iterator mi = this->Map.begin();
151 mi != this->Map.end(); ++mi)
153 // Use this key if it is not already set or unset.
154 if(defined.find(mi->first) == defined.end() &&
155 undefined.find(mi->first) == undefined.end())
157 std::set<cmStdString>& m = mi->second.Exists? defined : undefined;
158 m.insert(mi->first);
162 // Traverse parents.
163 if(cmDefinitions const* up = this->Up)
165 up->ClosureKeys(defined, undefined);