public member function
<ios> <iostream>
std::ios_base::register_callback
void register_callback (event_callback fn, int index);
Register event callback function
Registers fn as a callback function to be called automatically with index as argument when a stream event occurs.
If more than one callback function is registered, they are all called, in the inverse order of registration.
The callback function shall be of a type convertible to event_callback. And it is called by an expression equivalent to:
where index is the index argument passed when the callback function is registered with this function, stream is a pointer to the stream object suffering the event, and ev is an object of member enum type event indicating which event occurred. It can be one of the following member values:
member constant | event triggered |
copyfmt_event | on a call to member copyfmt (at the moment where all format flags have been copied, but before the exception mask is) |
erase_event | on a call to the stream destructor (also called at the beginning of basic_ios::copyfmt). |
imbue_event | on a call to imbue (just before the function returns). |
All registered functions are called on all of the cases above. The function itself can use the ev parameter to discern which event triggered the function call.
Parameters
- fn
- Pointer to the function to be called.
The event_callback member type is defined as:
1
|
typedef void (*event_callback) (event ev, ios_base& ios, int index);
|
- index
- Integer value passed as parameter to the callback function.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
// stream callbacks
#include <iostream> // std::cout, std::ios_base
#include <fstream> // ofstream
void testfn (std::ios::event ev, std::ios_base& stream, int index)
{
switch (ev)
{
case stream.copyfmt_event:
std::cout << "copyfmt_event\n"; break;
case stream.imbue_event:
std::cout << "imbue_event\n"; break;
case stream.erase_event:
std::cout << "erase_event\n"; break;
}
}
int main () {
std::ofstream filestr;
filestr.register_callback (testfn,0);
filestr.imbue (std::cout.getloc());
return 0;
}
|
Output:
Data races
Modifies the stream object.
Concurrent access to the same stream object may cause data races.
Exception safety
Basic guarantee: if an exception is thrown, the stream is in a valid state.