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