Loading...
Searching...
No Matches
callback_service.h
Go to the documentation of this file.
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2019 John Wellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#ifndef ETL_CALLBACK_SERVICE_INCLUDED
#define ETL_CALLBACK_SERVICE_INCLUDED
#include "platform.h"
#include "array.h"
#include "function.h"
#include "nullptr.h"
#include "static_assert.h"
namespace etl
{
//***************************************************************************
//***************************************************************************
template <size_t Range, size_t Offset = 0U>
class callback_service
{
public:
//*************************************************************************
//*************************************************************************
callback_service()
: unhandled_callback(*this)
, p_unhandled(ETL_NULLPTR)
{
lookup.fill(&unhandled_callback);
}
callback_service(const callback_service&) ETL_DELETE;
callback_service& operator=(const callback_service&) ETL_DELETE;
#if ETL_USING_CPP11
callback_service(callback_service&&) ETL_DELETE;
callback_service& operator=(callback_service&&) ETL_DELETE;
#endif
//*************************************************************************
//*************************************************************************
template <size_t Id>
void register_callback(etl::ifunction<size_t>& callback)
{
ETL_STATIC_ASSERT(Id < (Offset + Range), "Callback Id out of range");
ETL_STATIC_ASSERT(Id >= Offset, "Callback Id out of range");
lookup[Id - Offset] = &callback;
}
//*************************************************************************
//*************************************************************************
void register_callback(size_t id, etl::ifunction<size_t>& callback)
{
if ((id >= Offset) && (id < (Offset + Range)))
{
lookup[id - Offset] = &callback;
}
}
//*************************************************************************
//*************************************************************************
void register_unhandled_callback(etl::ifunction<size_t>& callback)
{
p_unhandled = &callback;
}
//*************************************************************************
//*************************************************************************
template <size_t Id>
void callback()
{
ETL_STATIC_ASSERT(Id < (Offset + Range), "Callback Id out of range");
ETL_STATIC_ASSERT(Id >= Offset, "Callback Id out of range");
(*lookup[Id - Offset])(Id);
}
//*************************************************************************
//*************************************************************************
void callback(size_t id)
{
if ((id >= Offset) && (id < (Offset + Range)))
{
(*lookup[id - Offset])(id);
}
else
{
unhandled(id);
}
}
private:
//*************************************************************************
//*************************************************************************
void unhandled(size_t id)
{
if (p_unhandled != ETL_NULLPTR)
{
(*p_unhandled)(id);
}
}
etl::function_mp<callback_service<Range, Offset>, size_t, &callback_service<Range, Offset>::unhandled> unhandled_callback;
etl::ifunction<size_t>* p_unhandled;
etl::array<etl::ifunction<size_t>*, Range> lookup;
};
} // namespace etl
#endif
array.h
etl::callback_service
Definition callback_service.h:50
etl::callback_service::callback_service
callback_service()
Definition callback_service.h:57
etl::callback_service::register_callback
void register_callback(size_t id, etl::ifunction< size_t > &callback)
Definition callback_service.h:93
etl::callback_service::register_callback
void register_callback(etl::ifunction< size_t > &callback)
Definition callback_service.h:79
etl::callback_service::register_unhandled_callback
void register_unhandled_callback(etl::ifunction< size_t > &callback)
Definition callback_service.h:105
etl::callback_service::callback
void callback()
Definition callback_service.h:116
etl::callback_service::callback
void callback(size_t id)
Definition callback_service.h:128
function.h
etl::ifunction
Definition function.h:54
etl
Definition absolute.h:40
nullptr.h
platform.h