Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 1 | //===-- NamedOptionValue.cpp ------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "lldb/Interpreter/NamedOptionValue.h" |
| 11 | |
| 12 | // C Includes |
| 13 | // C++ Includes |
| 14 | // Other libraries and framework includes |
| 15 | // Project includes |
| 16 | #include "lldb/Core/Stream.h" |
| 17 | #include "lldb/Interpreter/Args.h" |
| 18 | |
| 19 | using namespace lldb; |
| 20 | using namespace lldb_private; |
| 21 | |
Greg Clayton | e972845 | 2011-04-21 17:46:10 +0000 | [diff] [blame] | 22 | |
| 23 | //------------------------------------------------------------------------- |
| 24 | // OptionValue |
| 25 | //------------------------------------------------------------------------- |
| 26 | OptionValueBoolean * |
| 27 | OptionValue::GetAsBooleanValue () |
| 28 | { |
| 29 | if (GetType () == OptionValue::eTypeBoolean) |
| 30 | return static_cast<OptionValueBoolean *>(this); |
| 31 | return NULL; |
| 32 | } |
| 33 | |
| 34 | OptionValueSInt64 * |
| 35 | OptionValue::GetAsSInt64Value () |
| 36 | { |
| 37 | if (GetType () == OptionValue::eTypeSInt64) |
| 38 | return static_cast<OptionValueSInt64 *>(this); |
| 39 | return NULL; |
| 40 | } |
| 41 | |
| 42 | OptionValueUInt64 * |
| 43 | OptionValue::GetAsUInt64Value () |
| 44 | { |
| 45 | if (GetType () == OptionValue::eTypeUInt64) |
| 46 | return static_cast<OptionValueUInt64 *>(this); |
| 47 | return NULL; |
| 48 | } |
| 49 | |
| 50 | OptionValueString * |
| 51 | OptionValue::GetAsStringValue () |
| 52 | { |
| 53 | if (GetType () == OptionValue::eTypeString) |
| 54 | return static_cast<OptionValueString *>(this); |
| 55 | return NULL; |
| 56 | } |
| 57 | |
| 58 | OptionValueFileSpec * |
| 59 | OptionValue::GetAsFileSpecValue () |
| 60 | { |
| 61 | if (GetType () == OptionValue::eTypeFileSpec) |
| 62 | return static_cast<OptionValueFileSpec *>(this); |
| 63 | return NULL; |
| 64 | } |
| 65 | |
| 66 | OptionValueArray * |
| 67 | OptionValue::GetAsArrayValue () |
| 68 | { |
| 69 | if (GetType () == OptionValue::eTypeArray) |
| 70 | return static_cast<OptionValueArray *>(this); |
| 71 | return NULL; |
| 72 | } |
| 73 | |
| 74 | OptionValueDictionary * |
| 75 | OptionValue::GetAsDictionaryValue () |
| 76 | { |
| 77 | if (GetType () == OptionValue::eTypeDictionary) |
| 78 | return static_cast<OptionValueDictionary *>(this); |
| 79 | return NULL; |
| 80 | } |
| 81 | |
| 82 | |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 83 | //------------------------------------------------------------------------- |
Greg Clayton | c08770b | 2011-04-21 19:21:29 +0000 | [diff] [blame^] | 84 | // OptionValueCollection |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 85 | //------------------------------------------------------------------------- |
| 86 | |
| 87 | void |
Greg Clayton | c08770b | 2011-04-21 19:21:29 +0000 | [diff] [blame^] | 88 | OptionValueCollection::GetQualifiedName (Stream &strm) |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 89 | { |
| 90 | if (m_parent) |
| 91 | { |
| 92 | m_parent->GetQualifiedName (strm); |
| 93 | strm.PutChar('.'); |
| 94 | } |
| 95 | strm << m_name; |
| 96 | } |
| 97 | |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 98 | |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 99 | //------------------------------------------------------------------------- |
| 100 | // OptionValueBoolean |
| 101 | //------------------------------------------------------------------------- |
| 102 | void |
| 103 | OptionValueBoolean::DumpValue (Stream &strm) |
| 104 | { |
| 105 | strm.PutCString (m_current_value ? "true" : "false"); |
| 106 | } |
| 107 | |
| 108 | bool |
| 109 | OptionValueBoolean::SetValueFromCString (const char *value_cstr) |
| 110 | { |
| 111 | bool success = false; |
| 112 | bool value = Args::StringToBoolean(value_cstr, false, &success); |
| 113 | if (success) |
| 114 | { |
| 115 | m_current_value = value; |
| 116 | return true; |
| 117 | } |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | //------------------------------------------------------------------------- |
| 122 | // OptionValueSInt64 |
| 123 | //------------------------------------------------------------------------- |
| 124 | void |
| 125 | OptionValueSInt64::DumpValue (Stream &strm) |
| 126 | { |
| 127 | strm.Printf ("%lli", m_current_value); |
| 128 | } |
| 129 | |
| 130 | bool |
| 131 | OptionValueSInt64::SetValueFromCString (const char *value_cstr) |
| 132 | { |
| 133 | bool success = false; |
| 134 | int64_t value = Args::StringToSInt64 (value_cstr, 0, 0, &success); |
| 135 | if (success) |
| 136 | { |
| 137 | m_current_value = value; |
| 138 | return true; |
| 139 | } |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | //------------------------------------------------------------------------- |
| 144 | // OptionValueUInt64 |
| 145 | //------------------------------------------------------------------------- |
| 146 | void |
| 147 | OptionValueUInt64::DumpValue (Stream &strm) |
| 148 | { |
| 149 | strm.Printf ("0x%llx", m_current_value); |
| 150 | } |
| 151 | |
| 152 | bool |
| 153 | OptionValueUInt64::SetValueFromCString (const char *value_cstr) |
| 154 | { |
| 155 | bool success = false; |
| 156 | uint64_t value = Args::StringToUInt64 (value_cstr, 0, 0, &success); |
| 157 | if (success) |
| 158 | { |
| 159 | m_current_value = value; |
| 160 | return true; |
| 161 | } |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | //------------------------------------------------------------------------- |
Greg Clayton | d12aeab | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 166 | // OptionValueDictionary |
| 167 | //------------------------------------------------------------------------- |
| 168 | void |
| 169 | OptionValueString::DumpValue (Stream &strm) |
| 170 | { |
| 171 | strm.Printf ("\"%s\"", m_current_value.c_str()); |
| 172 | } |
| 173 | |
| 174 | bool |
| 175 | OptionValueString::SetValueFromCString (const char *value_cstr) |
| 176 | { |
| 177 | SetCurrentValue (value_cstr); |
| 178 | return true; |
| 179 | } |
| 180 | |
| 181 | |
| 182 | |
| 183 | //------------------------------------------------------------------------- |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 184 | // OptionValueFileSpec |
| 185 | //------------------------------------------------------------------------- |
| 186 | void |
| 187 | OptionValueFileSpec::DumpValue (Stream &strm) |
| 188 | { |
| 189 | if (m_current_value) |
| 190 | { |
| 191 | if (m_current_value.GetDirectory()) |
| 192 | { |
| 193 | strm << '"' << m_current_value.GetDirectory(); |
| 194 | if (m_current_value.GetFilename()) |
| 195 | strm << '/' << m_current_value.GetFilename(); |
| 196 | strm << '"'; |
| 197 | } |
| 198 | else |
| 199 | { |
| 200 | strm << '"' << m_current_value.GetFilename() << '"'; |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | bool |
| 206 | OptionValueFileSpec::SetValueFromCString (const char *value_cstr) |
| 207 | { |
| 208 | if (value_cstr && value_cstr[0]) |
| 209 | m_current_value.SetFile(value_cstr, false); |
| 210 | else |
| 211 | m_current_value.Clear(); |
| 212 | return true; |
| 213 | } |
| 214 | |
| 215 | |
| 216 | //------------------------------------------------------------------------- |
| 217 | // OptionValueArray |
| 218 | //------------------------------------------------------------------------- |
| 219 | void |
| 220 | OptionValueArray::DumpValue (Stream &strm) |
| 221 | { |
| 222 | const uint32_t size = m_values.size(); |
| 223 | for (uint32_t i = 0; i<size; ++i) |
| 224 | { |
| 225 | strm.Printf("[%u] ", i); |
| 226 | m_values[i]->DumpValue (strm); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | bool |
| 231 | OptionValueArray::SetValueFromCString (const char *value_cstr) |
| 232 | { |
| 233 | // We must be able to set this using the array specific functions |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | //------------------------------------------------------------------------- |
| 238 | // OptionValueDictionary |
| 239 | //------------------------------------------------------------------------- |
| 240 | void |
| 241 | OptionValueDictionary::DumpValue (Stream &strm) |
| 242 | { |
| 243 | collection::iterator pos, end = m_values.end(); |
| 244 | |
| 245 | for (pos = m_values.begin(); pos != end; ++pos) |
| 246 | { |
| 247 | strm.Printf("%s=", pos->first.GetCString()); |
| 248 | pos->second->DumpValue (strm); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | bool |
| 253 | OptionValueDictionary::SetValueFromCString (const char *value_cstr) |
| 254 | { |
| 255 | // We must be able to set this using the array specific functions |
| 256 | return false; |
| 257 | } |
| 258 | |
Greg Clayton | e972845 | 2011-04-21 17:46:10 +0000 | [diff] [blame] | 259 | lldb::OptionValueSP |
| 260 | OptionValueDictionary::GetValueForKey (const ConstString &key) const |
| 261 | { |
| 262 | lldb::OptionValueSP value_sp; |
| 263 | collection::const_iterator pos = m_values.find (key); |
| 264 | if (pos != m_values.end()) |
| 265 | value_sp = pos->second; |
| 266 | return value_sp; |
| 267 | } |
| 268 | |
| 269 | const char * |
| 270 | OptionValueDictionary::GetStringValueForKey (const ConstString &key) |
| 271 | { |
| 272 | collection::const_iterator pos = m_values.find (key); |
| 273 | if (pos != m_values.end()) |
| 274 | { |
| 275 | if (pos->second->GetType() == OptionValue::eTypeString) |
| 276 | return static_cast<OptionValueString *>(pos->second.get())->GetCurrentValue(); |
| 277 | } |
| 278 | return NULL; |
| 279 | } |
| 280 | |
| 281 | |
| 282 | bool |
| 283 | OptionValueDictionary::SetStringValueForKey (const ConstString &key, |
| 284 | const char *value, |
| 285 | bool can_replace) |
| 286 | { |
| 287 | collection::const_iterator pos = m_values.find (key); |
| 288 | if (pos != m_values.end()) |
| 289 | { |
| 290 | if (!can_replace) |
| 291 | return false; |
| 292 | if (pos->second->GetType() == OptionValue::eTypeString) |
| 293 | { |
| 294 | pos->second->SetValueFromCString(value); |
| 295 | return true; |
| 296 | } |
| 297 | } |
| 298 | m_values[key] = OptionValueSP (new OptionValueString (value)); |
| 299 | return true; |
| 300 | |
| 301 | } |
| 302 | |
| 303 | bool |
| 304 | OptionValueDictionary::SetValueForKey (const ConstString &key, |
| 305 | const lldb::OptionValueSP &value_sp, |
| 306 | bool can_replace) |
| 307 | { |
| 308 | // Make sure the value_sp object is allowed to contain |
| 309 | // values of the type passed in... |
| 310 | if (value_sp && (m_type_mask & value_sp->GetTypeAsMask())) |
| 311 | { |
| 312 | if (!can_replace) |
| 313 | { |
| 314 | collection::const_iterator pos = m_values.find (key); |
| 315 | if (pos != m_values.end()) |
| 316 | return false; |
| 317 | } |
| 318 | m_values[key] = value_sp; |
| 319 | return true; |
| 320 | } |
| 321 | return false; |
| 322 | } |
| 323 | |
| 324 | bool |
| 325 | OptionValueDictionary::DeleteValueForKey (const ConstString &key) |
| 326 | { |
| 327 | collection::iterator pos = m_values.find (key); |
| 328 | if (pos != m_values.end()) |
| 329 | { |
| 330 | m_values.erase(pos); |
| 331 | return true; |
| 332 | } |
| 333 | return false; |
| 334 | } |
| 335 | |
Greg Clayton | 7406c39 | 2011-04-20 01:33:38 +0000 | [diff] [blame] | 336 | |