events.h
Go to the documentation of this file.
#ifndef EVENTS_H
#define EVENTS_H
#include <itpp/base/itassert.h>
#if (defined(_MSC_VER) && defined(ITPP_SHARED_LIB) && !(defined(itpp_EXPORTS) || defined(itpp_debug_EXPORTS)))
#ifndef ITPP_PROTOCOL_EXCLUDED
#define ITPP_PROTOCOL_EXCLUDED
#pragma message( "PROTOCOL definitions are not available for MSVC shared builds" )
#endif
#else
#include <queue>
#include <deque>
namespace itpp
{
// typedef long double Ttype; //!< 128-bit floating point time
typedef double Ttype;
// typedef long unsigned int Ttype; //!< 64-bit unsigned integer time
class Event_Queue;
class Base_Event;
class Base_Signal;
class Base_Event
{
public:
friend class Base_Signal;
friend class Event_Queue;
friend struct Compare_Base_Event_Times;
Base_Event(const Ttype delta_time) { // The event will occur in 'delta_time' time units from now!
it_assert(delta_time >= 0, "Only causal simulations are possible");
active = true;
delta_t = delta_time;
expire_t = 0; // Will be set correctly upon calling Event_Queue::add().
id = global_id++;
}
virtual ~Base_Event() {}
void cancel() { active = false; }
protected:
virtual void exec(void) = 0;
Ttype delta_t;
Ttype expire_t;
bool active;
unsigned long long int id;
static unsigned long long int global_id;
};
struct Compare_Base_Event_Times {
bool operator()(Base_Event *event1, Base_Event *event2) {
if (event1->expire_t == event2->expire_t) // Equal expire times.
return (event1->id > event2->id); // Base comparison on the event id.
else
return (event1->expire_t > event2->expire_t); // Different expire times. Regular comparison.
}
};
class Event_Queue
{
public:
friend class Base_Signal;
Event_Queue() {}
~Event_Queue() {}
static void add(Base_Event *e);
static Ttype now() {return t;}
static void start();
static void stop();
static void clear();
protected:
//static void cancel_all(Base_Signal *s);
private:
typedef std::deque<Base_Event*, std::allocator< Base_Event* > >::iterator Base_Event_Iterator;
static void _run();
static bool keep_running;
static Ttype t; // Current time.
static std::priority_queue < Base_Event*,
std::deque<Base_Event*, std::allocator<Base_Event*> >,
Compare_Base_Event_Times > event_queue; // Queue for the Events.
};
template <class ObjectType>
class Event : public Base_Event
{
public:
Event(ObjectType *object_pointer, void (ObjectType::*object_function_pointer)(), const Ttype delta_time) : Base_Event(delta_time) {
po = object_pointer;
pm = object_function_pointer;
}
virtual ~Event() {}
virtual void exec(void) {(*po.*pm)(); }
private:
void (ObjectType::*pm)(); // Pointer to class member function to be executed on event expire.
ObjectType *po; // Pointer to object who's member function is to be executed on event expire.
};
template <class ObjectType, class DataType> class Data_Event : public Base_Event
{
public:
Data_Event(ObjectType *object_pointer,
void (ObjectType::*object_function_pointer)(DataType data),
DataType data, const Ttype delta_time) : Base_Event(delta_time) {
po = object_pointer;
pm = object_function_pointer;
u = data;
}
virtual ~Data_Event() {}
virtual void exec(void) {
(*po.*pm)(u);
}
private:
void (ObjectType::*pm)(DataType data); // Pointer to class member function to be executed on event expire.
ObjectType* po; // Pointer to object who's member function is to be executed on event expire.
DataType u; // User data.
};
} // namespace itpp
#endif
#endif // #ifndef EVENTS_H
itpp::Data_Event
An Event class that executes a function with some data as input when the event expires.
Definition: events.h:189
itpp::Event::~Event
virtual ~Event()
Destructor.
Definition: events.h:174
itpp::Data_Event::exec
virtual void exec(void)
Execute (call) the assigned function with user data.
Definition: events.h:205
itpp::Base_Event
Base Event Class.
Definition: events.h:68
itpp::Base_Event::global_id
static unsigned long long int global_id
ADD DOCUMENTATION HERE.
Definition: events.h:104
itpp::Ttype
double Ttype
64-bit floating point time
Definition: events.h:54
itpp::Compare_Base_Event_Times
Compare to events, Returns true if expire time of event1 is larger than the expire time of event2...
Definition: events.h:108
itpp::Event_Queue::now
static Ttype now()
Return current time.
Definition: events.h:139
it_assert
#define it_assert(t, s)
Abort if t is not true.
Definition: itassert.h:94
itpp::Base_Event::delta_t
Ttype delta_t
ADD DOCUMENTATION HERE.
Definition: events.h:96
itpp::Base_Event::expire_t
Ttype expire_t
ADD DOCUMENTATION HERE.
Definition: events.h:98
itpp::Base_Event::~Base_Event
virtual ~Base_Event()
Destructor.
Definition: events.h:87
itpp::Data_Event::Data_Event
Data_Event(ObjectType *object_pointer, void(ObjectType::*object_function_pointer)(DataType data), DataType data, const Ttype delta_time)
Construct an Event to expire delta_time from now by calling the function (*object_pointer.*object_function_pointer)(data)
Definition: events.h:193
itpp::Event_Queue::~Event_Queue
~Event_Queue()
Destructor.
Definition: events.h:134
itpp::Event
An Event class that executes a function when the event expires.
Definition: events.h:164
itpp
itpp namespace
Definition: itmex.h:36
itpp::Data_Event::~Data_Event
virtual ~Data_Event()
Destructor.
Definition: events.h:202
itpp::Base_Event::exec
virtual void exec(void)=0
ADD DOCUMENTATION HERE.
itassert.h
Error handling functions - header file.
itpp::Compare_Base_Event_Times::operator()
bool operator()(Base_Event *event1, Base_Event *event2)
ADD DOCUMENTATION HERE.
Definition: events.h:110
itpp::Base_Event::active
bool active
ADD DOCUMENTATION HERE.
Definition: events.h:100
itpp::Event::exec
virtual void exec(void)
Execute (call) the assigned function.
Definition: events.h:177
itpp::Base_Event::id
unsigned long long int id
ADD DOCUMENTATION HERE.
Definition: events.h:102
itpp::Event_Queue
Event Queue class.
Definition: events.h:126
itpp::Event_Queue::Event_Queue
Event_Queue()
Constructor.
Definition: events.h:132
itpp::Base_Event::cancel
void cancel()
Cancel an event.
Definition: events.h:90
itpp::Base_Event::Base_Event
Base_Event(const Ttype delta_time)
Schedule an event at time delta_time from now.
Definition: events.h:78
itpp::Event::Event
Event(ObjectType *object_pointer, void(ObjectType::*object_function_pointer)(), const Ttype delta_time)
Construct an Event to expire delta_time from now by calling the function (*object_pointer.*object_function_pointer)()
Definition: events.h:168

Generated on Sat Sep 30 2017 07:04:09 for IT++ by Doxygen 1.8.13