DUDS
Distributed Update of Data from Something
Path.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the DUDS project. It is subject to the BSD-style
3  * license terms in the LICENSE file found in the top-level directory of this
4  * distribution and at https://github.com/jjackowski/duds/blob/master/LICENSE.
5  * No part of DUDS, including this file, may be copied, modified, propagated,
6  * or distributed except according to the terms contained in the LICENSE file.
7  *
8  * Copyright (C) 2020 Jeff Jackowski
9  */
10 #include <duds/ui/Path.hpp>
11 
12 namespace duds { namespace ui {
13 
14 Path::Path(const PageSptr &first) {
15  pages.push_back(first);
16  spot = 0;
17 }
18 
19 void Path::push(const PageSptr &page) {
20  // is the current page not the last page on the stack?
21  if (pages.size() > (spot + 1)) {
22  // remove pages past the new current page
23  pages.resize(spot + 2);
24  // place the new page after the current
25  pages[++spot] = page;
26  } else {
27  // add to the end
28  pages.emplace_back(page);
29  ++spot;
30  }
31 }
32 
33 void Path::push(PageSptr &&page) {
34  // is the current page not the last page on the stack?
35  if (pages.size() > (spot + 1)) {
36  // remove pages past the new current page
37  pages.resize(spot + 2);
38  // place the new page after the current
39  pages[++spot] = std::move(page);
40  } else {
41  // add to the end
42  pages.emplace_back(std::move(page));
43  ++spot;
44  }
45 }
46 
47 bool Path::move(int steps) {
48  // no change?
49  if (!steps) {
50  return false;
51  }
52  int ospot = spot;
53  // calcuate the new current page spot
54  int nspot = spot + steps;
55  // limit it to the range of the stack
56  if (steps > 0) {
57  spot = std::min((PageStack::size_type)nspot, pages.size() - 1);
58  } else {
59  spot = std::max(nspot, 0);
60  }
61  // compare current just set spot with the old one
62  return spot != ospot;
63 }
64 
65 void Path::clear() {
66  pages.clear();
67  spot = -1;
68 }
69 
71  pages.resize(spot + 1);
72 }
73 
74 } }
bool move(int steps)
Changes the current page by the given amount.
Definition: Path.cpp:47
move_impl move(unsigned int c, unsigned int r)
Display stream manipulator that moves the display cursor to the given location.
void push(const PageSptr &page)
Pushes a new page after the current page.
Definition: Path.cpp:19
void clear()
Clears out the stack of all pages.
Definition: Path.cpp:65
void clearPastCurrent()
Clears all pages on the stack that are forward of the current page.
Definition: Path.cpp:70
Path()
Constructs a new empty path.
Definition: Path.hpp:42
std::shared_ptr< Page > PageSptr
A shared pointer to a Page.
Definition: Page.hpp:58
PageStack pages
The pages in path order.
Definition: Path.hpp:33
int spot
Index of the current page.
Definition: Path.hpp:37