cmDefinitions.cxx 3.7 KB

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