cmDefinitions.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #ifndef cmDefinitions_h
  11. #define cmDefinitions_h
  12. #include "cmStandardIncludes.h"
  13. #if defined(CMAKE_BUILD_WITH_CMAKE)
  14. #include "cmsys/hash_map.hxx"
  15. #endif
  16. /** \class cmDefinitions
  17. * \brief Store a scope of variable definitions for CMake language.
  18. *
  19. * This stores the state of variable definitions (set or unset) for
  20. * one scope. Sets are always local. Gets search parent scopes
  21. * transitively and save results locally.
  22. */
  23. class cmDefinitions
  24. {
  25. public:
  26. /** Construct with the given parent scope. */
  27. cmDefinitions(cmDefinitions* parent = 0);
  28. /** Returns the parent scope, if any. */
  29. cmDefinitions* GetParent() const { return this->Up; }
  30. /** Get the value associated with a key; null if none.
  31. Store the result locally if it came from a parent. */
  32. const char* Get(const std::string& key);
  33. /** Set (or unset if null) a value associated with a key. */
  34. void Set(const std::string& key, const char* value);
  35. void Erase(const std::string& key);
  36. /** Get the set of all local keys. */
  37. std::vector<std::string> LocalKeys() const;
  38. /** Compute the closure of all defined keys with values.
  39. This flattens the scope. The result has no parent. */
  40. cmDefinitions Closure() const;
  41. /** Compute the set of all defined keys. */
  42. std::vector<std::string> ClosureKeys() const;
  43. private:
  44. // String with existence boolean.
  45. struct Def: public std::string
  46. {
  47. private:
  48. typedef std::string std_string;
  49. public:
  50. Def(): std_string(), Exists(false) {}
  51. Def(const char* v): std_string(v?v:""), Exists(v?true:false) {}
  52. Def(const std_string& v): std_string(v), Exists(true) {}
  53. Def(Def const& d): std_string(d), Exists(d.Exists) {}
  54. bool Exists;
  55. };
  56. static Def NoDef;
  57. // Parent scope, if any.
  58. cmDefinitions* Up;
  59. // Local definitions, set or unset.
  60. #if defined(CMAKE_BUILD_WITH_CMAKE)
  61. typedef cmsys::hash_map<std::string, Def> MapType;
  62. #else
  63. typedef std::map<std::string, Def> MapType;
  64. #endif
  65. MapType Map;
  66. // Internal query and update methods.
  67. Def const& GetInternal(const std::string& key);
  68. // Implementation of Closure() method.
  69. struct ClosureTag {};
  70. cmDefinitions(ClosureTag const&, cmDefinitions const* root);
  71. void ClosureImpl(std::set<std::string>& undefined,
  72. cmDefinitions const* defs);
  73. };
  74. #endif