forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex16_14_screen.h
76 lines (62 loc) · 1.77 KB
/
ex16_14_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
71
72
73
74
75
76
#ifndef CP5_EX16_14_SCREEN_H_
#define CP5_EX16_14_SCREEN_H_
#include <algorithm>
#include <iostream>
#include <string>
using pos = std::string::size_type;
template <pos, pos> class Screen;
template <pos H, pos W> std::istream& operator>>(std::istream&, Screen<H, W>&);
template <pos H, pos W>
std::ostream& operator<<(std::ostream&, const Screen<H, W>&);
template <pos H, pos W> class Screen {
friend std::istream& operator>><H, W>(std::istream&, Screen<H, W>&);
friend std::ostream& operator<<<H, W>(std::ostream&, const Screen<H, W>&);
public:
Screen() = default;
Screen(char c) : contents(H * W, c) {}
char get() const { return contents[cursor]; }
char get(pos r, pos c) const { return contents[r * W + c]; }
inline Screen& move(pos r, pos c);
inline Screen& set(char ch);
inline Screen& set(pos r, pos c, char ch);
private:
pos cursor = 0;
std::string contents;
};
template <pos H, pos W>
std::istream& operator>>(std::istream& is, Screen<H, W>& s)
{
std::string input;
is >> input;
for (char ch : input) s.set(ch);
return is;
}
template <pos H, pos W>
std::ostream& operator<<(std::ostream& os, const Screen<H, W>& s)
{
for (pos r = 0; r != H; ++r) {
for (pos c = 0; c != W; ++c) {
os << s.get(r, c);
}
os << std::endl;
}
return os;
}
template <pos H, pos W> inline Screen<H, W>& Screen<H, W>::move(pos r, pos c)
{
cursor = r * W + c;
return *this;
}
template <pos H, pos W> inline Screen<H, W>& Screen<H, W>::set(char ch)
{
contents[cursor++] = ch;
cursor = std::min(cursor, H * W);
return *this;
}
template <pos H, pos W>
inline Screen<H, W>& Screen<H, W>::set(pos r, pos c, char ch)
{
contents[r * W + c] = ch;
return *this;
}
#endif // CP5_EX16_14_SCREEN_H_