cmDefinitions.cxx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #include "cmDefinitions.h"
  11. #include <assert.h>
  12. cmDefinitions::Def cmDefinitions::NoDef;
  13. cmDefinitions::Def const& cmDefinitions::GetInternal(const std::string& key,
  14. StackIter begin,
  15. StackIter end, bool raise)
  16. {
  17. assert(begin != end);
  18. MapType::iterator i = begin->Map.find(key);
  19. if (i != begin->Map.end()) {
  20. i->second.Used = true;
  21. return i->second;
  22. }
  23. StackIter it = begin;
  24. ++it;
  25. if (it == end) {
  26. return cmDefinitions::NoDef;
  27. }
  28. Def const& def = cmDefinitions::GetInternal(key, it, end, raise);
  29. if (!raise) {
  30. return def;
  31. }
  32. return begin->Map.insert(MapType::value_type(key, def)).first->second;
  33. }
  34. const char* cmDefinitions::Get(const std::string& key, StackIter begin,
  35. StackIter end)
  36. {
  37. Def const& def = cmDefinitions::GetInternal(key, begin, end, false);
  38. return def.Exists ? def.c_str() : CM_NULLPTR;
  39. }
  40. void cmDefinitions::Raise(const std::string& key, StackIter begin,
  41. StackIter end)
  42. {
  43. cmDefinitions::GetInternal(key, begin, end, true);
  44. }
  45. bool cmDefinitions::HasKey(const std::string& key, StackIter begin,
  46. StackIter end)
  47. {
  48. for (StackIter it = begin; it != end; ++it) {
  49. MapType::const_iterator i = it->Map.find(key);
  50. if (i != it->Map.end()) {
  51. return true;
  52. }
  53. }
  54. return false;
  55. }
  56. void cmDefinitions::Set(const std::string& key, const char* value)
  57. {
  58. Def def(value);
  59. this->Map[key] = def;
  60. }
  61. std::vector<std::string> cmDefinitions::UnusedKeys() const
  62. {
  63. std::vector<std::string> keys;
  64. keys.reserve(this->Map.size());
  65. // Consider local definitions.
  66. for (MapType::const_iterator mi = this->Map.begin(); mi != this->Map.end();
  67. ++mi) {
  68. if (!mi->second.Used) {
  69. keys.push_back(mi->first);
  70. }
  71. }
  72. return keys;
  73. }
  74. cmDefinitions cmDefinitions::MakeClosure(StackIter begin, StackIter end)
  75. {
  76. cmDefinitions closure;
  77. std::set<std::string> undefined;
  78. for (StackIter it = begin; it != end; ++it) {
  79. // Consider local definitions.
  80. for (MapType::const_iterator mi = it->Map.begin(); mi != it->Map.end();
  81. ++mi) {
  82. // Use this key if it is not already set or unset.
  83. if (closure.Map.find(mi->first) == closure.Map.end() &&
  84. undefined.find(mi->first) == undefined.end()) {
  85. if (mi->second.Exists) {
  86. closure.Map.insert(*mi);
  87. } else {
  88. undefined.insert(mi->first);
  89. }
  90. }
  91. }
  92. }
  93. return closure;
  94. }
  95. std::vector<std::string> cmDefinitions::ClosureKeys(StackIter begin,
  96. StackIter end)
  97. {
  98. std::set<std::string> bound;
  99. std::vector<std::string> defined;
  100. for (StackIter it = begin; it != end; ++it) {
  101. defined.reserve(defined.size() + it->Map.size());
  102. for (MapType::const_iterator mi = it->Map.begin(); mi != it->Map.end();
  103. ++mi) {
  104. // Use this key if it is not already set or unset.
  105. if (bound.insert(mi->first).second && mi->second.Exists) {
  106. defined.push_back(mi->first);
  107. }
  108. }
  109. }
  110. return defined;
  111. }