1 //===-- OptionValueUInt64.cpp ------------------------------------*- C++
2 //-*-===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/Interpreter/OptionValueUInt64.h"
11 
12 #include "lldb/Host/StringConvert.h"
13 #include "lldb/Utility/Stream.h"
14 
15 using namespace lldb;
16 using namespace lldb_private;
17 
18 lldb::OptionValueSP OptionValueUInt64::Create(llvm::StringRef value_str,
19                                               Status &error) {
20   lldb::OptionValueSP value_sp(new OptionValueUInt64());
21   error = value_sp->SetValueFromString(value_str);
22   if (error.Fail())
23     value_sp.reset();
24   return value_sp;
25 }
26 
27 void OptionValueUInt64::DumpValue(const ExecutionContext *exe_ctx, Stream &strm,
28                                   uint32_t dump_mask) {
29   if (dump_mask & eDumpOptionType)
30     strm.Printf("(%s)", GetTypeAsCString());
31   if (dump_mask & eDumpOptionValue) {
32     if (dump_mask & eDumpOptionType)
33       strm.PutCString(" = ");
34     strm.Printf("%" PRIu64, m_current_value);
35   }
36 }
37 
38 Status OptionValueUInt64::SetValueFromString(llvm::StringRef value_ref,
39                                              VarSetOperationType op) {
40   Status error;
41   switch (op) {
42   case eVarSetOperationClear:
43     Clear();
44     NotifyValueChanged();
45     break;
46 
47   case eVarSetOperationReplace:
48   case eVarSetOperationAssign: {
49     bool success = false;
50     std::string value_str = value_ref.trim().str();
51     uint64_t value = StringConvert::ToUInt64(value_str.c_str(), 0, 0, &success);
52     if (success) {
53       m_value_was_set = true;
54       m_current_value = value;
55       NotifyValueChanged();
56     } else {
57       error.SetErrorStringWithFormat("invalid uint64_t string value: '%s'",
58                                      value_str.c_str());
59     }
60   } break;
61 
62   case eVarSetOperationInsertBefore:
63   case eVarSetOperationInsertAfter:
64   case eVarSetOperationRemove:
65   case eVarSetOperationAppend:
66   case eVarSetOperationInvalid:
67     error = OptionValue::SetValueFromString(value_ref, op);
68     break;
69   }
70   return error;
71 }
72 
73 lldb::OptionValueSP OptionValueUInt64::DeepCopy() const {
74   return OptionValueSP(new OptionValueUInt64(*this));
75 }
76