cmCacheManager.h 8.55 KB
Newer Older
1
2
3
/*============================================================================
  CMake - Cross Platform Makefile Generator
  Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
4

5
6
  Distributed under the OSI-approved BSD License (the "License");
  see accompanying file Copyright.txt for details.
7

8
9
10
11
  This software is distributed WITHOUT ANY WARRANTY; without even the
  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  See the License for more information.
============================================================================*/
12
13
14
15
#ifndef cmCacheManager_h
#define cmCacheManager_h

#include "cmStandardIncludes.h"
16
#include "cmPropertyMap.h"
17
class cmMakefile;
18
class cmMarkAsAdvancedCommand;
19
class cmake;
20
21
22
23
24

/** \class cmCacheManager
 * \brief Control class for cmake's cache
 *
 * Load and Save CMake cache files.
25
 *
26
27
28
 */
class cmCacheManager
{
29
public:
30
  cmCacheManager(cmake* cm);
Andy Cedilnik's avatar
Andy Cedilnik committed
31
32
  class CacheIterator;
  friend class cmCacheManager::CacheIterator;
33
  enum CacheEntryType{ BOOL=0, PATH, FILEPATH, STRING, INTERNAL,STATIC,
34
                       UNINITIALIZED };
35
36

private:
Bill Hoffman's avatar
Bill Hoffman committed
37
  struct CacheEntry
38
  {
Ken Martin's avatar
Ken Martin committed
39
40
    std::string Value;
    CacheEntryType Type;
41
    cmPropertyMap Properties;
42
43
44
    const char* GetProperty(const std::string&) const;
    void SetProperty(const std::string& property, const char* value);
    void AppendProperty(const std::string& property, const char* value,
45
                        bool asString=false);
Ken Martin's avatar
Ken Martin committed
46
47
    bool Initialized;
    CacheEntry() : Value(""), Type(UNINITIALIZED), Initialized(false)
48
      {}
49
  };
50
51

public:
Ken Martin's avatar
Ken Martin committed
52
53
54
  class CacheIterator
  {
  public:
Ken Martin's avatar
Ken Martin committed
55
    void Begin();
56
    bool Find(const std::string&);
57
    bool IsAtEnd() const;
Ken Martin's avatar
Ken Martin committed
58
    void Next();
59
60
    std::string GetName() const {
      return this->Position->first; }
61
62
63
64
65
    const char* GetProperty(const std::string&) const ;
    bool GetPropertyAsBool(const std::string&) const ;
    bool PropertyExists(const std::string&) const;
    void SetProperty(const std::string& property, const char* value);
    void AppendProperty(const std::string& property, const char* value,
66
                        bool asString=false);
67
    void SetProperty(const std::string& property, bool value);
68
    const char* GetValue() const { return this->GetEntry().Value.c_str(); }
69
    bool GetValueAsBool() const;
70
    void SetValue(const char*);
Ken Martin's avatar
Ken Martin committed
71
    CacheEntryType GetType() const { return this->GetEntry().Type; }
72
    void SetType(CacheEntryType ty) { this->GetEntry().Type = ty; }
Ken Martin's avatar
Ken Martin committed
73
74
    bool Initialized() { return this->GetEntry().Initialized; }
    cmCacheManager &Container;
75
    std::map<std::string, CacheEntry>::iterator Position;
Ken Martin's avatar
Ken Martin committed
76
    CacheIterator(cmCacheManager &cm) : Container(cm) {
Ken Martin's avatar
Ken Martin committed
77
78
      this->Begin();
    }
79
    CacheIterator(cmCacheManager &cm, const char* key) : Container(cm)
80
81
82
83
84
      {
      if ( key )
        {
        this->Find(key);
        }
Stephen Kelly's avatar
Stephen Kelly committed
85
      }
86
  private:
Ken Martin's avatar
Ken Martin committed
87
88
    CacheEntry const& GetEntry() const { return this->Position->second; }
    CacheEntry& GetEntry() { return this->Position->second; }
Ken Martin's avatar
Ken Martin committed
89
  };
90

Ken Martin's avatar
Ken Martin committed
91
  ///! return an iterator to iterate through the cache map
92
  cmCacheManager::CacheIterator NewIterator()
Ken Martin's avatar
Ken Martin committed
93
94
    {
      return CacheIterator(*this);
95
96
    }

97
98
99
  /**
   * Types for the cache entries.  These are useful as
   * hints for a cache editor program.  Path should bring
100
   * up a file chooser, BOOL a check box, and STRING a
101
102
   * text entry box, FILEPATH is a full path to a file which
   * 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 path/CMakeCache.txt.
109
110
  bool LoadCache(const std::string& path);
  bool LoadCache(const std::string& path, bool internal,
111
112
                 std::set<std::string>& excludes,
                 std::set<std::string>& includes);
113

114
  ///! Save cache for given makefile.  Saves to ouput path/CMakeCache.txt
115
  bool SaveCache(const std::string& path) ;
116

Ken Martin's avatar
Ken Martin committed
117
  ///! Delete the cache given
118
  bool DeleteCache(const std::string& path);
Ken Martin's avatar
Ken Martin committed
119

120
121
  ///! Print the cache to a stream
  void PrintCache(std::ostream&) const;
122

123
  ///! Get the iterator for an entry with a given key.
124
  cmCacheManager::CacheIterator GetCacheIterator(const char *key=0);
125

126
  ///! Remove an entry from the cache
127
  void RemoveCacheEntry(const std::string& key);
128

Ken Martin's avatar
Ken Martin committed
129
  ///! Get the number of entries in the cache
Ken Martin's avatar
Ken Martin committed
130
  int GetSize() {
Ken Martin's avatar
Ken Martin committed
131
    return static_cast<int>(this->Cache.size()); }
132

133
  ///! Break up a line like VAR:type="value" into var, type and value
134
  static bool ParseEntry(const std::string& entry,
135
136
137
                         std::string& var,
                         std::string& value,
                         CacheEntryType& type);
Ken Martin's avatar
Ken Martin committed
138
139

  ///! Get a value from the cache given a key
140
  const char* GetInitializedCacheValue(const std::string& key) const;
141

142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
  const char* GetCacheEntryValue(const std::string& key)
  {
    cmCacheManager::CacheIterator it = this->GetCacheIterator(key.c_str());
    if (it.IsAtEnd())
      {
      return 0;
      }
    return it.GetValue();
  }

  const char* GetCacheEntryProperty(std::string const& key,
                                    std::string const& propName)
  {
    return this->GetCacheIterator(key.c_str()).GetProperty(propName);
  }

  CacheEntryType GetCacheEntryType(std::string const& key)
  {
    return this->GetCacheIterator(key.c_str()).GetType();
  }

  bool GetCacheEntryPropertyAsBool(std::string const& key,
                                   std::string const& propName)
  {
    return this->GetCacheIterator(key.c_str()).GetPropertyAsBool(propName);
  }

  void SetCacheEntryProperty(std::string const& key,
                             std::string const& propName,
                             std::string const& value)
  {
    this->GetCacheIterator(key.c_str()).SetProperty(propName, value.c_str());
  }

  void SetCacheEntryBoolProperty(std::string const& key,
                                 std::string const& propName,
                                 bool value)
  {
    this->GetCacheIterator(key.c_str()).SetProperty(propName, value);
  }

  void SetCacheEntryValue(std::string const& key,
                          std::string const& value)
  {
    this->GetCacheIterator(key.c_str()).SetValue(value.c_str());
  }

  void RemoveCacheEntryProperty(std::string const& key,
                                std::string const& propName)
  {
    this->GetCacheIterator(key.c_str()).SetProperty(propName, (void*)0);
  }

  void AppendCacheEntryProperty(std::string const& key,
                                std::string const& propName,
                                std::string const& value,
                                bool asString = false)
  {
    this->GetCacheIterator(key.c_str()).AppendProperty(propName,
                                                       value.c_str(),
                                                       asString);
  }

  std::vector<std::string> GetCacheEntryKeys()
  {
    std::vector<std::string> definitions;
    definitions.reserve(this->GetSize());
    cmCacheManager::CacheIterator cit = this->GetCacheIterator();
    for ( cit.Begin(); !cit.IsAtEnd(); cit.Next() )
      {
      definitions.push_back(cit.GetName());
      }
    return definitions;
  }

217
  /** Get the version of CMake that wrote the cache.  */
218
219
220
221
  unsigned int GetCacheMajorVersion() const
    { return this->CacheMajorVersion; }
  unsigned int GetCacheMinorVersion() const
    { return this->CacheMinorVersion; }
222

223
224
protected:
  ///! Add an entry into the cache
225
  void AddCacheEntry(const std::string& key, const char* value,
226
227
                     const char* helpString, CacheEntryType type);

228
  ///! Get a cache entry object for a key
229
  CacheEntry *GetCacheEntry(const std::string& key);
230
  ///! Clean out the CMakeFiles directory if no CMakeCache.txt
231
  void CleanCMakeFiles(const std::string& path);
232
233
234
235

  // Cache version info
  unsigned int CacheMajorVersion;
  unsigned int CacheMinorVersion;
236
private:
237
  cmake* CMakeInstance;
238
  typedef  std::map<std::string, CacheEntry> CacheEntryMap;
239
  static void OutputHelpString(std::ostream& fout,
Bill Hoffman's avatar
Bill Hoffman committed
240
                               const std::string& helpString);
241
242
243
244
245
246
247
  static void OutputKey(std::ostream& fout, std::string const& key);
  static void OutputValue(std::ostream& fout, std::string const& value);

  static const char* PersistentProperties[];
  bool ReadPropertyEntry(std::string const& key, CacheEntry& e);
  void WritePropertyEntries(std::ostream& os, CacheIterator const& i);

Ken Martin's avatar
Ken Martin committed
248
  CacheEntryMap Cache;
249
250
  // Only cmake and cmMakefile should be able to add cache values
  // the commands should never use the cmCacheManager directly
Stephen Kelly's avatar
Stephen Kelly committed
251
  friend class cmState; // allow access to add cache values
252
  friend class cmake; // allow access to add cache values
253
254
255
};

#endif