1 
2 /******************************************************************************
3 * MODULE     : modification.hpp
4 * DESCRIPTION: elementary tree modifications
5 * COPYRIGHT  : (C) 2008  Joris van der Hoeven
6 *******************************************************************************
7 * This software falls under the GNU general public license version 3 or later.
8 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
9 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
10 ******************************************************************************/
11 
12 #ifndef MODIFICATION_H
13 #define MODIFICATION_H
14 #include "tree.hpp"
15 #include "path.hpp"
16 
17 /******************************************************************************
18 * Elementary tree modifications
19 ******************************************************************************/
20 
21 #define MOD_ASSIGN       1
22 #define MOD_INSERT       2
23 #define MOD_REMOVE       3
24 #define MOD_SPLIT        4
25 #define MOD_JOIN         5
26 #define MOD_ASSIGN_NODE  6
27 #define MOD_INSERT_NODE  7
28 #define MOD_REMOVE_NODE  8
29 #define MOD_SET_CURSOR   9
30 
31 /******************************************************************************
32 * The modification class
33 ******************************************************************************/
34 
35 typedef int modification_type;
36 class modification;
37 
38 class modification_rep: concrete_struct {
39 public:
40   modification_type k;
41   path p;
42   tree t;
43 
44 public:
modification_rep(modification_type k2,path p2)45   inline modification_rep (modification_type k2, path p2):
46     k (k2), p (p2) {}
modification_rep(modification_type k2,path p2,tree t2)47   inline modification_rep (modification_type k2, path p2, tree t2):
48     k (k2), p (p2), t (t2) {}
~modification_rep()49   inline ~modification_rep () {}
50   friend class modification;
51 };
52 
53 class modification {
54 CONCRETE(modification);
modification(modification_type k,path p)55   inline modification (modification_type k, path p):
56     rep (tm_new<modification_rep> (k, p)) {}
modification(modification_type k,path p,tree t)57   inline modification (modification_type k, path p, tree t):
58     rep (tm_new<modification_rep> (k, p, t)) {}
59 };
60 CONCRETE_CODE(modification);
61 
62 bool operator == (modification m1, modification m2);
63 bool operator != (modification m1, modification m2);
64 tm_ostream& operator << (tm_ostream& out, modification mod);
65 
66 /******************************************************************************
67 * Constructors and accessors
68 ******************************************************************************/
69 
mod_assign(path p,tree t)70 inline modification mod_assign (path p, tree t) {
71   return modification (MOD_ASSIGN, p, t); }
mod_insert(path p,int pos,tree t)72 inline modification mod_insert (path p, int pos, tree t) {
73   return modification (MOD_INSERT, p * pos, t); }
mod_remove(path p,int pos,int nr)74 inline modification mod_remove (path p, int pos, int nr) {
75   return modification (MOD_REMOVE, p * path (pos, nr)); }
mod_split(path p,int pos,int at)76 inline modification mod_split (path p, int pos, int at) {
77   return modification (MOD_SPLIT, p * path (pos, at)); }
mod_join(path p,int pos)78 inline modification mod_join (path p, int pos) {
79   return modification (MOD_JOIN, p * pos); }
mod_assign_node(path p,tree_label lab)80 inline modification mod_assign_node (path p, tree_label lab) {
81   return modification (MOD_ASSIGN_NODE, p, tree (lab)); }
mod_insert_node(path p,int pos,tree t)82 inline modification mod_insert_node (path p, int pos, tree t) {
83   return modification (MOD_INSERT_NODE, p * pos, t); }
mod_remove_node(path p,int pos)84 inline modification mod_remove_node (path p, int pos) {
85   return modification (MOD_REMOVE_NODE, p * pos); }
mod_set_cursor(path p,int pos,tree data)86 inline modification mod_set_cursor (path p, int pos, tree data) {
87   return modification (MOD_SET_CURSOR, p * pos, data); }
operator *(int i,modification mod)88 inline modification operator * (int i, modification mod) {
89   return modification (mod->k, path (i, mod->p), mod->t); }
operator *(path p,modification mod)90 inline modification operator * (path p, modification mod) {
91   return modification (mod->k, p * mod->p, mod->t); }
operator *(modification mod,int i)92 inline modification operator * (modification mod, int i) {
93   return modification (mod->k, mod->p * i, mod->t); }
operator /(modification mod,path p)94 inline modification operator / (modification mod, path p) {
95   return modification (mod->k, mod->p / p, mod->t); }
copy(modification mod)96 inline modification copy (modification mod) {
97   return modification (mod->k, copy (mod->p), copy (mod->t)); }
98 
99 path root (modification mod);
100 int index (modification mod);
101 int argument (modification mod);
102 tree_label L (modification mod);
103 
104 modification make_modification (string k, path p, tree t);
105 string get_type (modification mod);
106 path   get_path (modification mod);
107 tree   get_tree (modification mod);
108 
109 /******************************************************************************
110 * Further routines on modifications
111 ******************************************************************************/
112 
113 bool is_applicable (tree t, modification mod);
114 tree clean_apply (tree t, modification mod);
115 void raw_apply (tree& t, modification mod);      // in observer.cpp
116 void apply (tree& t, modification mod);          // in observer.cpp
117 
118 /******************************************************************************
119 * Hooks
120 ******************************************************************************/
121 
122 void edit_announce (editor_rep* ed, modification mod);
123 void edit_done (editor_rep* ed, modification mod);
124 void edit_touch (editor_rep* ed, path p);
125 void archive_announce (archiver_rep* buf, modification mod);
126 void link_announce (observer obs, modification mod);
127 
128 #endif // defined MODIFICATION_H
129