cmCacheManager.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. class cmMakefile;
  15. class cmMarkAsAdvancedCommand;
  16. class cmake;
  17. /** \class cmCacheManager
  18. * \brief Control class for cmake's cache
  19. *
  20. * Load and Save CMake cache files.
  21. *
  22. */
  23. class cmCacheManager
  24. {
  25. public:
  26. cmCacheManager(cmake* cm);
  27. class CacheIterator;
  28. friend class cmCacheManager::CacheIterator;
  29. enum CacheEntryType{ BOOL=0, PATH, FILEPATH, STRING, INTERNAL,STATIC,
  30. UNINITIALIZED };
  31. private:
  32. struct CacheEntry
  33. {
  34. std::string Value;
  35. CacheEntryType Type;
  36. cmPropertyMap Properties;
  37. const char* GetProperty(const std::string&) const;
  38. void SetProperty(const std::string& property, const char* value);
  39. void AppendProperty(const std::string& property, const char* value,
  40. bool asString=false);
  41. bool Initialized;
  42. CacheEntry() : Value(""), Type(UNINITIALIZED), 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. CacheEntryType GetType() const { return this->GetEntry().Type; }
  66. void SetType(CacheEntryType ty) { this->GetEntry().Type = ty; }
  67. bool Initialized() { return this->GetEntry().Initialized; }
  68. cmCacheManager &Container;
  69. std::map<std::string, CacheEntry>::iterator Position;
  70. CacheIterator(cmCacheManager &cm) : Container(cm) {
  71. this->Begin();
  72. }
  73. CacheIterator(cmCacheManager &cm, const char* key) : Container(cm)
  74. {
  75. if ( key )
  76. {
  77. this->Find(key);
  78. }
  79. }
  80. private:
  81. CacheEntry const& GetEntry() const { return this->Position->second; }
  82. CacheEntry& GetEntry() { return this->Position->second; }
  83. };
  84. ///! return an iterator to iterate through the cache map
  85. cmCacheManager::CacheIterator NewIterator()
  86. {
  87. return CacheIterator(*this);
  88. }
  89. /**
  90. * Types for the cache entries. These are useful as
  91. * hints for a cache editor program. Path should bring
  92. * up a file chooser, BOOL a check box, and STRING a
  93. * text entry box, FILEPATH is a full path to a file which
  94. * can be different than just a path input
  95. */
  96. static CacheEntryType StringToType(const char*);
  97. static const char* TypeToString(CacheEntryType);
  98. static bool IsType(const char*);
  99. ///! Load a cache for given makefile. Loads from path/CMakeCache.txt.
  100. bool LoadCache(const std::string& path);
  101. bool LoadCache(const std::string& path, bool internal,
  102. std::set<std::string>& excludes,
  103. std::set<std::string>& includes);
  104. ///! Save cache for given makefile. Saves to ouput path/CMakeCache.txt
  105. bool SaveCache(const std::string& path) ;
  106. ///! Delete the cache given
  107. bool DeleteCache(const std::string& path);
  108. ///! Print the cache to a stream
  109. void PrintCache(std::ostream&) const;
  110. ///! Get the iterator for an entry with a given key.
  111. cmCacheManager::CacheIterator GetCacheIterator(const char *key=0);
  112. ///! Remove an entry from the cache
  113. void RemoveCacheEntry(const std::string& key);
  114. ///! Get the number of entries in the cache
  115. int GetSize() {
  116. return static_cast<int>(this->Cache.size()); }
  117. ///! Break up a line like VAR:type="value" into var, type and value
  118. static bool ParseEntry(const std::string& entry,
  119. std::string& var,
  120. std::string& value,
  121. CacheEntryType& type);
  122. ///! Get a value from the cache given a key
  123. const char* GetInitializedCacheValue(const std::string& key) const;
  124. const char* GetCacheEntryValue(const std::string& key)
  125. {
  126. cmCacheManager::CacheIterator it = this->GetCacheIterator(key.c_str());
  127. if (it.IsAtEnd())
  128. {
  129. return 0;
  130. }
  131. return it.GetValue();
  132. }
  133. const char* GetCacheEntryProperty(std::string const& key,
  134. std::string const& propName)
  135. {
  136. return this->GetCacheIterator(key.c_str()).GetProperty(propName);
  137. }
  138. CacheEntryType GetCacheEntryType(std::string const& key)
  139. {
  140. return this->GetCacheIterator(key.c_str()).GetType();
  141. }
  142. bool GetCacheEntryPropertyAsBool(std::string const& key,
  143. std::string const& propName)
  144. {
  145. return this->GetCacheIterator(key.c_str()).GetPropertyAsBool(propName);
  146. }
  147. void SetCacheEntryProperty(std::string const& key,
  148. std::string const& propName,
  149. std::string const& value)
  150. {
  151. this->GetCacheIterator(key.c_str()).SetProperty(propName, value.c_str());
  152. }
  153. void SetCacheEntryBoolProperty(std::string const& key,
  154. std::string const& propName,
  155. bool value)
  156. {
  157. this->GetCacheIterator(key.c_str()).SetProperty(propName, value);
  158. }
  159. void SetCacheEntryValue(std::string const& key,
  160. std::string const& value)
  161. {
  162. this->GetCacheIterator(key.c_str()).SetValue(value.c_str());
  163. }
  164. void RemoveCacheEntryProperty(std::string const& key,
  165. std::string const& propName)
  166. {
  167. this->GetCacheIterator(key.c_str()).SetProperty(propName, (void*)0);
  168. }
  169. void AppendCacheEntryProperty(std::string const& key,
  170. std::string const& propName,
  171. std::string const& value,
  172. bool asString = false)
  173. {
  174. this->GetCacheIterator(key.c_str()).AppendProperty(propName,
  175. value.c_str(),
  176. asString);
  177. }
  178. std::vector<std::string> GetCacheEntryKeys()
  179. {
  180. std::vector<std::string> definitions;
  181. definitions.reserve(this->GetSize());
  182. cmCacheManager::CacheIterator cit = this->GetCacheIterator();
  183. for ( cit.Begin(); !cit.IsAtEnd(); cit.Next() )
  184. {
  185. definitions.push_back(cit.GetName());
  186. }
  187. return definitions;
  188. }
  189. /** Get the version of CMake that wrote the cache. */
  190. unsigned int GetCacheMajorVersion() const
  191. { return this->CacheMajorVersion; }
  192. unsigned int GetCacheMinorVersion() const
  193. { return this->CacheMinorVersion; }
  194. protected:
  195. ///! Add an entry into the cache
  196. void AddCacheEntry(const std::string& key, const char* value,
  197. const char* helpString, CacheEntryType type);
  198. ///! Get a cache entry object for a key
  199. CacheEntry *GetCacheEntry(const std::string& key);
  200. ///! Clean out the CMakeFiles directory if no CMakeCache.txt
  201. void CleanCMakeFiles(const std::string& path);
  202. // Cache version info
  203. unsigned int CacheMajorVersion;
  204. unsigned int CacheMinorVersion;
  205. private:
  206. cmake* CMakeInstance;
  207. typedef std::map<std::string, CacheEntry> CacheEntryMap;
  208. static void OutputHelpString(std::ostream& fout,
  209. const std::string& helpString);
  210. static void OutputKey(std::ostream& fout, std::string const& key);
  211. static void OutputValue(std::ostream& fout, std::string const& value);
  212. static const char* PersistentProperties[];
  213. bool ReadPropertyEntry(std::string const& key, CacheEntry& e);
  214. void WritePropertyEntries(std::ostream& os, CacheIterator const& i);
  215. CacheEntryMap Cache;
  216. // Only cmake and cmMakefile should be able to add cache values
  217. // the commands should never use the cmCacheManager directly
  218. friend class cmMakefile; // allow access to add cache values
  219. friend class cmake; // allow access to add cache values
  220. friend class cmMarkAsAdvancedCommand; // allow access to add cache values
  221. };
  222. #endif