From f45884975bbcdffea07227aa42c371ea306d13fa Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Sat, 2 May 2015 02:02:48 +0200 Subject: [PATCH] generalise to arbitrary acceptable attribute values ...not yet able to pick up the closure argument type automagically however, right now we can only hypothesise this might be possible --- src/lib/diff/tree-mutator.hpp | 24 ++++++++++++------- tests/15library.tests | 2 +- .../diff/generic-tree-mutator-test.cpp | 2 +- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/lib/diff/tree-mutator.hpp b/src/lib/diff/tree-mutator.hpp index f0bb4713c..f597b155b 100644 --- a/src/lib/diff/tree-mutator.hpp +++ b/src/lib/diff/tree-mutator.hpp @@ -135,24 +135,26 @@ namespace diff{ namespace { - template + template struct ChangeOperation : PAR { + using Closure = function; + ID attribID_; - function change_; + Closure change_; virtual void setAttribute (ID id, Attribute& newValue) { if (id == attribID_) - change_(newValue.get()); + change_(newValue.get()); else // delegate to other closures (Decorator-style) PAR::setAttribute(id, newValue); } - ChangeOperation(ID id, function clo, PAR const& chain) + ChangeOperation(ID id, Closure clo, PAR const& chain) : PAR(chain) , attribID_(id) , change_(clo) @@ -163,19 +165,23 @@ namespace diff{ struct Builder : PAR { - using Change = ChangeOperation; - Builder(PAR par) : PAR(par) { } + template + using Change = ChangeOperation; + template + using Closure = typename Change::Closure; + /* ==== binding API ==== */ - Builder - change (Literal attributeID, function closure) + template + Builder> + change (Literal attributeID, Closure closure) { - return Change (attributeID, closure, *this); + return Change (attributeID, closure, *this); } }; diff --git a/tests/15library.tests b/tests/15library.tests index d9720dd6b..6cd619bd3 100644 --- a/tests/15library.tests +++ b/tests/15library.tests @@ -223,7 +223,7 @@ END TEST "Generic recursive mutation with closure binding" GenericTreeMutator_test <("data", [&](string val) { cout << "\"data\" closure received something "<