The Battle for Wesnoth  1.17.0-dev
walker_grid.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  : grid_(grid), widget_(&grid), itor_(grid.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_ != grid_.end()) {
51  ++itor_;
52  return itor_ == grid_.end() ? invalid : valid;
53  }
54  }
55 
56  assert(false);
57  return fail;
58 }
59 
60 bool grid::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_ == grid_.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_ == grid_.end()) {
84  return nullptr;
85  } else {
86  return *itor_;
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
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.
Definition: walker_grid.cpp:60
gui2::grid & grid_
The grid which the walker is attached to.
Definition: walker_grid.hpp:50
level
The level to walk at.
Definition: walker.hpp:35
Base class for all widgets.
Definition: widget.hpp:49
Generic file dialog.
Definition: field-fwd.hpp:23
Base container class.
Definition: grid.hpp:31
gui2::widget * widget_
The grid which the walker is attached to.
Definition: walker_grid.hpp:58
state_t
The state of the walker.
Definition: walker.hpp:49
gui2::grid::iterator itor_
The iterator to the children of grid_.
Definition: walker_grid.hpp:66
virtual gui2::widget * get(const level level)
Inherited from gui2::iteration::walker_base.
Definition: walker_grid.cpp:75
A walker for a gui2::grid.
Definition: walker_grid.hpp:29
virtual state_t next(const level level)
Inherited from gui2::iteration::walker_base.
Definition: walker_grid.cpp:33
Visit the children of its nested grid.
Definition: walker.hpp:41
grid(gui2::grid &grid)
Constructor.
Definition: walker_grid.cpp:28
iterator end()
Definition: grid.hpp:484