cmCacheManager.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. /** Get the version of CMake that wrote the cache. */
  125. unsigned int GetCacheMajorVersion() const
  126. { return this->CacheMajorVersion; }
  127. unsigned int GetCacheMinorVersion() const
  128. { return this->CacheMinorVersion; }
  129. protected:
  130. ///! Add an entry into the cache
  131. void AddCacheEntry(const std::string& key, const char* value,
  132. const char* helpString, CacheEntryType type);
  133. ///! Get a cache entry object for a key
  134. CacheEntry *GetCacheEntry(const std::string& key);
  135. ///! Clean out the CMakeFiles directory if no CMakeCache.txt
  136. void CleanCMakeFiles(const std::string& path);
  137. // Cache version info
  138. unsigned int CacheMajorVersion;
  139. unsigned int CacheMinorVersion;
  140. private:
  141. cmake* CMakeInstance;
  142. typedef std::map<std::string, CacheEntry> CacheEntryMap;
  143. static void OutputHelpString(std::ostream& fout,
  144. const std::string& helpString);
  145. static void OutputKey(std::ostream& fout, std::string const& key);
  146. static void OutputValue(std::ostream& fout, std::string const& value);
  147. static const char* PersistentProperties[];
  148. bool ReadPropertyEntry(std::string const& key, CacheEntry& e);
  149. void WritePropertyEntries(std::ostream& os, CacheIterator const& i);
  150. CacheEntryMap Cache;
  151. // Only cmake and cmMakefile should be able to add cache values
  152. // the commands should never use the cmCacheManager directly
  153. friend class cmMakefile; // allow access to add cache values
  154. friend class cmake; // allow access to add cache values
  155. friend class cmMarkAsAdvancedCommand; // allow access to add cache values
  156. };
  157. #endif