A program can register a callback function on a particular stream as follows:
// Callback function void show_event(ios_base::event e, iosbase& io, int index) { if (e == ios_base::imbue_event) cout << "imbue called" << endl; else if (e == ios_base::erase_event) cout << "stream destroyed" << endl; else cout << "cpyfmt called" << endl; } ostringstream s; s.register_callback(my_callback,0); //1 s.imbue(locale::global()); //2
//1 | The function show_event is now called with either ios_base::erase_event, ios_base::imbue_event, or ios_base::copyfmt_event as the first argument, depending on whether the destruction of the stream, the imbuing of a new locale, or a call to cpyfmt initiated the callback. |
//2 | This causes show_event to be called. The first argument is ios_base::imbue_event; the second argument is a reference to the stream where the event occurred, which is s in this case; and the third argument is always the index provided in the call to register_callback, which is 0 in this case.
If more than one function is registered, functions are called in the opposite order of registration. |
Please refer to Section 5.9.2 for an example using callback functions with a user-defined stream inserter.
OEM Edition, OEM Edition, ©Copyright 1999, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.