cmCacheManager.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm 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. };
  36. public:
  37. class CacheIterator
  38. {
  39. public:
  40. void Begin();
  41. bool Find(const char*);
  42. bool IsAtEnd();
  43. void Next();
  44. const char *GetName() const {
  45. return m_Position->first.c_str(); }
  46. const char* GetProperty(const char*) const ;
  47. bool GetPropertyAsBool(const char*) const ;
  48. void SetProperty(const char* property, const char* value);
  49. void SetProperty(const char* property, bool value);
  50. const char* GetValue() const { return this->GetEntry().m_Value.c_str(); }
  51. void SetValue(const char*);
  52. CacheEntryType GetType() const { return this->GetEntry().m_Type; }
  53. cmCacheManager &m_Container;
  54. std::map<cmStdString, CacheEntry>::iterator m_Position;
  55. CacheIterator(cmCacheManager &foo) : m_Container(foo) {
  56. this->Begin();
  57. }
  58. CacheIterator(cmCacheManager &foo, const char* key) : m_Container(foo) {
  59. this->Find(key);
  60. }
  61. private:
  62. CacheEntry const& GetEntry() const { return m_Position->second; }
  63. CacheEntry& GetEntry() { return m_Position->second; }
  64. };
  65. ///! return an iterator to iterate through the cache map
  66. cmCacheManager::CacheIterator NewIterator()
  67. {
  68. return CacheIterator(*this);
  69. }
  70. /**
  71. * Types for the cache entries. These are useful as
  72. * hints for a cache editor program. Path should bring
  73. * up a file chooser, BOOL a check box, and STRING a
  74. * text entry box, FILEPATH is a full path to a file which
  75. * can be different than just a path input
  76. */
  77. static CacheEntryType StringToType(const char*);
  78. ///! Load a cache for given makefile. Loads from ouput home.
  79. bool LoadCache(cmMakefile*);
  80. ///! Load a cache for given makefile. Loads from path/CMakeCache.txt.
  81. bool LoadCache(const char* path);
  82. bool LoadCache(const char* path, bool internal);
  83. bool LoadCache(const char* path, bool internal,
  84. std::set<std::string>& excludes,
  85. std::set<std::string>& includes);
  86. ///! Save cache for given makefile. Saves to ouput home CMakeCache.txt.
  87. bool SaveCache(cmMakefile*) ;
  88. ///! Save cache for given makefile. Saves to ouput path/CMakeCache.txt
  89. bool SaveCache(const char* path) ;
  90. ///! Print the cache to a stream
  91. void PrintCache(std::ostream&) const;
  92. ///! Get the iterator for an entry with a given key.
  93. cmCacheManager::CacheIterator GetCacheIterator(const char *key);
  94. ///! Remove an entry from the cache
  95. void RemoveCacheEntry(const char* key);
  96. ///! Get the number of entries in the cache
  97. int GetSize() {
  98. return static_cast<int>(m_Cache.size()); }
  99. ///! Break up a line like VAR:type="value" into var, type and value
  100. static bool ParseEntry(const char* entry,
  101. std::string& var,
  102. std::string& value,
  103. CacheEntryType& type);
  104. ///! Get a value from the cache given a key
  105. const char* GetCacheValue(const char* key) const;
  106. protected:
  107. ///! Add an entry into the cache
  108. void AddCacheEntry(const char* key, const char* value,
  109. const char* helpString, CacheEntryType type);
  110. ///! Add a BOOL entry into the cache
  111. void AddCacheEntry(const char* key, bool, const char* helpString);
  112. ///! Get a cache entry object for a key
  113. CacheEntry *GetCacheEntry(const char *key);
  114. private:
  115. typedef std::map<cmStdString, CacheEntry> CacheEntryMap;
  116. static void OutputHelpString(std::ofstream& fout,
  117. const std::string& helpString);
  118. CacheEntryMap m_Cache;
  119. // Only cmake and cmMakefile should be able to add cache values
  120. // the commands should never use the cmCacheManager directly
  121. friend class cmMakefile; // allow access to add cache values
  122. friend class cmake; // allow access to add cache values
  123. friend class cmakewizard; // allow access to add cache values
  124. };
  125. #endif