本文整理汇总了C++中DocNode::getKind方法的典型用法代码示例。如果您正苦于以下问题:C++ DocNode::getKind方法的具体用法?C++ DocNode::getKind怎么用?C++ DocNode::getKind使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DocNode
的用法示例。
在下文中一共展示了DocNode::getKind方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: readFromBlob
// Read a document from a binary msgpack blob.
// The blob data must remain valid for the lifetime of this Document (because a
// string object in the document contains a StringRef into the original blob).
// If Multi, then this sets root to an array and adds top-level objects to it.
// If !Multi, then it only reads a single top-level object, even if there are
// more, and sets root to that.
// Returns false if failed due to illegal format.
bool Document::readFromBlob(StringRef Blob, bool Multi) {
msgpack::Reader MPReader(Blob);
SmallVector<StackLevel, 4> Stack;
if (Multi) {
// Create the array for multiple top-level objects.
Root = getArrayNode();
Stack.push_back(StackLevel({Root, (size_t)-1, nullptr}));
}
do {
// On to next element (or key if doing a map key next).
// Read the value.
Object Obj;
if (!MPReader.read(Obj)) {
if (Multi && Stack.size() == 1) {
// OK to finish here as we've just done a top-level element with Multi
break;
}
return false; // Finished too early
}
// Convert it into a DocNode.
DocNode Node;
switch (Obj.Kind) {
case Type::Nil:
Node = getNode();
break;
case Type::Int:
Node = getNode(Obj.Int);
break;
case Type::UInt:
Node = getNode(Obj.UInt);
break;
case Type::Boolean:
Node = getNode(Obj.Bool);
break;
case Type::Float:
Node = getNode(Obj.Float);
break;
case Type::String:
Node = getNode(Obj.Raw);
break;
case Type::Map:
Node = getMapNode();
break;
case Type::Array:
Node = getArrayNode();
break;
default:
return false; // Raw and Extension not supported
}
// Store it.
if (Stack.empty())
Root = Node;
else if (Stack.back().Node.getKind() == Type::Array) {
// Reading an array entry.
auto &Array = Stack.back().Node.getArray();
Array.push_back(Node);
} else {
auto &Map = Stack.back().Node.getMap();
if (!Stack.back().MapEntry) {
// Reading a map key.
Stack.back().MapEntry = &Map[Node];
} else {
// Reading the value for the map key read in the last iteration.
*Stack.back().MapEntry = Node;
Stack.back().MapEntry = nullptr;
}
}
// See if we're starting a new array or map.
switch (Node.getKind()) {
case msgpack::Type::Array:
case msgpack::Type::Map:
Stack.push_back(StackLevel({Node, Obj.Length, nullptr}));
break;
default:
break;
}
// Pop finished stack levels.
while (!Stack.empty()) {
if (Stack.back().Node.getKind() == msgpack::Type::Array) {
if (Stack.back().Node.getArray().size() != Stack.back().Length)
break;
} else {
if (Stack.back().MapEntry ||
Stack.back().Node.getMap().size() != Stack.back().Length)
break;
}
Stack.pop_back();
}
} while (!Stack.empty());
return true;
//.........这里部分代码省略.........
示例2: writeToBlob
/// Write a MsgPack document to a binary MsgPack blob.
void Document::writeToBlob(std::string &Blob) {
Blob.clear();
raw_string_ostream OS(Blob);
msgpack::Writer MPWriter(OS);
SmallVector<WriterStackLevel, 4> Stack;
DocNode Node = getRoot();
for (;;) {
switch (Node.getKind()) {
case Type::Array:
MPWriter.writeArraySize(Node.getArray().size());
Stack.push_back(
{Node, DocNode::MapTy::iterator(), Node.getArray().begin(), false});
break;
case Type::Map:
MPWriter.writeMapSize(Node.getMap().size());
Stack.push_back(
{Node, Node.getMap().begin(), DocNode::ArrayTy::iterator(), true});
break;
case Type::Nil:
MPWriter.writeNil();
break;
case Type::Boolean:
MPWriter.write(Node.getBool());
break;
case Type::Int:
MPWriter.write(Node.getInt());
break;
case Type::UInt:
MPWriter.write(Node.getUInt());
break;
case Type::String:
MPWriter.write(Node.getString());
break;
default:
llvm_unreachable("unhandled msgpack object kind");
}
// Pop finished stack levels.
while (!Stack.empty()) {
if (Stack.back().Node.getKind() == Type::Map) {
if (Stack.back().MapIt != Stack.back().Node.getMap().end())
break;
} else {
if (Stack.back().ArrayIt != Stack.back().Node.getArray().end())
break;
}
Stack.pop_back();
}
if (Stack.empty())
break;
// Get the next value.
if (Stack.back().Node.getKind() == Type::Map) {
if (Stack.back().OnKey) {
// Do the key of a key,value pair in a map.
Node = Stack.back().MapIt->first;
Stack.back().OnKey = false;
} else {
Node = Stack.back().MapIt->second;
++Stack.back().MapIt;
Stack.back().OnKey = true;
}
} else {
Node = *Stack.back().ArrayIt;
++Stack.back().ArrayIt;
}
}
}