cmCacheManager.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #ifndef cmCacheManager_h
  11. #define cmCacheManager_h
  12. #include "cmStandardIncludes.h"
  13. #include "cmPropertyMap.h"
  14. #include "cmState.h"
  15. class cmMarkAsAdvancedCommand;
  16. /** \class cmCacheManager
  17. * \brief Control class for cmake's cache
  18. *
  19. * Load and Save CMake cache files.
  20. *
  21. */
  22. class cmCacheManager
  23. {
  24. public:
  25. cmCacheManager();
  26. class CacheIterator;
  27. friend class cmCacheManager::CacheIterator;
  28. private:
  29. struct CacheEntry
  30. {
  31. std::string Value;
  32. cmState::CacheEntryType Type;
  33. cmPropertyMap Properties;
  34. const char* GetProperty(const std::string&) const;
  35. void SetProperty(const std::string& property, const char* value);
  36. void AppendProperty(const std::string& property, const char* value,
  37. bool asString=false);
  38. bool Initialized;
  39. CacheEntry()
  40. : Value(""),
  41. Type(cmState::UNINITIALIZED),
  42. Initialized(false)
  43. {}
  44. };
  45. public:
  46. class CacheIterator
  47. {
  48. public:
  49. void Begin();
  50. bool Find(const std::string&);
  51. bool IsAtEnd() const;
  52. void Next();
  53. std::string GetName() const {
  54. return this->Position->first; }
  55. const char* GetProperty(const std::string&) const ;
  56. bool GetPropertyAsBool(const std::string&) const ;
  57. bool PropertyExists(const std::string&) const;
  58. void SetProperty(const std::string& property, const char* value);
  59. void AppendProperty(const std::string& property, const char* value,
  60. bool asString=false);
  61. void SetProperty(const std::string& property, bool value);
  62. const char* GetValue() const { return this->GetEntry().Value.c_str(); }
  63. bool GetValueAsBool() const;
  64. void SetValue(const char*);
  65. cmState::CacheEntryType GetType() const
  66. { return this->GetEntry().Type; }
  67. void SetType(cmState::CacheEntryType ty)
  68. { this->GetEntry().Type = ty; }
  69. bool Initialized() { return this->GetEntry().Initialized; }
  70. cmCacheManager &Container;
  71. std::map<std::string, CacheEntry>::iterator Position;
  72. CacheIterator(cmCacheManager &cm) : Container(cm) {
  73. this->Begin();
  74. }
  75. CacheIterator(cmCacheManager &cm, const char* key) : Container(cm)
  76. {
  77. if ( key )
  78. {
  79. this->Find(key);
  80. }
  81. }
  82. private:
  83. CacheEntry const& GetEntry() const { return this->Position->second; }
  84. CacheEntry& GetEntry() { return this->Position->second; }
  85. };
  86. ///! return an iterator to iterate through the cache map
  87. cmCacheManager::CacheIterator NewIterator()
  88. {
  89. return CacheIterator(*this);
  90. }
  91. ///! Load a cache for given makefile. Loads from path/CMakeCache.txt.
  92. bool LoadCache(const std::string& path, bool internal,
  93. std::set<std::string>& excludes,
  94. std::set<std::string>& includes);
  95. ///! Save cache for given makefile. Saves to ouput path/CMakeCache.txt
  96. bool SaveCache(const std::string& path) ;
  97. ///! Delete the cache given
  98. bool DeleteCache(const std::string& path);
  99. ///! Print the cache to a stream
  100. void PrintCache(std::ostream&) const;
  101. ///! Get the iterator for an entry with a given key.
  102. cmCacheManager::CacheIterator GetCacheIterator(const char *key=0);
  103. ///! Remove an entry from the cache
  104. void RemoveCacheEntry(const std::string& key);
  105. ///! Get the number of entries in the cache
  106. int GetSize() {
  107. return static_cast<int>(this->Cache.size()); }
  108. ///! Get a value from the cache given a key
  109. const char* GetInitializedCacheValue(const std::string& key) const;
  110. const char* GetCacheEntryValue(const std::string& key)
  111. {
  112. cmCacheManager::CacheIterator it = this->GetCacheIterator(key.c_str());
  113. if (it.IsAtEnd())
  114. {
  115. return 0;
  116. }
  117. return it.GetValue();
  118. }
  119. const char* GetCacheEntryProperty(std::string const& key,
  120. std::string const& propName)
  121. {
  122. return this->GetCacheIterator(key.c_str()).GetProperty(propName);
  123. }
  124. cmState::CacheEntryType GetCacheEntryType(std::string const& key)
  125. {
  126. return this->GetCacheIterator(key.c_str()).GetType();
  127. }
  128. bool GetCacheEntryPropertyAsBool(std::string const& key,
  129. std::string const& propName)
  130. {
  131. return this->GetCacheIterator(key.c_str()).GetPropertyAsBool(propName);
  132. }
  133. void SetCacheEntryProperty(std::string const& key,
  134. std::string const& propName,
  135. std::string const& value)
  136. {
  137. this->GetCacheIterator(key.c_str()).SetProperty(propName, value.c_str());
  138. }
  139. void SetCacheEntryBoolProperty(std::string const& key,
  140. std::string const& propName,
  141. bool value)
  142. {
  143. this->GetCacheIterator(key.c_str()).SetProperty(propName, value);
  144. }
  145. void SetCacheEntryValue(std::string const& key,
  146. std::string const& value)
  147. {
  148. this->GetCacheIterator(key.c_str()).SetValue(value.c_str());
  149. }
  150. void RemoveCacheEntryProperty(std::string const& key,
  151. std::string const& propName)
  152. {
  153. this->GetCacheIterator(key.c_str()).SetProperty(propName, (void*)0);
  154. }
  155. void AppendCacheEntryProperty(std::string const& key,
  156. std::string const& propName,
  157. std::string const& value,
  158. bool asString = false)
  159. {
  160. this->GetCacheIterator(key.c_str()).AppendProperty(propName,
  161. value.c_str(),
  162. asString);
  163. }
  164. std::vector<std::string> GetCacheEntryKeys()
  165. {
  166. std::vector<std::string> definitions;
  167. definitions.reserve(this->GetSize());
  168. cmCacheManager::CacheIterator cit = this->GetCacheIterator();
  169. for ( cit.Begin(); !cit.IsAtEnd(); cit.Next() )
  170. {
  171. definitions.push_back(cit.GetName());
  172. }
  173. return definitions;
  174. }
  175. /** Get the version of CMake that wrote the cache. */
  176. unsigned int GetCacheMajorVersion() const
  177. { return this->CacheMajorVersion; }
  178. unsigned int GetCacheMinorVersion() const
  179. { return this->CacheMinorVersion; }
  180. protected:
  181. ///! Add an entry into the cache
  182. void AddCacheEntry(const std::string& key, const char* value,
  183. const char* helpString,
  184. cmState::CacheEntryType type);
  185. ///! Get a cache entry object for a key
  186. CacheEntry *GetCacheEntry(const std::string& key);
  187. ///! Clean out the CMakeFiles directory if no CMakeCache.txt
  188. void CleanCMakeFiles(const std::string& path);
  189. // Cache version info
  190. unsigned int CacheMajorVersion;
  191. unsigned int CacheMinorVersion;
  192. private:
  193. cmake* CMakeInstance;
  194. typedef std::map<std::string, CacheEntry> CacheEntryMap;
  195. static void OutputHelpString(std::ostream& fout,
  196. const std::string& helpString);
  197. static void OutputKey(std::ostream& fout, std::string const& key);
  198. static void OutputValue(std::ostream& fout, std::string const& value);
  199. static const char* PersistentProperties[];
  200. bool ReadPropertyEntry(std::string const& key, CacheEntry& e);
  201. void WritePropertyEntries(std::ostream& os, CacheIterator const& i);
  202. CacheEntryMap Cache;
  203. // Only cmake and cmState should be able to add cache values
  204. // the commands should never use the cmCacheManager directly
  205. friend class cmState; // allow access to add cache values
  206. friend class cmake; // allow access to add cache values
  207. };
  208. #endif