cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : IOstream Library : ios_base : register_callback
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
IOstream Library
manipulators
classes:
· filebuf
· fstream
· ifstream
· ios
· iostream
· ios_base
· istream
· istringstream
· ofstream
· ostream
· ostringstream
· streambuf
· stringbuf
· stringstream
objects:
· cerr
· cin
· clog
· cout
types:
· fpos
· streamoff
· streampos
· streamsize
ios_base
· ios_base::ios_base
· ios_base::~ios_base
member classes:
· ios_base::failure
· ios_base::Init
member functions:
· ios_base::flags
· ios_base::getloc
· ios_base::imbue
· ios_base::iword
· ios_base::precision
· ios_base::pword
· ios_base::register_call...
· ios_base::setf
· ios_base::sync_with_stdio
· ios_base::unsetf
· ios_base::width
· ios_base::xalloc
member types:
· ios_base::event
· ios_base::event_callback
· ios_base::fmtflags
· ios_base::iostate
· ios_base::openmode
· ios_base::seekdir

-

ios_base::register_callback public member function
void register_callback ( event_callback fn, int index );

Register event callback function

Registers the function passed as argument fn such that when an event occurs it is automatically called. index is the argument passed to fn when it is called.

If more than one callback function is registered, these are called in opposite order of registration.

The callback function must be of type compatible with member type event_callback. And it is called by an expression equivalent to:

(*fn)(ev,*this,index)

where index is the index argument used when the callback function was registered, *this is a pointer to the calling object, and ev is an object of member enum type event indicating the type of event that happened. It can be one of the following member values:

valueevent triggered
copyfmt_event on a call to ios::copyfmt (at the moment where all format flags have been copied, but before the exception mask is)
erase_event on a call to the destructor (also called by at the beginning of 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 should use the ev parameter to identify which event triggered the function call and determine if it has to react.

Parameters

fn
Pointer to the function to be called. The event_callback member function type is defined as:

typedef void (* event_callback) (event ev, ios_base& ios, int index);

index
Integer value passed as parameter to the callback function. It can be used to specify some parameter value to be used within the callback function.

Return Value

none

Example

// stream callbacks
#include <iostream>
#include <fstream>
using namespace std;

void testfn (ios_base::event ev, ios_base& iosobj, int index)
{
  switch (ev)
  {
    case ios_base::copyfmt_event:
      cout << "copyfmt_event\n"; break;
    case ios_base::imbue_event:
      cout << "imbue_event\n"; break;
    case ios_base::erase_event:
      cout << "erase_event\n"; break;
  }
}

int main () {
  ofstream filestr;
  filestr.register_callback (testfn,0);
  filestr.imbue (cout.getloc());
  return 0;
}

The execution of this example will display something similar to:

imbue_event
erase_event

See also

ios_base::imbue Imbue locale (public member function)
ios::copyfmt Copy formatting information (public member functions)
ios_base::event Type to indicate event type (public member types)

© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us