2014-12-15 01:27:03 +01:00
|
|
|
/*
|
2016-02-19 20:25:30 +01:00
|
|
|
TREE-DIFF-APPLICATION.hpp - consume and apply a tree diff
|
2014-12-15 01:27:03 +01:00
|
|
|
|
|
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2014, Hermann Vosseler <Ichthyostega@web.de>
|
|
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License as
|
|
|
|
|
published by the Free Software Foundation; either version 2 of
|
|
|
|
|
the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @file tree-diff-application.hpp
|
2016-02-19 20:25:30 +01:00
|
|
|
** Concrete implementation to apply structural changes to hierarchical
|
2014-12-15 03:21:19 +01:00
|
|
|
** data structures. Together with the generic #DiffApplicator, this allows
|
|
|
|
|
** to receive linearised structural diff descriptions and apply them to
|
2015-10-02 18:47:44 +02:00
|
|
|
** a given target data structure, to effect the corresponding changes.
|
2014-12-15 01:27:03 +01:00
|
|
|
**
|
2016-02-19 20:25:30 +01:00
|
|
|
** ## Design considerations
|
2015-10-23 19:24:34 +02:00
|
|
|
** While -- conceptually -- our tree diff handling can be seen as an extension
|
|
|
|
|
** and generalisation of list diffing, the decision was \em not to embody this
|
|
|
|
|
** extension into the implementation technically, for sake of clarity. More so,
|
|
|
|
|
** since the Record, which serves as foundation for our »External Tree Description«,
|
|
|
|
|
** was made to look and behave like a list-like entity, but built with two distinct
|
|
|
|
|
** scopes at implementation level: the attribute scope and the contents scope. This
|
|
|
|
|
** carries over to the fine points of the list diff language semantics, especially
|
|
|
|
|
** when it comes to fault tolerance and strictness vs fuzziness in diff application.
|
|
|
|
|
** The implementation is thus faced with having to deal with an internal focus and
|
|
|
|
|
** a switch from scope to scope, which adds a lot of complexity. So the list diff
|
|
|
|
|
** application strategy can be seen as blueprint and demonstration of principles.
|
|
|
|
|
**
|
2016-02-17 01:38:04 +01:00
|
|
|
** Another point in question is whether to treat the diff application as
|
2015-11-01 07:03:47 +01:00
|
|
|
** manipulating a target data structure, or rather building a reshaped copy.
|
|
|
|
|
** The fact that GenNode and Record are designed as immutable values seems to favour
|
|
|
|
|
** the latter, yet the very reason to engage into building this diff framework was
|
|
|
|
|
** how to handle partial updates within a expectedly very large UI model, reflecting
|
2015-10-23 19:24:34 +02:00
|
|
|
** the actual session model in Proc-Layer. So we end up working on a Mutator,
|
|
|
|
|
** which clearly signals we're going to reshape and re-rig the target data.
|
|
|
|
|
**
|
2016-02-19 20:25:30 +01:00
|
|
|
** \par related
|
|
|
|
|
** Closely related to this generic application of tree changes is the situation,
|
|
|
|
|
** where we want to apply structural changes to some non-generic and private data
|
|
|
|
|
** structure. In fact, it is possible to _use the same tree diff language_ for
|
|
|
|
|
** this specific case, with the help of an _adapter_. Thus, within our diff
|
|
|
|
|
** framework, we provide a _similar binding_ for the DiffApplicator, but
|
|
|
|
|
** then targetted towards such an [structure adapter](\ref TreeMutator)
|
|
|
|
|
**
|
|
|
|
|
** ## State and nested scopes
|
2015-11-01 07:03:47 +01:00
|
|
|
** Within the level of a single #Record, our tree diff language works similar to
|
|
|
|
|
** the list diff (with the addition of the \c after(ID) verb, which is just a
|
|
|
|
|
** shortcut to accept parts of the contents unaltered). But after possibly rearranging
|
|
|
|
|
** the contents of an "object" (Record), the diff might open some of its child "objects"
|
|
|
|
|
** by entering a nested scope. This is done with the \c mut(ID)....emu(ID) bracketing
|
|
|
|
|
** construct. On the implementation side, this means we need to use a stack somehow.
|
|
|
|
|
** The decision was to manage this stack explicitly, as a std::stack (heap memory).
|
|
|
|
|
** Each entry on this stack is a "context frame" for list diff. Which makes the
|
|
|
|
|
** tree diff applicator a highly statefull component.
|
|
|
|
|
**
|
|
|
|
|
** Even more so, since -- for \em performance reasons -- we try to alter the
|
|
|
|
|
** tree shaped data structure \em in-place. We want to avoid the copy of possibly
|
|
|
|
|
** deep sub-trees, when in the end we might be just rearranging their sequence order.
|
|
|
|
|
** This design decision comes at a price tag though
|
|
|
|
|
** - it subverts the immutable nature of \c Record<GenNode> and leads to
|
|
|
|
|
** high dependency on data layout and implementation details of the latter.
|
|
|
|
|
** This is at least prominently marked by working on a diff::Record::Mutator,
|
|
|
|
|
** so the client has first to "open up" the otherwise immutable tree
|
|
|
|
|
** - the actual list diff on each level works by first \em moving the entire
|
|
|
|
|
** Record contents away into a temporary buffer and then \em moving them
|
|
|
|
|
** back into new shape one by one. In case of a diff conflict (i.e. a
|
|
|
|
|
** mismatch between the actual data structure and the assumptions made
|
|
|
|
|
** for the diff message on the sender / generator side), an exception
|
|
|
|
|
** is thrown, leaving the client with a possibly corrupted tree, where
|
|
|
|
|
** parts might even still be stashed away in the temporary buffer,
|
|
|
|
|
** and thus be lost.
|
|
|
|
|
** We consider this unfortunate, yet justified by the very nature of applying a diff.
|
|
|
|
|
** When the user needs safety or transactional behaviour, a deep copy should be made
|
|
|
|
|
** before attaching the #DiffApplicator
|
|
|
|
|
**
|
|
|
|
|
** @see DiffTreeApplication_test
|
|
|
|
|
** @see DiffListApplication_test
|
|
|
|
|
** @see GenNodeBasic_test
|
|
|
|
|
** @see tree-diff.hpp
|
2014-12-15 01:27:03 +01:00
|
|
|
**
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef LIB_DIFF_TREE_DIFF_APPLICATION_H
|
|
|
|
|
#define LIB_DIFF_TREE_DIFF_APPLICATION_H
|
|
|
|
|
|
|
|
|
|
|
2014-12-15 03:21:19 +01:00
|
|
|
#include "lib/diff/tree-diff.hpp"
|
2015-10-02 18:47:44 +02:00
|
|
|
#include "lib/diff/gen-node.hpp"
|
2015-10-29 04:14:18 +01:00
|
|
|
#include "lib/format-string.hpp"
|
2015-10-30 04:45:22 +01:00
|
|
|
#include "lib/util.hpp"
|
2014-12-15 01:27:03 +01:00
|
|
|
|
2015-10-23 01:32:47 +02:00
|
|
|
#include <utility>
|
2015-10-30 03:11:33 +01:00
|
|
|
#include <stack>
|
2015-10-23 01:32:47 +02:00
|
|
|
|
2014-12-15 01:27:03 +01:00
|
|
|
namespace lib {
|
2014-12-15 03:21:19 +01:00
|
|
|
namespace diff{
|
2014-12-15 01:27:03 +01:00
|
|
|
|
2015-10-30 04:45:22 +01:00
|
|
|
using util::unConst;
|
2015-11-01 04:12:55 +01:00
|
|
|
using util::cStr;
|
2015-10-29 04:14:18 +01:00
|
|
|
using util::_Fmt;
|
2015-10-23 01:32:47 +02:00
|
|
|
using std::move;
|
|
|
|
|
using std::swap;
|
|
|
|
|
|
|
|
|
|
|
2015-10-02 18:47:44 +02:00
|
|
|
/**
|
2015-10-30 03:11:33 +01:00
|
|
|
* Interpreter for the tree-diff-language to work on GenNode elements
|
|
|
|
|
* A concrete strategy to apply a structural diff to a target data structure
|
2015-10-23 01:32:47 +02:00
|
|
|
* made from #Record<GenNode> elements. This data structure is assumed to be
|
|
|
|
|
* recursive, tree-like. But because Record elements are conceived as immutable
|
|
|
|
|
* and value-like, the tree diff application actually works on a Rec::Mutator
|
|
|
|
|
* wrapping the target record to be altered through consuming the diff.
|
2015-10-02 18:47:44 +02:00
|
|
|
* @throws lumiera::error::State when diff application fails due to the
|
|
|
|
|
* target sequence being different than assumed by the given diff.
|
|
|
|
|
* @see #TreeDiffInterpreter explanation of the verbs
|
|
|
|
|
*/
|
|
|
|
|
template<>
|
2015-10-23 01:32:47 +02:00
|
|
|
class DiffApplicationStrategy<Rec::Mutator>
|
2015-10-09 03:03:27 +02:00
|
|
|
: public TreeDiffInterpreter
|
2014-12-15 01:27:03 +01:00
|
|
|
{
|
2015-10-30 03:11:33 +01:00
|
|
|
using Mutator = Rec::Mutator;
|
2015-10-23 21:08:33 +02:00
|
|
|
using Content = Rec::ContentMutator;
|
2015-10-29 04:14:18 +01:00
|
|
|
using Iter = Content::Iter;
|
2015-10-23 01:32:47 +02:00
|
|
|
|
2015-10-30 03:11:33 +01:00
|
|
|
struct ScopeFrame
|
|
|
|
|
{
|
|
|
|
|
Mutator& target;
|
|
|
|
|
Content content;
|
|
|
|
|
|
|
|
|
|
ScopeFrame(Mutator& toModify)
|
|
|
|
|
: target(toModify)
|
|
|
|
|
, content()
|
2015-10-31 05:15:47 +01:00
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
void init()
|
2015-10-30 03:11:33 +01:00
|
|
|
{
|
|
|
|
|
target.swapContent (content);
|
2015-11-01 04:49:22 +01:00
|
|
|
content.resetPos();
|
2015-10-30 03:11:33 +01:00
|
|
|
}
|
|
|
|
|
};
|
2014-12-15 01:27:03 +01:00
|
|
|
|
2015-10-30 03:11:33 +01:00
|
|
|
/** Storage: a stack of workspaces
|
|
|
|
|
* used to handle nested child objects */
|
|
|
|
|
std::stack<ScopeFrame> scopes_;
|
2015-10-09 03:44:38 +02:00
|
|
|
|
2015-10-29 04:14:18 +01:00
|
|
|
|
2015-10-30 03:11:33 +01:00
|
|
|
Mutator& out() { return scopes_.top().target; }
|
|
|
|
|
Content& src() { return scopes_.top().content; }
|
|
|
|
|
Iter& srcPos() { return scopes_.top().content.pos; }
|
|
|
|
|
bool endOfData() { return srcPos() == src().end(); }
|
2015-10-30 04:51:13 +01:00
|
|
|
Rec& alteredRec() { return out(); }
|
2015-10-30 03:11:33 +01:00
|
|
|
|
2015-10-29 04:14:18 +01:00
|
|
|
|
|
|
|
|
void
|
|
|
|
|
__expect_in_target (GenNode const& elm, Literal oper)
|
|
|
|
|
{
|
2015-10-30 03:11:33 +01:00
|
|
|
if (endOfData())
|
2015-10-29 04:14:18 +01:00
|
|
|
throw error::State(_Fmt("Unable to %s element %s from target as demanded; "
|
|
|
|
|
"no (further) elements in target sequence") % oper % elm
|
|
|
|
|
, LUMIERA_ERROR_DIFF_CONFLICT);
|
2015-11-01 02:33:35 +01:00
|
|
|
|
|
|
|
|
if (elm.matches(Ref::CHILD) and not srcPos()->isNamed())
|
|
|
|
|
return; // allow for anonymous pick or delete of children
|
|
|
|
|
|
2015-11-01 02:20:54 +01:00
|
|
|
if (not srcPos()->matches(elm))
|
2015-10-29 04:14:18 +01:00
|
|
|
throw error::State(_Fmt("Unable to %s element %s from target as demanded; "
|
|
|
|
|
"found element %s on current target position instead")
|
2015-10-30 03:11:33 +01:00
|
|
|
% oper % elm % *srcPos()
|
2015-10-29 04:14:18 +01:00
|
|
|
, LUMIERA_ERROR_DIFF_CONFLICT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
__expect_further_elements (GenNode const& elm)
|
|
|
|
|
{
|
2015-10-30 03:11:33 +01:00
|
|
|
if (endOfData())
|
2015-10-29 04:14:18 +01:00
|
|
|
throw error::State(_Fmt("Premature end of target sequence, still expecting element %s; "
|
|
|
|
|
"unable to apply diff further.") % elm
|
|
|
|
|
, LUMIERA_ERROR_DIFF_CONFLICT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
__expect_found (GenNode const& elm, Iter const& targetPos)
|
|
|
|
|
{
|
2015-10-30 03:11:33 +01:00
|
|
|
if (targetPos == src().end())
|
2015-10-29 04:14:18 +01:00
|
|
|
throw error::State(_Fmt("Premature end of sequence; unable to locate "
|
|
|
|
|
"element %s in the remainder of the target.") % elm
|
|
|
|
|
, LUMIERA_ERROR_DIFF_CONFLICT);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-31 04:25:43 +01:00
|
|
|
void
|
|
|
|
|
__expect_successful_location (GenNode const& elm)
|
|
|
|
|
{
|
|
|
|
|
if (endOfData()
|
|
|
|
|
and not ( elm.matches(Ref::END) // after(_END_) -> its OK we hit the end
|
|
|
|
|
or (elm.matches(Ref::ATTRIBS) and src().children.empty()))) // after(_ATTRIBS_) -> if there are no children, it's OK to hit the end
|
|
|
|
|
throw error::State(_Fmt("Unable locate position 'after(%s)'") % elm.idi
|
|
|
|
|
, LUMIERA_ERROR_DIFF_CONFLICT);
|
|
|
|
|
}
|
2015-11-01 07:03:47 +01:00
|
|
|
|
2015-10-30 04:51:13 +01:00
|
|
|
void
|
|
|
|
|
__expect_valid_parent_scope (GenNode::ID const& idi)
|
|
|
|
|
{
|
|
|
|
|
if (scopes_.empty())
|
|
|
|
|
throw error::State(_Fmt("Unbalanced child scope bracketing tokens in diff; "
|
|
|
|
|
"When leaving scope %s, we fell out of root scope.") % idi.getSym()
|
|
|
|
|
, LUMIERA_ERROR_DIFF_CONFLICT);
|
|
|
|
|
|
|
|
|
|
if (alteredRec().empty())
|
|
|
|
|
throw error::State(_Fmt("Corrupted state. When leaving scope %s, "
|
|
|
|
|
"we found an empty parent scope.") % idi.getSym()
|
|
|
|
|
, LUMIERA_ERROR_DIFF_CONFLICT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
__expect_end_of_scope (GenNode::ID const& idi)
|
|
|
|
|
{
|
2015-10-30 05:10:16 +01:00
|
|
|
if (not endOfData())
|
2015-10-30 04:51:13 +01:00
|
|
|
throw error::State(_Fmt("Incomplete diff: when about to leave scope %s, "
|
|
|
|
|
"not all previously existing elements have been confirmed by the diff. "
|
|
|
|
|
"At least one spurious element %s was left over") % idi.getSym() % *srcPos()
|
|
|
|
|
, LUMIERA_ERROR_DIFF_CONFLICT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-10-29 04:14:18 +01:00
|
|
|
Iter
|
|
|
|
|
find_in_current_scope (GenNode const& elm)
|
|
|
|
|
{
|
2015-10-30 03:11:33 +01:00
|
|
|
Iter end_of_scope = src().currIsAttrib()? src().attribs.end()
|
|
|
|
|
: src().children.end();
|
2015-11-01 23:11:55 +01:00
|
|
|
return std::find_if (srcPos()
|
|
|
|
|
,end_of_scope
|
|
|
|
|
,[&](auto& entry)
|
|
|
|
|
{
|
|
|
|
|
return entry.matches(elm);
|
|
|
|
|
});
|
2015-10-29 04:14:18 +01:00
|
|
|
}
|
|
|
|
|
|
2015-10-30 04:45:22 +01:00
|
|
|
GenNode const&
|
|
|
|
|
find_child (GenNode::ID const& idi)
|
|
|
|
|
{
|
2015-11-01 03:29:35 +01:00
|
|
|
if (alteredRec().empty())
|
|
|
|
|
throw error::State(_Fmt("Attempt to mutate element %s, but current target data scope is empty. "
|
|
|
|
|
"Sender and receiver out of sync?") % idi.getSym()
|
|
|
|
|
, LUMIERA_ERROR_DIFF_CONFLICT);
|
|
|
|
|
|
|
|
|
|
// Short-cut-mutation: look at the last element.
|
|
|
|
|
// this should be the one just added. BUT NOTE: this fails
|
|
|
|
|
// when adding an attribute after entering the child scope.
|
|
|
|
|
// Since attributes are typically values and not mutated,
|
|
|
|
|
// this inaccuracy was deemed acceptable
|
|
|
|
|
auto& current = out().accessLast();
|
|
|
|
|
if (Ref::THIS.matches(idi) or current.matches(idi))
|
|
|
|
|
return current;
|
|
|
|
|
|
2015-10-30 04:51:13 +01:00
|
|
|
for (auto & child : alteredRec())
|
2015-10-30 04:45:22 +01:00
|
|
|
if (child.idi == idi)
|
|
|
|
|
return child;
|
|
|
|
|
|
|
|
|
|
throw error::State(_Fmt("Attempt to mutate non existing child record; unable to locate child %s "
|
2015-10-30 04:51:13 +01:00
|
|
|
"after applying the diff. Current scope: %s") % idi.getSym() % alteredRec()
|
2015-10-30 04:45:22 +01:00
|
|
|
, LUMIERA_ERROR_DIFF_CONFLICT);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-29 04:14:18 +01:00
|
|
|
void
|
|
|
|
|
move_into_new_sequence (Iter pos)
|
|
|
|
|
{
|
2015-10-30 03:11:33 +01:00
|
|
|
if (src().currIsAttrib())
|
2015-11-01 03:29:35 +01:00
|
|
|
out().appendAttrib (move(*pos)); //////////////TICKET #969 was it a good idea to allow adding attributes "after the fact"?
|
2015-10-29 04:14:18 +01:00
|
|
|
else
|
2015-10-30 03:11:33 +01:00
|
|
|
out().appendChild (move(*pos));
|
2015-10-29 04:14:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-10-09 03:44:38 +02:00
|
|
|
/* == Implementation of the list diff application primitives == */
|
|
|
|
|
|
2016-02-17 01:38:04 +01:00
|
|
|
virtual void
|
2015-10-24 02:42:13 +02:00
|
|
|
ins (GenNode const& n) override
|
2015-10-09 03:44:38 +02:00
|
|
|
{
|
2015-10-24 03:15:35 +02:00
|
|
|
if (n.isNamed())
|
2015-10-30 05:10:16 +01:00
|
|
|
if (n.isTypeID())
|
|
|
|
|
out().setType (n.data.get<string>());
|
|
|
|
|
else
|
2015-11-01 03:29:35 +01:00
|
|
|
out().appendAttrib(n); //////////////TICKET #969 dto.
|
2015-10-24 03:15:35 +02:00
|
|
|
else
|
|
|
|
|
{
|
2015-10-30 03:11:33 +01:00
|
|
|
out().appendChild(n);
|
|
|
|
|
if (src().currIsAttrib())
|
|
|
|
|
src().jumpToChildScope();
|
2015-10-24 03:15:35 +02:00
|
|
|
}
|
2015-10-09 03:44:38 +02:00
|
|
|
}
|
|
|
|
|
|
2016-02-17 01:38:04 +01:00
|
|
|
virtual void
|
2015-10-24 02:42:13 +02:00
|
|
|
del (GenNode const& n) override
|
2015-10-09 03:44:38 +02:00
|
|
|
{
|
2015-10-29 04:14:18 +01:00
|
|
|
__expect_in_target(n, "remove");
|
2015-10-30 03:11:33 +01:00
|
|
|
++src();
|
2015-10-09 03:44:38 +02:00
|
|
|
}
|
|
|
|
|
|
2016-02-17 01:38:04 +01:00
|
|
|
virtual void
|
2015-10-24 02:42:13 +02:00
|
|
|
pick (GenNode const& n) override
|
2015-10-09 03:44:38 +02:00
|
|
|
{
|
2015-10-29 04:14:18 +01:00
|
|
|
__expect_in_target(n, "pick");
|
2015-10-30 03:11:33 +01:00
|
|
|
move_into_new_sequence (srcPos());
|
|
|
|
|
++src();
|
2015-10-09 03:44:38 +02:00
|
|
|
}
|
|
|
|
|
|
2016-02-17 01:38:04 +01:00
|
|
|
virtual void
|
2015-10-24 02:42:13 +02:00
|
|
|
skip (GenNode const& n) override
|
2015-10-09 03:44:38 +02:00
|
|
|
{
|
2015-10-29 04:14:18 +01:00
|
|
|
__expect_further_elements (n);
|
2015-10-30 03:11:33 +01:00
|
|
|
++src();
|
2015-10-09 03:44:38 +02:00
|
|
|
} // assume the actual content has been moved away by a previous find()
|
|
|
|
|
|
2016-02-17 01:38:04 +01:00
|
|
|
virtual void
|
2015-10-24 02:42:13 +02:00
|
|
|
find (GenNode const& n) override
|
2015-10-09 03:44:38 +02:00
|
|
|
{
|
2015-10-29 04:14:18 +01:00
|
|
|
__expect_further_elements (n);
|
|
|
|
|
Iter found = find_in_current_scope(n);
|
|
|
|
|
__expect_found (n, found);
|
|
|
|
|
move_into_new_sequence (found);
|
2015-10-09 03:44:38 +02:00
|
|
|
} // consume and leave waste, expected to be cleaned-up by skip() later
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* == Implementation of the tree diff application primitives == */
|
|
|
|
|
|
2015-10-31 04:25:43 +01:00
|
|
|
/** cue to a position behind the named node,
|
|
|
|
|
* thereby picking (accepting) all traversed elements
|
|
|
|
|
* into the reshaped new data structure as-is */
|
2016-02-17 01:38:04 +01:00
|
|
|
virtual void
|
2015-10-24 02:42:13 +02:00
|
|
|
after (GenNode const& n) override
|
2015-10-09 03:44:38 +02:00
|
|
|
{
|
2015-10-31 04:43:18 +01:00
|
|
|
if (n.matches(Ref::ATTRIBS))
|
|
|
|
|
while (not endOfData() and srcPos()->isNamed())
|
|
|
|
|
{
|
|
|
|
|
move_into_new_sequence (srcPos());
|
|
|
|
|
++src();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
if (n.matches(Ref::END))
|
|
|
|
|
while (not endOfData())
|
|
|
|
|
{
|
|
|
|
|
move_into_new_sequence (srcPos());
|
|
|
|
|
++src();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
while (not (endOfData() or srcPos()->matches(n)))
|
|
|
|
|
{
|
|
|
|
|
move_into_new_sequence (srcPos());
|
|
|
|
|
++src();
|
|
|
|
|
}
|
2015-10-31 04:25:43 +01:00
|
|
|
|
|
|
|
|
__expect_successful_location(n);
|
2015-10-31 04:43:18 +01:00
|
|
|
|
2015-10-31 05:15:47 +01:00
|
|
|
if (not endOfData() and srcPos()->matches(n))
|
2015-10-31 04:43:18 +01:00
|
|
|
++src(); // get /after/ an explicitly given position
|
2015-10-09 03:44:38 +02:00
|
|
|
}
|
|
|
|
|
|
2016-02-19 17:22:41 +01:00
|
|
|
/** assignement of changed value in one step */
|
|
|
|
|
virtual void
|
|
|
|
|
set (GenNode const& n) override
|
|
|
|
|
{
|
|
|
|
|
GenNode const& elm = find_child (n.idi);
|
|
|
|
|
unConst(elm).data = n.data;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-30 04:45:22 +01:00
|
|
|
/** open nested scope to apply diff to child object */
|
2016-02-17 01:38:04 +01:00
|
|
|
virtual void
|
2015-10-24 02:42:13 +02:00
|
|
|
mut (GenNode const& n) override
|
2015-10-09 03:44:38 +02:00
|
|
|
{
|
2015-10-30 04:45:22 +01:00
|
|
|
GenNode const& child = find_child (n.idi);
|
|
|
|
|
Rec const& childRecord = child.data.get<Rec>();
|
2015-11-01 04:12:55 +01:00
|
|
|
|
|
|
|
|
TRACE (diff, "tree-diff: ENTER scope %s", cStr(childRecord));
|
2015-10-30 04:45:22 +01:00
|
|
|
scopes_.emplace (mutateInPlace (unConst(childRecord)));
|
2015-11-01 04:12:55 +01:00
|
|
|
scopes_.top().init();
|
2015-10-09 03:44:38 +02:00
|
|
|
}
|
|
|
|
|
|
2015-10-30 04:45:22 +01:00
|
|
|
/** finish and leave child object scope, return to parent */
|
2016-02-17 01:38:04 +01:00
|
|
|
virtual void
|
2015-10-24 02:42:13 +02:00
|
|
|
emu (GenNode const& n) override
|
2015-10-09 03:44:38 +02:00
|
|
|
{
|
2015-11-01 04:12:55 +01:00
|
|
|
TRACE (diff, "tree-diff: LEAVE scope %s", cStr(alteredRec()));
|
|
|
|
|
|
2015-10-30 04:51:13 +01:00
|
|
|
__expect_end_of_scope (n.idi);
|
|
|
|
|
scopes_.pop();
|
|
|
|
|
__expect_valid_parent_scope (n.idi);
|
2015-10-09 03:44:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
explicit
|
2015-10-23 01:32:47 +02:00
|
|
|
DiffApplicationStrategy(Rec::Mutator& mutableTargetRecord)
|
2015-10-30 03:11:33 +01:00
|
|
|
: scopes_()
|
2015-10-23 00:40:02 +02:00
|
|
|
{
|
2015-10-30 03:11:33 +01:00
|
|
|
scopes_.emplace(mutableTargetRecord);
|
2015-10-23 00:40:02 +02:00
|
|
|
}
|
2015-10-31 05:15:47 +01:00
|
|
|
|
|
|
|
|
void
|
|
|
|
|
initDiffApplication()
|
|
|
|
|
{
|
|
|
|
|
REQUIRE (1 == scopes_.size());
|
|
|
|
|
scopes_.top().init();
|
|
|
|
|
}
|
2014-12-15 01:27:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2014-12-15 03:21:19 +01:00
|
|
|
}} // namespace lib::diff
|
|
|
|
|
#endif /*LIB_DIFF_TREE_DIFF_APPLICATION_H*/
|