forked from chihyang/CPP_Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExer19_17_Screen.h
More file actions
39 lines (39 loc) · 1.18 KB
/
Copy pathExer19_17_Screen.h
File metadata and controls
39 lines (39 loc) · 1.18 KB
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
// Screen header from exercise 7.23.
#ifndef EXER19_17_SCREEN_H
#define EXER19_17_SCREEN_H
#include <string>
class Screen{
// for test, required by exercise 19.12
friend void ptrTest();
public:
typedef std::string::size_type pos;
// type alias for every kind of member function, required by exercise 19.17
using Action1 = char (Screen::*)() const;
using Action2 = char (Screen::*)(pos, pos) const;
using Action3 = Screen& (Screen::*)(pos, 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