You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
5.7 KiB

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