-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScreen.h
70 lines (57 loc) · 1.91 KB
/
Screen.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#ifndef SCREEN_H
#define SCREEN_H
#include <iostream>
#include <string>
template <std::string::size_type height, std::string::size_type width>
class Screen {
public:
typedef std::string::size_type pos;
Screen() : contents(height * width, ' ') {}
Screen(char c) : contents(height * width, c) {}
char get() const { return contents[cursor]; }
char get(pos, pos) const;
Screen &move(pos, pos);
Screen &set(char);
Screen &set(pos, pos, char);
Screen &display(std::ostream &os) { do_display(os); return *this; }
const Screen &display(std::ostream &os) const { do_display(os); return *this; }
private:
void do_display(std::ostream &os) const { os << contents; }
pos cursor = 0;
std::string contents;
};
template <std::string::size_type height, std::string::size_type width>
inline char Screen<height, width>::get(pos r, pos c) const {
return contents[r * width + c];
}
template <std::string::size_type height, std::string::size_type width>
inline Screen<height, width> &Screen<height, width>::move(pos r, pos c) {
cursor = r * width + c;
return *this;
}
template <std::string::size_type height, std::string::size_type width>
inline Screen<height, width> &Screen<height, width>::set(char c) {
contents[cursor] = c;
return *this;
}
template <std::string::size_type height, std::string::size_type width>
inline Screen<height, width> &Screen<height, width>::set(pos r, pos c, char ch) {
contents[r * width + c] = ch;
return *this;
}
template <std::string::size_type height, std::string::size_type width>
std::ostream &operator<<(std::ostream &os, const Screen<height, width> &s) {
s.display(os);
return os;
}
template <std::string::size_type height, std::string::size_type width>
std::istream &operator>>(std::istream &is, Screen<height, width> &s) {
char ch;
for (std::string::size_type y = 0; y != height; ++y)
for (std::string::size_type x = 0; x != width; ++x) {
is >> ch;
s.set(y, x, ch);
}
return is;
}
#endif