-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventBuffer.cpp
More file actions
117 lines (102 loc) · 2.67 KB
/
EventBuffer.cpp
File metadata and controls
117 lines (102 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "EventBuffer.h"
namespace eventbuffer
{
EventBuffer::EventBuffer()
{
//store start time
start_time = time(nullptr);
}
/*
* @brief add a event to the event log moving the indices of
* the circular buffer
* @param
* @return pointer to an iterator object
*/
void EventBuffer::add(event_t evt)
{
// increment current size
if(current_size < EVENT_LOG_SIZE)
current_size++;
// move end location to the next element
end_loc = (end_loc + 1) % current_size;
// move start buffer if they coincide
if(end_loc == start_loc)
start_loc = (start_loc + 1) % current_size;
// store current event in the event buffer
eventlog[end_loc].event = evt;
eventlog[end_loc].when = time(nullptr);
}
/*
* @brief return date at which this class has been instantiated
* @param
* @return value of the start date
*/
time_t EventBuffer::getStartTime()
{
return start_time;
}
EventBuffer::iterator * EventBuffer::getIterator()
{
return new EventBuffer::iterator(this);
}
EventBuffer::iterator::iterator(EventBuffer* evtb)
{
inst = evtb;
iter_loc = inst->start_loc;
}
/*
* @brief return date of the event pointed by the current position of the iterator
* @param
* @return value of the date of the event
*/
time_t EventBuffer::iterator::getDate()
{
return inst->eventlog[iter_loc].when;
}
/*
* @brief return event pointed by the current position of the iterator
* @param
* @return value of the event enum
*/
event_t EventBuffer::iterator::getEvent()
{
return inst->eventlog[iter_loc].event;
}
/*
* @brief move iterator to the next object.
* @param
* @return FALSE if iterator is at the end of the array
*/
bool EventBuffer::iterator::next()
{
if(iter_loc == inst->end_loc)
{
return false;
}
iter_loc = (iter_loc + 1) % inst->current_size;
return true;
}
/*
* @brief move iterator to the previous object.
* @param
* @return FALSE if iterator is at the begin of the array
*/
bool EventBuffer::iterator::prev()
{
if(iter_loc == inst->start_loc)
{
return false;
}
iter_loc = (iter_loc - 1) % inst->current_size;
return true;
}
/*
* @brief return true if empty.
* @param
* @return TRUE if buffer is empty
*/
bool EventBuffer::iterator::empty()
{
return (inst->current_size > 0);
}
}