1 1.1 jkunz /* 2 1.1 jkunz * File: OptionDictionary.h 3 1.1 jkunz * 4 1.1 jkunz * Copyright (c) Freescale Semiconductor, Inc. All rights reserved. 5 1.1 jkunz * See included license file for license details. 6 1.1 jkunz */ 7 1.1 jkunz 8 1.1 jkunz #include "OptionDictionary.h" 9 1.1 jkunz 10 1.1 jkunz using namespace elftosb; 11 1.1 jkunz 12 1.1 jkunz //! Deletes all of the option values that have been assigned locally. 13 1.1 jkunz //! 14 1.1 jkunz OptionDictionary::~OptionDictionary() 15 1.1 jkunz { 16 1.1 jkunz option_map_t::iterator it = m_options.begin(); 17 1.1 jkunz for (; it != m_options.end(); ++it) 18 1.1 jkunz { 19 1.1 jkunz if (it->second.m_value) 20 1.1 jkunz { 21 1.1 jkunz delete it->second.m_value; 22 1.1 jkunz } 23 1.1 jkunz } 24 1.1 jkunz } 25 1.1 jkunz 26 1.1 jkunz //! If a parent context has been set and the option does not exist in 27 1.1 jkunz //! this instance, then the parent is asked if it contains the option. 28 1.1 jkunz //! 29 1.1 jkunz //! \param name The name of the option to query. 30 1.1 jkunz //! \retval true The option is present in this instance or one of the parent. 31 1.1 jkunz //! \retval false No option with that name is in the dictionary, or any parent 32 1.1 jkunz bool OptionDictionary::hasOption(const std::string & name) const 33 1.1 jkunz { 34 1.1 jkunz bool hasIt = (m_options.find(name) != m_options.end()); 35 1.1 jkunz if (!hasIt && m_parent) 36 1.1 jkunz { 37 1.1 jkunz return m_parent->hasOption(name); 38 1.1 jkunz } 39 1.1 jkunz return hasIt; 40 1.1 jkunz } 41 1.1 jkunz 42 1.1 jkunz //! If this object does not contain an option with the name of \a name, 43 1.1 jkunz //! then the parent is asked for the value (if a parent has been set). 44 1.1 jkunz //! 45 1.1 jkunz //! \param name The name of the option. 46 1.1 jkunz //! \return The value for the option named \a name. 47 1.1 jkunz //! \retval NULL No option is in the table with that name. An option may also 48 1.1 jkunz //! explicitly be set to a NULL value. The only way to tell the difference 49 1.1 jkunz //! is to use the hasOption() method. 50 1.1 jkunz const Value * OptionDictionary::getOption(const std::string & name) const 51 1.1 jkunz { 52 1.1 jkunz option_map_t::const_iterator it = m_options.find(name); 53 1.1 jkunz if (it == m_options.end()) 54 1.1 jkunz { 55 1.1 jkunz if (m_parent) 56 1.1 jkunz { 57 1.1 jkunz return m_parent->getOption(name); 58 1.1 jkunz } 59 1.1 jkunz else 60 1.1 jkunz { 61 1.1 jkunz return NULL; 62 1.1 jkunz } 63 1.1 jkunz } 64 1.1 jkunz 65 1.1 jkunz return it->second.m_value; 66 1.1 jkunz } 67 1.1 jkunz 68 1.1 jkunz //! If the option was not already present in the table, it is added. 69 1.1 jkunz //! Otherwise the old value is replaced. The option is always set locally; 70 1.1 jkunz //! parent objects are never modified. 71 1.1 jkunz //! 72 1.1 jkunz //! If the option has been locked with a call to lockOption() before trying 73 1.1 jkunz //! to set its value, the setOption() is effectively ignored. To tell if 74 1.1 jkunz //! an option is locked, use the isOptionLocked() method. 75 1.1 jkunz //! 76 1.1 jkunz //! \warning If the option already had a value, that previous value is deleted. 77 1.1 jkunz //! This means that it cannot currently be in use by another piece of code. 78 1.1 jkunz //! See the note in getOption(). 79 1.1 jkunz //! 80 1.1 jkunz //! \param name The option's name. 81 1.1 jkunz //! \param value New value for the option. 82 1.1 jkunz void OptionDictionary::setOption(const std::string & name, Value * value) 83 1.1 jkunz { 84 1.1 jkunz option_map_t::iterator it = m_options.find(name); 85 1.1 jkunz OptionValue newValue; 86 1.1 jkunz 87 1.1 jkunz // delete the option value instance before replacing it 88 1.1 jkunz if (it != m_options.end()) 89 1.1 jkunz { 90 1.1 jkunz // Cannot modify value if locked. 91 1.1 jkunz if (it->second.m_isLocked) 92 1.1 jkunz { 93 1.1 jkunz return; 94 1.1 jkunz } 95 1.1 jkunz 96 1.1 jkunz if (it->second.m_value) 97 1.1 jkunz { 98 1.1 jkunz delete it->second.m_value; 99 1.1 jkunz } 100 1.1 jkunz 101 1.1 jkunz // save previous locked value 102 1.1 jkunz newValue.m_isLocked = it->second.m_isLocked; 103 1.1 jkunz } 104 1.1 jkunz 105 1.1 jkunz // set new option value 106 1.1 jkunz newValue.m_value = value; 107 1.1 jkunz m_options[name] = newValue; 108 1.1 jkunz } 109 1.1 jkunz 110 1.1 jkunz //! \param name The name of the option to remove. 111 1.1 jkunz //! 112 1.1 jkunz void OptionDictionary::deleteOption(const std::string & name) 113 1.1 jkunz { 114 1.1 jkunz if (m_options.find(name) != m_options.end()) 115 1.1 jkunz { 116 1.1 jkunz if (m_options[name].m_value) 117 1.1 jkunz { 118 1.1 jkunz delete m_options[name].m_value; 119 1.1 jkunz } 120 1.1 jkunz m_options.erase(name); 121 1.1 jkunz } 122 1.1 jkunz } 123 1.1 jkunz 124 1.1 jkunz //! \param name Name of the option to query. 125 1.1 jkunz //! 126 1.1 jkunz //! \return True if the option is locked, false if unlocked or not present. 127 1.1 jkunz //! 128 1.1 jkunz bool OptionDictionary::isOptionLocked(const std::string & name) const 129 1.1 jkunz { 130 1.1 jkunz option_map_t::const_iterator it = m_options.find(name); 131 1.1 jkunz if (it != m_options.end()) 132 1.1 jkunz { 133 1.1 jkunz return it->second.m_isLocked; 134 1.1 jkunz } 135 1.1 jkunz 136 1.1 jkunz return false; 137 1.1 jkunz } 138 1.1 jkunz 139 1.1 jkunz //! \param name Name of the option to lock. 140 1.1 jkunz //! 141 1.1 jkunz void OptionDictionary::lockOption(const std::string & name) 142 1.1 jkunz { 143 1.1 jkunz if (!hasOption(name)) 144 1.1 jkunz { 145 1.1 jkunz m_options[name].m_value = 0; 146 1.1 jkunz } 147 1.1 jkunz 148 1.1 jkunz m_options[name].m_isLocked = true; 149 1.1 jkunz } 150 1.1 jkunz 151 1.1 jkunz //! \param name Name of the option to unlock. 152 1.1 jkunz //! 153 1.1 jkunz void OptionDictionary::unlockOption(const std::string & name) 154 1.1 jkunz { 155 1.1 jkunz if (!hasOption(name)) 156 1.1 jkunz { 157 1.1 jkunz m_options[name].m_value = 0; 158 1.1 jkunz } 159 1.1 jkunz 160 1.1 jkunz m_options[name].m_isLocked = false; 161 1.1 jkunz } 162 1.1 jkunz 163 1.1 jkunz 164 1.1 jkunz //! Simply calls getOption(). 165 1.1 jkunz //! 166 1.1 jkunz const Value * OptionDictionary::operator [] (const std::string & name) const 167 1.1 jkunz { 168 1.1 jkunz return getOption(name); 169 1.1 jkunz } 170 1.1 jkunz 171