forked from jaege/Cpp-Primer-5th-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
16.14.cpp
158 lines (130 loc) · 4.53 KB
/
16.14.cpp
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// based on ex7.32
#include <string>
#include <vector>
#include <iostream>
#include <cctype>
template <std::string::size_type height, std::string::size_type width>
class Screen;
class Window_mgr {
public:
using FixedScreen = Screen<24, 80>;
using screen_index = std::vector<FixedScreen>::size_type;
void clear(screen_index);
Window_mgr();
private:
std::vector<FixedScreen> screens;
};
// the Screen size is fixed at run time
template <std::string::size_type height, std::string::size_type width>
class Screen {
friend void Window_mgr::clear(screen_index);
public:
using size_type = std::string::size_type;
using content_type = char;
explicit Screen(content_type c = ' ')
: cursor(0), contents(height * width, c) { }
const content_type &get() const { return contents[cursor]; }
//content_type &get() { return contents[cursor]; }
const content_type &get(size_type row, size_type col) const;
//content_type &get(size_type row, size_type col);
Screen &set(content_type c);
Screen &set(size_type row, size_type col, content_type c);
Screen &move(size_type row, size_type col);
const Screen &display(std::ostream &os) const;
Screen &display(std::ostream &os);
private:
void do_display(std::ostream &os) const;
size_type cursor = 0;
//size_type height = 0;
//size_type width = 0;
std::string contents;
};
template <std::string::size_type height, std::string::size_type width>
inline const typename Screen<height, width>::content_type &
Screen<height, width>::get(size_type row, size_type col) const {
return contents[row * width + col];
}
//template <std::string::size_type height, std::string::size_type width>
//inline typename Screen<height, width>::content_type &
//Screen<height, width>::get(size_type row, size_type col) {
// return contents[row * width + col];
//}
template <std::string::size_type height, std::string::size_type width>
inline
Screen<height, width> &Screen<height, width>::set(content_type 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(size_type row, size_type col, content_type c) {
contents[row * width + col] = c;
return *this;
}
template <std::string::size_type height, std::string::size_type width>
inline Screen<height, width> &
Screen<height, width>::move(size_type row, size_type col) {
cursor = row * width + col;
return *this;
}
template <std::string::size_type height, std::string::size_type width>
inline const Screen<height, width> &
Screen<height, width>::display(std::ostream &os) const {
do_display(os);
return *this;
}
template <std::string::size_type height, std::string::size_type width>
inline
Screen<height, width> &Screen<height, width>::display(std::ostream &os) {
do_display(os);
return *this;
}
template <std::string::size_type height, std::string::size_type width>
inline
void Screen<height, width>::do_display(std::ostream &os) const {
for (size_type i = 0; i != contents.size(); ++i) {
os << contents[i];
if ((i + 1) % width == 0 && i + 1 != contents.size())
os << "\n";
}
}
// The input and output operators do not need to be friend of `Screen`, they
// can use the interface of `Screen`.
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 (Screen<height, width>::size_type i = 0; i != height; ++i)
for (Screen<height, width>::size_type j = 0; j != width; ++j) {
while (!std::isprint(ch = is.get()));
s.move(i, j).set(ch);
}
return is;
}
Window_mgr::Window_mgr() : screens{FixedScreen()} {}
//void Window_mgr::clear(screen_index i) {
// FixedScreen &s = screens[i];
// s.contents = std::string(s.height * s.width, ' '); // error in template
//}
int main() {
//Screen myScreen(5, 5, 'X');
Screen<5, 5> myScreen('X');
std::cout << myScreen << "\n-----\n";
myScreen.move(4,0).set('#').display(std::cout);
std::cout << "\n-----\n" << myScreen << "\n-----\n";
std::cin >> myScreen;
std::cout << "\n-----\n" << myScreen << "\n-----\n";
Screen<2, 3> myScreen2;
const Screen<2, 3> blank;
myScreen2.set('#').display(std::cout); // calls nonconst version
std::cout << "\n-----\n";
blank.display(std::cout); // calls const version
std::cout << "\n-----\n";
Window_mgr window;
//window.clear(0);
return 0;
}