cmCacheManager.h 5.6 KB

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