Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
biboumi
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
98
Issues
98
List
Boards
Labels
Service Desk
Milestones
Merge Requests
12
Merge Requests
12
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
louiz’
biboumi
Commits
5cca518c
Commit
5cca518c
authored
May 30, 2014
by
louiz’
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Timed events can have a name, and can be canceled based on their name
parent
a63faf6f
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
62 additions
and
7 deletions
+62
-7
src/test.cpp
src/test.cpp
+11
-0
src/utils/timed_events.cpp
src/utils/timed_events.cpp
+13
-5
src/utils/timed_events.hpp
src/utils/timed_events.hpp
+18
-2
src/utils/timed_events_manager.cpp
src/utils/timed_events_manager.cpp
+20
-0
No files found.
src/test.cpp
View file @
5cca518c
...
...
@@ -83,6 +83,17 @@ int main()
assert
(
te_manager
.
execute_expired_events
()
==
1
);
assert
(
te_manager
.
get_timeout
()
==
utils
::
no_timeout
);
// Test canceling events
te_manager
.
add_event
(
TimedEvent
(
std
::
chrono
::
steady_clock
::
now
()
+
100ms
,
[](){
},
"un"
));
te_manager
.
add_event
(
TimedEvent
(
std
::
chrono
::
steady_clock
::
now
()
+
200ms
,
[](){
},
"deux"
));
te_manager
.
add_event
(
TimedEvent
(
std
::
chrono
::
steady_clock
::
now
()
+
300ms
,
[](){
},
"deux"
));
assert
(
te_manager
.
get_timeout
()
>
0ms
);
assert
(
te_manager
.
size
()
==
3
);
assert
(
te_manager
.
cancel
(
"un"
)
==
1
);
assert
(
te_manager
.
size
()
==
2
);
assert
(
te_manager
.
cancel
(
"deux"
)
==
2
);
assert
(
te_manager
.
get_timeout
()
==
utils
::
no_timeout
);
/**
* Encoding
*/
...
...
src/utils/timed_events.cpp
View file @
5cca518c
#include <utils/timed_events.hpp>
TimedEvent
::
TimedEvent
(
std
::
chrono
::
steady_clock
::
time_point
&&
time_point
,
std
::
function
<
void
()
>
callback
)
:
std
::
function
<
void
()
>
callback
,
const
std
::
string
&
name
)
:
time_point
(
std
::
move
(
time_point
)),
callback
(
callback
),
repeat
(
false
),
repeat_delay
(
0
)
repeat_delay
(
0
),
name
(
name
)
{
}
TimedEvent
::
TimedEvent
(
std
::
chrono
::
milliseconds
&&
duration
,
std
::
function
<
void
()
>
callback
)
:
std
::
function
<
void
()
>
callback
,
const
std
::
string
&
name
)
:
time_point
(
std
::
chrono
::
steady_clock
::
now
()
+
duration
),
callback
(
callback
),
repeat
(
true
),
repeat_delay
(
std
::
move
(
duration
))
repeat_delay
(
std
::
move
(
duration
)),
name
(
name
)
{
}
...
...
@@ -22,7 +24,8 @@ TimedEvent::TimedEvent(TimedEvent&& other):
time_point
(
std
::
move
(
other
.
time_point
)),
callback
(
std
::
move
(
other
.
callback
)),
repeat
(
other
.
repeat
),
repeat_delay
(
std
::
move
(
other
.
repeat_delay
))
repeat_delay
(
std
::
move
(
other
.
repeat_delay
)),
name
(
std
::
move
(
other
.
name
))
{
}
...
...
@@ -52,3 +55,8 @@ void TimedEvent::execute()
{
this
->
callback
();
}
const
std
::
string
&
TimedEvent
::
get_name
()
const
{
return
this
->
name
;
}
src/utils/timed_events.hpp
View file @
5cca518c
...
...
@@ -25,9 +25,9 @@ public:
* An event the occurs only once, at the given time_point
*/
explicit
TimedEvent
(
std
::
chrono
::
steady_clock
::
time_point
&&
time_point
,
std
::
function
<
void
()
>
callback
);
std
::
function
<
void
()
>
callback
,
const
std
::
string
&
name
=
""
);
explicit
TimedEvent
(
std
::
chrono
::
milliseconds
&&
duration
,
std
::
function
<
void
()
>
callback
);
std
::
function
<
void
()
>
callback
,
const
std
::
string
&
name
=
""
);
explicit
TimedEvent
(
TimedEvent
&&
);
~
TimedEvent
();
...
...
@@ -43,6 +43,7 @@ public:
*/
std
::
chrono
::
milliseconds
get_timeout
()
const
;
void
execute
();
const
std
::
string
&
get_name
()
const
;
private:
/**
...
...
@@ -62,6 +63,12 @@ private:
* if repeat is true. Otherwise it is ignored.
*/
const
std
::
chrono
::
milliseconds
repeat_delay
;
/**
* A name that is used to identify that event. If you want to find your
* event (for example if you want to cancel it), the name should be
* unique.
*/
const
std
::
string
name
;
TimedEvent
(
const
TimedEvent
&
)
=
delete
;
TimedEvent
&
operator
=
(
const
TimedEvent
&
)
=
delete
;
...
...
@@ -99,6 +106,15 @@ public:
* Returns the number of executed events.
*/
std
::
size_t
execute_expired_events
();
/**
* Remove (and thus cancel) all the timed events with the given name.
* Returns the number of canceled events.
*/
std
::
size_t
cancel
(
const
std
::
string
&
name
);
/**
* Return the number of managed events.
*/
std
::
size_t
size
()
const
;
private:
std
::
list
<
TimedEvent
>
events
;
...
...
src/utils/timed_events_manager.cpp
View file @
5cca518c
...
...
@@ -53,3 +53,23 @@ std::size_t TimedEventsManager::execute_expired_events()
return
count
;
}
std
::
size_t
TimedEventsManager
::
cancel
(
const
std
::
string
&
name
)
{
std
::
size_t
res
=
0
;
for
(
auto
it
=
this
->
events
.
begin
();
it
!=
this
->
events
.
end
();)
{
if
(
it
->
get_name
()
==
name
)
{
it
=
this
->
events
.
erase
(
it
);
res
++
;
}
else
++
it
;
}
return
res
;
}
std
::
size_t
TimedEventsManager
::
size
()
const
{
return
this
->
events
.
size
();
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment