SIMLIB/C++  3.07
sampler.cc
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 //! \file sampler.cc Periodic sampler
3 //
4 // Copyright (c) 1994-2004 Petr Peringer
5 //
6 // This library is licensed under GNU Library GPL. See the file COPYING.
7 //
8 
9 //
10 // description:
11 // implementation of class Sampler
12 // - calls global function (function) periodicaly (step)
13 // - automatic initialization by Init() (set on)
14 // - automatic activation by Run() (if on)
15 // - Start/Stop
16 //
17 
18 ////////////////////////////////////////////////////////////////////////////
19 // interface
20 //
21 
22 #include "simlib.h"
23 #include "internal.h"
24 
25 ////////////////////////////////////////////////////////////////////////////
26 // implementation
27 //
28 
29 namespace simlib3 {
30 
32 
33 // init
35 
36 ////////////////////////////////////////////////////////////////////////////
37 // constructor
38 //
39 Sampler::Sampler(void(*pf)(), double dt) :
40  Next(0),
41  function(pf),
42  last(-1),
43  step((dt>0.0) ? dt : 0.0),
44  on(true)
45 {
46  Dprintf(("Sampler::Sampler(%p,%g)", pf, dt));
47  if( First==0 ) { // first created sampler
48  INSTALL_HOOK( SamplerInit, Sampler::InitAll );
49  INSTALL_HOOK( SamplerAct, Sampler::ActivateAll );
50  }
51  // insert into list:
52  Next = First;
53  First = this;
54 }
55 
56 ////////////////////////////////////////////////////////////////////////////
57 // destructor
58 //
60 {
61  Dprintf(("Sampler::~Sampler() // \"%p\" ", function));
62  // remove from list:
63  if (this==First)
64  First = Next;
65  else
66  {
67  Sampler *i;
68  for(i=First; i && i->Next!=this; i=i->Next) { /*empty*/ }
69  if (i) i->Next = this->Next;
70  }
71  if( First==0 ) {
72  INSTALL_HOOK( SamplerInit, 0 );
73  INSTALL_HOOK( SamplerAct, 0 );
74  }
75 }
76 
77 ////////////////////////////////////////////////////////////////////////////
78 // Output
79 //
80 void Sampler::Output() const
81 {
82  Print("Sampler%p, function=%p, last=%g, on=%i\n",
83  this, function, last, on);
84 }
85 
86 
87 ////////////////////////////////////////////////////////////////////////////
88 // Sample::Behavior --- call function and reactivate sample event
89 //
91  Dprintf(("Sampler::Behavior()"));
92  Sample(); // call of global function
93  if( on && step > 0.0 )
94  Activate( Time + step ); // schedule next sample
95  else
96  Passivate(); // should be passivated before ###????
97 }
98 
99 
100 ////////////////////////////////////////////////////////////////////////////
101 // Sampler::InitAll --- init all samplers - called by Init()
102 // static
104  for( Sampler *i = First; i; i = i->Next ) {
105  i->last = -1;
106  i->on = true;
107  }
108 }
109 
110 ////////////////////////////////////////////////////////////////////////////
111 // ActivateAll - activate all Samplers - called by Run()
112 // static
114 {
115  for(Sampler *i=First; i; i=i->Next)
116  {
117  i->last = -1; // really needed ???? ######
118  if(i->on) // activate this one
119  i->Activate();
120  }
121 }
122 
123 ////////////////////////////////////////////////////////////////////////////
124 // Start() -- start of sampling
125 //
127 {
128  on=true;
129  Activate();
130 }
131 
132 ////////////////////////////////////////////////////////////////////////////
133 // Stop -- end of sampling
134 //
136 {
137  on=false;
138  if(last==Time) // was sample at this time
139  Passivate();
140  else
141  Activate();
142 }
143 
144 ////////////////////////////////////////////////////////////////////////////
145 // Sample -- explicit sample
146 //
148 {
149  if(function)
150  function(); // call global function
151  last = Time;
152 }
153 
154 ////////////////////////////////////////////////////////////////////////////
155 // SetStep -- step of sampling added 13.5.95
156 //
157 double Sampler::SetStep(double dt)
158 {
159  double laststep=step;
160  step = (dt>0.0) ? dt : 0.0;
161  return laststep;
162 }
163 
164 } // end of SAMPLER.CPP
165 
static void InitAll()
initialize all samplers (Init)
Definition: sampler.cc:103
bool on
switch on/off
Definition: simlib.h:513
void Stop()
sample + stop
Definition: sampler.cc:135
double step
step of sampling
Definition: simlib.h:512
void Sample()
performs sample (function call)
Definition: sampler.cc:147
virtual ~Sampler()
Definition: sampler.cc:59
double last
last sample time – prevents sample duplication
Definition: simlib.h:511
Sampler(void(*pf)(), double dt=0.0)
Definition: sampler.cc:39
virtual void Passivate()
deactivation
Definition: entity.cc:68
static Sampler * First
Definition: simlib.h:507
int Print(const char *fmt,...)
for Output methods, can be redirected
Definition: print.cc:92
objects of this class call global function periodically (typicaly used for output of continuous model...
Definition: simlib.h:506
Implementation of class CalendarList interface is static - using global functions in SQS namespace...
Definition: algloop.cc:32
const double & Time
model time (is NOT the block)
Definition: run.cc:48
virtual void Behavior() override
behavior description
Definition: sampler.cc:90
static void ActivateAll()
start all samplers (Run)
Definition: sampler.cc:113
Internal header file for SIMLIB/C++.
void Start()
start + sample
Definition: sampler.cc:126
Main SIMLIB/C++ interface.
#define INSTALL_HOOK(name, function)
Definition: internal.h:255
SIMLIB_IMPLEMENTATION
Definition: algloop.cc:34
double SetStep(double dt=0.0)
change step
Definition: sampler.cc:157
virtual void Output() const override
print object to default output
Definition: sampler.cc:80
void Activate()
activate now
Definition: simlib.h:408
#define Dprintf(f)
Definition: internal.h:100
Sampler * Next
Definition: simlib.h:508