Imported Upstream version 2.8.9
[platform/upstream/cmake.git] / Source / cmDefinitions.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 cmDefinitions_h
13 #define cmDefinitions_h
14
15 #include "cmStandardIncludes.h"
16
17 /** \class cmDefinitions
18  * \brief Store a scope of variable definitions for CMake language.
19  *
20  * This stores the state of variable definitions (set or unset) for
21  * one scope.  Sets are always local.  Gets search parent scopes
22  * transitively and save results locally.
23  */
24 class cmDefinitions
25 {
26 public:
27   /** Construct with the given parent scope.  */
28   cmDefinitions(cmDefinitions* parent = 0);
29
30   /** Reset object as if newly constructed.  */
31   void Reset(cmDefinitions* parent = 0);
32
33   /** Returns the parent scope, if any.  */
34   cmDefinitions* GetParent() const { return this->Up; }
35
36   /** Get the value associated with a key; null if none.
37       Store the result locally if it came from a parent.  */
38   const char* Get(const char* key);
39
40   /** Set (or unset if null) a value associated with a key.  */
41   const char* Set(const char* key, const char* value);
42
43   /** Get the set of all local keys.  */
44   std::set<cmStdString> LocalKeys() const;
45
46   /** Compute the closure of all defined keys with values.
47       This flattens the scope.  The result has no parent.  */
48   cmDefinitions Closure() const;
49
50   /** Compute the set of all defined keys.  */
51   std::set<cmStdString> ClosureKeys() const;
52
53 private:
54   // String with existence boolean.
55   struct Def: public cmStdString
56   {
57     Def(): cmStdString(), Exists(false) {}
58     Def(const char* v): cmStdString(v?v:""), Exists(v?true:false) {}
59     Def(Def const& d): cmStdString(d), Exists(d.Exists) {}
60     bool Exists;
61   };
62   static Def NoDef;
63
64   // Parent scope, if any.
65   cmDefinitions* Up;
66
67   // Local definitions, set or unset.
68   typedef std::map<cmStdString, Def> MapType;
69   MapType Map;
70
71   // Internal query and update methods.
72   Def const& GetInternal(const char* key);
73   Def const& SetInternal(const char* key, Def const& def);
74
75   // Implementation of Closure() method.
76   struct ClosureTag {};
77   cmDefinitions(ClosureTag const&, cmDefinitions const* root);
78   void ClosureImpl(std::set<cmStdString>& undefined,
79                    cmDefinitions const* defs);
80
81   // Implementation of ClosureKeys() method.
82   void ClosureKeys(std::set<cmStdString>& defined,
83                    std::set<cmStdString>& undefined) const;
84 };
85
86 #endif