|
Boost : |
From: nbecker_at_[hidden]
Date: 2001-12-13 08:42:38
An approach I have used to a limited degree is to convert objects to
be printed into a special print object that encapsulates all the
information necessary to print into the desired format.
For example, I want to print a number in hex with some special
format:
struct HexObj {
HexObj (int _x, int _bits) : x (_x), bits (_bits) {}
int x;
int bits;
};
inline ostream& operator << (ostream& os, const HexObj& h) {
return PrintHex (os, h.bits, h.x, 0);
}
cout << hexobj (2, <parameters>);
In case you're wondering:
inline ostream& PrintHex (ostream& os, int width, int x, char ret = '\n') {
const int mask = ~(unsigned(-1) << width);
os << std::hex << std::setw ((width+3)/4) << std::setfill ('0') << (x & mask);
if (ret)
os << ret;
return os;
}
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk