Lumiera  0.pre.03
»edit your freedom«
tree-mutator-listener-binding.hpp
Go to the documentation of this file.
1 /*
2  TREE-MUTATOR-LISTENER-BINDING.hpp - decorator for TreeMutator to attach change listeners
3 
4  Copyright (C)
5  2019, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 */
13 
14 
38 #ifndef LIB_DIFF_TREE_MUTATOR_LISTENER_BINDING_H
39 #define LIB_DIFF_TREE_MUTATOR_LISTENER_BINDING_H
40 
41 
42 #include "lib/error.hpp"
43 #include "lib/diff/gen-node.hpp"
45 
46 namespace lib {
47 namespace diff{
48 
49  namespace { // Mutator-Builder decorator components...
50 
51 
74  template<class PAR, typename LIS, bool assign =false>
76  : public PAR
77  {
78  LIS changeListener_;
79  bool triggered_ = false;
80 
81  void
82  trigger(bool relevant =true)
83  {
84  if (relevant)
85  triggered_ = true;
86  }
87 
88  public:
89  Detector4StructuralChanges (LIS functor, PAR&& chain)
90  : PAR(std::forward<PAR>(chain))
91  , changeListener_{functor}
92  { }
93 
94  // move construction allowed and expected to happen
96 
101  {
102  if (triggered_)
103  changeListener_();
104  }
105 
106  /* ==== TreeMutator API ==== */
107 
108  using Elm = GenNode const&;
109 
110  bool injectNew (Elm elm) override { trigger(); return PAR::injectNew (elm); }
111  bool findSrc (Elm elm) override { trigger(); return PAR::findSrc (elm); }
112  void skipSrc (Elm elm) override { trigger(); PAR::skipSrc (elm); }
113  bool assignElm (Elm elm) override { trigger(assign); return PAR::assignElm (elm); }
114  };
115 
116 
117 
120  template<class PAR>
121  template<typename LIS>
122  inline auto
123  Builder<PAR>::onSeqChange (LIS changeListener)
124  {
125  ASSERT_VALID_SIGNATURE (LIS, void(void))
126 
127  return chainedBuilder<Detector4StructuralChanges<PAR,LIS>> (changeListener);
128  }
129 
133  template<class PAR>
134  template<typename LIS>
135  inline auto
136  Builder<PAR>::onLocalChange (LIS changeListener)
137  {
138  ASSERT_VALID_SIGNATURE (LIS, void(void))
139  // vvvv---note: including assignments
140  return chainedBuilder<Detector4StructuralChanges<PAR,LIS, true>> (changeListener);
141  }
142 
143  }//(END)Mutator-Builder decorator components...
144 
145 }} // namespace lib::diff
146 #endif /*LIB_DIFF_TREE_MUTATOR_LISTENER_BINDING_H*/
#define ASSERT_VALID_SIGNATURE(_FUN_, _SIG_)
Macro for a compile-time check to verify the given generic functors or lambdas expose some expected s...
Definition: function.hpp:247
Decorator for TreeMutator bindings, to fire a listener function when the applied diff describes a rel...
Implementation namespace for support and library code.
~Detector4StructuralChanges()
once the diff for this level is completely applied, the TreeMutator will be discarded, and we can fire our change listener at that point.
Generic building block for tree shaped (meta)data structures.
Lumiera error handling (C++ interface).
Customisable intermediary to abstract generic tree mutation operations.
generic data element node within a tree
Definition: gen-node.hpp:222