4 * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
5 * See included license file for license details.
8 #include "EvalContext.h"
10 #include "format_string.h"
12 using namespace elftosb;
14 EvalContext::EvalContext()
19 EvalContext::~EvalContext()
23 bool EvalContext::isVariableDefined(const std::string & name)
25 variable_map_t::const_iterator it = m_variables.find(name);
26 return it != m_variables.end();
29 uint32_t EvalContext::getVariableValue(const std::string & name)
31 variable_map_t::const_iterator it = m_variables.find(name);
32 if (it == m_variables.end())
34 throw std::runtime_error(format_string("undefined variable '%s'", name.c_str()));
37 return it->second.m_value;
40 int_size_t EvalContext::getVariableSize(const std::string & name)
42 variable_map_t::const_iterator it = m_variables.find(name);
43 if (it == m_variables.end())
45 throw std::runtime_error(format_string("undefined variable '%s'", name.c_str()));
48 return it->second.m_size;
51 void EvalContext::setVariable(const std::string & name, uint32_t value, int_size_t size)
53 // check if var is locked
54 variable_map_t::const_iterator it = m_variables.find(name);
55 if (it != m_variables.end() && it->second.m_isLocked)
64 info.m_isLocked = false;
65 m_variables[name] = info;
68 bool EvalContext::isVariableLocked(const std::string & name)
70 variable_map_t::const_iterator it = m_variables.find(name);
71 if (it == m_variables.end())
73 throw std::runtime_error(format_string("undefined variable '%s'", name.c_str()));
76 return it->second.m_isLocked;
79 void EvalContext::lockVariable(const std::string & name)
81 variable_map_t::iterator it = m_variables.find(name);
82 if (it == m_variables.end())
84 throw std::runtime_error(format_string("undefined variable '%s'", name.c_str()));
87 it->second.m_isLocked = true;
90 void EvalContext::unlockVariable(const std::string & name)
92 variable_map_t::iterator it = m_variables.find(name);
93 if (it == m_variables.end())
95 throw std::runtime_error("undefined variable");
98 it->second.m_isLocked = false;
101 void EvalContext::dump()
103 variable_map_t::iterator it = m_variables.begin();
104 for (; it != m_variables.end(); ++it)
106 variable_info_t & info = it->second;
107 const char * lockedString = info.m_isLocked ? "locked" : "unlocked";
108 printf("%s = %u:%d (%s)\n", it->first.c_str(), info.m_value, (int)info.m_size, lockedString);