cmCacheManager.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. void SetType(CacheEntryType ty) { this->GetEntry().Type = ty; }
  62. bool Initialized() { return this->GetEntry().Initialized; }
  63. cmCacheManager &Container;
  64. std::map<cmStdString, CacheEntry>::iterator Position;
  65. CacheIterator(cmCacheManager &cm) : Container(cm) {
  66. this->Begin();
  67. }
  68. CacheIterator(cmCacheManager &cm, const char* key) : Container(cm)
  69. {
  70. if ( key )
  71. {
  72. this->Find(key);
  73. }
  74. }
  75. private:
  76. CacheEntry const& GetEntry() const { return this->Position->second; }
  77. CacheEntry& GetEntry() { return this->Position->second; }
  78. };
  79. ///! return an iterator to iterate through the cache map
  80. cmCacheManager::CacheIterator NewIterator()
  81. {
  82. return CacheIterator(*this);
  83. }
  84. /**
  85. * Types for the cache entries. These are useful as
  86. * hints for a cache editor program. Path should bring
  87. * up a file chooser, BOOL a check box, and STRING a
  88. * text entry box, FILEPATH is a full path to a file which
  89. * can be different than just a path input
  90. */
  91. static CacheEntryType StringToType(const char*);
  92. static const char* TypeToString(CacheEntryType);
  93. ///! Load a cache for given makefile. Loads from ouput home.
  94. bool LoadCache(cmMakefile*);
  95. ///! Load a cache for given makefile. Loads from path/CMakeCache.txt.
  96. bool LoadCache(const char* path);
  97. bool LoadCache(const char* path, bool internal);
  98. bool LoadCache(const char* path, bool internal,
  99. std::set<cmStdString>& excludes,
  100. std::set<cmStdString>& includes);
  101. ///! Save cache for given makefile. Saves to ouput home CMakeCache.txt.
  102. bool SaveCache(cmMakefile*) ;
  103. ///! Save cache for given makefile. Saves to ouput path/CMakeCache.txt
  104. bool SaveCache(const char* path) ;
  105. ///! Delete the cache given
  106. bool DeleteCache(const char* path);
  107. ///! Print the cache to a stream
  108. void PrintCache(std::ostream&) const;
  109. ///! Get the iterator for an entry with a given key.
  110. cmCacheManager::CacheIterator GetCacheIterator(const char *key=0);
  111. ///! Remove an entry from the cache
  112. void RemoveCacheEntry(const char* key);
  113. ///! Get the number of entries in the cache
  114. int GetSize() {
  115. return static_cast<int>(this->Cache.size()); }
  116. ///! Break up a line like VAR:type="value" into var, type and value
  117. static bool ParseEntry(const char* entry,
  118. std::string& var,
  119. std::string& value,
  120. CacheEntryType& type);
  121. static bool ParseEntry(const char* entry,
  122. std::string& var,
  123. std::string& value);
  124. ///! Get a value from the cache given a key
  125. const char* GetCacheValue(const char* key) const;
  126. /** Get the version of CMake that wrote the cache. */
  127. unsigned int GetCacheMajorVersion() { return this->CacheMajorVersion; }
  128. unsigned int GetCacheMinorVersion() { return this->CacheMinorVersion; }
  129. bool NeedCacheCompatibility(int major, int minor);
  130. protected:
  131. ///! Add an entry into the cache
  132. void AddCacheEntry(const char* key, const char* value,
  133. const char* helpString, CacheEntryType type);
  134. ///! Add a BOOL entry into the cache
  135. void AddCacheEntry(const char* key, bool, const char* helpString);
  136. ///! Get a cache entry object for a key
  137. CacheEntry *GetCacheEntry(const char *key);
  138. ///! Clean out the CMakeFiles directory if no CMakeCache.txt
  139. void CleanCMakeFiles(const char* path);
  140. // Cache version info
  141. unsigned int CacheMajorVersion;
  142. unsigned int CacheMinorVersion;
  143. private:
  144. typedef std::map<cmStdString, CacheEntry> CacheEntryMap;
  145. static void OutputHelpString(std::ofstream& fout,
  146. const std::string& helpString);
  147. CacheEntryMap Cache;
  148. // Only cmake and cmMakefile should be able to add cache values
  149. // the commands should never use the cmCacheManager directly
  150. friend class cmMakefile; // allow access to add cache values
  151. friend class cmake; // allow access to add cache values
  152. friend class cmakewizard; // allow access to add cache values
  153. friend class cmMarkAsAdvancedCommand; // allow access to add cache values
  154. };
  155. #endif