Cryptography Reference
In-Depth Information
15.3.2 Manipulators
Building on the previous mechanisms, we would like in this section to obtain
more convenient possibilities for controlling the output format for
LINT
objects.
To this end we use
manipulators
, which are placed directly into the output stream
and thus display the same effects as occur in calling the above status functions.
Manipulators are addresses of functions for which there exist special
insert
operators that on their part accept a pointer to a function as argument. As an
example we consider the following function:
ostream& LintHex (ostream& s)
{
LINT::setf (s, LINT::linthex);
return s;
}
This function calls the status function
setf(s, LINT::linthex)
in the context
of the specified output stream
ostream& s
and thereby effects the output of
LINT
objects as hexadecimal numbers. The name
LintHex
of the function without
parentheses is viewed as a pointer to the function (cf. [Lipp], page 202) and can
be set in an output stream as a manipulator with the help of the
insert
operator
ostream& ostream::operator<< (ostream& (*pf)(ostream&))
{
return (*pf)(*this);
}
defined in the class
ostream
:
LINT a ("0x123456789abcdef0");
cout << LintHex << a;
ostream s;
s << LintDec << a;
The
LINT
manipulator functions operate according to the same pattern as
the standard manipulators in the C++ library, for example
dec
,
hex
,
oct
,
flush
,
and
endl
: The insert operator
<<
simply calls the manipulator function
LintHex()
or
LintDec()
at the appropriate place. The manipulators ensure that the status
flags belonging to the output streams
cout
, respectively
s
, are set. The overloaded
operator<<
for the output of
LINT
objects takes over the representation of the
LINT
object
a
in the requested form.