forked from chihyang/CPP_Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExer19_12_Screen.h
More file actions
35 lines (35 loc) · 978 Bytes
/
Copy pathExer19_12_Screen.h
File metadata and controls
35 lines (35 loc) · 978 Bytes
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
// Screen header from exercise 7.23.
#ifndef EXER19_12_SCREEN_H
#define EXER19_12_SCREEN_H
#include <string>
class Screen{
// for test, required by exercise 19.12
friend void ptrTest();
public:
typedef std::string::size_type pos;
// return a pointer to data member, required by exercise 19.12
static const std::string Screen::*data()
{ return &Screen::contents; }
Screen() = default;
Screen(pos ht, pos wd, char c) : height(ht), width(wd), contents(ht * wd, c) {}
char get() const { return contents[cursor]; }
char get_cursor() const { return contents[cursor]; }
inline char get(pos ht, pos wd) const;
Screen &move(pos r, pos c);
private:
pos cursor = 0;
pos height = 0, width = 0;
std::string contents;
};
char Screen::get(pos r, pos c) const
{
pos row = r * width;
return contents[row + c];
}
Screen& Screen::move(pos r, pos c)
{
pos row = r * width;
cursor = row + c;
return *this;
}
#endif