forked from chihyang/CPP_Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPage855_bit_fields.cpp
More file actions
60 lines (60 loc) · 1.65 KB
/
Copy pathPage855_bit_fields.cpp
File metadata and controls
60 lines (60 loc) · 1.65 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Warning: this is for verification. Class definitions should be put into header file.
#include <iostream>
using std::cout;
using std::endl;
typedef unsigned int Bit;
class File {
Bit mode: 2; // mode has 2 bits
Bit modified: 1; // modified has 1 bit
Bit prot_owner: 3; // prot_owner has 3 bits
Bit prot_group: 3; // prot_group has 3 bits
Bit prot_world: 3; // prot_world has 3 bits
public:
// file modes specified as octal literals
enum modes { READ = 01, WRITE = 02, EXECUTE = 03 };
File& open(modes);
void close();
void write();
bool isRead() const;
bool isWrite() const;
bool isExecute() const;
void setRead();
void setWrite();
void setExecute();
};
void File::write()
{
modified = 1;
// other operations for writing
}
void File::close()
{
if(modified)
{} // ...save contents
}
File& File::open(File::modes m)
{
mode |= READ; // set the READ bit by default
// other processing
if (m & WRITE) // if opening READ and WRITE
{} // processing to open the file in read/write mode
return *this;
}
// define inline member to test and set the value of bit-field
inline bool File::isRead() const { return mode & READ; }
inline bool File::isWrite() const { return mode & WRITE; }
inline bool File::isExecute() const { return mode & EXECUTE; }
inline void File::setRead() { mode |= READ; }
inline void File::setWrite() { mode |= WRITE; }
inline void File::setExecute() { mode |= EXECUTE; }
int main()
{
File f;
f.setWrite();
f.setRead();
f.setExecute();
cout << f.isRead() << endl;
cout << f.isWrite() << endl;
cout << f.isExecute() << endl;
return 0;
}