SIMLIB/C++  3.07
event.cc
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 //! \file event.cc Discrete events base class
3 //
4 // Copyright (c) 1991-2004 Petr Peringer
5 //
6 // This library is licensed under GNU Library GPL. See the file COPYING.
7 //
8 
9 // implementation of class Event
10 
11 ////////////////////////////////////////////////////////////////////////////
12 // interface
13 //
14 
15 #include "simlib.h"
16 #include "internal.h"
17 
18 ////////////////////////////////////////////////////////////////////////////
19 // implementation
20 //
21 
22 namespace simlib3 {
23 
25 
26 ////////////////////////////////////////////////////////////////////////////
27 /// constructor, default priority if not specified
29 {
30  Dprintf(("Event::Event(%u)", p));
31 }
32 
33 ////////////////////////////////////////////////////////////////////////////
34 /// destructor
36 {
37  Dprintf(("Event::~Event()"));
38 }
39 
40 ////////////////////////////////////////////////////////////////////////////
41 /// activation method, called from simulation control algorithm
42 void Event::_Run() noexcept
43 {
44  Behavior();
45  if(Idle()) // not scheduled
46  Terminate();
47 }
48 
49 ////////////////////////////////////////////////////////////////////////////
50 /// passivate event, it will be destroyed if not activated later in Behavior()
51 /// <br> Should not be used. It is there only for backwards compatibility.
53 {
54  Dprintf(("%s.Terminate()",Name().c_str()));
55  if(!Idle()) // if scheduled
56  SQS::Get(this); // remove from calendar
57  if(isAllocated() && this != SIMLIB_Current)
58  delete this; // destroy entity (if not currently running Behavior)
59 }
60 
61 // Event activation (scheduling) at time t
62 void Event::Activate(double t)
63 {
64  Dprintf(("Event#%lu.Activate(%g)",_Ident,t));
66 }
67 
68 #if 1
69 ////////////////////////////////////////////////////////////////////////////
70 /// get name of event. It is generic "Event#" if not explicitly named
71 std::string Event::Name() const
72 {
73  const std::string name = SimObject::Name();
74  if(!name.empty()) return name; // has explicit name
75  else return SIMLIB_create_tmp_name("Event#%lu", _Ident);
76 }
77 #endif
78 
79 } // namespace
80 
virtual void Terminate() override
passivate event, it will be destroyed if not activated later in Behavior() Should not be used...
Definition: event.cc:52
virtual std::string Name() const override
name of object
Definition: event.cc:71
Event(Priority_t p=DEFAULT_PRIORITY)
constructor, default priority if not specified
Definition: event.cc:28
void Get(Entity *e)
remove selected entity activation record from calendar
Definition: calendar.cc:1308
bool isAllocated() const
Definition: simlib.h:318
Implementation of class CalendarList interface is static - using global functions in SQS namespace...
Definition: algloop.cc:32
unsigned long _Ident
unique identification number of entity
Definition: simlib.h:378
std::string SIMLIB_create_tmp_name(const char *fmt,...)
printf-like function to create temporary name (the length of temporary names is limited) used only ...
Definition: name.cc:80
abstract base class for active entities (Process, Event) instances of derived classes provide Behavio...
Definition: simlib.h:375
Test t(F)
Internal header file for SIMLIB/C++.
virtual void Behavior()=0
behavior description
virtual void _Run() noexcept override
activation method, called from simulation control algorithm
Definition: event.cc:42
Main SIMLIB/C++ interface.
virtual std::string Name() const
get object name
Definition: object.cc:134
SIMLIB_IMPLEMENTATION
Definition: algloop.cc:34
Entity * SIMLIB_Current
Definition: run.cc:53
bool Idle()
entity activation is not scheduled in calendar
Definition: simlib.h:412
void Activate()
activate now
Definition: simlib.h:408
EntityPriority_t Priority_t
Definition: simlib.h:397
#define Dprintf(f)
Definition: internal.h:100
virtual ~Event()
destructor
Definition: event.cc:35