cmCacheManager.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #ifndef cmCacheManager_h
  14. #define cmCacheManager_h
  15. #include "cmStandardIncludes.h"
  16. #include "cmPropertyMap.h"
  17. class cmMakefile;
  18. class cmMarkAsAdvancedCommand;
  19. /** \class cmCacheManager
  20. * \brief Control class for cmake's cache
  21. *
  22. * Load and Save CMake cache files.
  23. *
  24. */
  25. class cmCacheManager
  26. {
  27. public:
  28. cmCacheManager();
  29. class CacheIterator;
  30. friend class cmCacheManager::CacheIterator;
  31. enum CacheEntryType{ BOOL=0, PATH, FILEPATH, STRING, INTERNAL,STATIC,
  32. UNINITIALIZED };
  33. private:
  34. struct CacheEntry
  35. {
  36. std::string Value;
  37. CacheEntryType Type;
  38. cmPropertyMap Properties;
  39. const char* GetProperty(const char*) const;
  40. void SetProperty(const char* property, const char* value);
  41. void AppendProperty(const char* property, const char* value);
  42. bool Initialized;
  43. CacheEntry() : Value(""), Type(UNINITIALIZED), Initialized(false)
  44. {}
  45. };
  46. public:
  47. class CacheIterator
  48. {
  49. public:
  50. void Begin();
  51. bool Find(const char*);
  52. bool IsAtEnd() const;
  53. void Next();
  54. const char *GetName() const {
  55. return this->Position->first.c_str(); }
  56. const char* GetProperty(const char*) const ;
  57. bool GetPropertyAsBool(const char*) const ;
  58. bool PropertyExists(const char*) const;
  59. void SetProperty(const char* property, const char* value);
  60. void AppendProperty(const char* property, const char* value);
  61. void SetProperty(const char* 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<cmStdString, 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 ouput home.
  100. bool LoadCache(cmMakefile*);
  101. ///! Load a cache for given makefile. Loads from path/CMakeCache.txt.
  102. bool LoadCache(const char* path);
  103. bool LoadCache(const char* path, bool internal);
  104. bool LoadCache(const char* path, bool internal,
  105. std::set<cmStdString>& excludes,
  106. std::set<cmStdString>& includes);
  107. ///! Save cache for given makefile. Saves to ouput home CMakeCache.txt.
  108. bool SaveCache(cmMakefile*) ;
  109. ///! Save cache for given makefile. Saves to ouput path/CMakeCache.txt
  110. bool SaveCache(const char* path) ;
  111. ///! Delete the cache given
  112. bool DeleteCache(const char* path);
  113. ///! Print the cache to a stream
  114. void PrintCache(std::ostream&) const;
  115. ///! Get the iterator for an entry with a given key.
  116. cmCacheManager::CacheIterator GetCacheIterator(const char *key=0);
  117. ///! Remove an entry from the cache
  118. void RemoveCacheEntry(const char* key);
  119. ///! Get the number of entries in the cache
  120. int GetSize() {
  121. return static_cast<int>(this->Cache.size()); }
  122. ///! Break up a line like VAR:type="value" into var, type and value
  123. static bool ParseEntry(const char* entry,
  124. std::string& var,
  125. std::string& value,
  126. CacheEntryType& type);
  127. static bool ParseEntry(const char* entry,
  128. std::string& var,
  129. std::string& value);
  130. ///! Get a value from the cache given a key
  131. const char* GetCacheValue(const char* key) const;
  132. /** Get the version of CMake that wrote the cache. */
  133. unsigned int GetCacheMajorVersion() { return this->CacheMajorVersion; }
  134. unsigned int GetCacheMinorVersion() { return this->CacheMinorVersion; }
  135. bool NeedCacheCompatibility(int major, int minor);
  136. /** Define and document CACHE entry properties. */
  137. static void DefineProperties(cmake *cm);
  138. protected:
  139. ///! Add an entry into the cache
  140. void AddCacheEntry(const char* key, const char* value,
  141. const char* helpString, CacheEntryType type);
  142. ///! Add a BOOL entry into the cache
  143. void AddCacheEntry(const char* key, bool, const char* helpString);
  144. ///! Get a cache entry object for a key
  145. CacheEntry *GetCacheEntry(const char *key);
  146. ///! Clean out the CMakeFiles directory if no CMakeCache.txt
  147. void CleanCMakeFiles(const char* path);
  148. // Cache version info
  149. unsigned int CacheMajorVersion;
  150. unsigned int CacheMinorVersion;
  151. private:
  152. typedef std::map<cmStdString, CacheEntry> CacheEntryMap;
  153. static void OutputHelpString(std::ostream& fout,
  154. const std::string& helpString);
  155. static void OutputKey(std::ostream& fout, std::string const& key);
  156. static void OutputValue(std::ostream& fout, std::string const& value);
  157. static const char* PersistentProperties[];
  158. bool ReadPropertyEntry(std::string const& key, CacheEntry& e);
  159. void WritePropertyEntries(std::ostream& os, CacheIterator const& i);
  160. CacheEntryMap Cache;
  161. // Only cmake and cmMakefile should be able to add cache values
  162. // the commands should never use the cmCacheManager directly
  163. friend class cmMakefile; // allow access to add cache values
  164. friend class cmake; // allow access to add cache values
  165. friend class cmakewizard; // allow access to add cache values
  166. friend class cmMarkAsAdvancedCommand; // allow access to add cache values
  167. };
  168. #endif