Jive reference manual
Event.cpp
/************************************************************************
*
* This example shows how to use the Event class
*
************************************************************************/
#include <jem/base/System.h>
#include <jem/util/Event.h>
using namespace jem;
using namespace jem::io;
using namespace jem::util;
//-----------------------------------------------------------------------
// printValue
//-----------------------------------------------------------------------
void printValue ( int i )
{
print ( System::out(), i, '\n' );
}
//-----------------------------------------------------------------------
// class Listener
//-----------------------------------------------------------------------
class Listener : public Object
{
public:
Listener ();
void printValue
( double f );
void printHello ();
protected:
virtual ~Listener ();
};
Listener::Listener ()
{}
Listener::~Listener ()
{}
void Listener::printValue ( double f )
{
print ( System::out(), f, endl );
}
void Listener::printHello ()
{
print ( System::out(), "Hello\n" );
}
//-----------------------------------------------------------------------
// class Signaller
//-----------------------------------------------------------------------
class Signaller : public Object
{
public:
Event<> zeroReached;
Event<int,int> newValue;
Signaller ();
void setAndDecr
( int i );
void destroy ();
static
Ref<Signaller> instance;
protected:
virtual ~Signaller ();
private:
int value_;
};
Ref<Signaller> Signaller::instance;
Signaller::Signaller () : value_ ( 0 )
{}
Signaller::~Signaller ()
{
print ( System::out(), "Bye Bye ...\n" );
}
void Signaller::setAndDecr ( int i )
{
int oldValue = value_;
value_ = (--i);
if ( value_ <= 0 )
{
zeroReached.emit ();
}
else
{
newValue.emit ( value_, oldValue );
}
}
void Signaller::destroy ()
{
instance = NIL;
}
//-----------------------------------------------------------------------
// run
//-----------------------------------------------------------------------
int run()
{
Ref<Listener> listener = newInstance<Listener> ();
Signaller::instance = newInstance<Signaller> ();
{
Ref<Signaller> s = Signaller::instance;
connect ( s->zeroReached, listener, & Listener::printHello );
connect ( s->zeroReached, s, & Signaller::destroy );
connect ( s->newValue, s, & Signaller::setAndDecr );
connect ( s->newValue, listener, & Listener::printValue );
connect ( s->newValue, & printValue );
}
Signaller::instance->setAndDecr ( 10 );
return 0;
}
//-----------------------------------------------------------------------
// main
//-----------------------------------------------------------------------
int main ()
{
return System::exec ( & run );
}