The Battle for Wesnoth  1.19.0-dev
walker_tree_node.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2024
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::iteration
23 {
24 
26  : children_(children), widget_(&node), itor_(children.begin())
27 {
28 }
29 
31 {
32  if(at_end(level)) {
33  return fail;
34  }
35 
36  switch(level) {
37  case self:
38  if(widget_) {
39  widget_ = nullptr;
40  return invalid;
41  }
42  [[fallthrough]];
43  case internal:
44  assert(false);
45  return fail;
46  case child:
47  if(itor_ != children_.end()) {
48  ++itor_;
49  return itor_ == children_.end() ? invalid : valid;
50  }
51  }
52 
53  assert(false);
54  return fail;
55 }
56 
57 bool tree_node::at_end(const level level) const
58 {
59  switch(level) {
60  case self:
61  return widget_ == nullptr;
62  case internal:
63  return true;
64  case child:
65  return (itor_ == children_.end());
66  }
67 
68  assert(false);
69  return true;
70 }
71 
73 {
74  switch(level) {
75  case self:
76  return widget_;
77  case internal:
78  return nullptr;
79  case child:
80  if(itor_ == children_.end()) {
81  return nullptr;
82  } else {
83  return itor_.operator->()->get();
84  }
85  }
86 
87  assert(false);
88  return nullptr;
89 }
90 
91 } // namespace gui2::iteration
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.
tree_view_node::node_children_vector::iterator itor_
The iterator to the children of the node.
gui2::widget * widget_
The node which the walker is attached to.
virtual bool at_end(const level level) const
Inherited from gui2::iteration::walker_base.
tree_node(gui2::tree_view_node &node, tree_view_node::node_children_vector &children)
Constructor.
virtual gui2::widget * get(const level level)
Inherited from gui2::iteration::walker_base.
level
The level to walk at.
Definition: walker.hpp:35
@ child
Visit the children of its nested grid.
Definition: walker.hpp:41
state_t
The state of the walker.
Definition: walker.hpp:49
@ invalid
When calling next the following it has the following results.
Definition: walker.hpp:69
@ valid
When calling next the following it has the following results.
Definition: walker.hpp:58
std::vector< std::shared_ptr< tree_view_node > > node_children_vector
Base class for all widgets.
Definition: widget.hpp:53