cmCacheManager.h 5.4 KB

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