[d4ba3f] | 1 | /*
|
---|
| 2 | * EntityObserver_impl.hpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: Dec 29, 2015
|
---|
| 5 | * Author: heber
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 |
|
---|
| 9 | #ifndef ENTITYOBSERVER_IMPL_HPP_
|
---|
| 10 | #define ENTITYOBSERVER_IMPL_HPP_
|
---|
| 11 |
|
---|
| 12 | // include config.h
|
---|
| 13 | #ifdef HAVE_CONFIG_H
|
---|
| 14 | #include <config.h>
|
---|
| 15 | #endif
|
---|
| 16 |
|
---|
| 17 | #include <algorithm>
|
---|
| 18 |
|
---|
| 19 | #include <boost/bind.hpp>
|
---|
| 20 |
|
---|
| 21 | #include "CodePatterns/Observer/Channels.hpp"
|
---|
| 22 |
|
---|
| 23 | /** Private constructor of class EntityObserver.
|
---|
| 24 | *
|
---|
| 25 | */
|
---|
| 26 | template <class T>
|
---|
| 27 | EntityObserver<T>::EntityObserver(
|
---|
| 28 | const Observable::channels_t &_Channels,
|
---|
| 29 | const std::string &_name) :
|
---|
| 30 | Relay(_name),
|
---|
| 31 | EntityChannels(_Channels)
|
---|
| 32 | {
|
---|
| 33 | Channels *OurChannel = new Channels();
|
---|
| 34 | Observable::insertNotificationChannel( std::make_pair(static_cast<Observable *>(this), OurChannel) );
|
---|
| 35 | std::for_each(EntityChannels.begin(), EntityChannels.end(),
|
---|
| 36 | boost::bind(&Channels::addChannel, OurChannel, _1));
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | /** Function to call when a new atom has been created.
|
---|
| 40 | *
|
---|
| 41 | * @param res
|
---|
| 42 | */
|
---|
| 43 | template <class T>
|
---|
| 44 | void EntityObserver<T>::Inserted(Observable *res)
|
---|
| 45 | {
|
---|
| 46 | ASSERT( dynamic_cast<Observable *>(res) != NULL,
|
---|
| 47 | "EntityObserver<T>::Inserted - type is not derived from Observable.");
|
---|
| 48 | std::for_each(EntityChannels.begin(), EntityChannels.end(),
|
---|
| 49 | boost::bind(&Observable::signOn, res, this, _1, GlobalObservableInfo::PriorityDefault));
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | /** Function to call when an atom is about to get deleted.
|
---|
| 53 | *
|
---|
| 54 | * @param res
|
---|
| 55 | */
|
---|
| 56 | template <class T>
|
---|
| 57 | void EntityObserver<T>::Removed(Observable *res)
|
---|
| 58 | {
|
---|
| 59 | std::for_each(EntityChannels.begin(), EntityChannels.end(),
|
---|
| 60 | boost::bind(&Observable::signOff, res, this, _1));
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | #endif /* ENTITYOBSERVER_IMPL_HPP_ */
|
---|