SIMLIB/C++ 3.09
Loading...
Searching...
No Matches
entity.cc
Go to the documentation of this file.
1/////////////////////////////////////////////////////////////////////////////
2//! \file entity.cc Active entities base class
3//
4// Copyright (c) 1991-2016 Petr Peringer
5//
6// This library is licensed under GNU Library GPL. See the file COPYING.
7//
8
9// Entity is base class of the entity hierarchy (Process, Event)
10
11////////////////////////////////////////////////////////////////////////////
12// interface
13//
14
15#include "simlib.h"
16#include "internal.h"
17
18////////////////////////////////////////////////////////////////////////////
19// implementation
20//
21
22namespace simlib3 {
23
25
26/// current number of entities in model
27static unsigned long SIMLIB_Entity_Count = 0L; // # of entities in model
28/// serial number of created entity
29unsigned long Entity::_Number = 0L; // # of entity creations
30
31////////////////////////////////////////////////////////////////////////////
32/// constructor
34 _Ident(SIMLIB_Entity_Count++), // unique identification
35 _MarkTime(0.0),
36 _SPrio(0),
37 Priority(p),
38 _evn(0) // pointer to calendar item
39{
40 _Number++; // # of entities
41 Dprintf(("Entity#%lu{%p}::Entity(%d)", _Ident, this, p));
42}
43
44////////////////////////////////////////////////////////////////////////////
45/// destructor
47 Dprintf(("Entity#%lu{%p}::~Entity()", _Ident, this));
48 if (!Idle()) {
49 SQS::Get(this); // remove from calendar
50// _warning(DeletingActive); // TODO:can be important? if sim SIMLIB_error else _warn
51 }
52 Entity::_Number--; // # of entities in model
53}
54
55/// entity activation at given time
56void Entity::Activate(double t)
57{
58 if (!Idle()) { // rescheduling
59 SQS::Get(this); // remove from calendar
60 }
61 SQS::ScheduleAt(this,t);
62}
63
64
65////////////////////////////////////////////////////////////////////////////
66// Passivate - deactivation of process (entity)
67//
69{
70 if(!Idle()) // if scheduled
71 SQS::Get(this); // remove from calendar
72}
73
74
75////////////////////////////////////////////////////////////////////////////
76// Into --- inserting into queue (TODO: problem with continuation)
77//
78#if 0
79void Entity::Into(Queue *q)
80{
82 if(where()!=0) Out(); // if already in queue then remove ### +warning ???
83 q->Insert(this); // insert
84}
85#endif
86
87////////////////////////////////////////////////////////////////////////////
88// Out - leaving queue
89//
91{
92 Link::Out(); // equal
93}
94
95////////////////////////////////////////////////////////////////////////////
96/// passivate and destroy entity
98{
99 Dprintf(("%s.Terminate()",Name().c_str()));
100 if(!Idle())
101 SQS::Get(this); // remove from calendar
102
103 if(isAllocated() && this != SIMLIB_Current)
104 delete this; // destroy entity (if not currently running Behavior)
105}
106
107#if 0
108////////////////////////////////////////////////////////////////////////////
109/// get name - either explicit or default "Entity#{ptr}"
110const char *Entity::Name() const
111{
112 const char *name = SimObject::Name();
113 if(*name) return name; // return explicit name
114 else return SIMLIB_create_tmp_name("Entity%lu{%p}", _Ident, this);
115}
116#endif
117
118} // namespace
119
Test t(F)
EntityPriority_t Priority_t
Definition: simlib.h:397
Entity(Priority_t p=DEFAULT_PRIORITY)
constructor
Definition: entity.cc:33
bool Idle()
entity activation is not scheduled in calendar
Definition: simlib.h:412
void Activate()
activate now
Definition: simlib.h:408
virtual ~Entity()
destructor
Definition: entity.cc:46
static unsigned long _Number
current number of entities
Definition: simlib.h:377
virtual void Terminate()=0
end Behavior() and remove entity
Definition: entity.cc:97
virtual void Out() override
remove entity from queue
Definition: entity.cc:90
virtual void Passivate()
deactivation
Definition: entity.cc:68
unsigned long _Ident
unique identification number of entity
Definition: simlib.h:378
priority queue
Definition: simlib.h:685
virtual void Insert(Entity *e)
Definition: queue.cc:50
bool isAllocated() const
Definition: simlib.h:318
virtual std::string Name() const
get object name
Definition: object.cc:134
@ QueueRefError
Definition: errors.h:36
Internal header file for SIMLIB/C++.
#define Dprintf(f)
Definition: internal.h:100
void Get(Entity *e)
remove selected entity activation record from calendar
Definition: calendar.cc:1308
void ScheduleAt(Entity *e, double t)
schedule entity e at given time t using scheduling priority from e
Definition: calendar.cc:1287
Implementation of class CalendarList interface is static - using global functions in SQS namespace.
Definition: algloop.cc:32
void SIMLIB_error(const enum _ErrEnum N)
print error message and abort program
Definition: error.cc:38
SIMLIB_IMPLEMENTATION
Definition: algloop.cc:34
Entity * SIMLIB_Current
Definition: run.cc:53
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
static unsigned long SIMLIB_Entity_Count
current number of entities in model
Definition: entity.cc:27
Main SIMLIB/C++ interface.