File: byte.yo

package info (click to toggle)
c%2B%2B-annotations 13.02.02-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,576 kB
  • sloc: cpp: 25,297; makefile: 1,523; ansic: 165; sh: 126; perl: 90; fortran: 27
file content (58 lines) | stat: -rw-r--r-- 3,062 bytes parent folder | download | duplicates (2)
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
In chapter ref(FirstImpression) the tt(std::byte) type was introduced. It
offers bit-wise and comparison operators but lacks other arithmetic operators
as well as insertion and extraction operators, which may render it less
useful. Fortunately, using the facilities provided by operator overloading a
more generically useful byte-type can be developed.

In this section we develop a class tt(Byte) offering all facilities of numeric
types, as well as insertion and extraction operators, while the size of a
tt(Byte) object equals the size of an tt(unsigned char): 1 byte.

A size 1 tt(Byte) type is realized by defining a class tt(Byte) having a
single tt(uint8_t) data member. Insertion and extraction operators are
provided by free functions. The class tt(Byte), therefore, starts as follows:
    verbinsert(-s4 //start examples/byte.cc)

All the class's member functions are public: tt(Byte) objects can be defined
using the default constructor, the copy constructor, and a constructor
accepting any argument that can be converted to a tt(uint8_t):
    verbinsert(-s4 //init examples/byte.cc)

tt(Byte) objects can be used as lvalues, so assignment operators must be
provided. The first (default) assignment operator handles assignment of
tt(Byte) objects, while the second assignment operator handles assignment of
numeric types which can be converted to tt(uint8_t) values:
    verbinsert(-s4 //assign examples/byte.cc)

Conversion operators are provided so tt(Byte) objects can be used in
situations where tt(uint8_t) values are used:
    verbinsert(-s4 //uint8 examples/byte.cc)
    Athough conversion operators usually return const references, an excption
is made for tt(Byte) because of its semantics: as a tt(Byte) is essentially a
wrapper around a tt(uint8_t) a non-const conversion operator is provided
allowing the use of arithmetic assignment operators. A statement like tt(byte
+= 13) (having defined tt(Byte byte)) is compiled as tt(byte.operator() = 13),
which requires the non-const conversion operator.

Most members require a single statement and can very well be implemented
inline. Here are their implementations:
    verbinsert(-s4 //members examples/byte.cc)

The insertion and extraction operators act identically to the standard
insertion and extraction operators for tt(uint8_t) type values: they insert
and extract the tt(char) representation of the tt(Byte)'s value, because
that's what insertion and extraction operators do: they process text. Here are
their one-line implementations, using the conversion operators to insert or
assign the tt(Byte's d_byte) value:
    verbinsert(-s4 //io examples/byte.cc)
    To write the tt(d_byte's) binary value the stream's tt(write) member
should be used in the way it's always used: reinterpret a tt(Byte) as a
tt(char const *) and write a single byte. Reading a tt(Byte)'s binary value is
implemented analogously, using the stream's tt(read) member.

Finally, here are some examples of how tt(Byte) variables can be used in
practice: 
    verbinsert(-s4 //demo examples/byte.cc)