Imported Upstream version 2.8.12.2
[platform/upstream/cmake.git] / Source / cmCacheManager.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 cmCacheManager_h
13 #define cmCacheManager_h
14
15 #include "cmStandardIncludes.h"
16 #include "cmPropertyMap.h"
17 class cmMakefile;
18 class cmMarkAsAdvancedCommand;
19 class cmake;
20
21 /** \class cmCacheManager
22  * \brief Control class for cmake's cache
23  *
24  * Load and Save CMake cache files.
25  *
26  */
27 class cmCacheManager
28 {
29 public:
30   cmCacheManager(cmake* cm);
31   class CacheIterator;
32   friend class cmCacheManager::CacheIterator;
33   enum CacheEntryType{ BOOL=0, PATH, FILEPATH, STRING, INTERNAL,STATIC,
34                        UNINITIALIZED };
35
36 private:
37   struct CacheEntry
38   {
39     std::string Value;
40     CacheEntryType Type;
41     cmPropertyMap Properties;
42     const char* GetProperty(const char*) const;
43     void SetProperty(const char* property, const char* value);
44     void AppendProperty(const char* property, const char* value,
45                         bool asString=false);
46     bool Initialized;
47     CacheEntry() : Value(""), Type(UNINITIALIZED), Initialized(false)
48       {}
49   };
50
51 public:
52   class CacheIterator
53   {
54   public:
55     void Begin();
56     bool Find(const char*);
57     bool IsAtEnd() const;
58     void Next();
59     const char *GetName() const {
60       return this->Position->first.c_str(); }
61     const char* GetProperty(const char*) const ;
62     bool GetPropertyAsBool(const char*) const ;
63     bool PropertyExists(const char*) const;
64     void SetProperty(const char* property, const char* value);
65     void AppendProperty(const char* property, const char* value,
66                         bool asString=false);
67     void SetProperty(const char* property, bool value);
68     const char* GetValue() const { return this->GetEntry().Value.c_str(); }
69     bool GetValueAsBool() const;
70     void SetValue(const char*);
71     CacheEntryType GetType() const { return this->GetEntry().Type; }
72     void SetType(CacheEntryType ty) { this->GetEntry().Type = ty; }
73     bool Initialized() { return this->GetEntry().Initialized; }
74     cmCacheManager &Container;
75     std::map<cmStdString, CacheEntry>::iterator Position;
76     CacheIterator(cmCacheManager &cm) : Container(cm) {
77       this->Begin();
78     }
79     CacheIterator(cmCacheManager &cm, const char* key) : Container(cm)
80       {
81       if ( key )
82         {
83         this->Find(key);
84         }
85       }
86   private:
87     CacheEntry const& GetEntry() const { return this->Position->second; }
88     CacheEntry& GetEntry() { return this->Position->second; }
89   };
90
91   ///! return an iterator to iterate through the cache map
92   cmCacheManager::CacheIterator NewIterator()
93     {
94       return CacheIterator(*this);
95     }
96
97   /**
98    * Types for the cache entries.  These are useful as
99    * hints for a cache editor program.  Path should bring
100    * up a file chooser, BOOL a check box, and STRING a
101    * text entry box, FILEPATH is a full path to a file which
102    * can be different than just a path input
103    */
104   static CacheEntryType StringToType(const char*);
105   static const char* TypeToString(CacheEntryType);
106   static bool IsType(const char*);
107
108   ///! Load a cache for given makefile.  Loads from ouput home.
109   bool LoadCache(cmMakefile*);
110   ///! Load a cache for given makefile.  Loads from path/CMakeCache.txt.
111   bool LoadCache(const char* path);
112   bool LoadCache(const char* path, bool internal);
113   bool LoadCache(const char* path, bool internal,
114                  std::set<cmStdString>& excludes,
115                  std::set<cmStdString>& includes);
116
117   ///! Save cache for given makefile.  Saves to ouput home CMakeCache.txt.
118   bool SaveCache(cmMakefile*) ;
119   ///! Save cache for given makefile.  Saves to ouput path/CMakeCache.txt
120   bool SaveCache(const char* path) ;
121
122   ///! Delete the cache given
123   bool DeleteCache(const char* path);
124
125   ///! Print the cache to a stream
126   void PrintCache(std::ostream&) const;
127
128   ///! Get the iterator for an entry with a given key.
129   cmCacheManager::CacheIterator GetCacheIterator(const char *key=0);
130
131   ///! Remove an entry from the cache
132   void RemoveCacheEntry(const char* key);
133
134   ///! Get the number of entries in the cache
135   int GetSize() {
136     return static_cast<int>(this->Cache.size()); }
137
138   ///! Break up a line like VAR:type="value" into var, type and value
139   static bool ParseEntry(const char* entry,
140                          std::string& var,
141                          std::string& value,
142                          CacheEntryType& type);
143
144   ///! Get a value from the cache given a key
145   const char* GetCacheValue(const char* key) const;
146
147   /** Get the version of CMake that wrote the cache.  */
148   unsigned int GetCacheMajorVersion() { return this->CacheMajorVersion; }
149   unsigned int GetCacheMinorVersion() { return this->CacheMinorVersion; }
150   bool NeedCacheCompatibility(int major, int minor);
151
152   /** Define and document CACHE entry properties.  */
153   static void DefineProperties(cmake *cm);
154
155 protected:
156   ///! Add an entry into the cache
157   void AddCacheEntry(const char* key, const char* value,
158                      const char* helpString, CacheEntryType type);
159
160   ///! Get a cache entry object for a key
161   CacheEntry *GetCacheEntry(const char *key);
162   ///! Clean out the CMakeFiles directory if no CMakeCache.txt
163   void CleanCMakeFiles(const char* path);
164
165   // Cache version info
166   unsigned int CacheMajorVersion;
167   unsigned int CacheMinorVersion;
168 private:
169   cmake* CMakeInstance;
170   typedef  std::map<cmStdString, CacheEntry> CacheEntryMap;
171   static void OutputHelpString(std::ostream& fout,
172                                const std::string& helpString);
173   static void OutputKey(std::ostream& fout, std::string const& key);
174   static void OutputValue(std::ostream& fout, std::string const& value);
175
176   static const char* PersistentProperties[];
177   bool ReadPropertyEntry(std::string const& key, CacheEntry& e);
178   void WritePropertyEntries(std::ostream& os, CacheIterator const& i);
179
180   CacheEntryMap Cache;
181   // Only cmake and cmMakefile should be able to add cache values
182   // the commands should never use the cmCacheManager directly
183   friend class cmMakefile; // allow access to add cache values
184   friend class cmake; // allow access to add cache values
185   friend class cmakewizard; // allow access to add cache values
186   friend class cmMarkAsAdvancedCommand; // allow access to add cache values
187 };
188
189 #endif