-
Manipulators
I've got a HTML-based Log class which I've designed to work in the same way as streams to allow for formatting. Now I want to have a manipulator similar to endl which will insert <BR> at the current position.
Can someone tell me how to code a manipulator like this?
-
The typical stream manipulator is simply a function that takes a single argument, in its full versions:
Code:
// Flag manipulator
ios_base &name(ios_base &stream) {
// insertion/extraction action
return stream;
}
// State manipulator
template<typename C, typename T = std::char_traits<C> >
basic_ios<C, T> &name(basic_ios<C, T> &stream) {
return stream;
}
// Input manipulator
template<typename C, typename T = std::char_traits<C> >
basic_istream<C, T> &name(basic_istream<C, T> &stream) {
return stream;
}
// Output manipulator
template<typename C, typename T = std::char_traits<C> >
basic_ostream<C, T> &name(basic_ostream<C, T> &stream) {
return stream;
}
The << and >> operators are overloaded to allow such functions to be inserted, the operators then call the function.
Parametrized manipulators like setw are something different.