summaryrefslogtreecommitdiff
path: root/include/lldb/Core/StructuredData.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/lldb/Core/StructuredData.h')
-rw-r--r--include/lldb/Core/StructuredData.h406
1 files changed, 353 insertions, 53 deletions
diff --git a/include/lldb/Core/StructuredData.h b/include/lldb/Core/StructuredData.h
index a4cabf4fe352..8acfa310deac 100644
--- a/include/lldb/Core/StructuredData.h
+++ b/include/lldb/Core/StructuredData.h
@@ -13,10 +13,11 @@
// C Includes
// C++ Includes
+#include <functional>
#include <map>
+#include <string>
#include <utility>
#include <vector>
-#include <string>
#include "llvm/ADT/StringRef.h"
@@ -54,14 +55,22 @@ public:
class Boolean;
class String;
class Dictionary;
+ class Generic;
typedef std::shared_ptr<Object> ObjectSP;
typedef std::shared_ptr<Array> ArraySP;
+ typedef std::shared_ptr<Integer> IntegerSP;
+ typedef std::shared_ptr<Float> FloatSP;
+ typedef std::shared_ptr<Boolean> BooleanSP;
+ typedef std::shared_ptr<String> StringSP;
typedef std::shared_ptr<Dictionary> DictionarySP;
+ typedef std::shared_ptr<Generic> GenericSP;
- enum class Type {
+ enum class Type
+ {
eTypeInvalid = -1,
eTypeNull = 0,
+ eTypeGeneric,
eTypeArray,
eTypeInteger,
eTypeFloat,
@@ -84,6 +93,12 @@ public:
{
}
+ virtual bool
+ IsValid() const
+ {
+ return true;
+ }
+
virtual void
Clear ()
{
@@ -126,6 +141,15 @@ public:
return NULL;
}
+ uint64_t
+ GetIntegerValue (uint64_t fail_value = 0)
+ {
+ Integer *integer = GetAsInteger ();
+ if (integer)
+ return integer->GetValue();
+ return fail_value;
+ }
+
Float *
GetAsFloat ()
{
@@ -134,6 +158,15 @@ public:
return NULL;
}
+ double
+ GetFloatValue (double fail_value = 0.0)
+ {
+ Float *f = GetAsFloat ();
+ if (f)
+ return f->GetValue();
+ return fail_value;
+ }
+
Boolean *
GetAsBoolean ()
{
@@ -142,6 +175,15 @@ public:
return NULL;
}
+ bool
+ GetBooleanValue (bool fail_value = false)
+ {
+ Boolean *b = GetAsBoolean ();
+ if (b)
+ return b->GetValue();
+ return fail_value;
+ }
+
String *
GetAsString ()
{
@@ -150,9 +192,32 @@ public:
return NULL;
}
+ std::string
+ GetStringValue(const char *fail_value = NULL)
+ {
+ String *s = GetAsString ();
+ if (s)
+ return s->GetValue();
+
+ if (fail_value && fail_value[0])
+ return std::string(fail_value);
+
+ return std::string();
+ }
+
+ Generic *
+ GetAsGeneric()
+ {
+ if (m_type == Type::eTypeGeneric)
+ return (Generic *)this;
+ return NULL;
+ }
+
ObjectSP
GetObjectForDotSeparatedPath (llvm::StringRef path);
+ void DumpToStdout() const;
+
virtual void
Dump (Stream &s) const = 0;
@@ -173,8 +238,19 @@ public:
{
}
+ void
+ ForEach (std::function <bool(Object* object)> const &foreach_callback) const
+ {
+ for (const auto &object_sp : m_items)
+ {
+ if (foreach_callback(object_sp.get()) == false)
+ break;
+ }
+ }
+
+
size_t
- GetSize()
+ GetSize() const
{
return m_items.size();
}
@@ -188,13 +264,97 @@ public:
}
ObjectSP
- GetItemAtIndex (size_t idx)
+ GetItemAtIndex(size_t idx) const
{
+ assert(idx < GetSize());
if (idx < m_items.size())
return m_items[idx];
return ObjectSP();
}
+ template <class IntType>
+ bool
+ GetItemAtIndexAsInteger(size_t idx, IntType &result) const
+ {
+ ObjectSP value = GetItemAtIndex(idx);
+ if (auto int_value = value->GetAsInteger())
+ {
+ result = static_cast<IntType>(int_value->GetValue());
+ return true;
+ }
+ return false;
+ }
+
+ template <class IntType>
+ bool
+ GetItemAtIndexAsInteger(size_t idx, IntType &result, IntType default_val) const
+ {
+ bool success = GetItemAtIndexAsInteger(idx, result);
+ if (!success)
+ result = default_val;
+ return success;
+ }
+
+ bool
+ GetItemAtIndexAsString(size_t idx, std::string &result) const
+ {
+ ObjectSP value = GetItemAtIndex(idx);
+ if (auto string_value = value->GetAsString())
+ {
+ result = string_value->GetValue();
+ return true;
+ }
+ return false;
+ }
+
+ bool
+ GetItemAtIndexAsString(size_t idx, std::string &result, const std::string &default_val) const
+ {
+ bool success = GetItemAtIndexAsString(idx, result);
+ if (!success)
+ result = default_val;
+ return success;
+ }
+
+ bool
+ GetItemAtIndexAsString(size_t idx, ConstString &result) const
+ {
+ ObjectSP value = GetItemAtIndex(idx);
+ if (!value)
+ return false;
+ if (auto string_value = value->GetAsString())
+ {
+ result = ConstString(string_value->GetValue());
+ return true;
+ }
+ return false;
+ }
+
+ bool
+ GetItemAtIndexAsString(size_t idx, ConstString &result, const char *default_val) const
+ {
+ bool success = GetItemAtIndexAsString(idx, result);
+ if (!success)
+ result.SetCString(default_val);
+ return success;
+ }
+
+ bool
+ GetItemAtIndexAsDictionary(size_t idx, Dictionary *&result) const
+ {
+ ObjectSP value = GetItemAtIndex(idx);
+ result = value->GetAsDictionary();
+ return (result != nullptr);
+ }
+
+ bool
+ GetItemAtIndexAsArray(size_t idx, Array *&result) const
+ {
+ ObjectSP value = GetItemAtIndex(idx);
+ result = value->GetAsArray();
+ return (result != nullptr);
+ }
+
void
Push(ObjectSP item)
{
@@ -207,8 +367,7 @@ public:
m_items.push_back(item);
}
- virtual void
- Dump (Stream &s) const;
+ void Dump(Stream &s) const override;
protected:
typedef std::vector<ObjectSP> collection;
@@ -219,9 +378,9 @@ public:
class Integer : public Object
{
public:
- Integer () :
+ Integer (uint64_t i = 0) :
Object (Type::eTypeInteger),
- m_value ()
+ m_value (i)
{
}
@@ -241,8 +400,7 @@ public:
return m_value;
}
- virtual void
- Dump (Stream &s) const;
+ void Dump(Stream &s) const override;
protected:
uint64_t m_value;
@@ -251,9 +409,9 @@ public:
class Float : public Object
{
public:
- Float () :
+ Float (double d = 0.0) :
Object (Type::eTypeFloat),
- m_value ()
+ m_value (d)
{
}
@@ -273,8 +431,7 @@ public:
return m_value;
}
- virtual void
- Dump (Stream &s) const;
+ void Dump(Stream &s) const override;
protected:
double m_value;
@@ -283,9 +440,9 @@ public:
class Boolean : public Object
{
public:
- Boolean () :
+ Boolean (bool b = false) :
Object (Type::eTypeBoolean),
- m_value ()
+ m_value (b)
{
}
@@ -305,8 +462,7 @@ public:
return m_value;
}
- virtual void
- Dump (Stream &s) const;
+ void Dump(Stream &s) const override;
protected:
bool m_value;
@@ -317,26 +473,39 @@ public:
class String : public Object
{
public:
- String () :
+ String (const char *cstr = NULL) :
Object (Type::eTypeString),
m_value ()
{
+ if (cstr)
+ m_value = cstr;
+ }
+
+ String (const std::string &s) :
+ Object (Type::eTypeString),
+ m_value (s)
+ {
+ }
+
+ String (const std::string &&s) :
+ Object (Type::eTypeString),
+ m_value (s)
+ {
}
void
- SetValue (std::string string)
+ SetValue (const std::string &string)
{
m_value = string;
}
- std::string
+ const std::string &
GetValue ()
{
return m_value;
}
- virtual void
- Dump (Stream &s) const;
+ void Dump(Stream &s) const override;
protected:
std::string m_value;
@@ -345,6 +514,7 @@ public:
class Dictionary : public Object
{
public:
+
Dictionary () :
Object (Type::eTypeDictionary),
m_dict ()
@@ -354,14 +524,25 @@ public:
virtual ~Dictionary()
{
}
+
size_t
- GetSize()
+ GetSize() const
{
return m_dict.size();
}
+ void
+ ForEach (std::function <bool(ConstString key, Object* object)> const &callback) const
+ {
+ for (const auto &pair : m_dict)
+ {
+ if (callback (pair.first, pair.second.get()) == false)
+ break;
+ }
+ }
+
ObjectSP
- GetKeys()
+ GetKeys() const
{
ObjectSP object_sp(new Array ());
Array *array = object_sp->GetAsArray();
@@ -376,10 +557,10 @@ public:
}
ObjectSP
- GetValueForKey (const char *key)
+ GetValueForKey(llvm::StringRef key) const
{
ObjectSP value_sp;
- if (key)
+ if (!key.empty())
{
ConstString key_cs(key);
for (collection::const_iterator iter = m_dict.begin(); iter != m_dict.end(); ++iter)
@@ -394,62 +575,144 @@ public:
return value_sp;
}
+ template <class IntType>
bool
- HasKey (const char *key)
+ GetValueForKeyAsInteger(llvm::StringRef key, IntType &result) const
{
- ConstString key_cs (key);
- collection::const_iterator search = m_dict.find(key_cs);
- if (search != m_dict.end())
+ ObjectSP value = GetValueForKey(key);
+ if (!value)
+ return false;
+ if (auto int_value = value->GetAsInteger())
{
+ result = static_cast<IntType>(int_value->GetValue());
return true;
}
- else
+ return false;
+ }
+
+ template <class IntType>
+ bool
+ GetValueForKeyAsInteger(llvm::StringRef key, IntType &result, IntType default_val) const
+ {
+ bool success = GetValueForKeyAsInteger<IntType>(key, result);
+ if (!success)
+ result = default_val;
+ return success;
+ }
+
+ bool
+ GetValueForKeyAsString(llvm::StringRef key, std::string &result) const
+ {
+ ObjectSP value = GetValueForKey(key);
+ if (!value)
+ return false;
+ if (auto string_value = value->GetAsString())
{
+ result = string_value->GetValue();
+ return true;
+ }
+ return false;
+ }
+
+ bool
+ GetValueForKeyAsString(llvm::StringRef key, std::string &result, const char *default_val) const
+ {
+ bool success = GetValueForKeyAsString(key, result);
+ if (!success)
+ {
+ if (default_val)
+ result = default_val;
+ else
+ result.clear();
+ }
+ return success;
+ }
+
+ bool
+ GetValueForKeyAsString(llvm::StringRef key, ConstString &result) const
+ {
+ ObjectSP value = GetValueForKey(key);
+ if (!value)
return false;
+ if (auto string_value = value->GetAsString())
+ {
+ result = ConstString(string_value->GetValue());
+ return true;
}
+ return false;
+ }
+
+ bool
+ GetValueForKeyAsString(llvm::StringRef key, ConstString &result, const char *default_val) const
+ {
+ bool success = GetValueForKeyAsString(key, result);
+ if (!success)
+ result.SetCString(default_val);
+ return success;
+ }
+
+ bool
+ GetValueForKeyAsDictionary(llvm::StringRef key, Dictionary *&result) const
+ {
+ result = nullptr;
+ ObjectSP value = GetValueForKey(key);
+ if (!value)
+ return false;
+ result = value->GetAsDictionary();
+ return true;
+ }
+
+ bool
+ GetValueForKeyAsArray(llvm::StringRef key, Array *&result) const
+ {
+ result = nullptr;
+ ObjectSP value = GetValueForKey(key);
+ if (!value)
+ return false;
+ result = value->GetAsArray();
+ return true;
+ }
+
+ bool
+ HasKey(llvm::StringRef key) const
+ {
+ ConstString key_cs(key);
+ collection::const_iterator search = m_dict.find(key_cs);
+ return search != m_dict.end();
}
void
- AddItem (const char *key, ObjectSP value)
+ AddItem (llvm::StringRef key, ObjectSP value)
{
ConstString key_cs(key);
m_dict[key_cs] = value;
}
void
- AddIntegerItem (const char *key, uint64_t value)
+ AddIntegerItem (llvm::StringRef key, uint64_t value)
{
- ObjectSP val_obj (new Integer());
- val_obj->GetAsInteger()->SetValue (value);
- AddItem (key, val_obj);
+ AddItem (key, ObjectSP (new Integer(value)));
}
void
- AddFloatItem (const char *key, double value)
+ AddFloatItem (llvm::StringRef key, double value)
{
- ObjectSP val_obj (new Float());
- val_obj->GetAsFloat()->SetValue (value);
- AddItem (key, val_obj);
+ AddItem (key, ObjectSP (new Float(value)));
}
void
- AddStringItem (const char *key, std::string value)
+ AddStringItem (llvm::StringRef key, std::string value)
{
- ObjectSP val_obj (new String());
- val_obj->GetAsString()->SetValue (value);
- AddItem (key, val_obj);
+ AddItem (key, ObjectSP (new String(std::move(value))));
}
void
- AddBooleanItem (const char *key, bool value)
+ AddBooleanItem (llvm::StringRef key, bool value)
{
- ObjectSP val_obj (new Boolean());
- val_obj->GetAsBoolean()->SetValue (value);
- AddItem (key, val_obj);
+ AddItem (key, ObjectSP (new Boolean(value)));
}
- virtual void
- Dump (Stream &s) const;
+ void Dump(Stream &s) const override;
protected:
typedef std::map<ConstString, ObjectSP> collection;
@@ -468,12 +731,49 @@ public:
{
}
- virtual void
- Dump (Stream &s) const;
+ bool
+ IsValid() const override
+ {
+ return false;
+ }
+
+ void Dump(Stream &s) const override;
protected:
};
+ class Generic : public Object
+ {
+ public:
+ explicit Generic(void *object = nullptr) :
+ Object (Type::eTypeGeneric),
+ m_object (object)
+ {
+ }
+
+ void
+ SetValue(void *value)
+ {
+ m_object = value;
+ }
+
+ void *
+ GetValue() const
+ {
+ return m_object;
+ }
+
+ bool
+ IsValid() const override
+ {
+ return m_object != nullptr;
+ }
+
+ void Dump(Stream &s) const override;
+
+ private:
+ void *m_object;
+ };
static ObjectSP
ParseJSON (std::string json_text);