ARB
ap_main_type.hxx
Go to the documentation of this file.
1 // ================================================================ //
2 // //
3 // File : ap_main_type.hxx //
4 // Purpose : //
5 // //
6 // Institute of Microbiology (Technical University Munich) //
7 // http://www.arb-home.de/ //
8 // //
9 // ================================================================ //
10 
11 #ifndef AP_MAIN_TYPE_HXX
12 #define AP_MAIN_TYPE_HXX
13 
14 #ifndef AP_BUFFER_HXX
15 #include "AP_buffer.hxx"
16 #endif
17 #ifndef PARS_DTREE_HXX
18 #include "pars_dtree.hxx"
19 #endif
20 
21 class UserFrame {
22  Level frameLevel;
23  // @@@ store more information here (e.g. comment, parsimony value, timestamp)
24 public:
25  UserFrame(Level level) : frameLevel(level) {}
26  Level get_level() const { return frameLevel; }
27 };
28 
30 
31 class AP_main : virtual Noncopyable {
32  NodeStack *currFrame;
33  FrameStack frames;
34  UserFrameStack user_frames;
35  Level frameLevel;
36  AWT_graphic_parsimony *agt; // provides access to tree!
37  StackFrameData *frameData; // saved/restored by remember/revert
38  GBDATA *gb_main;
39 
40  void push_nodes_changed_since(Level wanted_frameLevel);
41  void rollback_to(Level wanted_frameLevel);
42 
43 public:
45  : currFrame(NULp),
46  frameLevel(0),
47  agt(NULp),
48  frameData(NULp),
49  gb_main(NULp)
50  {}
52  if (gb_main) GB_close(gb_main);
53  ap_assert(!frameData);
54  delete currFrame;
55  }
56 
59  AP_pars_root *get_tree_root() const { return agt->get_tree_root(); }
60 
61  DEFINE_READ_ACCESSORS(AP_tree_nlen*, get_root_node, agt->get_root_node());
62 
63  GBDATA *get_gb_main() const {
64  ap_assert(gb_main); // you need to call open() before you can use get_gb_main()
65  return gb_main;
66  }
67  const char *get_aliname() const;
68  size_t get_user_push_counter() const { return user_frames.count_elements(); }
69  Level get_frameLevel() const { return frameLevel; }
70 
71  GB_ERROR open(const char *db_server);
72 
73  bool push_node(AP_tree_nlen *node, AP_STACK_MODE);
74 
75  void remember_user_state();
76  void revert_user_state();
77  // @@@ add accept_user_state (#632)
78 
79  void remember();
80  void revert();
81  void accept();
82  void accept_all() { while (currFrame) accept(); }
83 
84 #if defined(UNIT_TESTS)
85  void remember_whole_tree(); // dont use in production
86 #endif
87 
88  void accept_if(bool cond) { if (cond) accept(); else revert(); }
89  void revert_if(bool cond) { accept_if(!cond); }
90 
91  bool remember_and_rollback_to(Level wanted_frameLevel) {
92  if (wanted_frameLevel>=frameLevel) {
93  return false;
94  }
95  remember();
96  push_nodes_changed_since(wanted_frameLevel);
97  rollback_to(wanted_frameLevel);
98  return true;
99  }
105  return remember_and_rollback_to(frameLevel-1);
106  }
107 
108 #if defined(PROVIDE_PRINT)
109  void print(std::ostream& out);
110  void print2file(const char *file_in_ARBHOME);
111  void dump2file(const char *name) {
112  static int counter = 0;
113 
114  if (counter == 0) {
115  system("rm $ARBHOME/[0-9][0-9]_*.log");
116  }
117 
118  char *numbered_name = GBS_global_string_copy("%02i_%s.log", ++counter, name);
119  print2file(numbered_name);
120  free(numbered_name);
121  }
122 
123 #endif
124 
125 #if defined(ASSERTION_USED) || defined(UNIT_TESTS)
127  ASSERT_VALID_TREE(get_root_node());
128  ASSERT_RESULT(bool, true, remember_and_rollback_to_previous()); // otherwise stack is empty
129  Validity valid(tree_is_valid(get_root_node(), false));
130  revert(); // undo remember_and_rollback_to_previous
131  ASSERT_VALID_TREE(get_root_node());
132  return valid;
133  }
135  ASSERT_VALID_TREE(get_root_node());
136  Level pops_avail = get_frameLevel();
137  Validity valid;
138  for (Level rollback = 1; rollback<=pops_avail && valid; ++rollback) {
139  Level wanted_level = pops_avail-rollback;
140  ASSERT_RESULT(bool, true, remember_and_rollback_to(wanted_level)); // otherwise stack is empty
141 
142  // dump2file(GBS_global_string("after_remember_and_rollback_to_%lu", wanted_level));
143  valid = Validity(tree_is_valid(get_root_node(), false));
144 
145  revert(); // undo remember_and_rollback_to
146  // dump2file("after_pop_to_undo__remember_and_rollback_to");
147 
148  ASSERT_VALID_TREE(get_root_node());
149  }
150  return valid;
151  }
152 #endif
153 
154  inline AP_tree_nlen *makeNode(AP_pars_root *proot);
155  inline AP_tree_edge *makeEdge(AP_tree_nlen *n1, AP_tree_nlen *n2);
156  inline void destroyNode(AP_tree_nlen *node);
157  inline void destroyEdge(AP_tree_edge *edge);
158 };
159 
160 #else
161 #error ap_main_type.hxx included twice
162 #endif // AP_MAIN_TYPE_HXX
163 
const char * GB_ERROR
Definition: arb_core.h:25
void accept()
Definition: AP_main.cxx:205
size_t count_elements() const
Definition: AP_buffer.hxx:90
void revert_if(bool cond)
#define ASSERT_RESULT(Type, Expected, Expr)
Definition: arb_assert.h:336
UserFrame(Level level)
void remember_user_state()
Definition: AP_main.cxx:27
AP_tree_nlen * get_root_node()
Definition: pars_dtree.hxx:36
bool remember_and_rollback_to_previous()
GBDATA * get_gb_main() const
Validity revert_will_produce_valid_tree()
size_t get_user_push_counter() const
Validity all_available_reverts_will_produce_valid_trees()
bool push_node(AP_tree_nlen *node, AP_STACK_MODE)
Definition: AP_main.cxx:252
void revert_user_state()
Definition: AP_main.cxx:32
AP_STACK_MODE
Definition: AP_buffer.hxx:117
void accept_if(bool cond)
Level get_level() const
void remember()
Definition: AP_main.cxx:50
DEFINE_READ_ACCESSORS(AP_tree_nlen *, get_root_node, agt->get_root_node())
#define ap_assert(cond)
AWT_graphic_parsimony * get_graphic_tree()
CONSTEXPR_INLINE bool valid(SpeciesCreationMode m)
Definition: ed4_class.hxx:2249
void destroyNode(AP_tree_nlen *node)
Definition: ap_main.hxx:90
GB_ERROR open(const char *db_server)
Definition: AP_main.cxx:20
AP_pars_root * get_tree_root()
Definition: pars_dtree.hxx:39
AP_STACK< UserFrame > UserFrameStack
AP_tree_edge * makeEdge(AP_tree_nlen *n1, AP_tree_nlen *n2)
Definition: ap_main.hxx:87
bool remember_and_rollback_to(Level wanted_frameLevel)
AP_pars_root * get_tree_root() const
void destroyEdge(AP_tree_edge *edge)
Definition: ap_main.hxx:100
void revert()
Definition: AP_main.cxx:66
#define NULp
Definition: cxxforward.h:97
Level get_frameLevel() const
void accept_all()
void set_tree_root(AWT_graphic_parsimony *agt_)
Definition: AP_main.cxx:299
const char * get_aliname() const
Definition: AP_main.cxx:304
#define ASSERT_VALID_TREE(tree)
Definition: TreeNode.h:606
char * GBS_global_string_copy(const char *templat,...)
Definition: arb_msg.cxx:195
void GB_close(GBDATA *gbd)
Definition: arbdb.cxx:625
unsigned long Level
Definition: AP_buffer.hxx:126
AP_tree_nlen * makeNode(AP_pars_root *proot)
Definition: ap_main.hxx:84
void print(const T &t)
Definition: test_unit.h:348