Back to index

texmacs  1.0.7.15
observer.hpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : observer.hpp
00004 * DESCRIPTION: Observers of trees
00005 * COPYRIGHT  : (C) 2004  Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #ifndef OBSERVER_H
00013 #define OBSERVER_H
00014 #include "string.hpp"
00015 enum  tree_label;
00016 class tree;
00017 class hard_link_rep;
00018 class observer;
00019 class modification;
00020 class blackbox;
00021 template<class T> class list;
00022 template<class T> class array;
00023 typedef hard_link_rep* weak_link;
00024 typedef list<int> path;
00025 
00026 #define OBSERVER_UNKNOWN    0
00027 #define OBSERVER_LIST       1
00028 #define OBSERVER_IP         2
00029 #define OBSERVER_POINTER    3
00030 #define OBSERVER_POSITION   4
00031 #define OBSERVER_ADDENDUM   5
00032 #define OBSERVER_EDIT       6
00033 #define OBSERVER_UNDO       7
00034 #define OBSERVER_HIGHLIGHT  8
00035 
00036 /******************************************************************************
00037 * The observer class
00038 ******************************************************************************/
00039 
00040 extern int observer_count;
00041 class observer_rep: public abstract_struct {
00042 public:
00043   inline observer_rep () { TM_DEBUG(observer_count++); }
00044   inline virtual ~observer_rep () { TM_DEBUG(observer_count--); }
00045   inline virtual int get_type () { return OBSERVER_UNKNOWN; }
00046   inline virtual tm_ostream& print (tm_ostream& out) { return out; }
00047 
00048   // Announcing modifications in subtrees
00049   virtual void announce             (tree& ref, modification mod);
00050   virtual void announce_assign      (tree& ref, path p, tree t);
00051   virtual void announce_insert      (tree& ref, path p, tree ins);
00052   virtual void announce_remove      (tree& ref, path p, int nr);
00053   virtual void announce_split       (tree& ref, path p);
00054   virtual void announce_join        (tree& ref, path p);
00055   virtual void announce_assign_node (tree& ref, path p, tree_label op);
00056   virtual void announce_insert_node (tree& ref, path p, tree ins);
00057   virtual void announce_remove_node (tree& ref, path p);
00058   virtual void announce_set_cursor  (tree& ref, path p, tree data);
00059   virtual void done                 (tree& ref, modification mod);
00060   virtual void touched              (tree& ref, path p);
00061 
00062   // Call back routines for tree modifications
00063   virtual void notify_assign        (tree& ref, tree t);
00064   virtual void notify_insert        (tree& ref, int pos, int nr);
00065   virtual void notify_remove        (tree& ref, int pos, int nr);
00066   virtual void notify_split         (tree& ref, int pos, tree prev);
00067   virtual void notify_var_split     (tree& ref, tree t1, tree t2);
00068   virtual void notify_join          (tree& ref, int pos, tree next);
00069   virtual void notify_var_join      (tree& ref, tree t, int offset);
00070   virtual void notify_assign_node   (tree& ref, tree_label op);
00071   virtual void notify_insert_node   (tree& ref, int pos);
00072   virtual void notify_remove_node   (tree& ref, int pos);
00073   virtual void notify_set_cursor    (tree& ref, int pos, tree data);
00074   virtual void notify_detach        (tree& ref, tree closest, bool right);
00075 
00076   // Extra routines for particular types of observers
00077   virtual bool get_ip (path& ip);
00078   virtual bool set_ip (path ip);
00079   virtual bool get_position (tree& t, int& index);
00080   virtual bool set_position (tree t, int index);
00081   virtual observer& get_child (int which);
00082   virtual list<observer> get_tree_pointers ();
00083   virtual bool get_tree (tree& t);
00084   virtual bool get_contents (int kind, blackbox& bb);
00085   virtual bool set_highlight (int lan, int col, int start, int end);
00086   virtual bool get_highlight (int lan, array<int>& cols);
00087 };
00088 
00089 class observer {
00090 public:
00091   ABSTRACT_NULL(observer);
00092   inline friend bool operator == (observer o1, observer o2) {
00093     return o1.rep == o2.rep; }
00094   inline friend bool operator != (observer o1, observer o2) {
00095     return o1.rep != o2.rep; }
00096   inline friend int hash (observer o1) {
00097     return hash ((pointer) o1.rep); }
00098 };
00099 ABSTRACT_NULL_CODE(observer);
00100 
00101 tm_ostream& operator << (tm_ostream& out, observer o);
00102 
00103 class editor_rep;
00104 class archiver_rep;
00105 
00106 extern observer nil_observer;
00107 observer ip_observer (path ip);
00108 observer list_observer (observer o1, observer o2);
00109 observer tree_pointer (tree t, bool flag= false);
00110 observer tree_position (tree t, int index);
00111 observer edit_observer (editor_rep* ed);
00112 observer undo_observer (archiver_rep* arch);
00113 observer highlight_observer (int lan, array<int> cols);
00114 
00115 /******************************************************************************
00116 * Modification routines for trees and other observer-related facilities
00117 ******************************************************************************/
00118 
00119 extern bool versioning_busy;
00120 bool busy_tree (tree& ref);
00121 
00122 void assign      (tree& ref, tree t);
00123 void insert      (tree& ref, int pos, tree t);
00124 void remove      (tree& ref, int pos, int nr);
00125 void split       (tree& ref, int pos, int at);
00126 void join        (tree& ref, int pos);
00127 void assign_node (tree& ref, tree_label op);
00128 void insert_node (tree& ref, int pos, tree t);
00129 void remove_node (tree& ref, int pos);
00130 void set_cursor  (tree& ref, int pos, tree data);
00131 void touch       (tree& ref);
00132 
00133 void assign      (path p, tree t);
00134 void insert      (path p, tree ins);
00135 void remove      (path p, int nr);
00136 void split       (path p);
00137 void join        (path p);
00138 void assign_node (path p, tree_label op);
00139 void insert_node (path p, tree ins);
00140 void remove_node (path p);
00141 void set_cursor  (path p, tree data);
00142 void touch       (path p);
00143 
00144 void insert_observer (observer& o, observer what);
00145 void remove_observer (observer& o, observer what);
00146 void attach_observer (tree& ref, observer o);
00147 void detach_observer (tree& ref, observer o);
00148 void clean_observers (tree& ref);
00149 
00150 path obtain_ip (tree& ref);
00151 void attach_ip (tree& ref, path ip);
00152 void detach_ip (tree& ref);
00153 bool ip_attached (path ip);
00154 
00155 tree obtain_tree (observer o);
00156 observer tree_pointer_new (tree t);
00157 void tree_pointer_delete (observer o);
00158 
00159 path obtain_position (observer o);
00160 
00161 observer tree_addendum_new (tree t, int kind, blackbox bb);
00162 void tree_addendum_new (observer o);
00163 
00164 observer search_observer (tree& ref, int type);
00165 bool admits_edit_observer (tree t);
00166 
00167 void attach_highlight (tree& ref, int lan);
00168 void attach_highlight (tree& ref, int lan, int col, int start, int end);
00169 bool has_highlight (tree& ref, int lan);
00170 array<int> obtain_highlight (tree& ref, int lan);
00171 void detach_highlight (tree& ref, int lan);
00172 
00173 void stretched_print (tree t, bool ips= false, int indent= 0);
00174 
00175 #endif // defined OBSERVER_H