2 * File: OptionDictionary.h
4 * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
5 * See included license file for license details.
8 #include "OptionDictionary.h"
10 using namespace elftosb;
12 //! Deletes all of the option values that have been assigned locally.
14 OptionDictionary::~OptionDictionary()
16 option_map_t::iterator it = m_options.begin();
17 for (; it != m_options.end(); ++it)
19 if (it->second.m_value)
21 delete it->second.m_value;
26 //! If a parent context has been set and the option does not exist in
27 //! this instance, then the parent is asked if it contains the option.
29 //! \param name The name of the option to query.
30 //! \retval true The option is present in this instance or one of the parent.
31 //! \retval false No option with that name is in the dictionary, or any parent
32 bool OptionDictionary::hasOption(const std::string & name) const
34 bool hasIt = (m_options.find(name) != m_options.end());
35 if (!hasIt && m_parent)
37 return m_parent->hasOption(name);
42 //! If this object does not contain an option with the name of \a name,
43 //! then the parent is asked for the value (if a parent has been set).
45 //! \param name The name of the option.
46 //! \return The value for the option named \a name.
47 //! \retval NULL No option is in the table with that name. An option may also
48 //! explicitly be set to a NULL value. The only way to tell the difference
49 //! is to use the hasOption() method.
50 const Value * OptionDictionary::getOption(const std::string & name) const
52 option_map_t::const_iterator it = m_options.find(name);
53 if (it == m_options.end())
57 return m_parent->getOption(name);
65 return it->second.m_value;
68 //! If the option was not already present in the table, it is added.
69 //! Otherwise the old value is replaced. The option is always set locally;
70 //! parent objects are never modified.
72 //! If the option has been locked with a call to lockOption() before trying
73 //! to set its value, the setOption() is effectively ignored. To tell if
74 //! an option is locked, use the isOptionLocked() method.
76 //! \warning If the option already had a value, that previous value is deleted.
77 //! This means that it cannot currently be in use by another piece of code.
78 //! See the note in getOption().
80 //! \param name The option's name.
81 //! \param value New value for the option.
82 void OptionDictionary::setOption(const std::string & name, Value * value)
84 option_map_t::iterator it = m_options.find(name);
87 // delete the option value instance before replacing it
88 if (it != m_options.end())
90 // Cannot modify value if locked.
91 if (it->second.m_isLocked)
96 if (it->second.m_value)
98 delete it->second.m_value;
101 // save previous locked value
102 newValue.m_isLocked = it->second.m_isLocked;
105 // set new option value
106 newValue.m_value = value;
107 m_options[name] = newValue;
110 //! \param name The name of the option to remove.
112 void OptionDictionary::deleteOption(const std::string & name)
114 if (m_options.find(name) != m_options.end())
116 if (m_options[name].m_value)
118 delete m_options[name].m_value;
120 m_options.erase(name);
124 //! \param name Name of the option to query.
126 //! \return True if the option is locked, false if unlocked or not present.
128 bool OptionDictionary::isOptionLocked(const std::string & name) const
130 option_map_t::const_iterator it = m_options.find(name);
131 if (it != m_options.end())
133 return it->second.m_isLocked;
139 //! \param name Name of the option to lock.
141 void OptionDictionary::lockOption(const std::string & name)
143 if (!hasOption(name))
145 m_options[name].m_value = 0;
148 m_options[name].m_isLocked = true;
151 //! \param name Name of the option to unlock.
153 void OptionDictionary::unlockOption(const std::string & name)
155 if (!hasOption(name))
157 m_options[name].m_value = 0;
160 m_options[name].m_isLocked = false;
164 //! Simply calls getOption().
166 const Value * OptionDictionary::operator [] (const std::string & name) const
168 return getOption(name);