cmDefinitions.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmDefinitions_h
  4. #define cmDefinitions_h
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include "cm_string_view.hxx"
  7. #include "cmLinkedTree.h"
  8. #include <string>
  9. #include <unordered_map>
  10. #include <vector>
  11. /** \class cmDefinitions
  12. * \brief Store a scope of variable definitions for CMake language.
  13. *
  14. * This stores the state of variable definitions (set or unset) for
  15. * one scope. Sets are always local. Gets search parent scopes
  16. * transitively and save results locally.
  17. */
  18. class cmDefinitions
  19. {
  20. typedef cmLinkedTree<cmDefinitions>::iterator StackIter;
  21. public:
  22. static const std::string* Get(const std::string& key, StackIter begin,
  23. StackIter end);
  24. static void Raise(const std::string& key, StackIter begin, StackIter end);
  25. static bool HasKey(const std::string& key, StackIter begin, StackIter end);
  26. /** Set a value associated with a key. */
  27. void Set(const std::string& key, cm::string_view value);
  28. /** Unset a definition. */
  29. void Unset(const std::string& key);
  30. std::vector<std::string> UnusedKeys() const;
  31. static std::vector<std::string> ClosureKeys(StackIter begin, StackIter end);
  32. static cmDefinitions MakeClosure(StackIter begin, StackIter end);
  33. private:
  34. /** String with existence boolean. */
  35. struct Def
  36. {
  37. public:
  38. Def() = default;
  39. Def(cm::string_view value)
  40. : Value(value)
  41. , Exists(true)
  42. {
  43. }
  44. std::string Value;
  45. bool Exists = false;
  46. bool Used = false;
  47. };
  48. static Def NoDef;
  49. typedef std::unordered_map<std::string, Def> MapType;
  50. MapType Map;
  51. static Def const& GetInternal(const std::string& key, StackIter begin,
  52. StackIter end, bool raise);
  53. };
  54. #endif