cmCacheManager.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. class cmMakefile;
  17. class cmMarkAsAdvancedCommand;
  18. /** \class cmCacheManager
  19. * \brief Control class for cmake's cache
  20. *
  21. * Load and Save CMake cache files.
  22. *
  23. */
  24. class cmCacheManager
  25. {
  26. public:
  27. cmCacheManager();
  28. class CacheIterator;
  29. friend class cmCacheManager::CacheIterator;
  30. enum CacheEntryType{ BOOL=0, PATH, FILEPATH, STRING, INTERNAL,STATIC,
  31. UNINITIALIZED };
  32. private:
  33. struct CacheEntry
  34. {
  35. std::string Value;
  36. CacheEntryType Type;
  37. std::map<cmStdString,cmStdString> Properties;
  38. bool Initialized;
  39. CacheEntry() : Value(""), Type(UNINITIALIZED), Initialized(false)
  40. {}
  41. };
  42. public:
  43. class CacheIterator
  44. {
  45. public:
  46. void Begin();
  47. bool Find(const char*);
  48. bool IsAtEnd() const;
  49. void Next();
  50. const char *GetName() const {
  51. return this->Position->first.c_str(); }
  52. const char* GetProperty(const char*) const ;
  53. bool GetPropertyAsBool(const char*) const ;
  54. bool PropertyExists(const char*) const;
  55. void SetProperty(const char* property, const char* value);
  56. void SetProperty(const char* property, bool value);
  57. const char* GetValue() const { return this->GetEntry().Value.c_str(); }
  58. bool GetValueAsBool() const;
  59. void SetValue(const char*);
  60. CacheEntryType GetType() const { return this->GetEntry().Type; }
  61. bool Initialized() { return this->GetEntry().Initialized; }
  62. cmCacheManager &Container;
  63. std::map<cmStdString, CacheEntry>::iterator Position;
  64. CacheIterator(cmCacheManager &cm) : Container(cm) {
  65. this->Begin();
  66. }
  67. CacheIterator(cmCacheManager &cm, const char* key) : Container(cm)
  68. {
  69. if ( key )
  70. {
  71. this->Find(key);
  72. }
  73. }
  74. private:
  75. CacheEntry const& GetEntry() const { return this->Position->second; }
  76. CacheEntry& GetEntry() { return this->Position->second; }
  77. };
  78. ///! return an iterator to iterate through the cache map
  79. cmCacheManager::CacheIterator NewIterator()
  80. {
  81. return CacheIterator(*this);
  82. }
  83. /**
  84. * Types for the cache entries. These are useful as
  85. * hints for a cache editor program. Path should bring
  86. * up a file chooser, BOOL a check box, and STRING a
  87. * text entry box, FILEPATH is a full path to a file which
  88. * can be different than just a path input
  89. */
  90. static CacheEntryType StringToType(const char*);
  91. static const char* TypeToString(CacheEntryType);
  92. ///! Load a cache for given makefile. Loads from ouput home.
  93. bool LoadCache(cmMakefile*);
  94. ///! Load a cache for given makefile. Loads from path/CMakeCache.txt.
  95. bool LoadCache(const char* path);
  96. bool LoadCache(const char* path, bool internal);
  97. bool LoadCache(const char* path, bool internal,
  98. std::set<cmStdString>& excludes,
  99. std::set<cmStdString>& includes);
  100. ///! Save cache for given makefile. Saves to ouput home CMakeCache.txt.
  101. bool SaveCache(cmMakefile*) ;
  102. ///! Save cache for given makefile. Saves to ouput path/CMakeCache.txt
  103. bool SaveCache(const char* path) ;
  104. ///! Delete the cache given
  105. bool DeleteCache(const char* path);
  106. ///! Print the cache to a stream
  107. void PrintCache(std::ostream&) const;
  108. ///! Get the iterator for an entry with a given key.
  109. cmCacheManager::CacheIterator GetCacheIterator(const char *key=0);
  110. ///! Remove an entry from the cache
  111. void RemoveCacheEntry(const char* key);
  112. ///! Get the number of entries in the cache
  113. int GetSize() {
  114. return static_cast<int>(this->Cache.size()); }
  115. ///! Break up a line like VAR:type="value" into var, type and value
  116. static bool ParseEntry(const char* entry,
  117. std::string& var,
  118. std::string& value,
  119. CacheEntryType& type);
  120. static bool ParseEntry(const char* entry,
  121. std::string& var,
  122. std::string& value);
  123. ///! Get a value from the cache given a key
  124. const char* GetCacheValue(const char* key) const;
  125. /** Get the version of CMake that wrote the cache. */
  126. unsigned int GetCacheMajorVersion() { return this->CacheMajorVersion; }
  127. unsigned int GetCacheMinorVersion() { return this->CacheMinorVersion; }
  128. bool NeedCacheCompatibility(int major, int minor);
  129. protected:
  130. ///! Add an entry into the cache
  131. void AddCacheEntry(const char* key, const char* value,
  132. const char* helpString, CacheEntryType type);
  133. ///! Add a BOOL entry into the cache
  134. void AddCacheEntry(const char* key, bool, const char* helpString);
  135. ///! Get a cache entry object for a key
  136. CacheEntry *GetCacheEntry(const char *key);
  137. ///! Clean out the CMakeFiles directory if no CMakeCache.txt
  138. void CleanCMakeFiles(const char* path);
  139. // Cache version info
  140. unsigned int CacheMajorVersion;
  141. unsigned int CacheMinorVersion;
  142. private:
  143. typedef std::map<cmStdString, CacheEntry> CacheEntryMap;
  144. static void OutputHelpString(std::ofstream& fout,
  145. const std::string& helpString);
  146. CacheEntryMap Cache;
  147. // Only cmake and cmMakefile should be able to add cache values
  148. // the commands should never use the cmCacheManager directly
  149. friend class cmMakefile; // allow access to add cache values
  150. friend class cmake; // allow access to add cache values
  151. friend class cmakewizard; // allow access to add cache values
  152. friend class cmMarkAsAdvancedCommand; // allow access to add cache values
  153. };
  154. #endif