forked from huangmingchuan/Cpp_Primer_Answers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreen.h
52 lines (47 loc) · 1.1 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
#ifndef SCREEN_H
#define SCREEN_H
#include <string>
#include <iostream>
template<unsigned H, unsigned W>
class Screen
{
public:
typedef std::string::size_type pos;
Screen() = default; // needed because Screen has another constructor
// cursor initialized to 0 by its in-class initializer
Screen(char c) :contents(H * W, c) {}
char get() const // get the character at the cursor
{
return contents[cursor];
} // implicitly inline
Screen &move(pos r, pos c); // can be made inline later
friend std::ostream & operator<< (std::ostream &os, const Screen<H, W> & c)
{
unsigned int i, j;
for (i = 0; i<c.height; i++)
{
os << c.contents.substr(0, W) << std::endl;
}
return os;
}
friend std::istream & operator>> (std::istream &is, Screen & c)
{
char a;
is >> a;
std::string temp(H*W, a);
c.contents = temp;
return is;
}
private:
pos cursor = 0;
pos height = H, width = W;
std::string contents;
};
template<unsigned H, unsigned W>
inline Screen<H, W>& Screen<H, W>::move(pos r, pos c)
{
pos row = r * width;
cursor = row + c;
return *this;
}
#endif // SCREEN_H