/Context.cpp

http://github.com/fawek/cjango · C++ · 93 lines · 56 code · 16 blank · 21 comment · 8 complexity · dc8227989753e3cd07c0ef3bb18c9c58 MD5 · raw file

  1. /**
  2. * Copyright (C) 2010 Jakub Wieczorek <fawek@fawek.net>
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. #include "Context.h"
  23. Context::Context(const VariableMap& variables)
  24. {
  25. m_variableStack.push_back(variables);
  26. }
  27. Context::~Context()
  28. {
  29. m_variableStack.clear();
  30. }
  31. void Context::push()
  32. {
  33. m_variableStack.push_back(VariableMap());
  34. }
  35. void Context::pop()
  36. {
  37. assert(m_variableStack.size() > 1);
  38. m_variableStack.pop_back();
  39. }
  40. bool Context::contains(const std::string& key) const
  41. {
  42. assert(!m_variableStack.empty());
  43. std::vector<VariableMap>::const_reverse_iterator it = m_variableStack.rbegin();
  44. std::vector<VariableMap>::const_reverse_iterator end = m_variableStack.rend();
  45. for (it; it != end; ++it) {
  46. const VariableMap& map = *it;
  47. if (map.find(key) != map.end())
  48. return true;
  49. }
  50. return false;
  51. }
  52. const Variant& Context::get(const std::string& key) const
  53. {
  54. static Variant nullVariant;
  55. assert(!m_variableStack.empty());
  56. std::vector<VariableMap>::const_reverse_iterator it = m_variableStack.rbegin();
  57. std::vector<VariableMap>::const_reverse_iterator end = m_variableStack.rend();
  58. for (it; it != end; ++it) {
  59. const VariableMap& map = *it;
  60. VariableMap::const_iterator found = map.find(key);
  61. if (found != map.end())
  62. return found->second;
  63. }
  64. return nullVariant;
  65. }
  66. void Context::set(const std::string& key, const Variant& variant)
  67. {
  68. assert(!m_variableStack.empty());
  69. VariableMap& map = m_variableStack.back();
  70. map[key] = variant;
  71. }
  72. void Context::remove(const std::string& key)
  73. {
  74. assert(!m_variableStack.empty());
  75. VariableMap& map = m_variableStack.back();
  76. map.erase(key);
  77. }