XGCa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
step_trigger.hpp
Go to the documentation of this file.
1 #ifndef STEP_TRIGGER_HPP
2 #define STEP_TRIGGER_HPP
3 
4 class StepTrigger{
5  bool initialized;
6  int period;
7  int first_step;
8 
9  public:
10 
12  : initialized(false) {}
13 
14  StepTrigger(int period, int first_step_in=-1)
15  : initialized(true),
16  period(period),
17  // If no value is provided for the optional first_step_in argument, then the first step is the period
18  // i.e. if period is 2, then the first call is on step 2
19  first_step(first_step_in==-1 ? period : first_step_in) {}
20 
21  /* Returns whether or not the action is triggered this step
22  * */
23  bool is_triggered(int step) const{
24  int steps_since_first = step - first_step;
25  return (initialized && (step>=first_step && steps_since_first%period==0));
26  }
27 
28  /* Returns whether or not we have passed the first trigger yet
29  * */
30  bool has_been_triggered(int step) const{
31  return (initialized && step>=first_step);
32  }
33 
34  /* Returns period
35  * */
36  int get_period() const{
37  return period;
38  }
39 
45  int trigger_count(int step) const{
46  if (initialized){
47  // C++ rounds down by default
48  return ( (step-first_step)/period );
49  } else {
50  return (0);
51  }
52  }
53 };
54 
55 #endif
bool has_been_triggered(int step) const
Definition: step_trigger.hpp:30
int period
Number of steps between trigger.
Definition: step_trigger.hpp:6
bool initialized
Definition: step_trigger.hpp:5
StepTrigger()
Definition: step_trigger.hpp:11
int first_step
First step it can be called.
Definition: step_trigger.hpp:7
int trigger_count(int step) const
Definition: step_trigger.hpp:45
StepTrigger(int period, int first_step_in=-1)
Definition: step_trigger.hpp:14
bool is_triggered(int step) const
Definition: step_trigger.hpp:23
int get_period() const
Definition: step_trigger.hpp:36
Definition: step_trigger.hpp:4