Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F94209477
aka_event_handler_manager.hh
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Wed, Dec 4, 18:30
Size
4 KB
Mime Type
text/x-c++
Expires
Fri, Dec 6, 18:30 (1 d, 16 h)
Engine
blob
Format
Raw Data
Handle
22464089
Attached To
rAKA akantu
aka_event_handler_manager.hh
View Options
/**
* Copyright (©) 2010-2023 EPFL (Ecole Polytechnique Fédérale de Lausanne)
* Laboratory (LSMS - Laboratoire de Simulation en Mécanique des Solides)
*
* This file is part of Akantu
*
* Akantu is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* Akantu is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Akantu. If not, see <http://www.gnu.org/licenses/>.
*/
/* -------------------------------------------------------------------------- */
#ifndef AKANTU_AKA_EVENT_HANDLER_MANAGER_HH_
#define AKANTU_AKA_EVENT_HANDLER_MANAGER_HH_
/* -------------------------------------------------------------------------- */
#include "aka_common.hh"
/* -------------------------------------------------------------------------- */
#include <algorithm>
#include <list>
/* -------------------------------------------------------------------------- */
namespace
akantu
{
template
<
class
EventHandler
>
class
EventHandlerManager
{
private
:
using
priority_value
=
std
::
pair
<
EventHandlerPriority
,
EventHandler
*>
;
using
priority_list
=
std
::
list
<
priority_value
>
;
struct
KeyComp
{
bool
operator
()(
const
priority_value
&
a
,
const
priority_value
&
b
)
const
{
return
(
a
.
first
<
b
.
first
);
}
bool
operator
()(
const
priority_value
&
a
,
Int
b
)
const
{
return
(
a
.
first
<
b
);
}
};
/* ------------------------------------------------------------------------ */
/* Constructors/Destructors */
/* ------------------------------------------------------------------------ */
public
:
virtual
~
EventHandlerManager
()
=
default
;
/* ------------------------------------------------------------------------ */
/* Methods */
/* ------------------------------------------------------------------------ */
public
:
/// register a new EventHandler to the Manager. The register object
/// will then be informed about the events the manager observes.
void
registerEventHandler
(
EventHandler
&
event_handler
,
EventHandlerPriority
priority
=
_ehp_highest
)
{
auto
it
=
this
->
searchEventHandler
(
event_handler
);
if
(
it
!=
this
->
event_handlers
.
end
())
{
AKANTU_EXCEPTION
(
"This event handler was already registered (priority: "
<<
priority
<<
")"
);
}
auto
pos
=
std
::
lower_bound
(
this
->
event_handlers
.
begin
(),
this
->
event_handlers
.
end
(),
priority
,
KeyComp
());
this
->
event_handlers
.
insert
(
pos
,
std
::
make_pair
(
priority
,
&
event_handler
));
}
/// unregister a EventHandler object. This object will not be
/// notified anymore about the events this manager observes.
void
unregisterEventHandler
(
EventHandler
&
event_handler
)
{
auto
it
=
this
->
searchEventHandler
(
event_handler
);
if
(
it
==
this
->
event_handlers
.
end
())
{
AKANTU_EXCEPTION
(
"This event handler is not registered"
);
}
this
->
event_handlers
.
erase
(
it
);
}
/// Notify all the registered EventHandlers about the event that just occured.
template
<
class
Event
>
void
sendEvent
(
const
Event
&
event
)
{
for
(
auto
&
pair
:
this
->
event_handlers
)
{
pair
.
second
->
sendEvent
(
event
);
}
}
private
:
typename
priority_list
::
iterator
searchEventHandler
(
EventHandler
&
handler
)
{
auto
it
=
this
->
event_handlers
.
begin
();
auto
end
=
this
->
event_handlers
.
end
();
for
(;
it
!=
end
&&
it
->
second
!=
&
handler
;
++
it
)
{
;
}
return
it
;
}
/* ------------------------------------------------------------------------ */
/* Class Members */
/* ------------------------------------------------------------------------ */
private
:
/// list of the event handlers
priority_list
event_handlers
;
};
}
// namespace akantu
#endif
/* AKANTU_AKA_EVENT_HANDLER_MANAGER_HH_ */
Event Timeline
Log In to Comment