From 2d802ae3a5e4df1ca488e7faed6a856cf7c7641c Mon Sep 17 00:00:00 2001 From: Xie Gengxin Date: Tue, 20 Dec 2022 16:43:40 +0800 Subject: [PATCH 1/2] feat: New APIs of merge, diff and update --- include/sonic/dom/dynamicnode.h | 4 +- include/sonic/dom/generic_document.h | 18 +++ include/sonic/dom/genericnode.h | 167 ++++++++++++++++++++++++++- tests/node_test.cpp | 118 +++++++++++++++++++ 4 files changed, 303 insertions(+), 4 deletions(-) diff --git a/include/sonic/dom/dynamicnode.h b/include/sonic/dom/dynamicnode.h index e95a5ff0..ca19b0dd 100644 --- a/include/sonic/dom/dynamicnode.h +++ b/include/sonic/dom/dynamicnode.h @@ -34,10 +34,10 @@ namespace sonic_json { template -class DNode : public GenericNode { +class DNode : public GenericNode> { public: using NodeType = DNode; - using BaseNode = GenericNode; + using BaseNode = GenericNode; using AllocatorType = Allocator; using MemberNode = typename NodeTraits::MemberNode; using MemberIterator = typename NodeTraits::MemberIterator; diff --git a/include/sonic/dom/generic_document.h b/include/sonic/dom/generic_document.h index eab9e7d8..710d2f8d 100644 --- a/include/sonic/dom/generic_document.h +++ b/include/sonic/dom/generic_document.h @@ -260,4 +260,22 @@ class GenericDocument : public NodeType { using Document = GenericDocument>; +template +static ReturnDocType Diff(const T& from, const U& to) { + ReturnDocType ret; + using RetNodeType = typename ReturnDocType::NodeType; + Diff(from, to, (RetNodeType&)(ret), ret.GetAllocator()); + + return ret; /* RVO */ +} + +template +static Document Diff(const T& from, const U& to) { + Document ret; + using RetNodeType = typename Document::NodeType; + Diff(from, to, (RetNodeType&)(ret), ret.GetAllocator()); + + return ret; /* RVO */ +} + } // namespace sonic_json diff --git a/include/sonic/dom/genericnode.h b/include/sonic/dom/genericnode.h index a895382d..1714b4e5 100644 --- a/include/sonic/dom/genericnode.h +++ b/include/sonic/dom/genericnode.h @@ -48,19 +48,37 @@ class MemberNodeT { template struct NodeTraits; +template +static inline void Update(T&, const S&, Allocator&); +// +// template +// static inline void Merge(T&, const S&, Allocator&); + +// Copied from http://coliru.stacked-crooked.com/a/8dd19d21817cadf5 +template