The Battle for Wesnoth  1.17.0-dev
walker_tree_node.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2021
3  by Mark de Wever <koraq@xs4all.nl>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #define GETTEXT_DOMAIN "wesnoth-lib"
17 
19 
20 #include <cassert>
21 
22 namespace gui2
23 {
24 
25 namespace iteration
26 {
27 
29  : children_(children), widget_(&node), itor_(children.begin())
30 {
31 }
32 
34 {
35  if(at_end(level)) {
36  return fail;
37  }
38 
39  switch(level) {
40  case self:
41  if(widget_) {
42  widget_ = nullptr;
43  return invalid;
44  }
45  [[fallthrough]];
46  case internal:
47  assert(false);
48  return fail;
49  case child:
50  if(itor_ != children_.end()) {
51  ++itor_;
52  return itor_ == children_.end() ? invalid : valid;
53  }
54  }
55 
56  assert(false);
57  return fail;
58 }
59 
60 bool tree_node::at_end(const level level) const
61 {
62  switch(level) {
63  case self:
64  return widget_ == nullptr;
65  case internal:
66  return true;
67  case child:
68  return (itor_ == children_.end());
69  }
70 
71  assert(false);
72  return true;
73 }
74 
76 {
77  switch(level) {
78  case self:
79  return widget_;
80  case internal:
81  return nullptr;
82  case child:
83  if(itor_ == children_.end()) {
84  return nullptr;
85  } else {
86  return itor_.operator->()->get();
87  }
88  }
89 
90  assert(false);
91  return nullptr;
92 }
93 
94 } // namespace iteration
95 
96 } // namespace gui2
When calling next the following it has the following results.
Definition: walker.hpp:69
std::vector< std::shared_ptr< tree_view_node > > node_children_vector
When calling next the following it has the following results.
Definition: walker.hpp:58
virtual bool at_end(const level level) const
Inherited from gui2::iteration::walker_base.
level
The level to walk at.
Definition: walker.hpp:35
Base class for all widgets.
Definition: widget.hpp:49
virtual gui2::widget * get(const level level)
Inherited from gui2::iteration::walker_base.
tree_node(gui2::tree_view_node &node, tree_view_node::node_children_vector &children)
Constructor.
Generic file dialog.
Definition: field-fwd.hpp:23
gui2::widget * widget_
The node which the walker is attached to.
state_t
The state of the walker.
Definition: walker.hpp:49
virtual state_t next(const level level)
Inherited from gui2::iteration::walker_base.
tree_view_node::node_children_vector & children_
The children of the node which the walker is attached to.
Visit the children of its nested grid.
Definition: walker.hpp:41
tree_view_node::node_children_vector::iterator itor_
The iterator to the children of the node.