diff options
author | Tim Renouf <tpr@botech.co.uk> | 2020-05-18 14:13:57 +0100 |
---|---|---|
committer | Tim Renouf <tpr.ll@botech.co.uk> | 2020-05-21 22:13:19 +0100 |
commit | db16eb33ce43792e0758edf958bbb175eb6a60e2 (patch) | |
tree | 2452f245890fe6717e9bcfb2dec6c7aa2cfc0fb2 /llvm/lib/BinaryFormat/MsgPackDocument.cpp | |
parent | 7019cea26dfef5882c96f278c32d0f9c49a5e516 (diff) | |
download | llvm-db16eb33ce43792e0758edf958bbb175eb6a60e2.zip llvm-db16eb33ce43792e0758edf958bbb175eb6a60e2.tar.gz llvm-db16eb33ce43792e0758edf958bbb175eb6a60e2.tar.bz2 |
[MsgPack] Added convenience assignment to MsgPackDocument
This commit increases the convenience of using the MsgPackDocument API,
especially when creating a document for writing out.
It adds direct assignment of bool, integer and string types to a
DocNode, as long as that DocNode is already inside a document, e.g. the
result of a map lookup. It also adds map lookup given an integer type
(it already had that for string).
So, to assign a string to a map element whose key is an int, you can
now write
MyMap[42] = "towel";
instead of
MyMap[MyMap.getDocument()->getNode(42)] =
MyMap.getDocument()->getNode("towel");
Also added MapDocNode::erase methods.
Differential Revision: https://reviews.llvm.org/D80121
Change-Id: I17301fa15bb9802231c52542798af5b54beb583e
Diffstat (limited to 'llvm/lib/BinaryFormat/MsgPackDocument.cpp')
-rw-r--r-- | llvm/lib/BinaryFormat/MsgPackDocument.cpp | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/llvm/lib/BinaryFormat/MsgPackDocument.cpp b/llvm/lib/BinaryFormat/MsgPackDocument.cpp index a7e43a8..53720c5 100644 --- a/llvm/lib/BinaryFormat/MsgPackDocument.cpp +++ b/llvm/lib/BinaryFormat/MsgPackDocument.cpp @@ -48,6 +48,20 @@ DocNode &MapDocNode::operator[](DocNode Key) { return N; } +/// Member access for MapDocNode for integer key. +DocNode &MapDocNode::operator[](int Key) { + return (*this)[getDocument()->getNode(Key)]; +} +DocNode &MapDocNode::operator[](unsigned Key) { + return (*this)[getDocument()->getNode(Key)]; +} +DocNode &MapDocNode::operator[](int64_t Key) { + return (*this)[getDocument()->getNode(Key)]; +} +DocNode &MapDocNode::operator[](uint64_t Key) { + return (*this)[getDocument()->getNode(Key)]; +} + /// Array element access. This extends the array if necessary. DocNode &ArrayDocNode::operator[](size_t Index) { if (size() <= Index) { @@ -57,6 +71,36 @@ DocNode &ArrayDocNode::operator[](size_t Index) { return (*Array)[Index]; } +// Convenience assignment operators. This only works if the destination +// DocNode has an associated Document, i.e. it was not constructed using the +// default constructor. The string one does not copy, so the string must +// remain valid for the lifetime of the Document. Use fromString to avoid +// that restriction. +DocNode &DocNode::operator=(StringRef Val) { + *this = getDocument()->getNode(Val); + return *this; +} +DocNode &DocNode::operator=(bool Val) { + *this = getDocument()->getNode(Val); + return *this; +} +DocNode &DocNode::operator=(int Val) { + *this = getDocument()->getNode(Val); + return *this; +} +DocNode &DocNode::operator=(unsigned Val) { + *this = getDocument()->getNode(Val); + return *this; +} +DocNode &DocNode::operator=(int64_t Val) { + *this = getDocument()->getNode(Val); + return *this; +} +DocNode &DocNode::operator=(uint64_t Val) { + *this = getDocument()->getNode(Val); + return *this; +} + // A level in the document reading stack. struct StackLevel { StackLevel(DocNode Node, size_t StartIndex, size_t Length, |