The Battle for Wesnoth  1.19.7+dev
walker_container.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  : container_(container), widget_(&container), itor_(container.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  assert(false);
43  return fail;
44  case internal:
45  if(!entered_grid) {
46  entered_grid = true;
47  in_grid = true;
48  return valid;
49  }
50  if(in_grid) {
51  in_grid = false;
52  return invalid;
53  }
54  assert(false);
55  return fail;
56  case child:
57  if(itor_ != container_.end()) {
58  ++itor_;
59  return itor_ == container_.end() ? invalid : valid;
60  }
61  }
62 
63  assert(false);
64  return fail;
65 }
66 
67 bool container::at_end(const level level) const
68 {
69  switch(level) {
70  case self:
71  return widget_ == nullptr;
72  case internal:
73  return entered_grid && !in_grid;
74  case child:
75  return (itor_ == container_.end());
76  }
77 
78  assert(false);
79  return true;
80 }
81 
83 {
84  switch(level) {
85  case self:
86  return widget_;
87  case internal:
88  return &container_.get_grid();
89  case child:
90  if(itor_ == container_.end()) {
91  return nullptr;
92  } else {
93  return *itor_;
94  }
95  }
96 
97  assert(false);
98  return nullptr;
99 }
100 
101 } // namespace gui2::iteration
A generic container base class.
const grid & get_grid() const
grid::iterator end()
A walker for a gui2::container_base.
container(gui2::container_base &container)
Constructor.
gui2::grid::iterator itor_
The iterator to the children of container_.
virtual bool at_end(const level level) const
Inherited from gui2::iteration::walker_base.
gui2::container_base & container_
The container which the walker is attached to.
virtual gui2::widget * get(const level level)
Inherited from gui2::iteration::walker_base.
gui2::widget * widget_
The widget which the walker is attached to.
virtual state_t next(const level level)
Inherited from gui2::iteration::walker_base.
bool entered_grid
Whether the grid has been yielded.
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
Base class for all widgets.
Definition: widget.hpp:55