PageRenderTime 29ms CodeModel.GetById 22ms app.highlight 5ms RepoModel.GetById 0ms app.codeStats 1ms

/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
23#include "Context.h"
24
25Context::Context(const VariableMap& variables)
26{
27    m_variableStack.push_back(variables);
28}
29
30Context::~Context()
31{
32    m_variableStack.clear();
33}
34
35void Context::push()
36{
37    m_variableStack.push_back(VariableMap());
38}
39
40void Context::pop()
41{
42    assert(m_variableStack.size() > 1);
43
44    m_variableStack.pop_back();
45}
46
47bool Context::contains(const std::string& key) const
48{
49    assert(!m_variableStack.empty());
50
51    std::vector<VariableMap>::const_reverse_iterator it = m_variableStack.rbegin();
52    std::vector<VariableMap>::const_reverse_iterator end = m_variableStack.rend();
53    for (it; it != end; ++it) {
54        const VariableMap& map = *it;
55        if (map.find(key) != map.end())
56            return true;
57    }
58
59    return false;
60}
61
62const Variant& Context::get(const std::string& key) const
63{
64    static Variant nullVariant;
65    assert(!m_variableStack.empty());
66
67    std::vector<VariableMap>::const_reverse_iterator it = m_variableStack.rbegin();
68    std::vector<VariableMap>::const_reverse_iterator end = m_variableStack.rend();
69    for (it; it != end; ++it) {
70        const VariableMap& map = *it;
71        VariableMap::const_iterator found = map.find(key);
72        if (found != map.end())
73            return found->second;
74    }
75
76    return nullVariant;
77}
78
79void Context::set(const std::string& key, const Variant& variant)
80{
81    assert(!m_variableStack.empty());
82
83    VariableMap& map = m_variableStack.back();
84    map[key] = variant;
85}
86
87void Context::remove(const std::string& key)
88{
89    assert(!m_variableStack.empty());
90
91    VariableMap& map = m_variableStack.back();
92    map.erase(key);
93}