Loading...
Searching...
No Matches
successor.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) 2021 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_SUCCESSOR_INCLUDED
#define ETL_SUCCESSOR_INCLUDED
#include "platform.h"
#include "error_handler.h"
#include "exception.h"
#include "file_error_numbers.h"
#include "nullptr.h"
namespace etl
{
//***************************************************************************
//***************************************************************************
class successor_exception : public etl::exception
{
public:
successor_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
: exception(reason_, file_name_, line_number_)
{
}
};
//***************************************************************************
//***************************************************************************
class successor_invalid : public etl::successor_exception
{
public:
successor_invalid(string_type file_name_, numeric_type line_number_)
: etl::successor_exception(ETL_ERROR_TEXT("successor:invalid", ETL_SUCCESSOR_FILE_ID"A"), file_name_, line_number_)
{
}
};
//***************************************************************************
//***************************************************************************
template <typename T>
class successor
{
public:
typedef T successor_type;
//*************************************************************************
//*************************************************************************
successor()
: p_successor(ETL_NULLPTR)
{
}
//*************************************************************************
//*************************************************************************
successor(successor_type& s)
: p_successor(&s)
{
}
//*************************************************************************
//*************************************************************************
void set_successor(successor_type& s)
{
p_successor = &s;
}
#if ETL_CPP11_SUPPORTED
//*************************************************************************
//*************************************************************************
template <typename... TSuccessors>
void set_successor(successor_type& s, TSuccessors&... rest)
{
set_successor(s);
s.set_successor(rest...);
}
#endif
//*************************************************************************
//*************************************************************************
template <typename TSuccessor>
void append_successor(TSuccessor& s)
{
if (has_successor())
{
get_successor().append_successor(s);
}
else
{
set_successor(s);
}
}
#if ETL_CPP11_SUPPORTED
//*************************************************************************
//*************************************************************************
template <typename TSuccessor, typename... TSuccessors>
void append_successor(TSuccessor& s, TSuccessors&... rest)
{
if (has_successor())
{
get_successor().append_successor(s);
get_successor().append_successor(rest...);
}
else
{
set_successor(s);
append_successor(rest...);
}
}
#endif
//*************************************************************************
//*************************************************************************
void clear_successor()
{
p_successor = ETL_NULLPTR;
}
//*************************************************************************
//*************************************************************************
void clear_successor_chain()
{
if (has_successor())
{
get_successor().clear_successor_chain();
clear_successor();
}
}
//*************************************************************************
//*************************************************************************
successor_type& get_successor() const
{
ETL_ASSERT(has_successor(), ETL_ERROR(successor_invalid));
return *p_successor;
}
//*************************************************************************
//*************************************************************************
bool has_successor() const
{
return (p_successor != ETL_NULLPTR);
}
private:
successor_type* p_successor;
};
} // namespace etl
#endif
etl::successor_exception
Exception for the successor.
Definition successor.h:46
etl::successor_invalid
Invalid exception for successor.
Definition successor.h:59
etl::successor::clear_successor
void clear_successor()
Clear the successor.
Definition successor.h:153
etl::successor::set_successor
void set_successor(successor_type &s)
Set the successor.
Definition successor.h:97
etl::successor::has_successor
bool has_successor() const
Does this have a successor?
Definition successor.h:184
etl::successor::clear_successor_chain
void clear_successor_chain()
Clear the successor chain.
Definition successor.h:161
etl::successor::append_successor
void append_successor(TSuccessor &s)
Append a successor.
Definition successor.h:118
etl::successor::get_successor
successor_type & get_successor() const
Definition successor.h:174
etl::successor::successor
successor(successor_type &s)
Construct from a successor type.
Definition successor.h:89
etl::successor::successor
successor()
Default constructor.
Definition successor.h:81
error_handler.h
exception.h
file_error_numbers.h
ETL_ASSERT
#define ETL_ASSERT(b, e)
Definition error_handler.h:511
etl::exception::exception
ETL_EXCEPTION_CONSTEXPR exception(string_type reason_, string_type, numeric_type)
Constructor.
Definition exception.h:81
etl::exception
Definition exception.h:59
etl
Definition absolute.h:40
nullptr.h
platform.h