Lumiera  0.pre.03
»edit your freedom«
tree-mutator.hpp
Go to the documentation of this file.
1 /*
2  TREE-MUTATOR.hpp - flexible binding to map generic tree changing operations
3 
4  Copyright (C) Lumiera.org
5  2015, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 */
22 
23 
95 #ifndef LIB_DIFF_TREE_MUTATOR_H
96 #define LIB_DIFF_TREE_MUTATOR_H
97 
98 
99 #include "lib/error.hpp"
100 #include "lib/nocopy.hpp"
101 #include "lib/symbol.hpp"
102 #include "lib/meta/trait.hpp"
103 #include "lib/diff/gen-node.hpp"
104 #include "lib/opaque-holder.hpp"
105 #include "lib/iter-adapter-stl.hpp"
106 #include "lib/format-string.hpp"
107 #include "lib/idi/entry-id.hpp"
108 
109 #include <utility>
110 #include <string>
111 
112 
113 namespace lib {
114 namespace diff{
115 
116  namespace error = lumiera::error;
117 
118  using lib::Symbol;
119  using std::string;
120  using util::_Fmt;
121  using lib::idi::BareEntryID;
122 
123 
124 
125 
126  class TestMutationTarget; // for unit testing
127 
128 
129  namespace {
130  template<class PAR>
131  struct Builder;
132  }
133 
134 
147  {
148 
149  public:
150  virtual ~TreeMutator();
151 
152  // only default and move construction allowed
153  TreeMutator () =default;
154  TreeMutator (TreeMutator&&) =default;
155 
156 
157 
158  /* ==== operation API ==== */
159 
166  virtual void
167  init() { }
168 
169  virtual bool
170  hasSrc ()
171  {
172  return false;
173  // do nothing by default
174  }
175 
178  virtual bool
180  {
181  // do nothing by default
182  return false;
183  }
184 
186  virtual bool
187  matchSrc (GenNode const&)
188  {
189  // do nothing by default
190  return false;
191  }
192 
203  virtual void
204  skipSrc (GenNode const&)
205  {
206  // do nothing by default
207  }
208 
210  virtual bool
212  {
213  // do nothing by default
214  return false;
215  }
216 
218  virtual bool
219  accept_until (GenNode const& spec)
220  {
221  return (Ref::END == spec or Ref::ATTRIBS == spec);
222  // contents are exhausted by default,
223  // yet we're unable to find something specific
224  }
225 
227  virtual bool
228  findSrc (GenNode const&)
229  {
230  // do nothing by default
231  return false;
232  }
233 
239  virtual bool
241  {
242  // do nothing by default
243  return false;
244  }
245 
246 
248 
254  virtual bool
256  {
257  // do nothing by default
258  return false;
259  }
260 
265  virtual bool
267  {
268  // nothing to clean-up or verify by default
269  return true;
270  }
271 
272 
273 
279  static Builder<TreeMutator> build();
280  };
281 
282 
283 
284 
285 
286  namespace { // Mutator-Builder decorator components...
287 
288  using std::forward;
289  using std::move;
290 
291 
300  template<class PAR>
301  struct Builder
302  : PAR
303  {
304  explicit
305  Builder(PAR&& par)
306  : PAR{forward<PAR> (par)}
307  { }
308 
309  template<typename BIN, typename...ARGS>
310  Builder<BIN>
311  chainedBuilder (ARGS&&...args)
312  {
313  return Builder<BIN> (BIN{forward<ARGS>(args)..., move(*this)});
314  }
315 
316 
317 
318  /* ==== binding API ==== */
319 
341  template<typename CLO>
342  auto change (Symbol attributeID, CLO setterClosure);
343 
344 
360  template<typename CLO>
361  auto mutateAttrib (Symbol attributeID, CLO mutatorBuilderClosure);
362 
367  template<typename CLO>
368  auto mutateAttrib (idi::BareEntryID const& rawID, CLO mutatorBuilderClosure);
369 
370 
415  template<typename BIN>
416  auto attach (BIN&& collectionBindingSetup);
417 
418 
429  auto attach (Rec::Mutator& targetTree);
430 
431 
435  auto attachDummy (TestMutationTarget& dummy);
436 
437 
439  auto ignoreAllChanges();
440 
441 
451  template<typename LIS>
452  auto onSeqChange (LIS changeListener);
453 
459  template<typename LIS>
460  auto onLocalChange (LIS changeListener);
461  };
462 
463  }//(END) Mutator-Builder...
464 
465 
466  inline Builder<TreeMutator>
468  {
469  return Builder<TreeMutator>{TreeMutator()};
470  }
471 
472 
473 }} // namespace lib::diff
474 
475 
476  /* == implementation detail headers == */
477 
483 
484 
485 
486 #endif /*LIB_DIFF_TREE_MUTATOR_H*/
type erased baseclass for building a combined hash and symbolic ID.
Definition: entry-id.hpp:142
virtual bool matchSrc(GenNode const &)
ensure the next source element matches with given spec
Builder-DSL to create and configure a concrete TreeMutator.
virtual bool completeScope()
ensure the scope addressed by this TreeMutator was processed and exhausted without mismatch ...
Test adapter to watch and verify how the TreeMutator binds to custom tree data structures.
virtual bool acceptSrc(GenNode const &)
accept existing element, when matching the given spec
Types marked with this mix-in may be moved but not copied.
Definition: nocopy.hpp:58
virtual bool findSrc(GenNode const &)
locate designated element and accept it at current position
Front-end for printf-style string template interpolation.
virtual bool injectNew(GenNode const &)
establish new element at current position
A front-end for using printf-style formatting.
Special binding implementation for TreeMutator, allowing to map tree diff operations onto native obje...
Implementation namespace for support and library code.
static Builder< TreeMutator > build()
DSL: start building a custom adapted tree mutator, where the operations are tied by closures or wrapp...
Token or Atom with distinct identity.
Definition: symbol.hpp:126
Mix-Ins to allow or prohibit various degrees of copying and cloning.
A handle to allow for safe »remote implantation« of an unknown subclass into a given opaque InPlaceBu...
Definition: record.hpp:113
static const Ref END
symbolic ID ref "_END_"
Definition: gen-node.hpp:871
Special supplement for TreeMutator, to attach listeners for notification on specific changes...
Marker types to indicate a literal string and a Symbol.
virtual void skipSrc(GenNode const &)
skip next src element and advance abstract source position.
Helper allowing type erasure while holding the actual object inline.
Generic building block for tree shaped (meta)data structures.
virtual bool assignElm(GenNode const &)
locate the designated target element (must be already accepted into the target sequence).
virtual bool accept_until(GenNode const &spec)
repeatedly accept, until after the designated location
Special binding implementation for TreeMutator, allowing to map tree diff operations onto an »Externa...
Helpers for type detection, type rewriting and metaprogramming.
Lumiera error handling (C++ interface).
Special binding implementation for TreeMutator, allowing to accept and ignore any tree diff without t...
static const Ref ATTRIBS
symbolic ID ref "_ATTRIBS_"
Definition: gen-node.hpp:874
Special binding implementation for lib::diff::TreeMutator, allowing to map tree diff operations onto ...
Bare symbolic and hash ID used for accounting of asset like entries.
Preconfigured adapters for some STL container standard usage situations.
virtual bool mutateChild(GenNode const &, Handle)
locate the designated target element and build a suitable sub-mutator for this element into the provi...
generic data element node within a tree
Definition: gen-node.hpp:231
virtual void init()
initialisation immediately before start of diff application
Customisable intermediary to abstract mutating operations on arbitrary, hierarchical object-like data...