add Chapter18
This commit is contained in:
96
Chapter18/cib/libs/etl/include/etl/absolute.h
Normal file
96
Chapter18/cib/libs/etl/include/etl/absolute.h
Normal file
@@ -0,0 +1,96 @@
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2018 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_ABSOLUTE_INCLUDED
|
||||
#define ETL_ABSOLUTE_INCLUDED
|
||||
|
||||
#include "type_traits.h"
|
||||
#include "integral_limits.h"
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
// For signed types.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR
|
||||
typename etl::enable_if<etl::is_signed<T>::value, T>::type
|
||||
absolute(T value) ETL_NOEXCEPT
|
||||
{
|
||||
return (value < T(0)) ? -value : value;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
// For unsigned types.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR
|
||||
typename etl::enable_if<etl::is_unsigned<T>::value, T>::type
|
||||
absolute(T value) ETL_NOEXCEPT
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
// For signed types.
|
||||
// Returns the result as the unsigned type.
|
||||
//***************************************************************************
|
||||
#if ETL_USING_CPP11
|
||||
template <typename T, typename TReturn = typename etl::make_unsigned<T>::type>
|
||||
#else
|
||||
template <typename T, typename TReturn>
|
||||
#endif
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR
|
||||
typename etl::enable_if<etl::is_signed<T>::value, TReturn>::type
|
||||
absolute_unsigned(T value) ETL_NOEXCEPT
|
||||
{
|
||||
return (value == etl::integral_limits<T>::min) ? (etl::integral_limits<TReturn>::max / 2U) + 1U
|
||||
: (value < T(0)) ? TReturn(-value) : TReturn(value);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
// For unsigned types.
|
||||
// Returns the result as the unsigned type.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR
|
||||
typename etl::enable_if<etl::is_unsigned<T>::value, T>::type
|
||||
absolute_unsigned(T value) ETL_NOEXCEPT
|
||||
{
|
||||
return etl::absolute(value);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
3425
Chapter18/cib/libs/etl/include/etl/algorithm.h
Normal file
3425
Chapter18/cib/libs/etl/include/etl/algorithm.h
Normal file
File diff suppressed because it is too large
Load Diff
339
Chapter18/cib/libs/etl/include/etl/alignment.h
Normal file
339
Chapter18/cib/libs/etl/include/etl/alignment.h
Normal file
@@ -0,0 +1,339 @@
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2014 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_ALIGNMENT_INCLUDED
|
||||
#define ETL_ALIGNMENT_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "type_traits.h"
|
||||
#include "static_assert.h"
|
||||
#include "error_handler.h"
|
||||
#include "exception.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
///\defgroup alignment alignment
|
||||
/// Creates a variable of the specified type at the specified alignment.
|
||||
/// \ingroup utilities
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
/// Exception base for alignment
|
||||
//***************************************************************************
|
||||
class alignment_exception : public etl::exception
|
||||
{
|
||||
public:
|
||||
|
||||
alignment_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
|
||||
: exception(reason_, file_name_, line_number_)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Memory misalignment exception.
|
||||
//***************************************************************************
|
||||
class alignment_error : public alignment_exception
|
||||
{
|
||||
public:
|
||||
|
||||
alignment_error(string_type file_name_, numeric_type line_number_)
|
||||
: alignment_exception(ETL_ERROR_TEXT("alignment:error", ETL_ALIGNMENT_FILE_ID"A"), file_name_, line_number_)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
//*****************************************************************************
|
||||
/// Check that 'p' has 'required_alignment'.
|
||||
//*****************************************************************************
|
||||
inline bool is_aligned(void* p, size_t required_alignment)
|
||||
{
|
||||
uintptr_t address = reinterpret_cast<uintptr_t>(p);
|
||||
return (address % required_alignment) == 0U;
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
/// Check that 'p' has 'Alignment'.
|
||||
//*****************************************************************************
|
||||
template <size_t Alignment>
|
||||
bool is_aligned(void* p)
|
||||
{
|
||||
uintptr_t address = reinterpret_cast<uintptr_t>(p);
|
||||
return (address % Alignment) == 0U;
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
/// Check that 'p' has the alignment of 'T'.
|
||||
//*****************************************************************************
|
||||
template <typename T>
|
||||
bool is_aligned(void* p)
|
||||
{
|
||||
return is_aligned<etl::alignment_of<T>::value>(p);
|
||||
}
|
||||
|
||||
namespace private_alignment
|
||||
{
|
||||
#if ETL_USING_CPP11
|
||||
//***************************************************************************
|
||||
// Matcher.
|
||||
//***************************************************************************
|
||||
template <bool Is_Match, size_t Alignment, typename... TRest>
|
||||
class type_with_alignment_matcher;
|
||||
|
||||
// Matching alignment.
|
||||
template <size_t Alignment, typename T1, typename... TRest>
|
||||
class type_with_alignment_matcher<true, Alignment, T1, TRest...>
|
||||
{
|
||||
public:
|
||||
|
||||
typedef T1 type;
|
||||
};
|
||||
|
||||
// Non-matching alignment
|
||||
template <size_t Alignment, typename T1, typename T2, typename... TRest>
|
||||
class type_with_alignment_matcher <false, Alignment, T1, T2, TRest...>
|
||||
{
|
||||
public:
|
||||
|
||||
typedef typename type_with_alignment_matcher < Alignment <= etl::alignment_of<T2>::value , Alignment, T2, TRest... > ::type type;
|
||||
};
|
||||
|
||||
// Non-matching alignment, none left.
|
||||
template <size_t Alignment, typename T1>
|
||||
class type_with_alignment_matcher <false, Alignment, T1>
|
||||
{
|
||||
public:
|
||||
|
||||
typedef char type;
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
// Helper.
|
||||
//***************************************************************************
|
||||
template <size_t Alignment, typename T1, typename... T>
|
||||
class type_with_alignment_helper
|
||||
{
|
||||
public:
|
||||
|
||||
typedef typename type_with_alignment_matcher<Alignment <= etl::alignment_of<T1>::value, Alignment, T1, T...>::type type;
|
||||
};
|
||||
#else
|
||||
//***************************************************************************
|
||||
// Matcher.
|
||||
//***************************************************************************
|
||||
template <bool Is_Match, const size_t Alignment, typename T1 = void, typename T2 = void, typename T3 = void, typename T4 = void,
|
||||
typename T5 = void, typename T6 = void, typename T7 = void, typename T8 = void>
|
||||
class type_with_alignment_matcher;
|
||||
|
||||
// Matching alignment.
|
||||
template <size_t Alignment, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8>
|
||||
class type_with_alignment_matcher <true, Alignment, T1, T2, T3, T4, T5, T6, T7, T8>
|
||||
{
|
||||
public:
|
||||
|
||||
typedef T1 type;
|
||||
};
|
||||
|
||||
// Non-matching alignment.
|
||||
template <size_t Alignment, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8>
|
||||
class type_with_alignment_matcher <false, Alignment, T1, T2, T3, T4, T5, T6, T7, T8>
|
||||
{
|
||||
public:
|
||||
|
||||
typedef typename type_with_alignment_matcher<Alignment <= etl::alignment_of<T2>::value, Alignment, T2, T3, T4, T5, T6, T7, T8, void>::type type;
|
||||
};
|
||||
|
||||
// Non-matching alignment, none left.
|
||||
template <size_t Alignment>
|
||||
class type_with_alignment_matcher <false, Alignment, void, void, void, void, void, void, void, void>
|
||||
{
|
||||
public:
|
||||
|
||||
typedef char type;
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
// Helper.
|
||||
//***************************************************************************
|
||||
template <size_t Alignment, typename T1, typename T2 = void, typename T3 = void, typename T4 = void,
|
||||
typename T5 = void, typename T6 = void, typename T7 = void, typename T8 = void>
|
||||
class type_with_alignment_helper
|
||||
{
|
||||
public:
|
||||
|
||||
typedef typename type_with_alignment_matcher<Alignment <= etl::alignment_of<T1>::value, Alignment, T1, T2, T3, T4, T5, T6, T7, T8>::type type;
|
||||
};
|
||||
#endif
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Gets a type that has the same as the specified alignment.
|
||||
///\ingroup alignment
|
||||
//***************************************************************************
|
||||
template <size_t Alignment>
|
||||
class type_with_alignment
|
||||
{
|
||||
public:
|
||||
|
||||
#if ETL_USING_CPP11
|
||||
typedef struct { alignas(Alignment) char dummy; } type;
|
||||
#else
|
||||
#if ETL_NOT_USING_64BIT_TYPES
|
||||
typedef typename private_alignment::type_with_alignment_helper<Alignment, int_least8_t, int_least16_t, int32_t, float, double, void*>::type type;
|
||||
#else
|
||||
typedef typename private_alignment::type_with_alignment_helper<Alignment, int_least8_t, int_least16_t, int32_t, int64_t, float, double, void*>::type type;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
ETL_STATIC_ASSERT(etl::alignment_of<type>::value == Alignment, "Unable to create the type with the specified alignment");
|
||||
};
|
||||
|
||||
#if ETL_USING_CPP11
|
||||
template <size_t Alignment>
|
||||
using type_with_alignment_t = typename type_with_alignment<Alignment>::type;
|
||||
#endif
|
||||
|
||||
//***************************************************************************
|
||||
/// Aligned storage
|
||||
/// Length should be determined in terms of sizeof()
|
||||
///\ingroup alignment
|
||||
//***************************************************************************
|
||||
template <size_t Length, const size_t Alignment>
|
||||
struct aligned_storage
|
||||
{
|
||||
struct type
|
||||
{
|
||||
//type()
|
||||
// : data()
|
||||
//{
|
||||
//}
|
||||
|
||||
/// Convert to T reference.
|
||||
template <typename T>
|
||||
operator T& ()
|
||||
{
|
||||
ETL_STATIC_ASSERT((etl::is_same<T*, void*>:: value || ((Alignment % etl::alignment_of<T>::value) == 0)), "Incompatible alignment");
|
||||
T* t = *this;
|
||||
return *t;
|
||||
}
|
||||
|
||||
/// Convert to const T reference.
|
||||
template <typename T>
|
||||
operator const T& () const
|
||||
{
|
||||
ETL_STATIC_ASSERT((etl::is_same<T*, void*>:: value || ((Alignment % etl::alignment_of<T>::value) == 0)), "Incompatible alignment");
|
||||
const T* t = *this;
|
||||
return *t;
|
||||
}
|
||||
|
||||
/// Convert to T pointer.
|
||||
template <typename T>
|
||||
operator T* ()
|
||||
{
|
||||
ETL_STATIC_ASSERT((etl::is_same<T*, void*>:: value || ((Alignment % etl::alignment_of<T>::value) == 0)), "Incompatible alignment");
|
||||
return reinterpret_cast<T*>(data);
|
||||
}
|
||||
|
||||
/// Convert to const T pointer.
|
||||
template <typename T>
|
||||
operator const T* () const
|
||||
{
|
||||
ETL_STATIC_ASSERT((etl::is_same<T*, void*>:: value || ((Alignment % etl::alignment_of<T>::value) == 0)), "Incompatible alignment");
|
||||
return reinterpret_cast<const T*>(data);
|
||||
}
|
||||
|
||||
/// Get address as T reference.
|
||||
template <typename T>
|
||||
T& get_reference()
|
||||
{
|
||||
ETL_STATIC_ASSERT((etl::is_same<T*, void*>:: value || ((Alignment % etl::alignment_of<T>::value) == 0)), "Incompatible alignment");
|
||||
T* t = *this;
|
||||
return *t;
|
||||
}
|
||||
|
||||
/// Get address as const T reference.
|
||||
template <typename T>
|
||||
const T& get_reference() const
|
||||
{
|
||||
ETL_STATIC_ASSERT((etl::is_same<T*, void*>:: value || ((Alignment % etl::alignment_of<T>::value) == 0)), "Incompatible alignment");
|
||||
const T* t = *this;
|
||||
return *t;
|
||||
}
|
||||
|
||||
/// Get address as T pointer.
|
||||
template <typename T>
|
||||
T* get_address()
|
||||
{
|
||||
ETL_STATIC_ASSERT((etl::is_same<T*, void*>:: value || ((Alignment % etl::alignment_of<T>::value) == 0)), "Incompatible alignment");
|
||||
return reinterpret_cast<T*>(data);
|
||||
}
|
||||
|
||||
/// Get address as const T pointer.
|
||||
template <typename T>
|
||||
const T* get_address() const
|
||||
{
|
||||
ETL_STATIC_ASSERT((etl::is_same<T*, void*>:: value || ((Alignment % etl::alignment_of<T>::value) == 0)), "Incompatible alignment");
|
||||
return reinterpret_cast<const T*>(data);
|
||||
}
|
||||
|
||||
#if ETL_USING_CPP11 && !defined(ETL_COMPILER_ARM5)
|
||||
alignas(Alignment) char data[Length];
|
||||
#else
|
||||
union
|
||||
{
|
||||
char data[Length];
|
||||
typename etl::type_with_alignment<Alignment>::type etl_alignment_type; // A POD type that has the same alignment as Alignment.
|
||||
};
|
||||
#endif
|
||||
};
|
||||
};
|
||||
|
||||
#if ETL_USING_CPP11
|
||||
template <size_t Length, const size_t Alignment>
|
||||
using aligned_storage_t = typename aligned_storage<Length, Alignment>::type;
|
||||
#endif
|
||||
|
||||
//***************************************************************************
|
||||
/// Aligned storage as
|
||||
///\ingroup alignment
|
||||
//***************************************************************************
|
||||
template <size_t Length, typename T>
|
||||
struct aligned_storage_as : public etl::aligned_storage<Length, etl::alignment_of<T>::value>
|
||||
{
|
||||
};
|
||||
|
||||
#if ETL_USING_CPP11
|
||||
template <size_t Length, typename T>
|
||||
using aligned_storage_as_t = typename aligned_storage_as<Length, T>::type;
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
741
Chapter18/cib/libs/etl/include/etl/array.h
Normal file
741
Chapter18/cib/libs/etl/include/etl/array.h
Normal file
@@ -0,0 +1,741 @@
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2014 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_ARRAY_INCLUDED
|
||||
#define ETL_ARRAY_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "algorithm.h"
|
||||
#include "iterator.h"
|
||||
#include "functional.h"
|
||||
#include "exception.h"
|
||||
#include "type_traits.h"
|
||||
#include "parameter_type.h"
|
||||
#include "static_assert.h"
|
||||
#include "error_handler.h"
|
||||
#include "nth_type.h"
|
||||
#include "initializer_list.h"
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
///\defgroup array array
|
||||
/// A replacement for std::array if you haven't got C++0x11.
|
||||
///\ingroup containers
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
///\ingroup array
|
||||
/// The base class for array exceptions.
|
||||
//***************************************************************************
|
||||
class array_exception : public exception
|
||||
{
|
||||
public:
|
||||
|
||||
array_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
|
||||
: exception(reason_, file_name_, line_number_)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
///\ingroup array
|
||||
/// The out of range exceptions.
|
||||
//***************************************************************************
|
||||
class array_out_of_range : public array_exception
|
||||
{
|
||||
public:
|
||||
|
||||
array_out_of_range(string_type file_name_, numeric_type line_number_)
|
||||
: array_exception("array:range", file_name_, line_number_)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
///\ingroup array
|
||||
/// A replacement for std::array if you haven't got C++0x11.
|
||||
//***************************************************************************
|
||||
template <typename T, size_t SIZE_>
|
||||
class array
|
||||
{
|
||||
private:
|
||||
|
||||
typedef typename etl::parameter_type<T>::type parameter_t;
|
||||
|
||||
public:
|
||||
|
||||
static ETL_CONSTANT size_t SIZE = SIZE_;
|
||||
|
||||
typedef T value_type;
|
||||
typedef size_t size_type;
|
||||
typedef ptrdiff_t difference_type;
|
||||
typedef T& reference;
|
||||
typedef const T& const_reference;
|
||||
typedef T* pointer;
|
||||
typedef const T* const_pointer;
|
||||
typedef T* iterator;
|
||||
typedef const T* const_iterator;
|
||||
typedef ETL_OR_STD::reverse_iterator<iterator> reverse_iterator;
|
||||
typedef ETL_OR_STD::reverse_iterator<const_iterator> const_reverse_iterator;
|
||||
|
||||
//*************************************************************************
|
||||
// Element access
|
||||
//*************************************************************************
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a reference to the value at index 'i'.
|
||||
///\param i The index of the element to access.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR14
|
||||
reference at(size_t i)
|
||||
{
|
||||
ETL_ASSERT(i < SIZE, ETL_ERROR(array_out_of_range));
|
||||
|
||||
return _buffer[i];
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const reference to the value at index 'i'.
|
||||
///\param i The index of the element to access.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR14
|
||||
const_reference at(size_t i) const
|
||||
{
|
||||
ETL_ASSERT(i < SIZE, ETL_ERROR(array_out_of_range));
|
||||
|
||||
return _buffer[i];
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// [] operator.
|
||||
/// Returns a reference to the value at index 'i'.
|
||||
///\param i The index of the element to access.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR14
|
||||
reference operator[](size_t i)
|
||||
{
|
||||
return _buffer[i];
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// [] operator.
|
||||
/// Returns a const reference to the value at index 'i'.
|
||||
///\param i The index of the element to access.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR const_reference operator[](size_t i) const
|
||||
{
|
||||
return _buffer[i];
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a reference to the first element.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR14
|
||||
reference front()
|
||||
{
|
||||
return _buffer[0];
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const reference to the first element.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR const_reference front() const
|
||||
{
|
||||
return _buffer[0];
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a reference to the last element.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR14
|
||||
reference back()
|
||||
{
|
||||
return _buffer[SIZE - 1];
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const reference to the last element.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR const_reference back() const
|
||||
{
|
||||
return _buffer[SIZE - 1];
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a pointer to the first element of the internal buffer.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR14
|
||||
pointer data() ETL_NOEXCEPT
|
||||
{
|
||||
return &_buffer[0];
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const pointer to the first element of the internal buffer.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR const_pointer data() const ETL_NOEXCEPT
|
||||
{
|
||||
return &_buffer[0];
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
// Iterators
|
||||
//*************************************************************************
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns an iterator to the beginning of the array.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR14
|
||||
iterator begin() ETL_NOEXCEPT
|
||||
{
|
||||
return &_buffer[0];
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const iterator to the beginning of the array.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR const_iterator begin() const ETL_NOEXCEPT
|
||||
{
|
||||
return &_buffer[0];
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const iterator to the beginning of the array.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR const_iterator cbegin() const ETL_NOEXCEPT
|
||||
{
|
||||
return begin();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns an iterator to the end of the array.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR14
|
||||
iterator end() ETL_NOEXCEPT
|
||||
{
|
||||
return _buffer + SIZE;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const iterator to the end of the array.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR const_iterator end() const ETL_NOEXCEPT
|
||||
{
|
||||
return _buffer + SIZE;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
// Returns a const iterator to the end of the array.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR const_iterator cend() const ETL_NOEXCEPT
|
||||
{
|
||||
return _buffer + SIZE;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
// Returns an reverse iterator to the reverse beginning of the array.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR14
|
||||
reverse_iterator rbegin() ETL_NOEXCEPT
|
||||
{
|
||||
return reverse_iterator(end());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const reverse iterator to the reverse beginning of the array.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR const_reverse_iterator rbegin() const ETL_NOEXCEPT
|
||||
{
|
||||
return const_reverse_iterator(end());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const reverse iterator to the reverse beginning of the array.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR const_reverse_iterator crbegin() const ETL_NOEXCEPT
|
||||
{
|
||||
return const_reverse_iterator(end());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a reverse iterator to the end of the array.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR14
|
||||
reverse_iterator rend() ETL_NOEXCEPT
|
||||
{
|
||||
return reverse_iterator(begin());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const reverse iterator to the end of the array.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR const_reverse_iterator rend() const ETL_NOEXCEPT
|
||||
{
|
||||
return const_reverse_iterator(begin());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const reverse iterator to the end of the array.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR const_reverse_iterator crend() const ETL_NOEXCEPT
|
||||
{
|
||||
return const_reverse_iterator(begin());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
// Capacity
|
||||
//*************************************************************************
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns <b>true</b> if the array size is zero.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR bool empty() const ETL_NOEXCEPT
|
||||
{
|
||||
return (SIZE == 0);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns the size of the array.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR size_t size() const ETL_NOEXCEPT
|
||||
{
|
||||
return SIZE;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns the maximum possible size of the array.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR size_t max_size() const ETL_NOEXCEPT
|
||||
{
|
||||
return SIZE;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
// Operations
|
||||
//*************************************************************************
|
||||
|
||||
//*************************************************************************
|
||||
/// Fills the array with the specified value.
|
||||
///\param value The value to fill the array with.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14 void fill(parameter_t value)
|
||||
{
|
||||
etl::fill(_buffer, _buffer + SIZE, value);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Swaps the contents of this array and another.
|
||||
///\param other A reference to the other array.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14 void swap(array& other) ETL_NOEXCEPT
|
||||
{
|
||||
using ETL_OR_STD::swap; // Allow ADL
|
||||
|
||||
for (size_t i = 0UL; i < SIZE; ++i)
|
||||
{
|
||||
swap(_buffer[i], other._buffer[i]);
|
||||
}
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Fills the array from the range.
|
||||
/// If the range is smaller than the array then the unused array elements are left unmodified.
|
||||
///\param first The iterator to the first item in the range.
|
||||
///\param last The iterator to one past the final item in the range.
|
||||
///\return An iterator to the first unassigned array element, or end().
|
||||
//*************************************************************************
|
||||
template <typename TIterator>
|
||||
iterator assign(TIterator first, const TIterator last)
|
||||
{
|
||||
return etl::copy_s(first, last, begin(), end());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Fills the array from the range.
|
||||
/// If the range is smaller than the array then the unused array elements are initialised with the supplied value.
|
||||
///\param first The iterator to the first item in the range.
|
||||
///\param last The iterator to one past the final item in the range.
|
||||
///\return An iterator to the first array element set to 'value', or end().
|
||||
//*************************************************************************
|
||||
template <typename TIterator>
|
||||
iterator assign(TIterator first, const TIterator last, parameter_t value)
|
||||
{
|
||||
// Copy from the range.
|
||||
iterator p = etl::copy_s(first, last, begin(), end());
|
||||
|
||||
// Initialise any that are left.
|
||||
etl::fill(p, end(), value);
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Inserts a value into the array.
|
||||
///\param position The index of the position to insert at.
|
||||
///\param value The value to insert.
|
||||
//*************************************************************************
|
||||
inline iterator insert_at(size_t position, parameter_t value)
|
||||
{
|
||||
return insert(begin() + position, value);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Inserts a value into the array.
|
||||
///\param position The iterator to the position to insert at.
|
||||
///\param value The value to insert.
|
||||
//*************************************************************************
|
||||
iterator insert(const_iterator position, parameter_t value)
|
||||
{
|
||||
iterator p = to_iterator(position);
|
||||
|
||||
etl::move_backward(p, end() - 1, end());
|
||||
*p = value;
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Insert into the array from the range.
|
||||
///\param position The position to insert at.
|
||||
///\param first The iterator to the first item in the range.
|
||||
///\param last The iterator to one past the final item in the range.
|
||||
//*************************************************************************
|
||||
template <typename TIterator>
|
||||
inline iterator insert_at(size_t position, TIterator first, const TIterator last)
|
||||
{
|
||||
return insert(begin() + position, first, last);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Insert into the array from the range.
|
||||
///\param position The position to insert at.
|
||||
///\param first The iterator to the first item in the range.
|
||||
///\param last The iterator to one past the final item in the range.
|
||||
//*************************************************************************
|
||||
template <typename TIterator>
|
||||
iterator insert(const_iterator position, TIterator first, const TIterator last)
|
||||
{
|
||||
iterator p = to_iterator(position);
|
||||
iterator result(p);
|
||||
|
||||
size_t source_size = etl::distance(first, last);
|
||||
size_t destination_space = etl::distance(position, cend());
|
||||
|
||||
// Do we need to move anything?
|
||||
if (source_size < destination_space)
|
||||
{
|
||||
size_t length = SIZE - (etl::distance(begin(), p) + source_size);
|
||||
etl::move_backward(p, p + length, end());
|
||||
}
|
||||
|
||||
// Copy from the range.
|
||||
etl::copy_s(first, last, p, end());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Erases a value from the array.
|
||||
/// After erase, the last value in the array will be unmodified.
|
||||
///\param position The index of the position to erase at.
|
||||
//*************************************************************************
|
||||
inline iterator erase_at(size_t position)
|
||||
{
|
||||
return erase(begin() + position);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Erases a value from the array.
|
||||
/// After erase, the last value in the array will be unmodified.
|
||||
///\param position The iterator to the position to erase at.
|
||||
//*************************************************************************
|
||||
iterator erase(const_iterator position)
|
||||
{
|
||||
iterator p = to_iterator(position);
|
||||
etl::move(p + 1, end(), p);
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Erases a range of values from the array.
|
||||
/// After erase, the last values in the array will be unmodified.
|
||||
///\param first The first item to erase.
|
||||
///\param last The one past the last item to erase.
|
||||
//*************************************************************************
|
||||
iterator erase_range(size_t first, size_t last)
|
||||
{
|
||||
return erase(begin() + first, begin() + last);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Erases a range of values from the array.
|
||||
/// After erase, the last values in the array will be unmodified.
|
||||
///\param first The first item to erase.
|
||||
///\param last The one past the last item to erase.
|
||||
//*************************************************************************
|
||||
iterator erase(const_iterator first, const_iterator last)
|
||||
{
|
||||
iterator p = to_iterator(first);
|
||||
etl::move(last, cend(), p);
|
||||
return p;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Erases a value from the array.
|
||||
///\param position The index of the position to erase at.
|
||||
///\param value The value to use to overwrite the last element in the array.
|
||||
//*************************************************************************
|
||||
inline iterator erase_at(size_t position, parameter_t value)
|
||||
{
|
||||
return erase(begin() + position, value);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Erases a value from the array.
|
||||
///\param position The iterator to the position to erase at.
|
||||
///\param value The value to use to overwrite the last element in the array.
|
||||
//*************************************************************************
|
||||
iterator erase(const_iterator position, parameter_t value)
|
||||
{
|
||||
iterator p = to_iterator(position);
|
||||
|
||||
etl::move(p + 1, end(), p);
|
||||
back() = value;
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Erases a range of values from the array.
|
||||
///\param first The first item to erase.
|
||||
///\param last The one past the last item to erase.
|
||||
///\param value The value to use to overwrite the last elements in the array.
|
||||
//*************************************************************************
|
||||
iterator erase_range(size_t first, size_t last, parameter_t value)
|
||||
{
|
||||
return erase(begin() + first, begin() + last, value);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Erases a range of values from the array.
|
||||
///\param position The iterator to the position to erase at.
|
||||
///\param value The value to use to overwrite the last elements in the array.
|
||||
//*************************************************************************
|
||||
iterator erase(const_iterator first, const_iterator last, parameter_t value)
|
||||
{
|
||||
iterator p = to_iterator(first);
|
||||
|
||||
p = etl::move(last, cend(), p);
|
||||
etl::fill(p, end(), value);
|
||||
|
||||
return to_iterator(first);
|
||||
}
|
||||
|
||||
/// The array data.
|
||||
T _buffer[SIZE];
|
||||
|
||||
private:
|
||||
|
||||
//*************************************************************************
|
||||
/// Convert from const_iterator to iterator
|
||||
//*************************************************************************
|
||||
iterator to_iterator(const_iterator itr) const
|
||||
{
|
||||
return const_cast<iterator>(itr);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T, size_t SIZE_>
|
||||
ETL_CONSTANT size_t array<T, SIZE_>::SIZE;
|
||||
|
||||
//*************************************************************************
|
||||
/// Template deduction guides.
|
||||
//*************************************************************************
|
||||
#if ETL_USING_CPP17
|
||||
template <typename... T>
|
||||
array(T...) -> array<typename etl::common_type<T...>::type, sizeof...(T)>;
|
||||
#endif
|
||||
|
||||
//*************************************************************************
|
||||
/// Make
|
||||
//*************************************************************************
|
||||
#if ETL_HAS_INITIALIZER_LIST
|
||||
template <typename T, typename... TValues>
|
||||
constexpr auto make_array(TValues&&... values) -> etl::array<T, sizeof...(TValues)>
|
||||
{
|
||||
return { etl::forward<T>(values)... };
|
||||
}
|
||||
#endif
|
||||
|
||||
//*************************************************************************
|
||||
/// Overloaded swap for etl::array<T, SIZE>
|
||||
///\param lhs The first array.
|
||||
///\param rhs The second array.
|
||||
//*************************************************************************
|
||||
template <typename T, const size_t SIZE>
|
||||
void swap(etl::array<T, SIZE> &lhs, etl::array<T, SIZE> &rhs)
|
||||
{
|
||||
lhs.swap(rhs);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Equal operator.
|
||||
///\param lhs The first array.
|
||||
///\param rhs The second array.
|
||||
///\return <b>true</b> if the arrays are equal, otherwise <b>false</b>
|
||||
//*************************************************************************
|
||||
template <typename T, size_t SIZE>
|
||||
bool operator ==(const etl::array<T, SIZE>& lhs, const etl::array<T, SIZE>& rhs)
|
||||
{
|
||||
return etl::equal(lhs.cbegin(), lhs.cend(), rhs.cbegin());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Not equal operator.
|
||||
///\param lhs The first array.
|
||||
///\param rhs The second array.
|
||||
///\return <b>true</b> if the arrays are not equal, otherwise <b>false</b>
|
||||
//*************************************************************************
|
||||
template <typename T, size_t SIZE>
|
||||
bool operator !=(const etl::array<T, SIZE>& lhs, const etl::array<T, SIZE>& rhs)
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Less than operator.
|
||||
///\param lhs The first array.
|
||||
///\param rhs The second array.
|
||||
///\return <b>true</b> if the first array is lexicographically less than the second, otherwise <b>false</b>
|
||||
//*************************************************************************
|
||||
template <typename T, size_t SIZE>
|
||||
bool operator <(const etl::array<T, SIZE>& lhs, const etl::array<T, SIZE>& rhs)
|
||||
{
|
||||
return etl::lexicographical_compare(lhs.cbegin(),
|
||||
lhs.cend(),
|
||||
rhs.cbegin(),
|
||||
rhs.cend());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Less than or equal operator.
|
||||
///\param lhs The first array.
|
||||
///\param rhs The second array.
|
||||
///\return <b>true</b> if the first array is lexicographically less than or equal to the second, otherwise <b>false</b>
|
||||
//*************************************************************************
|
||||
template <typename T, size_t SIZE>
|
||||
bool operator <=(const etl::array<T, SIZE>& lhs, const etl::array<T, SIZE>& rhs)
|
||||
{
|
||||
return !(lhs > rhs);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Greater than operator.
|
||||
///\param lhs The first array.
|
||||
///\param rhs The second array.
|
||||
///\return <b>true</b> if the first array is lexicographically greater than the second, otherwise <b>false</b>
|
||||
template <typename T, size_t SIZE>
|
||||
//*************************************************************************
|
||||
bool operator >(const etl::array<T, SIZE>& lhs, const etl::array<T, SIZE>& rhs)
|
||||
{
|
||||
return (rhs < lhs);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Greater than or equal operator.
|
||||
///\param lhs The first array.
|
||||
///\param rhs The second array.
|
||||
///\return <b>true</b> if the first array is lexicographically greater than or equal to the second, otherwise <b>false</b>
|
||||
//*************************************************************************
|
||||
template <typename T, size_t SIZE>
|
||||
bool operator >=(const etl::array<T, SIZE>& lhs, const etl::array<T, SIZE>& rhs)
|
||||
{
|
||||
return !(lhs < rhs);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Gets a reference to an element in the array.
|
||||
///\tparam I The index.
|
||||
///\tparam T The type.
|
||||
///\tparam MAXN The array size.
|
||||
///\param a The array.
|
||||
///\return A reference to the element
|
||||
//*************************************************************************
|
||||
template <size_t I, typename T, size_t MAXN>
|
||||
inline T& get(array<T, MAXN>& a)
|
||||
{
|
||||
ETL_STATIC_ASSERT(I < MAXN, "Index out of bounds");
|
||||
return a[I];
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Gets a const reference to an element in the array.
|
||||
///\tparam I The index.
|
||||
///\tparam T The type.
|
||||
///\tparam MAXN The array size.
|
||||
///\param a The array.
|
||||
///\return A const reference to the element
|
||||
//*************************************************************************
|
||||
template <size_t I, typename T, size_t MAXN>
|
||||
inline const T& get(const array<T, MAXN>& a)
|
||||
{
|
||||
ETL_STATIC_ASSERT(I < MAXN, "Index out of bounds");
|
||||
return a[I];
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
655
Chapter18/cib/libs/etl/include/etl/array_view.h
Normal file
655
Chapter18/cib/libs/etl/include/etl/array_view.h
Normal file
@@ -0,0 +1,655 @@
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2017 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_ARRAY_VIEW_INCLUDED
|
||||
#define ETL_ARRAY_VIEW_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "memory.h"
|
||||
#include "array.h"
|
||||
#include "iterator.h"
|
||||
#include "error_handler.h"
|
||||
#include "exception.h"
|
||||
#include "nullptr.h"
|
||||
#include "hash.h"
|
||||
#include "algorithm.h"
|
||||
#include "type_traits.h"
|
||||
|
||||
#if ETL_USING_STL && ETL_USING_CPP11
|
||||
#include <array>
|
||||
#endif
|
||||
|
||||
///\defgroup array array
|
||||
/// A wrapper for arrays
|
||||
///\ingroup containers
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
/// The base class for array_view exceptions.
|
||||
//***************************************************************************
|
||||
class array_view_exception : public exception
|
||||
{
|
||||
public:
|
||||
|
||||
array_view_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
|
||||
: exception(reason_, file_name_, line_number_)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
///\ingroup stack
|
||||
/// The exception thrown when the index is out of bounds.
|
||||
//***************************************************************************
|
||||
class array_view_bounds : public array_view_exception
|
||||
{
|
||||
public:
|
||||
|
||||
array_view_bounds(string_type file_name_, numeric_type line_number_)
|
||||
: array_view_exception(ETL_ERROR_TEXT("array_view:bounds", ETL_ARRAY_VIEW_FILE_ID"A"), file_name_, line_number_)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
///\ingroup stack
|
||||
/// The exception thrown when the view is uninitialised.
|
||||
//***************************************************************************
|
||||
class array_view_uninitialised : public array_view_exception
|
||||
{
|
||||
public:
|
||||
|
||||
array_view_uninitialised(string_type file_name_, numeric_type line_number_)
|
||||
: array_view_exception(ETL_ERROR_TEXT("array_view:uninitialised", ETL_ARRAY_VIEW_FILE_ID"B"), file_name_, line_number_)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Array view.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
class array_view
|
||||
{
|
||||
public:
|
||||
|
||||
typedef T value_type;
|
||||
typedef size_t size_type;
|
||||
typedef const T& const_reference;
|
||||
typedef const T* const_pointer;
|
||||
typedef const T* const_iterator;
|
||||
typedef ETL_OR_STD::reverse_iterator<const_iterator> const_reverse_iterator;
|
||||
|
||||
#if defined(ETL_ARRAY_VIEW_IS_MUTABLE)
|
||||
typedef T* pointer;
|
||||
typedef T& reference;
|
||||
typedef T* iterator;
|
||||
typedef ETL_OR_STD::reverse_iterator<iterator> reverse_iterator;
|
||||
#else
|
||||
typedef const_pointer pointer;
|
||||
typedef const_reference reference;
|
||||
typedef const_pointer iterator;
|
||||
typedef const_reverse_iterator reverse_iterator;
|
||||
#endif
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR array_view()
|
||||
: mbegin(ETL_NULLPTR),
|
||||
mend(ETL_NULLPTR)
|
||||
{
|
||||
}
|
||||
|
||||
#if ETL_USING_CPP11
|
||||
//*************************************************************************
|
||||
/// Construct from etl::array.
|
||||
//*************************************************************************
|
||||
template <typename U, size_t N, typename = typename etl::enable_if<etl::is_same<etl::remove_cv_t<T>, etl::remove_cv_t<U>>::value, void>::type>
|
||||
ETL_CONSTEXPR array_view(etl::array<U, N>& a) ETL_NOEXCEPT
|
||||
: mbegin(a.data())
|
||||
, mend(a.data() + a.size())
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Construct from etl::array.
|
||||
//*************************************************************************
|
||||
template <typename U, size_t N, typename = typename etl::enable_if<etl::is_same<etl::remove_cv_t<T>, etl::remove_cv_t<U>>::value, void>::type>
|
||||
ETL_CONSTEXPR array_view(const etl::array<U, N>& a) ETL_NOEXCEPT
|
||||
: mbegin(a.data())
|
||||
, mend(a.data() + a.size())
|
||||
{
|
||||
}
|
||||
#else
|
||||
//*************************************************************************
|
||||
/// Construct from etl::array.
|
||||
//*************************************************************************
|
||||
template <typename U, size_t N>
|
||||
ETL_CONSTEXPR array_view(etl::array<U, N>& a, typename etl::enable_if<etl::is_same<typename etl::remove_cv<T>::type, typename etl::remove_cv<U>::type>::value, void>::type* = 0) ETL_NOEXCEPT
|
||||
: mbegin(a.data())
|
||||
, mend(a.data() + a.size())
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Construct from etl::array.
|
||||
//*************************************************************************
|
||||
template <typename U, size_t N>
|
||||
ETL_CONSTEXPR array_view(const etl::array<U, N>& a, typename etl::enable_if<etl::is_same<typename etl::remove_cv<T>::type, typename etl::remove_cv<U>::type>::value, void>::type* = 0) ETL_NOEXCEPT
|
||||
: mbegin(a.data())
|
||||
, mend(a.data() + a.size())
|
||||
{
|
||||
}
|
||||
#endif
|
||||
|
||||
#if ETL_USING_STL && ETL_USING_CPP11
|
||||
//*************************************************************************
|
||||
/// Construct from std::array.
|
||||
//*************************************************************************
|
||||
template <typename U, size_t N, typename = typename etl::enable_if<etl::is_same<etl::remove_cv_t<T>, etl::remove_cv_t<U>>::value, void>::type>
|
||||
ETL_CONSTEXPR array_view(std::array<U, N>& a) ETL_NOEXCEPT
|
||||
: mbegin(a.data())
|
||||
, mend(a.data() + a.size())
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Construct from std::array.
|
||||
//*************************************************************************
|
||||
template <typename U, size_t N, typename = typename etl::enable_if<etl::is_same<etl::remove_cv_t<T>, etl::remove_cv_t<U>>::value, void>::type>
|
||||
ETL_CONSTEXPR array_view(const std::array<U, N>& a) ETL_NOEXCEPT
|
||||
: mbegin(a.data())
|
||||
, mend(a.data() + a.size())
|
||||
{
|
||||
}
|
||||
#endif
|
||||
|
||||
#if ETL_USING_CPP11
|
||||
//*************************************************************************
|
||||
/// Construct from a container or other type that supports
|
||||
/// data() and size() member functions.
|
||||
//*************************************************************************
|
||||
template <typename TContainer, typename = typename etl::enable_if<!etl::is_pointer<etl::remove_reference_t<TContainer>>::value &&
|
||||
!etl::is_array<etl::remove_reference_t<TContainer>>::value &&
|
||||
etl::is_same<etl::remove_cv_t<T>, etl::remove_cv_t<typename etl::remove_reference_t<TContainer>::value_type>>::value, void>::type>
|
||||
ETL_CONSTEXPR array_view(TContainer&& a) ETL_NOEXCEPT
|
||||
: mbegin(a.data())
|
||||
, mend(a.data() + a.size())
|
||||
{
|
||||
}
|
||||
#else
|
||||
//*************************************************************************
|
||||
/// Construct from a container or other type that supports
|
||||
/// data() and size() member functions.
|
||||
//*************************************************************************
|
||||
template <typename TContainer>
|
||||
ETL_CONSTEXPR array_view(TContainer& a, typename etl::enable_if<!etl::is_pointer<typename etl::remove_reference<TContainer>::type>::value &&
|
||||
!etl::is_array<TContainer>::value &&
|
||||
etl::is_same<typename etl::remove_cv<T>::type, typename etl::remove_cv<typename etl::remove_reference<TContainer>::type::value_type>::type>::value, void>::type* = 0) ETL_NOEXCEPT
|
||||
: mbegin(a.data())
|
||||
, mend(a.data() + a.size())
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Construct from a container or other type that supports
|
||||
/// data() and size() member functions.
|
||||
//*************************************************************************
|
||||
template <typename TContainer>
|
||||
ETL_CONSTEXPR array_view(const TContainer& a, typename etl::enable_if<!etl::is_pointer<typename etl::remove_reference<TContainer>::type>::value &&
|
||||
!etl::is_array<TContainer>::value &&
|
||||
etl::is_same<typename etl::remove_cv<T>::type, typename etl::remove_cv<typename etl::remove_reference<TContainer>::type::value_type>::type>::value, void>::type* = 0) ETL_NOEXCEPT
|
||||
: mbegin(a.data())
|
||||
, mend(a.data() + a.size())
|
||||
{
|
||||
}
|
||||
#endif
|
||||
|
||||
//*************************************************************************
|
||||
/// Construct from iterators
|
||||
//*************************************************************************
|
||||
template <typename TIterator>
|
||||
ETL_CONSTEXPR array_view(const TIterator begin_, const TIterator end_)
|
||||
: mbegin(etl::addressof(*begin_)),
|
||||
mend(etl::addressof(*begin_) + etl::distance(begin_, end_))
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Construct from C array
|
||||
//*************************************************************************
|
||||
template <typename TIterator,
|
||||
typename TSize>
|
||||
ETL_CONSTEXPR array_view(const TIterator begin_, const TSize size_)
|
||||
: mbegin(etl::addressof(*begin_)),
|
||||
mend(etl::addressof(*begin_) + size_)
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Construct from C array
|
||||
//*************************************************************************
|
||||
template<size_t Array_Size>
|
||||
ETL_CONSTEXPR array_view(T(&begin_)[Array_Size])
|
||||
: mbegin(begin_),
|
||||
mend(begin_ + Array_Size)
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Copy constructor
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR array_view(const array_view& other)
|
||||
: mbegin(other.mbegin),
|
||||
mend(other.mend)
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a reference to the first element.
|
||||
//*************************************************************************
|
||||
reference front()
|
||||
{
|
||||
return *mbegin;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const reference to the first element.
|
||||
//*************************************************************************
|
||||
const_reference front() const
|
||||
{
|
||||
return *mbegin;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a reference to the last element.
|
||||
//*************************************************************************
|
||||
reference back()
|
||||
{
|
||||
return *(mend - 1);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const reference to the last element.
|
||||
//*************************************************************************
|
||||
const_reference back() const
|
||||
{
|
||||
return *(mend - 1);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a pointer to the first element of the internal storage.
|
||||
//*************************************************************************
|
||||
pointer data()
|
||||
{
|
||||
return mbegin;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const pointer to the first element of the internal storage.
|
||||
//*************************************************************************
|
||||
const_pointer data() const
|
||||
{
|
||||
return mbegin;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns an iterator to the beginning of the array.
|
||||
//*************************************************************************
|
||||
iterator begin()
|
||||
{
|
||||
return mbegin;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const iterator to the beginning of the array.
|
||||
//*************************************************************************
|
||||
const_iterator begin() const
|
||||
{
|
||||
return mbegin;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const iterator to the beginning of the array.
|
||||
//*************************************************************************
|
||||
const_iterator cbegin() const
|
||||
{
|
||||
return mbegin;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns an iterator to the end of the array.
|
||||
//*************************************************************************
|
||||
iterator end()
|
||||
{
|
||||
return mend;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const iterator to the end of the array.
|
||||
//*************************************************************************
|
||||
const_iterator end() const
|
||||
{
|
||||
return mend;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
// Returns a const iterator to the end of the array.
|
||||
//*************************************************************************
|
||||
const_iterator cend() const
|
||||
{
|
||||
return mend;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
// Returns an reverse iterator to the reverse beginning of the array.
|
||||
//*************************************************************************
|
||||
reverse_iterator rbegin()
|
||||
{
|
||||
return reverse_iterator(mend);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const reverse iterator to the reverse beginning of the array.
|
||||
//*************************************************************************
|
||||
const_reverse_iterator rbegin() const
|
||||
{
|
||||
return const_reverse_iterator(mend);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const reverse iterator to the reverse beginning of the array.
|
||||
//*************************************************************************
|
||||
const_reverse_iterator crbegin() const
|
||||
{
|
||||
return const_reverse_iterator(mend);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a reverse iterator to the end of the array.
|
||||
//*************************************************************************
|
||||
reverse_iterator rend()
|
||||
{
|
||||
return reverse_iterator(mbegin);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const reverse iterator to the end of the array.
|
||||
//*************************************************************************
|
||||
const_reverse_iterator rend() const
|
||||
{
|
||||
return const_reverse_iterator(mbegin);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const reverse iterator to the end of the array.
|
||||
//*************************************************************************
|
||||
const_reverse_iterator crend() const
|
||||
{
|
||||
return const_reverse_iterator(mbegin);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns <b>true</b> if the array size is zero.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR bool empty() const
|
||||
{
|
||||
return (mbegin == mend);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns the size of the array.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR size_t size() const
|
||||
{
|
||||
return static_cast<size_t>(mend - mbegin);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns the maximum possible size of the array.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR size_t max_size() const
|
||||
{
|
||||
return size();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Assign from a view.
|
||||
//*************************************************************************
|
||||
array_view& operator=(const array_view& other)
|
||||
{
|
||||
mbegin = other.mbegin;
|
||||
mend = other.mend;
|
||||
return *this;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Assign from iterators
|
||||
//*************************************************************************
|
||||
template <typename TIterator>
|
||||
void assign(const TIterator begin_, const TIterator end_)
|
||||
{
|
||||
mbegin = etl::addressof(*begin_);
|
||||
mend = etl::addressof(*begin_) + etl::distance(begin_, end_);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Assign from iterator and size.
|
||||
//*************************************************************************
|
||||
template <typename TIterator,
|
||||
typename TSize>
|
||||
void assign(const TIterator begin_, const TSize size_)
|
||||
{
|
||||
mbegin = etl::addressof(*begin_);
|
||||
mend = etl::addressof(*begin_) + size_;
|
||||
}
|
||||
|
||||
#if defined(ETL_ARRAY_VIEW_IS_MUTABLE)
|
||||
//*************************************************************************
|
||||
/// Returns a reference to the indexed value.
|
||||
//*************************************************************************
|
||||
reference operator[](const size_t i)
|
||||
{
|
||||
return mbegin[i];
|
||||
}
|
||||
#endif
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const reference to the indexed value.
|
||||
//*************************************************************************
|
||||
const_reference operator[](const size_t i) const
|
||||
{
|
||||
return mbegin[i];
|
||||
}
|
||||
|
||||
#if defined(ETL_ARRAY_VIEW_IS_MUTABLE)
|
||||
//*************************************************************************
|
||||
/// Returns a reference to the indexed value.
|
||||
//*************************************************************************
|
||||
reference at(const size_t i)
|
||||
{
|
||||
ETL_ASSERT((mbegin != ETL_NULLPTR && mend != ETL_NULLPTR), ETL_ERROR(array_view_uninitialised));
|
||||
ETL_ASSERT(i < size(), ETL_ERROR(array_view_bounds));
|
||||
return mbegin[i];
|
||||
}
|
||||
#endif
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const reference to the indexed value.
|
||||
//*************************************************************************
|
||||
const_reference at(const size_t i) const
|
||||
{
|
||||
ETL_ASSERT((mbegin != ETL_NULLPTR && mend != ETL_NULLPTR), ETL_ERROR(array_view_uninitialised));
|
||||
ETL_ASSERT(i < size(), ETL_ERROR(array_view_bounds));
|
||||
return mbegin[i];
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Swaps with another array_view.
|
||||
//*************************************************************************
|
||||
void swap(array_view& other)
|
||||
{
|
||||
using ETL_OR_STD::swap; // Allow ADL
|
||||
|
||||
swap(mbegin, other.mbegin);
|
||||
swap(mend, other.mend);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Shrinks the view by moving its start forward.
|
||||
//*************************************************************************
|
||||
void remove_prefix(const size_type n)
|
||||
{
|
||||
if (n < size())
|
||||
mbegin += n;
|
||||
else
|
||||
mbegin = mend;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Shrinks the view by moving its end backward.
|
||||
//*************************************************************************
|
||||
void remove_suffix(const size_type n)
|
||||
{
|
||||
if (n < size())
|
||||
mend -= n;
|
||||
else
|
||||
mend = mbegin;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Fills the array.
|
||||
//*************************************************************************
|
||||
void fill(const T& value)
|
||||
{
|
||||
etl::fill(begin(), end(), value);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Equality for array views.
|
||||
//*************************************************************************
|
||||
friend bool operator == (const array_view<T>& lhs, const array_view<T>& rhs)
|
||||
{
|
||||
return (lhs.size() == rhs.size()) &&
|
||||
etl::equal(lhs.begin(), lhs.end(), rhs.begin());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Inequality for array views.
|
||||
//*************************************************************************
|
||||
friend bool operator != (const array_view<T>& lhs, const array_view<T>& rhs)
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Less-than for array views.
|
||||
//*************************************************************************
|
||||
friend bool operator < (const array_view<T>& lhs, const array_view<T>& rhs)
|
||||
{
|
||||
return etl::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Greater-than for array views.
|
||||
//*************************************************************************
|
||||
friend bool operator > (const array_view<T>& lhs, const array_view<T>& rhs)
|
||||
{
|
||||
return rhs < lhs;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Less-than-equal for array views.
|
||||
//*************************************************************************
|
||||
friend bool operator <= (const array_view<T>& lhs, const array_view<T>& rhs)
|
||||
{
|
||||
return !(lhs > rhs);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Greater-than-equal for array views.
|
||||
//*************************************************************************
|
||||
friend bool operator >= (const array_view<T>& lhs, const array_view<T>& rhs)
|
||||
{
|
||||
return !(lhs < rhs);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
pointer mbegin;
|
||||
pointer mend;
|
||||
};
|
||||
|
||||
//*************************************************************************
|
||||
/// Template deduction guides.
|
||||
//*************************************************************************
|
||||
#if ETL_USING_CPP17
|
||||
template <typename TArray>
|
||||
array_view(TArray& a)
|
||||
-> array_view<typename TArray::value_type>;
|
||||
|
||||
template <typename TIterator>
|
||||
array_view(const TIterator begin_, const TIterator end_)
|
||||
-> array_view<etl::remove_pointer_t<TIterator>>;
|
||||
|
||||
template <typename TIterator,
|
||||
typename TSize>
|
||||
array_view(const TIterator begin_, const TSize size_)
|
||||
-> array_view<etl::remove_pointer_t<TIterator>>;
|
||||
#endif
|
||||
|
||||
//*************************************************************************
|
||||
/// Hash function.
|
||||
//*************************************************************************
|
||||
#if ETL_USING_8BIT_TYPES
|
||||
template <typename T>
|
||||
struct hash<etl::array_view<T> >
|
||||
{
|
||||
size_t operator()(const etl::array_view<T>& view) const
|
||||
{
|
||||
return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(view.data()),
|
||||
reinterpret_cast<const uint8_t*>(view.data() + view.size()));
|
||||
}
|
||||
};
|
||||
#endif
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Swaps the values.
|
||||
//*************************************************************************
|
||||
template <typename T>
|
||||
void swap(etl::array_view<T>& lhs, etl::array_view<T>& rhs)
|
||||
{
|
||||
lhs.swap(rhs);
|
||||
}
|
||||
|
||||
#endif
|
||||
418
Chapter18/cib/libs/etl/include/etl/array_wrapper.h
Normal file
418
Chapter18/cib/libs/etl/include/etl/array_wrapper.h
Normal file
@@ -0,0 +1,418 @@
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2017 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_ARRAY_WRAPPER_INCLUDED
|
||||
#define ETL_ARRAY_WRAPPER_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "iterator.h"
|
||||
#include "error_handler.h"
|
||||
#include "exception.h"
|
||||
#include "hash.h"
|
||||
#include "parameter_type.h"
|
||||
#include "algorithm.h"
|
||||
|
||||
///\defgroup array array
|
||||
/// A wrapper for arrays
|
||||
///\ingroup containers
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
/// The base class for array_wrapper exceptions.
|
||||
//***************************************************************************
|
||||
class array_wrapper_exception : public exception
|
||||
{
|
||||
public:
|
||||
|
||||
array_wrapper_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
|
||||
: exception(reason_, file_name_, line_number_)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
///\ingroup stack
|
||||
/// The exception thrown when the index is out of bounds.
|
||||
//***************************************************************************
|
||||
class array_wrapper_bounds : public array_wrapper_exception
|
||||
{
|
||||
public:
|
||||
|
||||
array_wrapper_bounds(string_type file_name_, numeric_type line_number_)
|
||||
: array_wrapper_exception(ETL_ERROR_TEXT("array_wrapper:bounds", ETL_ARRAY_WRAPPER_FILE_ID"A"), file_name_, line_number_)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Array wrapper.
|
||||
//***************************************************************************
|
||||
template <typename T, size_t SIZE_, T(&ARRAY_)[SIZE_]>
|
||||
class array_wrapper
|
||||
{
|
||||
public:
|
||||
|
||||
typedef T value_type;
|
||||
typedef size_t size_type;
|
||||
typedef T& reference;
|
||||
typedef const T& const_reference;
|
||||
typedef T* pointer;
|
||||
typedef const T* const_pointer;
|
||||
typedef T* iterator;
|
||||
typedef const T* const_iterator;
|
||||
typedef ETL_OR_STD::reverse_iterator<iterator> reverse_iterator;
|
||||
typedef ETL_OR_STD::reverse_iterator<const_iterator> const_reverse_iterator;
|
||||
|
||||
typedef typename etl::parameter_type<T>::type parameter_t;
|
||||
|
||||
// Indexes for positions in the array.
|
||||
enum
|
||||
{
|
||||
SIZE = SIZE_,
|
||||
MAX_SIZE = SIZE_,
|
||||
FRONT = 0,
|
||||
BACK = SIZE - 1,
|
||||
BEGIN = 0,
|
||||
END = SIZE,
|
||||
RBEGIN = SIZE - 1,
|
||||
REND = -1
|
||||
};
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a reference to the first element.
|
||||
//*************************************************************************
|
||||
reference front()
|
||||
{
|
||||
return *&ARRAY_[FRONT];
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const reference to the first element.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR const_reference front() const
|
||||
{
|
||||
return *&ARRAY_[FRONT];
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a reference to the last element.
|
||||
//*************************************************************************
|
||||
reference back()
|
||||
{
|
||||
return *&ARRAY_[BACK];
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const reference to the last element.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR const_reference back() const
|
||||
{
|
||||
return *&ARRAY_[BACK];
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a pointer to the first element of the internal storage.
|
||||
//*************************************************************************
|
||||
pointer data()
|
||||
{
|
||||
return &ARRAY_[BEGIN];
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const pointer to the first element of the internal storage.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR const_pointer data() const
|
||||
{
|
||||
return &ARRAY_[BEGIN];
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns an iterator to the beginning of the array.
|
||||
//*************************************************************************
|
||||
iterator begin()
|
||||
{
|
||||
return &ARRAY_[BEGIN];
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const iterator to the beginning of the array.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR const_iterator begin() const
|
||||
{
|
||||
return &ARRAY_[BEGIN];
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const iterator to the beginning of the array.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR const_iterator cbegin() const
|
||||
{
|
||||
return &ARRAY_[BEGIN];
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns an iterator to the end of the array.
|
||||
//*************************************************************************
|
||||
iterator end()
|
||||
{
|
||||
return &ARRAY_[END];
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const iterator to the end of the array.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR const_iterator end() const
|
||||
{
|
||||
return &ARRAY_[END];
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
// Returns a const iterator to the end of the array.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR const_iterator cend() const
|
||||
{
|
||||
return &ARRAY_[END];
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
// Returns an reverse iterator to the reverse beginning of the array.
|
||||
//*************************************************************************
|
||||
reverse_iterator rbegin()
|
||||
{
|
||||
return reverse_iterator(&ARRAY_[END]);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const reverse iterator to the reverse beginning of the array.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR const_reverse_iterator rbegin() const
|
||||
{
|
||||
return const_reverse_iterator(&ARRAY_[END]);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const reverse iterator to the reverse beginning of the array.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR const_reverse_iterator crbegin() const
|
||||
{
|
||||
return const_reverse_iterator(&ARRAY_[END]);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a reverse iterator to the end of the array.
|
||||
//*************************************************************************
|
||||
reverse_iterator rend()
|
||||
{
|
||||
return reverse_iterator(&ARRAY_[BEGIN]);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const reverse iterator to the end of the array.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR const_reverse_iterator rend() const
|
||||
{
|
||||
return const_reverse_iterator(&ARRAY_[BEGIN]);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const reverse iterator to the end of the array.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR const_reverse_iterator crend() const
|
||||
{
|
||||
return const_reverse_iterator(&ARRAY_[BEGIN]);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns the size of the array.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR size_t size() const
|
||||
{
|
||||
return SIZE;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns the maximum possible size of the array.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR size_t max_size() const
|
||||
{
|
||||
return MAX_SIZE;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a reference to the indexed value.
|
||||
//*************************************************************************
|
||||
reference operator[](size_t i)
|
||||
{
|
||||
return ARRAY_[i];
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const reference to the indexed value.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR const_reference operator[](size_t i) const
|
||||
{
|
||||
return ARRAY_[i];
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a reference to the indexed value.
|
||||
//*************************************************************************
|
||||
reference at(size_t i)
|
||||
{
|
||||
ETL_ASSERT(i < SIZE, ETL_ERROR(etl::array_wrapper_bounds));
|
||||
return ARRAY_[i];
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns a const reference to the indexed value.
|
||||
//*************************************************************************
|
||||
const_reference at(size_t i) const
|
||||
{
|
||||
ETL_ASSERT(i < SIZE, ETL_ERROR(etl::array_wrapper_bounds));
|
||||
return ARRAY_[i];
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Fills the array.
|
||||
//*************************************************************************
|
||||
void fill(parameter_t value)
|
||||
{
|
||||
etl::fill(begin(), end(), value);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Swaps the contents of arrays.
|
||||
//*************************************************************************
|
||||
template <typename U, U(&ARRAYOTHER)[SIZE_]>
|
||||
typename etl::enable_if<etl::is_same<T, U>::value, void>::type
|
||||
swap(etl::array_wrapper<U, SIZE_, ARRAYOTHER>& other)
|
||||
{
|
||||
using ETL_OR_STD::swap; // Allow ADL
|
||||
|
||||
for (size_t i = 0UL; i < SIZE; ++i)
|
||||
{
|
||||
swap(ARRAY_[i], other.begin()[i]);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
//*************************************************************************
|
||||
/// Equality for array wrappers.
|
||||
//*************************************************************************
|
||||
template <typename TL, typename TR, size_t SIZEL, size_t SIZER, TL(&ARRAYL)[SIZEL], TR(&ARRAYR)[SIZER]>
|
||||
bool operator == (const etl::array_wrapper<TL, SIZEL, ARRAYL>& lhs,
|
||||
const etl::array_wrapper<TR, SIZER, ARRAYR>& rhs)
|
||||
{
|
||||
return (SIZEL == SIZER) && etl::equal(lhs.begin(), lhs.end(), rhs.begin());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Inequality for array wrapper.
|
||||
//*************************************************************************
|
||||
template <typename TL, typename TR, size_t SIZEL, size_t SIZER, TL(&ARRAYL)[SIZEL], TR(&ARRAYR)[SIZER]>
|
||||
bool operator != (const etl::array_wrapper<TL, SIZEL, ARRAYL>& lhs,
|
||||
const etl::array_wrapper<TR, SIZER, ARRAYR>& rhs)
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Less-than for array wrapper.
|
||||
//*************************************************************************
|
||||
template <typename TL, typename TR, size_t SIZEL, size_t SIZER, TL(&ARRAYL)[SIZEL], TR(&ARRAYR)[SIZER]>
|
||||
bool operator < (const etl::array_wrapper<TL, SIZEL, ARRAYL>& lhs,
|
||||
const etl::array_wrapper<TR, SIZER, ARRAYR>& rhs)
|
||||
{
|
||||
return etl::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Greater-than for array wrapper.
|
||||
//*************************************************************************
|
||||
template <typename TL, typename TR, size_t SIZEL, size_t SIZER, TL(&ARRAYL)[SIZEL], TR(&ARRAYR)[SIZER]>
|
||||
bool operator > (const etl::array_wrapper<TL, SIZEL, ARRAYL>& lhs,
|
||||
const etl::array_wrapper<TR, SIZER, ARRAYR>& rhs)
|
||||
{
|
||||
return rhs < lhs;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Less-than-equal for array wrapper.
|
||||
//*************************************************************************
|
||||
template <typename TL, typename TR, size_t SIZEL, size_t SIZER, TL(&ARRAYL)[SIZEL], TR(&ARRAYR)[SIZER]>
|
||||
bool operator <= (const etl::array_wrapper<TL, SIZEL, ARRAYL>& lhs,
|
||||
const etl::array_wrapper<TR, SIZER, ARRAYR>& rhs)
|
||||
{
|
||||
return !(lhs > rhs);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Greater-than-equal for array wrapper.
|
||||
//*************************************************************************
|
||||
template <typename TL, typename TR, size_t SIZEL, size_t SIZER, TL(&ARRAYL)[SIZEL], TR(&ARRAYR)[SIZER]>
|
||||
bool operator >= (const etl::array_wrapper<TL, SIZEL, ARRAYL>& lhs,
|
||||
const etl::array_wrapper<TR, SIZER, ARRAYR>& rhs)
|
||||
{
|
||||
return !(lhs < rhs);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Hash function.
|
||||
//*************************************************************************
|
||||
#if ETL_USING_8BIT_TYPES
|
||||
template <typename T, size_t SIZE, T(&ARRAY)[SIZE]>
|
||||
struct hash<etl::array_wrapper<T, SIZE, ARRAY> >
|
||||
{
|
||||
size_t operator()(const etl::array_wrapper<T, SIZE, ARRAY>& aw) const
|
||||
{
|
||||
const uint8_t* pb = reinterpret_cast<const uint8_t*>(aw.data());
|
||||
const uint8_t* pe = pb + (SIZE * sizeof(T));
|
||||
|
||||
return etl::private_hash::generic_hash<size_t>(pb, pe);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Swap.
|
||||
//*************************************************************************
|
||||
template <typename T, size_t SIZE, T(&ARRAYL)[SIZE], T(&ARRAYR)[SIZE]>
|
||||
void swap(etl::array_wrapper<T, SIZE, ARRAYL>& lhs,
|
||||
etl::array_wrapper<T, SIZE, ARRAYR>& rhs)
|
||||
{
|
||||
lhs.swap(rhs);
|
||||
}
|
||||
|
||||
#define ETL_ARRAY_WRAPPER(arraytype, arrayobject) etl::array_wrapper<arraytype, ETL_ARRAY_SIZE(arrayobject), arrayobject>
|
||||
|
||||
#endif
|
||||
|
||||
48
Chapter18/cib/libs/etl/include/etl/atomic.h
Normal file
48
Chapter18/cib/libs/etl/include/etl/atomic.h
Normal file
@@ -0,0 +1,48 @@
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2017 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_ATOMIC_INCLUDED
|
||||
#define ETL_ATOMIC_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
|
||||
#if ETL_HAS_ATOMIC
|
||||
#if (ETL_USING_CPP11 && (ETL_USING_STL || defined(ETL_IN_UNIT_TEST)))
|
||||
#include "atomic/atomic_std.h"
|
||||
#elif defined(ETL_COMPILER_ARM5)
|
||||
#include "atomic/atomic_arm.h"
|
||||
#elif defined(ETL_COMPILER_ARM6)
|
||||
#include "atomic/atomic_arm.h"
|
||||
#elif defined(ETL_COMPILER_GCC)
|
||||
#include "atomic/atomic_gcc_sync.h"
|
||||
#elif defined(ETL_COMPILER_CLANG)
|
||||
#include "atomic/atomic_clang_sync.h"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif
|
||||
34
Chapter18/cib/libs/etl/include/etl/atomic/atomic_arm.h
Normal file
34
Chapter18/cib/libs/etl/include/etl/atomic/atomic_arm.h
Normal file
@@ -0,0 +1,34 @@
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2017 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_ATOMIC_ARM_INCLUDED
|
||||
#define ETL_ATOMIC_ARM_INCLUDED
|
||||
|
||||
#include "atomic_gcc_sync.h"
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,34 @@
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2017 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_ATOMIC_CLANG_INCLUDED
|
||||
#define ETL_ATOMIC_CLANG_INCLUDED
|
||||
|
||||
#include "atomic_gcc_sync.h"
|
||||
|
||||
#endif
|
||||
2236
Chapter18/cib/libs/etl/include/etl/atomic/atomic_gcc_sync.h
Normal file
2236
Chapter18/cib/libs/etl/include/etl/atomic/atomic_gcc_sync.h
Normal file
File diff suppressed because it is too large
Load Diff
120
Chapter18/cib/libs/etl/include/etl/atomic/atomic_std.h
Normal file
120
Chapter18/cib/libs/etl/include/etl/atomic/atomic_std.h
Normal file
@@ -0,0 +1,120 @@
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2018 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_ATOMIC_STD_INCLUDED
|
||||
#define ETL_ATOMIC_STD_INCLUDED
|
||||
|
||||
#include "../platform.h"
|
||||
#include "../nullptr.h"
|
||||
#include "../char_traits.h"
|
||||
|
||||
#include <atomic>
|
||||
#include <stdint.h>
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
// ETL Atomic type for compilers that support std::atomic.
|
||||
// etl::atomic is a simple wrapper around std::atomic.
|
||||
//***************************************************************************
|
||||
|
||||
template <typename T>
|
||||
using atomic = std::atomic<T>;
|
||||
|
||||
using memory_order = std::memory_order;
|
||||
|
||||
static ETL_CONSTANT etl::memory_order memory_order_relaxed = std::memory_order_relaxed;
|
||||
static ETL_CONSTANT etl::memory_order memory_order_consume = std::memory_order_consume;
|
||||
static ETL_CONSTANT etl::memory_order memory_order_acquire = std::memory_order_acquire;
|
||||
static ETL_CONSTANT etl::memory_order memory_order_release = std::memory_order_release;
|
||||
static ETL_CONSTANT etl::memory_order memory_order_acq_rel = std::memory_order_acq_rel;
|
||||
static ETL_CONSTANT etl::memory_order memory_order_seq_cst = std::memory_order_seq_cst;
|
||||
|
||||
using atomic_bool = std::atomic<bool>;
|
||||
using atomic_char = std::atomic<char>;
|
||||
using atomic_schar = std::atomic<signed char>;
|
||||
using atomic_uchar = std::atomic<unsigned char>;
|
||||
using atomic_short = std::atomic<short>;
|
||||
using atomic_ushort = std::atomic<unsigned short>;
|
||||
using atomic_int = std::atomic<int>;
|
||||
using atomic_uint = std::atomic<unsigned int>;
|
||||
using atomic_long = std::atomic<long>;
|
||||
using atomic_ulong = std::atomic<unsigned long>;
|
||||
using atomic_llong = std::atomic<long long>;
|
||||
using atomic_ullong = std::atomic<unsigned long long>;
|
||||
using atomic_wchar_t = std::atomic<wchar_t>;
|
||||
#if ETL_HAS_NATIVE_CHAR8_T
|
||||
using atomic_char8_t = std::atomic<char8_t>;
|
||||
#endif
|
||||
#if ETL_HAS_NATIVE_CHAR16_T
|
||||
using atomic_char16_t = std::atomic<char16_t>;
|
||||
#endif
|
||||
#if ETL_HAS_NATIVE_CHAR32_T
|
||||
using atomic_char32_t = std::atomic<char32_t>;
|
||||
#endif
|
||||
#if ETL_USING_8BIT_TYPES
|
||||
using atomic_uint8_t = std::atomic<uint8_t>;
|
||||
using atomic_int8_t = std::atomic<int8_t>;
|
||||
#endif
|
||||
using atomic_uint16_t = std::atomic<uint16_t>;
|
||||
using atomic_int16_t = std::atomic<int16_t>;
|
||||
using atomic_uint32_t = std::atomic<uint32_t>;
|
||||
using atomic_int32_t = std::atomic<int32_t>;
|
||||
#if ETL_USING_64BIT_TYPES
|
||||
using atomic_uint64_t = std::atomic<uint64_t>;
|
||||
using atomic_int64_t = std::atomic<int64_t>;
|
||||
#endif
|
||||
using atomic_int_least8_t = std::atomic<int_least8_t>;
|
||||
using atomic_uint_least8_t = std::atomic<uint_least8_t>;
|
||||
using atomic_int_least16_t = std::atomic<int_least16_t>;
|
||||
using atomic_uint_least16_t = std::atomic<uint_least16_t>;
|
||||
using atomic_int_least32_t = std::atomic<int_least32_t>;
|
||||
using atomic_uint_least32_t = std::atomic<uint_least32_t>;
|
||||
#if ETL_USING_64BIT_TYPES
|
||||
using atomic_int_least64_t = std::atomic<int_least64_t>;
|
||||
using atomic_uint_least64_t = std::atomic<uint_least64_t>;
|
||||
#endif
|
||||
using atomic_int_fast8_t = std::atomic<int_fast8_t>;
|
||||
using atomic_uint_fast8_t = std::atomic<uint_fast8_t>;
|
||||
using atomic_int_fast16_t = std::atomic<int_fast16_t>;
|
||||
using atomic_uint_fast16_t = std::atomic<uint_fast16_t>;
|
||||
using atomic_int_fast32_t = std::atomic<int_fast32_t>;
|
||||
using atomic_uint_fast32_t = std::atomic<uint_fast32_t>;
|
||||
#if ETL_USING_64BIT_TYPES
|
||||
using atomic_int_fast64_t = std::atomic<int_fast64_t>;
|
||||
using atomic_uint_fast64_t = std::atomic<uint_fast64_t>;
|
||||
#endif
|
||||
using atomic_intptr_t = std::atomic<intptr_t>;
|
||||
using atomic_uintptr_t = std::atomic<uintptr_t>;
|
||||
using atomic_size_t = std::atomic<size_t>;
|
||||
using atomic_ptrdiff_t = std::atomic<ptrdiff_t>;
|
||||
using atomic_intmax_t = std::atomic<intmax_t>;
|
||||
using atomic_uintmax_t = std::atomic<uintmax_t>;
|
||||
}
|
||||
|
||||
#endif
|
||||
218
Chapter18/cib/libs/etl/include/etl/base64.h
Normal file
218
Chapter18/cib/libs/etl/include/etl/base64.h
Normal file
@@ -0,0 +1,218 @@
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
Copyright(c) 2024 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_BASE64_INCLUDED
|
||||
#define ETL_BASE64_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "static_assert.h"
|
||||
#include "exception.h"
|
||||
#include "error_handler.h"
|
||||
#include "type_traits.h"
|
||||
#include "enum_type.h"
|
||||
#include "integral_limits.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/**************************************************************************************************************************************************************************
|
||||
* See https://en.wikipedia.org/wiki/Base64
|
||||
*
|
||||
* Encoding Encoding characters Separate encoding of lines Decoding non-encoding characters
|
||||
* 62nd 63rd Pad Separators Length Checksum
|
||||
* RFC 1421 : Base64 for Privacy - Enhanced Mail(deprecated) + / = mandatory CR + LF 64, or lower for the last line No No
|
||||
* RFC 2045 : Base64 transfer encoding for MIME + / = mandatory CR + LF At most 76 No Discarded
|
||||
* RFC 2152 : Base64 for UTF - 7 + / No No No
|
||||
* RFC 3501 : Base64 encoding for IMAP mailbox names + , No No No
|
||||
* RFC 4648 : base64(standard)[a] + / = optional No No
|
||||
* RFC 4648 : base64url(URL - and filename - safe standard) - _ = optional No No
|
||||
**************************************************************************************************************************************************************************/
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
/// Exception base for base64
|
||||
//***************************************************************************
|
||||
class base64_exception : public etl::exception
|
||||
{
|
||||
public:
|
||||
|
||||
base64_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
|
||||
: exception(reason_, file_name_, line_number_)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// buffer overflow exception.
|
||||
//***************************************************************************
|
||||
class base64_overflow : public base64_exception
|
||||
{
|
||||
public:
|
||||
|
||||
base64_overflow(string_type file_name_, numeric_type line_number_)
|
||||
: base64_exception(ETL_ERROR_TEXT("base64:overflow", ETL_BASE64_FILE_ID"A"), file_name_, line_number_)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Illegal character exception.
|
||||
//***************************************************************************
|
||||
class base64_invalid_data : public base64_exception
|
||||
{
|
||||
public:
|
||||
|
||||
base64_invalid_data(string_type file_name_, numeric_type line_number_)
|
||||
: base64_exception(ETL_ERROR_TEXT("base64:invalid data", ETL_BASE64_FILE_ID"B"), file_name_, line_number_)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Invalid decode input length exception.
|
||||
//***************************************************************************
|
||||
class base64_invalid_decode_input_length : public base64_exception
|
||||
{
|
||||
public:
|
||||
|
||||
base64_invalid_decode_input_length(string_type file_name_, numeric_type line_number_)
|
||||
: base64_exception(ETL_ERROR_TEXT("base64:invalid decode input length", ETL_BASE64_FILE_ID"C"), file_name_, line_number_)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Common Base64 definitions
|
||||
//***************************************************************************
|
||||
class base64
|
||||
{
|
||||
public:
|
||||
|
||||
struct Encoding
|
||||
{
|
||||
enum enum_type
|
||||
{
|
||||
//RFC_1421, // Not implemented
|
||||
//RFC_2045, // Not implemented
|
||||
RFC_2152,
|
||||
RFC_3501,
|
||||
RFC_4648,
|
||||
RFC_4648_PADDING,
|
||||
RFC_4648_URL,
|
||||
RFC_4648_URL_PADDING,
|
||||
};
|
||||
|
||||
ETL_DECLARE_ENUM_TYPE(Encoding, int)
|
||||
//ETL_ENUM_TYPE(RFC_1421, "RFC_1421") // Not implemented
|
||||
//ETL_ENUM_TYPE(RFC_2045, "RFC_2045") // Not implemented
|
||||
ETL_ENUM_TYPE(RFC_2152, "RFC_2152")
|
||||
ETL_ENUM_TYPE(RFC_3501, "RFC_3501")
|
||||
ETL_ENUM_TYPE(RFC_4648, "RFC_4648")
|
||||
ETL_ENUM_TYPE(RFC_4648_PADDING, "RFC_4648_PADDING")
|
||||
ETL_ENUM_TYPE(RFC_4648_URL, "RFC_4648_URL")
|
||||
ETL_ENUM_TYPE(RFC_4648_URL_PADDING, "RFC_4648_URL_PADDING")
|
||||
ETL_END_ENUM_TYPE
|
||||
};
|
||||
|
||||
struct Padding
|
||||
{
|
||||
enum enum_type
|
||||
{
|
||||
No_Padding = 0,
|
||||
Use_Padding = 1
|
||||
};
|
||||
|
||||
ETL_DECLARE_ENUM_TYPE(Padding, bool)
|
||||
ETL_ENUM_TYPE(No_Padding, "No_Padding")
|
||||
ETL_ENUM_TYPE(Use_Padding, "Use_Padding")
|
||||
ETL_END_ENUM_TYPE
|
||||
};
|
||||
|
||||
struct Non_Coding_Characters
|
||||
{
|
||||
enum enum_type
|
||||
{
|
||||
Ignore = 0,
|
||||
Reject = 1
|
||||
};
|
||||
|
||||
ETL_DECLARE_ENUM_TYPE(Non_Coding_Characters, bool)
|
||||
ETL_ENUM_TYPE(Ignore, "Ignore")
|
||||
ETL_ENUM_TYPE(Reject, "Reject")
|
||||
ETL_END_ENUM_TYPE
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
Invalid_Data = etl::integral_limits<int>::max,
|
||||
Min_Encode_Buffer_Size = 4,
|
||||
Min_Decode_Buffer_Size = 3
|
||||
};
|
||||
|
||||
protected:
|
||||
|
||||
ETL_CONSTEXPR14
|
||||
base64(const char* encoder_table_,
|
||||
bool use_padding_)
|
||||
: encoder_table(encoder_table_)
|
||||
, use_padding(use_padding_)
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
// Character set for RFC-1421, RFC-2045, RFC-2152 and RFC-4648
|
||||
//*************************************************************************
|
||||
static
|
||||
ETL_CONSTEXPR14
|
||||
const char* character_set_1()
|
||||
{
|
||||
return "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
// Character set for RFC-4648-URL
|
||||
//*************************************************************************
|
||||
static
|
||||
ETL_CONSTEXPR14
|
||||
const char* character_set_2()
|
||||
{
|
||||
return "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
// Character set for RFC-3501-URL
|
||||
//*************************************************************************
|
||||
static
|
||||
ETL_CONSTEXPR14
|
||||
const char* character_set_3()
|
||||
{
|
||||
return "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
|
||||
}
|
||||
|
||||
const char* encoder_table;
|
||||
const bool use_padding;
|
||||
};
|
||||
}
|
||||
#endif
|
||||
868
Chapter18/cib/libs/etl/include/etl/base64_decoder.h
Normal file
868
Chapter18/cib/libs/etl/include/etl/base64_decoder.h
Normal file
@@ -0,0 +1,868 @@
|
||||
//*************************************************************************
|
||||
///Decode from Base64 from and to pointer/length
|
||||
//*************************************************************************///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
Copyright(c) 2024 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_BASE64_DECODER_INCLUDED
|
||||
#define ETL_BASE64_DECODER_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "static_assert.h"
|
||||
#include "error_handler.h"
|
||||
#include "type_traits.h"
|
||||
#include "binary.h"
|
||||
#include "algorithm.h"
|
||||
#include "integral_limits.h"
|
||||
#include "iterator.h"
|
||||
#include "enum_type.h"
|
||||
#include "delegate.h"
|
||||
#include "span.h"
|
||||
|
||||
#include "base64.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#if ETL_USING_STL
|
||||
#include <iterator>
|
||||
#endif
|
||||
|
||||
#define ETL_IS_8_BIT_INTEGRAL(Type) (etl::is_integral<typename etl::remove_cv<Type>::type>::value && \
|
||||
(etl::integral_limits<typename etl::remove_cv<Type>::type>::bits == 8U))
|
||||
|
||||
#define ETL_IS_ITERATOR_TYPE_8_BIT_INTEGRAL(Type) (etl::is_integral<typename etl::iterator_traits<typename etl::remove_cv<Type>::type>::value_type>::value && \
|
||||
(etl::integral_limits<typename etl::iterator_traits<typename etl::remove_cv<Type>::type>::value_type>::bits == 8U))
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//*************************************************************************
|
||||
/// Base64 Decoder
|
||||
//*************************************************************************
|
||||
class ibase64_decoder : public base64
|
||||
{
|
||||
public:
|
||||
|
||||
typedef etl::span<const unsigned char> span_type;
|
||||
typedef etl::delegate<void(const span_type&)> callback_type;
|
||||
|
||||
//*************************************************************************
|
||||
/// Decode to Base64
|
||||
//*************************************************************************
|
||||
template <typename T>
|
||||
ETL_CONSTEXPR14
|
||||
bool decode(T value)
|
||||
{
|
||||
ETL_STATIC_ASSERT(ETL_IS_8_BIT_INTEGRAL(T), "Input type must be an 8 bit integral");
|
||||
|
||||
push_to_input_buffer(value);
|
||||
|
||||
if (input_buffer_is_full())
|
||||
{
|
||||
if (decode_block())
|
||||
{
|
||||
if (callback.is_valid())
|
||||
{
|
||||
if (output_buffer_is_full())
|
||||
{
|
||||
callback(span());
|
||||
reset_output_buffer();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
reset_input_buffer();
|
||||
}
|
||||
|
||||
return !error();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Decode from Base64
|
||||
//*************************************************************************
|
||||
template <typename TInputIterator>
|
||||
ETL_CONSTEXPR14
|
||||
bool decode(TInputIterator input_begin, TInputIterator input_end)
|
||||
{
|
||||
ETL_STATIC_ASSERT(ETL_IS_ITERATOR_TYPE_8_BIT_INTEGRAL(TInputIterator), "Input type must be an 8 bit integral");
|
||||
|
||||
while (input_begin != input_end)
|
||||
{
|
||||
if (!decode(*input_begin++))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Decode from Base64
|
||||
//*************************************************************************
|
||||
template <typename TInputIterator>
|
||||
ETL_CONSTEXPR14
|
||||
bool decode(TInputIterator input_begin, size_t input_length)
|
||||
{
|
||||
ETL_STATIC_ASSERT(ETL_IS_ITERATOR_TYPE_8_BIT_INTEGRAL(TInputIterator), "Input type must be an 8 bit integral");
|
||||
|
||||
while (input_length-- != 0)
|
||||
{
|
||||
if (!decode(*input_begin++))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Decode from Base64
|
||||
//*************************************************************************
|
||||
template <typename TInputIterator>
|
||||
ETL_CONSTEXPR14
|
||||
bool decode_final(TInputIterator input_begin, TInputIterator input_end)
|
||||
{
|
||||
return decode(input_begin, input_end) && flush();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Decode from Base64
|
||||
//*************************************************************************
|
||||
template <typename TInputIterator>
|
||||
ETL_CONSTEXPR14
|
||||
bool decode_final(TInputIterator input_begin, size_t input_length)
|
||||
{
|
||||
return decode(input_begin, input_length) && flush();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Flush any remaining data to the output.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
bool flush()
|
||||
{
|
||||
// Encode any remaining input data.
|
||||
bool success = decode_block();
|
||||
|
||||
reset_input_buffer();
|
||||
|
||||
if (success)
|
||||
{
|
||||
if (callback.is_valid())
|
||||
{
|
||||
// Send any remaining data.
|
||||
if (size() != 0)
|
||||
{
|
||||
callback(span());
|
||||
}
|
||||
|
||||
// Indicate this was the final block.
|
||||
callback(span_type());
|
||||
|
||||
reset_output_buffer();
|
||||
}
|
||||
}
|
||||
|
||||
padding_received = false;
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Reset the encoder.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
void restart()
|
||||
{
|
||||
reset_input_buffer();
|
||||
reset_output_buffer();
|
||||
overflow_detected = false;
|
||||
invalid_data_detected = false;
|
||||
padding_received = false;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns the beginning of the output buffer.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR14
|
||||
const unsigned char* begin() const
|
||||
{
|
||||
return p_output_buffer;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// This only returns a useful value if a callback has not been set or called.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR14
|
||||
const unsigned char* end() const
|
||||
{
|
||||
return p_output_buffer + output_buffer_length;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns the beginning of the output buffer.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR14
|
||||
const unsigned char* cbegin() const
|
||||
{
|
||||
return p_output_buffer;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// This only returns a useful value if a callback has not been set or called.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR14
|
||||
const unsigned char* cend() const
|
||||
{
|
||||
return p_output_buffer + output_buffer_length;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns the size of the output buffer.
|
||||
/// This only returns a useful value if a callback has not been set or called.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR14
|
||||
size_t size() const
|
||||
{
|
||||
return output_buffer_length;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns the maximum size of the output buffer.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR14
|
||||
size_t buffer_size() const
|
||||
{
|
||||
return output_buffer_max_size;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Get a span of the output data.
|
||||
/// This only returns a useful span if a callback has not been set or called.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR14
|
||||
span_type span() const
|
||||
{
|
||||
return span_type(begin(), end());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns true if the output buffer has overflowed
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR14
|
||||
bool overflow() const
|
||||
{
|
||||
return overflow_detected;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns true if an invalid character was detected.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR14
|
||||
bool invalid_data() const
|
||||
{
|
||||
return invalid_data_detected;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns true if an error was detected.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR14
|
||||
bool error() const
|
||||
{
|
||||
return overflow() || invalid_data();
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
ibase64_decoder(const char* encoder_table_,
|
||||
bool use_padding_,
|
||||
unsigned char* p_output_buffer_,
|
||||
size_t ouput_buffer_max_size_,
|
||||
callback_type callback_)
|
||||
: base64(encoder_table_, use_padding_)
|
||||
, input_buffer()
|
||||
, input_buffer_length(0)
|
||||
, p_output_buffer(p_output_buffer_)
|
||||
, output_buffer_length(0)
|
||||
, output_buffer_max_size(ouput_buffer_max_size_)
|
||||
, callback(callback_)
|
||||
, overflow_detected(false)
|
||||
, invalid_data_detected(false)
|
||||
, padding_received(false)
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Calculates the minimum buffer size required to decode from Base64
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
static
|
||||
ETL_CONSTEXPR14
|
||||
size_t decoded_size_from_valid_input_length(size_t input_length)
|
||||
{
|
||||
return input_length - (input_length / 4U);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
//*************************************************************************
|
||||
// Translates a sextet into an index
|
||||
//*************************************************************************
|
||||
template <typename T>
|
||||
ETL_CONSTEXPR14
|
||||
uint32_t get_index_from_sextet(T sextet)
|
||||
{
|
||||
const char* encoder_table_end = encoder_table + 64;
|
||||
const char* p_sextet = etl::find(encoder_table, encoder_table_end, static_cast<char>(sextet));
|
||||
|
||||
if (p_sextet != encoder_table_end)
|
||||
{
|
||||
return static_cast<uint32_t>(etl::distance(encoder_table, p_sextet));
|
||||
}
|
||||
else
|
||||
{
|
||||
invalid_data_detected = true;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Gets the padding character
|
||||
//*************************************************************************
|
||||
template <typename T>
|
||||
ETL_NODISCARD
|
||||
static
|
||||
ETL_CONSTEXPR14
|
||||
T padding()
|
||||
{
|
||||
return static_cast<T>('=');
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Decode one block of data.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
bool decode_block()
|
||||
{
|
||||
switch (input_buffer_length)
|
||||
{
|
||||
// Only triggered on call to flush().
|
||||
case 2:
|
||||
{
|
||||
uint32_t sextets = (get_index_from_sextet(input_buffer[0]) << 6);
|
||||
sextets = sextets | (get_index_from_sextet(input_buffer[1]));
|
||||
push_to_output_buffer((sextets >> 4) & 0xFF);
|
||||
break;
|
||||
}
|
||||
|
||||
// Only triggered on call to flush().
|
||||
case 3:
|
||||
{
|
||||
uint32_t sextets = (get_index_from_sextet(input_buffer[0]) << 12);
|
||||
sextets = sextets | (get_index_from_sextet(input_buffer[1]) << 6);
|
||||
sextets = sextets | (get_index_from_sextet(input_buffer[2]));
|
||||
push_to_output_buffer((sextets >> 10) & 0xFF);
|
||||
push_to_output_buffer((sextets >> 2) & 0xFF);
|
||||
break;
|
||||
}
|
||||
|
||||
// Only triggered on call to decode().
|
||||
case 4:
|
||||
{
|
||||
// Read in four sextets
|
||||
uint32_t sextets = (get_index_from_sextet(input_buffer[0]) << 18);
|
||||
sextets = sextets | (get_index_from_sextet(input_buffer[1]) << 12);
|
||||
sextets = sextets | (get_index_from_sextet(input_buffer[2]) << 6);
|
||||
sextets = sextets | (get_index_from_sextet(input_buffer[3]));
|
||||
|
||||
// Write out three octets
|
||||
push_to_output_buffer((sextets >> 16) & 0xFF);
|
||||
push_to_output_buffer((sextets >> 8) & 0xFF);
|
||||
push_to_output_buffer((sextets >> 0) & 0xFF);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ETL_ASSERT(!invalid_data_detected, ETL_ERROR(etl::base64_invalid_data));
|
||||
ETL_ASSERT(!overflow_detected, ETL_ERROR(etl::base64_overflow));
|
||||
|
||||
return (!invalid_data_detected && !overflow_detected);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
// Push to the output buffer.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
void push_to_output_buffer(unsigned char c)
|
||||
{
|
||||
if (output_buffer_length < output_buffer_max_size)
|
||||
{
|
||||
p_output_buffer[output_buffer_length++] = c;
|
||||
}
|
||||
else
|
||||
{
|
||||
overflow_detected = true;
|
||||
}
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
//
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
bool output_buffer_is_full() const
|
||||
{
|
||||
return output_buffer_length == output_buffer_max_size;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
//
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
bool output_buffer_is_empty() const
|
||||
{
|
||||
return output_buffer_length == 0;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
//
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
void reset_output_buffer()
|
||||
{
|
||||
output_buffer_length = 0;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
// Push to the input buffer.
|
||||
//*************************************************************************
|
||||
template <typename T>
|
||||
ETL_CONSTEXPR14
|
||||
void push_to_input_buffer(T value)
|
||||
{
|
||||
if (value == padding<T>())
|
||||
{
|
||||
padding_received = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (padding_received)
|
||||
{
|
||||
ETL_ASSERT_FAIL(ETL_ERROR(etl::base64_invalid_data));
|
||||
invalid_data_detected = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
input_buffer[input_buffer_length++] = static_cast<uint8_t>(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
//
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
bool input_buffer_is_full() const
|
||||
{
|
||||
return input_buffer_length == 4U;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
//
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
void reset_input_buffer()
|
||||
{
|
||||
input_buffer_length = 0;
|
||||
}
|
||||
|
||||
char input_buffer[4];
|
||||
size_t input_buffer_length;
|
||||
|
||||
unsigned char* p_output_buffer;
|
||||
size_t output_buffer_length;
|
||||
const size_t output_buffer_max_size;
|
||||
|
||||
callback_type callback;
|
||||
|
||||
bool overflow_detected;
|
||||
bool invalid_data_detected;
|
||||
bool padding_received;
|
||||
};
|
||||
|
||||
//*************************************************************************
|
||||
/// Base64 RFC-2152 Decoder
|
||||
//*************************************************************************
|
||||
template <size_t Buffer_Size = etl::base64::Min_Decode_Buffer_Size>
|
||||
class base64_rfc2152_decoder : public ibase64_decoder
|
||||
{
|
||||
public:
|
||||
|
||||
ETL_STATIC_ASSERT((Buffer_Size >= etl::base64::Min_Decode_Buffer_Size), "Buffer size must be greater than etl::base64::Min_Decode_Buffer_Size");
|
||||
|
||||
//*************************************************************************
|
||||
/// Base64 RFC-2152 constructor.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
base64_rfc2152_decoder()
|
||||
: ibase64_decoder(etl::base64::character_set_1(),
|
||||
etl::base64::Padding::No_Padding,
|
||||
output_buffer,
|
||||
Buffer_Size,
|
||||
callback_type())
|
||||
, output_buffer()
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Base64 RFC-2152 constructor.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
base64_rfc2152_decoder(callback_type callback_)
|
||||
: ibase64_decoder(etl::base64::character_set_1(),
|
||||
etl::base64::Padding::No_Padding,
|
||||
output_buffer,
|
||||
Buffer_Size,
|
||||
callback_)
|
||||
, output_buffer()
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Calculate the required output encode buffer size.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
static
|
||||
ETL_CONSTEXPR14
|
||||
size_t safe_output_buffer_size(size_t input_length)
|
||||
{
|
||||
return ibase64_decoder::decoded_size_from_valid_input_length(input_length);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
/// The internal output buffer.
|
||||
unsigned char output_buffer[Buffer_Size];
|
||||
};
|
||||
|
||||
//*************************************************************************
|
||||
/// Base64 RFC-3501 Decoder
|
||||
//*************************************************************************
|
||||
template <size_t Buffer_Size = etl::base64::Min_Decode_Buffer_Size>
|
||||
class base64_rfc3501_decoder : public ibase64_decoder
|
||||
{
|
||||
public:
|
||||
|
||||
ETL_STATIC_ASSERT((Buffer_Size >= etl::base64::Min_Decode_Buffer_Size), "Buffer size must be greater than etl::base64::Min_Decode_Buffer_Size");
|
||||
|
||||
//*************************************************************************
|
||||
/// Base64 RFC-3501 constructor.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
base64_rfc3501_decoder()
|
||||
: ibase64_decoder(etl::base64::character_set_3(),
|
||||
etl::base64::Padding::No_Padding,
|
||||
output_buffer,
|
||||
Buffer_Size,
|
||||
callback_type())
|
||||
, output_buffer()
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Base64 RFC-3501 constructor.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
base64_rfc3501_decoder(callback_type callback_)
|
||||
: ibase64_decoder(etl::base64::character_set_3(),
|
||||
etl::base64::Padding::No_Padding,
|
||||
output_buffer,
|
||||
Buffer_Size,
|
||||
callback_)
|
||||
, output_buffer()
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Calculate the required output encode buffer size.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
static
|
||||
ETL_CONSTEXPR14
|
||||
size_t safe_output_buffer_size(size_t input_length)
|
||||
{
|
||||
return ibase64_decoder::decoded_size_from_valid_input_length(input_length);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
/// The internal output buffer.
|
||||
unsigned char output_buffer[Buffer_Size];
|
||||
};
|
||||
|
||||
//*************************************************************************
|
||||
/// Base64 RFC-4648 Decoder
|
||||
//*************************************************************************
|
||||
template <size_t Buffer_Size = etl::base64::Min_Decode_Buffer_Size>
|
||||
class base64_rfc4648_decoder : public ibase64_decoder
|
||||
{
|
||||
public:
|
||||
|
||||
ETL_STATIC_ASSERT((Buffer_Size >= etl::base64::Min_Decode_Buffer_Size), "Buffer size must be greater than etl::base64::Min_Decode_Buffer_Size");
|
||||
|
||||
//*************************************************************************
|
||||
/// Base64 RFC-4648 constructor.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
base64_rfc4648_decoder()
|
||||
: ibase64_decoder(etl::base64::character_set_1(),
|
||||
etl::base64::Padding::No_Padding,
|
||||
output_buffer,
|
||||
Buffer_Size,
|
||||
callback_type())
|
||||
, output_buffer()
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Base64 RFC-4648 constructor.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
base64_rfc4648_decoder(callback_type callback_)
|
||||
: ibase64_decoder(etl::base64::character_set_1(),
|
||||
etl::base64::Padding::No_Padding,
|
||||
output_buffer,
|
||||
Buffer_Size,
|
||||
callback_)
|
||||
, output_buffer()
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Calculate the required output encode buffer size.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
static
|
||||
ETL_CONSTEXPR14
|
||||
size_t safe_output_buffer_size(size_t input_length)
|
||||
{
|
||||
return ibase64_decoder::decoded_size_from_valid_input_length(input_length);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
/// The internal output buffer.
|
||||
unsigned char output_buffer[Buffer_Size];
|
||||
};
|
||||
|
||||
//*************************************************************************
|
||||
/// Base64 RFC-4648-Padding Decoder
|
||||
//*************************************************************************
|
||||
template <size_t Buffer_Size = etl::base64::Min_Decode_Buffer_Size>
|
||||
class base64_rfc4648_padding_decoder : public ibase64_decoder
|
||||
{
|
||||
public:
|
||||
|
||||
ETL_STATIC_ASSERT((Buffer_Size >= etl::base64::Min_Decode_Buffer_Size), "Buffer size must be greater than etl::base64::Min_Decode_Buffer_Size");
|
||||
|
||||
//*************************************************************************
|
||||
/// Base64 RFC-4648-Padding constructor.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
base64_rfc4648_padding_decoder()
|
||||
: ibase64_decoder(etl::base64::character_set_1(),
|
||||
etl::base64::Padding::Use_Padding,
|
||||
output_buffer,
|
||||
Buffer_Size,
|
||||
callback_type())
|
||||
, output_buffer()
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Base64 RFC-4648-Padding constructor.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
base64_rfc4648_padding_decoder(callback_type callback_)
|
||||
: ibase64_decoder(etl::base64::character_set_1(),
|
||||
etl::base64::Padding::Use_Padding,
|
||||
output_buffer,
|
||||
Buffer_Size,
|
||||
callback_)
|
||||
, output_buffer()
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Calculate the required output encode buffer size.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
static
|
||||
ETL_CONSTEXPR14
|
||||
size_t safe_output_buffer_size(size_t input_length)
|
||||
{
|
||||
return ibase64_decoder::decoded_size_from_valid_input_length(input_length);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
/// The internal output buffer.
|
||||
unsigned char output_buffer[Buffer_Size];
|
||||
};
|
||||
|
||||
//*************************************************************************
|
||||
/// Base64 RFC-4648-URL Decoder
|
||||
//*************************************************************************
|
||||
template <size_t Buffer_Size = etl::base64::Min_Decode_Buffer_Size>
|
||||
class base64_rfc4648_url_decoder : public ibase64_decoder
|
||||
{
|
||||
public:
|
||||
|
||||
ETL_STATIC_ASSERT((Buffer_Size >= etl::base64::Min_Decode_Buffer_Size), "Buffer size must be greater than etl::base64::Min_Decode_Buffer_Size");
|
||||
|
||||
//*************************************************************************
|
||||
/// Base64 RFC-4648-Padding constructor.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
base64_rfc4648_url_decoder()
|
||||
: ibase64_decoder(etl::base64::character_set_2(),
|
||||
etl::base64::Padding::No_Padding,
|
||||
output_buffer,
|
||||
Buffer_Size,
|
||||
callback_type())
|
||||
, output_buffer()
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Base64 RFC-4648-Padding constructor.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
base64_rfc4648_url_decoder(callback_type callback_)
|
||||
: ibase64_decoder(etl::base64::character_set_2(),
|
||||
etl::base64::Padding::No_Padding,
|
||||
output_buffer,
|
||||
Buffer_Size,
|
||||
callback_)
|
||||
, output_buffer()
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Calculate the required output encode buffer size.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
static
|
||||
ETL_CONSTEXPR14
|
||||
size_t safe_output_buffer_size(size_t input_length)
|
||||
{
|
||||
return ibase64_decoder::decoded_size_from_valid_input_length(input_length);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
/// The internal output buffer.
|
||||
unsigned char output_buffer[Buffer_Size];
|
||||
};
|
||||
|
||||
//*************************************************************************
|
||||
/// Base64 RFC-4648-URL-Padding Decoder
|
||||
//*************************************************************************
|
||||
template <size_t Buffer_Size = etl::base64::Min_Decode_Buffer_Size>
|
||||
class base64_rfc4648_url_padding_decoder : public ibase64_decoder
|
||||
{
|
||||
public:
|
||||
|
||||
ETL_STATIC_ASSERT((Buffer_Size >= etl::base64::Min_Decode_Buffer_Size), "Buffer size must be greater than etl::base64::Min_Decode_Buffer_Size");
|
||||
|
||||
//*************************************************************************
|
||||
/// Base64 RFC-4648-Padding constructor.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
base64_rfc4648_url_padding_decoder()
|
||||
: ibase64_decoder(etl::base64::character_set_2(),
|
||||
etl::base64::Padding::Use_Padding,
|
||||
output_buffer,
|
||||
Buffer_Size,
|
||||
callback_type())
|
||||
, output_buffer()
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Base64 RFC-4648-Padding constructor.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
base64_rfc4648_url_padding_decoder(callback_type callback_)
|
||||
: ibase64_decoder(etl::base64::character_set_2(),
|
||||
etl::base64::Padding::Use_Padding,
|
||||
output_buffer,
|
||||
Buffer_Size,
|
||||
callback_)
|
||||
, output_buffer()
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Calculate the required output encode buffer size.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
static
|
||||
ETL_CONSTEXPR14
|
||||
size_t safe_output_buffer_size(size_t input_length)
|
||||
{
|
||||
return ibase64_decoder::decoded_size_from_valid_input_length(input_length);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
/// The internal output buffer.
|
||||
unsigned char output_buffer[Buffer_Size];
|
||||
};
|
||||
}
|
||||
|
||||
#undef ETL_IS_TYPE_8_BIT_INTEGRAL
|
||||
#undef ETL_IS_ITERATOR_TYPE_8_BIT_INTEGRAL
|
||||
|
||||
#endif
|
||||
843
Chapter18/cib/libs/etl/include/etl/base64_encoder.h
Normal file
843
Chapter18/cib/libs/etl/include/etl/base64_encoder.h
Normal file
@@ -0,0 +1,843 @@
|
||||
//*************************************************************************
|
||||
///Decode from Base64 from and to pointer/length
|
||||
//*************************************************************************///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
Copyright(c) 2024 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_BASE64_ENCODER_INCLUDED
|
||||
#define ETL_BASE64_ENCODER_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "static_assert.h"
|
||||
#include "error_handler.h"
|
||||
#include "type_traits.h"
|
||||
#include "binary.h"
|
||||
#include "algorithm.h"
|
||||
#include "integral_limits.h"
|
||||
#include "iterator.h"
|
||||
#include "enum_type.h"
|
||||
#include "delegate.h"
|
||||
#include "span.h"
|
||||
|
||||
#include "base64.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#if ETL_USING_STL
|
||||
#include <iterator>
|
||||
#endif
|
||||
|
||||
#define ETL_IS_8_BIT_INTEGRAL(Type) (etl::is_integral<typename etl::remove_cv<Type>::type>::value && \
|
||||
(etl::integral_limits<typename etl::remove_cv<Type>::type>::bits == 8U))
|
||||
|
||||
#define ETL_IS_ITERATOR_TYPE_8_BIT_INTEGRAL(Type) (etl::is_integral<typename etl::iterator_traits<typename etl::remove_cv<Type>::type>::value_type>::value && \
|
||||
(etl::integral_limits<typename etl::iterator_traits<typename etl::remove_cv<Type>::type>::value_type>::bits == 8U))
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//*************************************************************************
|
||||
/// Base64 Encoder
|
||||
//*************************************************************************
|
||||
class ibase64_encoder : public base64
|
||||
{
|
||||
public:
|
||||
|
||||
typedef etl::span<const char> span_type;
|
||||
typedef etl::delegate<void(const span_type&)> callback_type;
|
||||
|
||||
//*************************************************************************
|
||||
/// Encode to Base64
|
||||
//*************************************************************************
|
||||
template <typename T>
|
||||
ETL_CONSTEXPR14
|
||||
bool encode(T value)
|
||||
{
|
||||
ETL_STATIC_ASSERT(ETL_IS_8_BIT_INTEGRAL(T), "Input type must be an 8 bit integral");
|
||||
|
||||
push_to_input_buffer(value);
|
||||
|
||||
if (input_buffer_is_full())
|
||||
{
|
||||
encode_block();
|
||||
reset_input_buffer();
|
||||
|
||||
if (callback.is_valid())
|
||||
{
|
||||
if (output_buffer_is_full())
|
||||
{
|
||||
callback(span());
|
||||
reset_output_buffer();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return !error();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Encode to Base64
|
||||
//*************************************************************************
|
||||
template <typename TInputIterator>
|
||||
ETL_CONSTEXPR14
|
||||
bool encode(TInputIterator input_begin, size_t input_length)
|
||||
{
|
||||
ETL_STATIC_ASSERT(ETL_IS_ITERATOR_TYPE_8_BIT_INTEGRAL(TInputIterator), "Input type must be an 8 bit integral");
|
||||
|
||||
while (input_length-- != 0)
|
||||
{
|
||||
if (!encode(*input_begin++))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Encode to Base64
|
||||
//*************************************************************************
|
||||
template <typename TInputIterator>
|
||||
ETL_CONSTEXPR14
|
||||
bool encode(TInputIterator input_begin, TInputIterator input_end)
|
||||
{
|
||||
ETL_STATIC_ASSERT(ETL_IS_ITERATOR_TYPE_8_BIT_INTEGRAL(TInputIterator), "Input type must be an 8 bit integral");
|
||||
|
||||
while (input_begin != input_end)
|
||||
{
|
||||
if (!encode(*input_begin++))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Encode to Base64
|
||||
//*************************************************************************
|
||||
template <typename TInputIterator>
|
||||
ETL_CONSTEXPR14
|
||||
bool encode_final(TInputIterator input_begin, size_t input_length)
|
||||
{
|
||||
return encode(input_begin, input_length) && flush();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Encode to Base64
|
||||
//*************************************************************************
|
||||
template <typename TInputIterator>
|
||||
ETL_CONSTEXPR14
|
||||
bool encode_final(TInputIterator input_begin, TInputIterator input_end)
|
||||
{
|
||||
return encode(input_begin, input_end) && flush();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Flush any remaining data to the output.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
bool flush()
|
||||
{
|
||||
// Encode any remaining input data.
|
||||
bool success = encode_block();
|
||||
|
||||
reset_input_buffer();
|
||||
|
||||
if (success)
|
||||
{
|
||||
if (callback.is_valid())
|
||||
{
|
||||
// Send any remaining data.
|
||||
if (size() != 0)
|
||||
{
|
||||
callback(span());
|
||||
}
|
||||
|
||||
// Indicate this was the final block.
|
||||
callback(span_type());
|
||||
|
||||
reset_output_buffer();
|
||||
}
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Reset the encoder.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
void restart()
|
||||
{
|
||||
reset_input_buffer();
|
||||
reset_output_buffer();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns the beginning of the output buffer.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR14
|
||||
const char* begin() const
|
||||
{
|
||||
return p_output_buffer;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// This only returns a useful value if a callback has not been set or called.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR14
|
||||
const char* end() const
|
||||
{
|
||||
return p_output_buffer + output_buffer_length;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns the beginning of the output buffer.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR14
|
||||
const char* cbegin() const
|
||||
{
|
||||
return p_output_buffer;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// This only returns a useful value if a callback has not been set or called.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR14
|
||||
const char* cend() const
|
||||
{
|
||||
return p_output_buffer + output_buffer_length;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns the size of the output buffer.
|
||||
/// This only returns a useful value if a callback has not been set or called.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR14
|
||||
size_t size() const
|
||||
{
|
||||
return output_buffer_length;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns the maximum size of the output buffer.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR14
|
||||
size_t max_size() const
|
||||
{
|
||||
return output_buffer_max_size;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Get a span of the output data.
|
||||
/// This only returns a useful span if a callback has not been set or called.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR14
|
||||
span_type span() const
|
||||
{
|
||||
return span_type(begin(), end());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns true if the output buffer has overflowed
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR14
|
||||
bool overflow() const
|
||||
{
|
||||
return overflowed;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns true if an error was detected.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR14
|
||||
bool error() const
|
||||
{
|
||||
return overflow();
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
ibase64_encoder(const char* encoder_table_,
|
||||
bool use_padding_,
|
||||
char* p_output_buffer_,
|
||||
size_t ouput_buffer_max_size_,
|
||||
callback_type callback_)
|
||||
: base64(encoder_table_, use_padding_)
|
||||
, input_buffer()
|
||||
, input_buffer_length(0)
|
||||
, p_output_buffer(p_output_buffer_)
|
||||
, output_buffer_length(0)
|
||||
, output_buffer_max_size(ouput_buffer_max_size_)
|
||||
, callback(callback_)
|
||||
, overflowed(false)
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Encode one block of data.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
bool encode_block()
|
||||
{
|
||||
switch (input_buffer_length)
|
||||
{
|
||||
// Only triggered on call to flush().
|
||||
case 1:
|
||||
{
|
||||
uint32_t octets = input_buffer[0];
|
||||
octets = octets << 4; // Adjust one octet (8 bits) for two sextets worth of data (12 bits)
|
||||
|
||||
// Write out two sextets + optional padding.
|
||||
push_to_output_buffer(encoder_table[(octets >> 6) & 0x3F]);
|
||||
push_to_output_buffer(encoder_table[(octets >> 0) & 0x3F]);
|
||||
|
||||
if (use_padding)
|
||||
{
|
||||
push_to_output_buffer('=');
|
||||
push_to_output_buffer('=');
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// Only triggered on call to flush().
|
||||
case 2:
|
||||
{
|
||||
uint32_t octets = (input_buffer[0] << 8) | input_buffer[1];
|
||||
octets <<= 2; // Adjust two octets (16 bits) for three sextets worth of data (18 bits)
|
||||
|
||||
// Write out three sextets + optional padding.
|
||||
push_to_output_buffer(encoder_table[(octets >> 12) & 0x3F]);
|
||||
push_to_output_buffer(encoder_table[(octets >> 6) & 0x3F]);
|
||||
push_to_output_buffer(encoder_table[(octets >> 0) & 0x3F]);
|
||||
|
||||
if (use_padding)
|
||||
{
|
||||
push_to_output_buffer('=');
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// Only triggered on call to encode().
|
||||
case 3:
|
||||
{
|
||||
uint32_t octets = (input_buffer[0] << 16) | (input_buffer[1] << 8) | input_buffer[2];
|
||||
|
||||
// Write out four sextets
|
||||
push_to_output_buffer(encoder_table[(octets >> 18) & 0x3F]);
|
||||
push_to_output_buffer(encoder_table[(octets >> 12) & 0x3F]);
|
||||
push_to_output_buffer(encoder_table[(octets >> 6) & 0x3F]);
|
||||
push_to_output_buffer(encoder_table[(octets >> 0) & 0x3F]);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ETL_ASSERT(!overflowed, ETL_ERROR(etl::base64_overflow));
|
||||
|
||||
return !overflowed;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Calculates the minimum buffer size required to encode to Base64
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
static
|
||||
ETL_CONSTEXPR14
|
||||
size_t encoded_size(size_t input_length, bool use_padding)
|
||||
{
|
||||
size_t required_output_length = 0;
|
||||
|
||||
if (input_length == 0U)
|
||||
{
|
||||
return 0U;
|
||||
}
|
||||
|
||||
if (use_padding)
|
||||
{
|
||||
required_output_length = (input_length * 4U) / 3U;
|
||||
|
||||
while ((required_output_length % 4U) != 0)
|
||||
{
|
||||
++required_output_length;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
required_output_length = input_length + (((input_length - 1U) / 3U) + 1U);
|
||||
}
|
||||
|
||||
while (required_output_length % 4)
|
||||
{
|
||||
++required_output_length;
|
||||
}
|
||||
|
||||
return required_output_length;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
//*************************************************************************
|
||||
// Push to the output buffer.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
void push_to_output_buffer(char c)
|
||||
{
|
||||
if (output_buffer_length < output_buffer_max_size)
|
||||
{
|
||||
p_output_buffer[output_buffer_length++] = c;
|
||||
}
|
||||
else
|
||||
{
|
||||
overflowed = true;
|
||||
}
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
//
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
bool output_buffer_is_full() const
|
||||
{
|
||||
return output_buffer_length == output_buffer_max_size;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
//
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
bool output_buffer_is_empty() const
|
||||
{
|
||||
return output_buffer_length == 0;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
//
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
void reset_output_buffer()
|
||||
{
|
||||
output_buffer_length = 0;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
// Push to the input buffer.
|
||||
//*************************************************************************
|
||||
template <typename T>
|
||||
ETL_CONSTEXPR14
|
||||
void push_to_input_buffer(T value)
|
||||
{
|
||||
input_buffer[input_buffer_length++] = static_cast<uint8_t>(value);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
//
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
bool input_buffer_is_full() const
|
||||
{
|
||||
return input_buffer_length == 3U;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
//
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
void reset_input_buffer()
|
||||
{
|
||||
input_buffer_length = 0;
|
||||
}
|
||||
|
||||
uint8_t input_buffer[3];
|
||||
size_t input_buffer_length;
|
||||
|
||||
char* p_output_buffer;
|
||||
size_t output_buffer_length;
|
||||
const size_t output_buffer_max_size;
|
||||
|
||||
callback_type callback;
|
||||
|
||||
bool overflowed;
|
||||
};
|
||||
|
||||
//*************************************************************************
|
||||
/// Base64 RFC-2152 Encoder
|
||||
//*************************************************************************
|
||||
template <size_t Buffer_Size = etl::base64::Min_Encode_Buffer_Size>
|
||||
class base64_rfc2152_encoder : public ibase64_encoder
|
||||
{
|
||||
public:
|
||||
|
||||
ETL_STATIC_ASSERT((Buffer_Size >= etl::base64::Min_Encode_Buffer_Size), "Buffer size must be greater than etl::base64::Min_Encode_Buffer_Size");
|
||||
ETL_STATIC_ASSERT(((Buffer_Size % etl::base64::Min_Encode_Buffer_Size) == 0), "Buffer size must be a multiple of etl::base64::Min_Encode_Buffer_Size");
|
||||
|
||||
//*************************************************************************
|
||||
/// Base64 RFC-2152 constructor.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
base64_rfc2152_encoder()
|
||||
: ibase64_encoder(etl::base64::character_set_1(),
|
||||
etl::base64::Padding::No_Padding,
|
||||
output_buffer,
|
||||
Buffer_Size,
|
||||
callback_type())
|
||||
, output_buffer()
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Base64 RFC-2152 constructor.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
base64_rfc2152_encoder(callback_type callback_)
|
||||
: ibase64_encoder(etl::base64::character_set_1(),
|
||||
etl::base64::Padding::No_Padding,
|
||||
output_buffer,
|
||||
Buffer_Size,
|
||||
callback_)
|
||||
, output_buffer()
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Calculate the required output encode buffer size.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
static
|
||||
ETL_CONSTEXPR14
|
||||
size_t safe_output_buffer_size(size_t input_length)
|
||||
{
|
||||
return ibase64_encoder::encoded_size(input_length, etl::base64::Padding::No_Padding);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
/// The internal output buffer.
|
||||
char output_buffer[Buffer_Size];
|
||||
};
|
||||
|
||||
//*************************************************************************
|
||||
/// Base64 RFC-3501 Encoder
|
||||
//*************************************************************************
|
||||
template <size_t Buffer_Size = etl::base64::Min_Encode_Buffer_Size>
|
||||
class base64_rfc3501_encoder : public ibase64_encoder
|
||||
{
|
||||
public:
|
||||
|
||||
ETL_STATIC_ASSERT((Buffer_Size >= etl::base64::Min_Encode_Buffer_Size), "Buffer size must be greater than etl::base64::Min_Encode_Buffer_Size");
|
||||
ETL_STATIC_ASSERT(((Buffer_Size% etl::base64::Min_Encode_Buffer_Size) == 0), "Buffer size must be a multiple of etl::base64::Min_Encode_Buffer_Size");
|
||||
|
||||
//*************************************************************************
|
||||
/// Base64 RFC-3501 constructor.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
base64_rfc3501_encoder()
|
||||
: ibase64_encoder(etl::base64::character_set_3(),
|
||||
etl::base64::Padding::No_Padding,
|
||||
output_buffer,
|
||||
Buffer_Size,
|
||||
callback_type())
|
||||
, output_buffer()
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Base64 RFC-3501 constructor.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
base64_rfc3501_encoder(callback_type callback_)
|
||||
: ibase64_encoder(etl::base64::character_set_3(),
|
||||
etl::base64::Padding::No_Padding,
|
||||
output_buffer,
|
||||
Buffer_Size,
|
||||
callback_)
|
||||
, output_buffer()
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Calculate the required output encode buffer size.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
static
|
||||
ETL_CONSTEXPR14
|
||||
size_t safe_output_buffer_size(size_t input_length)
|
||||
{
|
||||
return ibase64_encoder::encoded_size(input_length, etl::base64::Padding::No_Padding);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
/// The internal output buffer.
|
||||
char output_buffer[Buffer_Size];
|
||||
};
|
||||
|
||||
//*************************************************************************
|
||||
/// Base64 RFC-4648 Encoder
|
||||
//*************************************************************************
|
||||
template <size_t Buffer_Size = etl::base64::Min_Encode_Buffer_Size>
|
||||
class base64_rfc4648_encoder : public ibase64_encoder
|
||||
{
|
||||
public:
|
||||
|
||||
ETL_STATIC_ASSERT((Buffer_Size >= etl::base64::Min_Encode_Buffer_Size), "Buffer size must be greater than etl::base64::Min_Encode_Buffer_Size");
|
||||
ETL_STATIC_ASSERT(((Buffer_Size % etl::base64::Min_Encode_Buffer_Size) == 0), "Buffer size must be a multiple of etl::base64::Min_Encode_Buffer_Size");
|
||||
|
||||
//*************************************************************************
|
||||
/// Base64 RFC-4648 constructor.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
base64_rfc4648_encoder()
|
||||
: ibase64_encoder(etl::base64::character_set_1(),
|
||||
etl::base64::Padding::No_Padding,
|
||||
output_buffer,
|
||||
Buffer_Size,
|
||||
callback_type())
|
||||
, output_buffer()
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Base64 RFC-4648 constructor.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
base64_rfc4648_encoder(callback_type callback_)
|
||||
: ibase64_encoder(etl::base64::character_set_1(),
|
||||
etl::base64::Padding::No_Padding,
|
||||
output_buffer,
|
||||
Buffer_Size,
|
||||
callback_)
|
||||
, output_buffer()
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Calculate the required output encode buffer size.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
static
|
||||
ETL_CONSTEXPR14
|
||||
size_t safe_output_buffer_size(size_t input_length)
|
||||
{
|
||||
return ibase64_encoder::encoded_size(input_length, etl::base64::Padding::No_Padding);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
/// The internal output buffer.
|
||||
char output_buffer[Buffer_Size];
|
||||
};
|
||||
|
||||
//*************************************************************************
|
||||
/// Base64 RFC-4648-Padding Encoder
|
||||
//*************************************************************************
|
||||
template <size_t Buffer_Size = etl::base64::Min_Encode_Buffer_Size>
|
||||
class base64_rfc4648_padding_encoder : public ibase64_encoder
|
||||
{
|
||||
public:
|
||||
|
||||
ETL_STATIC_ASSERT((Buffer_Size >= etl::base64::Min_Encode_Buffer_Size), "Buffer size must be greater than etl::base64::Min_Encode_Buffer_Size");
|
||||
ETL_STATIC_ASSERT(((Buffer_Size% etl::base64::Min_Encode_Buffer_Size) == 0), "Buffer size must be a multiple of etl::base64::Min_Encode_Buffer_Size");
|
||||
|
||||
//*************************************************************************
|
||||
/// Base64 RFC-4648-Padding constructor.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
base64_rfc4648_padding_encoder()
|
||||
: ibase64_encoder(etl::base64::character_set_1(),
|
||||
etl::base64::Padding::Use_Padding,
|
||||
output_buffer,
|
||||
Buffer_Size,
|
||||
callback_type())
|
||||
, output_buffer()
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Base64 RFC-4648-Padding constructor.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
base64_rfc4648_padding_encoder(callback_type callback_)
|
||||
: ibase64_encoder(etl::base64::character_set_1(),
|
||||
etl::base64::Padding::Use_Padding,
|
||||
output_buffer,
|
||||
Buffer_Size,
|
||||
callback_)
|
||||
, output_buffer()
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Calculate the required output encode buffer size.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
static
|
||||
ETL_CONSTEXPR14
|
||||
size_t safe_output_buffer_size(size_t input_length)
|
||||
{
|
||||
return ibase64_encoder::encoded_size(input_length, etl::base64::Padding::Use_Padding);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
/// The internal output buffer.
|
||||
char output_buffer[Buffer_Size];
|
||||
};
|
||||
|
||||
//*************************************************************************
|
||||
/// Base64 RFC-4648-URL Encoder
|
||||
//*************************************************************************
|
||||
template <size_t Buffer_Size = etl::base64::Min_Encode_Buffer_Size>
|
||||
class base64_rfc4648_url_encoder : public ibase64_encoder
|
||||
{
|
||||
public:
|
||||
|
||||
ETL_STATIC_ASSERT((Buffer_Size >= etl::base64::Min_Encode_Buffer_Size), "Buffer size must be greater than etl::base64::Min_Encode_Buffer_Size");
|
||||
ETL_STATIC_ASSERT(((Buffer_Size % etl::base64::Min_Encode_Buffer_Size) == 0), "Buffer size must be a multiple of etl::base64::Min_Encode_Buffer_Size");
|
||||
|
||||
//*************************************************************************
|
||||
/// Base64 RFC-4648-URL constructor.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
base64_rfc4648_url_encoder()
|
||||
: ibase64_encoder(etl::base64::character_set_2(),
|
||||
etl::base64::Padding::No_Padding,
|
||||
output_buffer,
|
||||
Buffer_Size,
|
||||
callback_type())
|
||||
, output_buffer()
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Base64 RFC-4648-URL constructor.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
base64_rfc4648_url_encoder(callback_type callback_)
|
||||
: ibase64_encoder(etl::base64::character_set_2(),
|
||||
etl::base64::Padding::No_Padding,
|
||||
output_buffer,
|
||||
Buffer_Size,
|
||||
callback_)
|
||||
, output_buffer()
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Calculate the required output encode buffer size.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
static
|
||||
ETL_CONSTEXPR14
|
||||
size_t safe_output_buffer_size(size_t input_length)
|
||||
{
|
||||
return ibase64_encoder::encoded_size(input_length, etl::base64::Padding::No_Padding);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
/// The internal output buffer.
|
||||
char output_buffer[Buffer_Size];
|
||||
};
|
||||
|
||||
//*************************************************************************
|
||||
/// Base64 RFC-4648-URL_Padding Encoder
|
||||
//*************************************************************************
|
||||
template <size_t Buffer_Size = etl::base64::Min_Encode_Buffer_Size>
|
||||
class base64_rfc4648_url_padding_encoder : public ibase64_encoder
|
||||
{
|
||||
public:
|
||||
|
||||
ETL_STATIC_ASSERT((Buffer_Size >= etl::base64::Min_Encode_Buffer_Size), "Buffer size must be greater than etl::base64::Min_Encode_Buffer_Size");
|
||||
ETL_STATIC_ASSERT(((Buffer_Size% etl::base64::Min_Encode_Buffer_Size) == 0), "Buffer size must be a multiple of etl::base64::Min_Encode_Buffer_Size");
|
||||
|
||||
//*************************************************************************
|
||||
/// Base64 RFC-4648-URL constructor.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
base64_rfc4648_url_padding_encoder()
|
||||
: ibase64_encoder(etl::base64::character_set_2(),
|
||||
etl::base64::Padding::Use_Padding,
|
||||
output_buffer,
|
||||
Buffer_Size,
|
||||
callback_type())
|
||||
, output_buffer()
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Base64 RFC-4648-URL constructor.
|
||||
//*************************************************************************
|
||||
ETL_CONSTEXPR14
|
||||
base64_rfc4648_url_padding_encoder(callback_type callback_)
|
||||
: ibase64_encoder(etl::base64::character_set_2(),
|
||||
etl::base64::Padding::Use_Padding,
|
||||
output_buffer,
|
||||
Buffer_Size,
|
||||
callback_)
|
||||
, output_buffer()
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Calculate the required output encode buffer size.
|
||||
//*************************************************************************
|
||||
ETL_NODISCARD
|
||||
static
|
||||
ETL_CONSTEXPR14
|
||||
size_t safe_output_buffer_size(size_t input_length)
|
||||
{
|
||||
return ibase64_encoder::encoded_size(input_length, etl::base64::Padding::Use_Padding);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
/// The internal output buffer.
|
||||
char output_buffer[Buffer_Size];
|
||||
};
|
||||
}
|
||||
|
||||
#undef ETL_IS_TYPE_8_BIT_INTEGRAL
|
||||
#undef ETL_IS_ITERATOR_TYPE_8_BIT_INTEGRAL
|
||||
|
||||
#endif
|
||||
496
Chapter18/cib/libs/etl/include/etl/basic_format_spec.h
Normal file
496
Chapter18/cib/libs/etl/include/etl/basic_format_spec.h
Normal file
@@ -0,0 +1,496 @@
|
||||
///\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_BASIC_FORMAT_SPEC_INCLUDED
|
||||
#define ETL_BASIC_FORMAT_SPEC_INCLUDED
|
||||
|
||||
///\ingroup string
|
||||
|
||||
#include "platform.h"
|
||||
#include "type_traits.h"
|
||||
#include "static_assert.h"
|
||||
|
||||
namespace etl
|
||||
{
|
||||
namespace private_basic_format_spec
|
||||
{
|
||||
//*******************************************************
|
||||
// Structures returned by stream formatting manipulators.
|
||||
//*******************************************************
|
||||
struct base_spec
|
||||
{
|
||||
ETL_CONSTEXPR base_spec(uint_least8_t base_)
|
||||
: base(base_)
|
||||
{
|
||||
}
|
||||
|
||||
const uint_least8_t base;
|
||||
};
|
||||
|
||||
//*********************************
|
||||
struct width_spec
|
||||
{
|
||||
ETL_CONSTEXPR width_spec(uint_least8_t width_)
|
||||
: width(width_)
|
||||
{
|
||||
}
|
||||
|
||||
const uint_least8_t width;
|
||||
};
|
||||
|
||||
//*********************************
|
||||
template <typename TChar>
|
||||
struct fill_spec
|
||||
{
|
||||
ETL_CONSTEXPR fill_spec(TChar fill_)
|
||||
: fill(fill_)
|
||||
{
|
||||
}
|
||||
|
||||
const TChar fill;
|
||||
};
|
||||
|
||||
//*********************************
|
||||
struct precision_spec
|
||||
{
|
||||
ETL_CONSTEXPR precision_spec(uint_least8_t precision_)
|
||||
: precision(precision_)
|
||||
{
|
||||
}
|
||||
|
||||
const uint_least8_t precision;
|
||||
};
|
||||
|
||||
//*********************************
|
||||
struct uppercase_spec
|
||||
{
|
||||
ETL_CONSTEXPR uppercase_spec(bool upper_case_)
|
||||
: upper_case(upper_case_)
|
||||
{
|
||||
}
|
||||
|
||||
const bool upper_case;
|
||||
};
|
||||
|
||||
//*********************************
|
||||
struct boolalpha_spec
|
||||
{
|
||||
ETL_CONSTEXPR boolalpha_spec(bool boolalpha_)
|
||||
: boolalpha(boolalpha_)
|
||||
{
|
||||
}
|
||||
|
||||
const bool boolalpha;
|
||||
};
|
||||
|
||||
//*********************************
|
||||
struct showbase_spec
|
||||
{
|
||||
ETL_CONSTEXPR showbase_spec(bool show_base_)
|
||||
: show_base(show_base_)
|
||||
{
|
||||
}
|
||||
|
||||
const bool show_base;
|
||||
};
|
||||
|
||||
//*********************************
|
||||
struct left_spec
|
||||
{
|
||||
};
|
||||
|
||||
//*********************************
|
||||
struct right_spec
|
||||
{
|
||||
};
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
// Stream formatting manipulators.
|
||||
//***************************************************************************
|
||||
static ETL_CONSTEXPR private_basic_format_spec::base_spec setbase(uint32_t base)
|
||||
{
|
||||
return private_basic_format_spec::base_spec(base);
|
||||
}
|
||||
|
||||
//*********************************
|
||||
static ETL_CONSTEXPR private_basic_format_spec::width_spec setw(uint32_t width)
|
||||
{
|
||||
return private_basic_format_spec::width_spec(width);
|
||||
}
|
||||
|
||||
//*********************************
|
||||
template <typename TChar>
|
||||
static ETL_CONSTEXPR private_basic_format_spec::fill_spec<TChar> setfill(TChar fill)
|
||||
{
|
||||
return private_basic_format_spec::fill_spec<TChar>(fill);
|
||||
}
|
||||
|
||||
//*********************************
|
||||
static ETL_CONSTEXPR private_basic_format_spec::precision_spec setprecision(uint32_t precision)
|
||||
{
|
||||
return private_basic_format_spec::precision_spec(precision);
|
||||
}
|
||||
|
||||
//*********************************
|
||||
static ETL_CONSTANT private_basic_format_spec::base_spec bin(2U);
|
||||
|
||||
//*********************************
|
||||
static ETL_CONSTANT private_basic_format_spec::base_spec oct(8U);
|
||||
|
||||
//*********************************
|
||||
static ETL_CONSTANT private_basic_format_spec::base_spec dec(10U);
|
||||
|
||||
//*********************************
|
||||
static ETL_CONSTANT private_basic_format_spec::base_spec hex(16U);
|
||||
|
||||
//*********************************
|
||||
static ETL_CONSTANT private_basic_format_spec::left_spec left = private_basic_format_spec::left_spec();
|
||||
|
||||
//*********************************
|
||||
static ETL_CONSTANT private_basic_format_spec::right_spec right = private_basic_format_spec::right_spec();
|
||||
|
||||
//*********************************
|
||||
static ETL_CONSTANT private_basic_format_spec::boolalpha_spec boolalpha(true);
|
||||
|
||||
//*********************************
|
||||
static ETL_CONSTANT private_basic_format_spec::boolalpha_spec noboolalpha(false);
|
||||
|
||||
//*********************************
|
||||
static ETL_CONSTANT private_basic_format_spec::uppercase_spec uppercase(true);
|
||||
|
||||
//*********************************
|
||||
static ETL_CONSTANT private_basic_format_spec::uppercase_spec nouppercase(false);
|
||||
|
||||
//*********************************
|
||||
static ETL_CONSTANT private_basic_format_spec::showbase_spec showbase(true);
|
||||
|
||||
//*********************************
|
||||
static ETL_CONSTANT private_basic_format_spec::showbase_spec noshowbase(false);
|
||||
|
||||
//***************************************************************************
|
||||
/// basic_format_spec
|
||||
//***************************************************************************
|
||||
template <typename TString>
|
||||
class basic_format_spec
|
||||
{
|
||||
public:
|
||||
|
||||
//***************************************************************************
|
||||
/// Default constructor.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR basic_format_spec()
|
||||
: base_(10U)
|
||||
, width_(0U)
|
||||
, precision_(0U)
|
||||
, upper_case_(false)
|
||||
, left_justified_(false)
|
||||
, boolalpha_(false)
|
||||
, show_base_(false)
|
||||
, fill_(typename TString::value_type(' '))
|
||||
{
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Constructor.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR basic_format_spec(uint_least8_t base__,
|
||||
uint_least8_t width__,
|
||||
uint_least8_t precision__,
|
||||
bool upper_case__,
|
||||
bool left_justified__,
|
||||
bool boolalpha__,
|
||||
bool show_base__,
|
||||
typename TString::value_type fill__)
|
||||
: base_(base__)
|
||||
, width_(width__)
|
||||
, precision_(precision__)
|
||||
, upper_case_(upper_case__)
|
||||
, left_justified_(left_justified__)
|
||||
, boolalpha_(boolalpha__)
|
||||
, show_base_(show_base__)
|
||||
, fill_(fill__)
|
||||
{
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Clears the format spec back to default.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 void clear()
|
||||
{
|
||||
base_ = 10U;
|
||||
width_ = 0U;
|
||||
precision_ = 0U;
|
||||
upper_case_ = false;
|
||||
left_justified_ = false;
|
||||
boolalpha_ = false;
|
||||
show_base_ = false;
|
||||
fill_ = typename TString::value_type(' ');
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Sets the base.
|
||||
/// \return A reference to the basic_format_spec.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 basic_format_spec& base(uint32_t b)
|
||||
{
|
||||
base_ = static_cast<uint_least8_t>(b);
|
||||
return *this;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Sets the base to binary.
|
||||
/// \return A reference to the basic_format_spec.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 basic_format_spec& binary()
|
||||
{
|
||||
base(2);
|
||||
return *this;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Sets the base to octal.
|
||||
/// \return A reference to the basic_format_spec.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 basic_format_spec& octal()
|
||||
{
|
||||
base(8);
|
||||
return *this;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Sets the base to decimal.
|
||||
/// \return A reference to the basic_format_spec.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 basic_format_spec& decimal()
|
||||
{
|
||||
base(10);
|
||||
return *this;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Sets the base to hex.
|
||||
/// \return A reference to the basic_format_spec.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 basic_format_spec& hex()
|
||||
{
|
||||
base(16);
|
||||
return *this;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Gets the base.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR uint32_t get_base() const
|
||||
{
|
||||
return base_;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Sets the show base flag.
|
||||
/// \return A reference to the basic_format_spec.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 basic_format_spec& show_base(bool b)
|
||||
{
|
||||
show_base_ = b;
|
||||
return *this;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Gets the show base flag.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR bool is_show_base() const
|
||||
{
|
||||
return show_base_;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Sets the width.
|
||||
/// \return A reference to the basic_format_spec.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 basic_format_spec& width(uint32_t w)
|
||||
{
|
||||
width_ = static_cast<uint_least8_t>(w);
|
||||
return *this;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Gets the width.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR uint32_t get_width() const
|
||||
{
|
||||
return width_;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Sets the precision.
|
||||
/// \return A reference to the basic_format_spec.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 basic_format_spec& precision(uint32_t p)
|
||||
{
|
||||
precision_ = static_cast<uint_least8_t>(p);
|
||||
return *this;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Gets the precision.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR uint32_t get_precision() const
|
||||
{
|
||||
return precision_;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Sets the upper case flag.
|
||||
/// \return A reference to the basic_format_spec.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 basic_format_spec& upper_case(bool u)
|
||||
{
|
||||
upper_case_ = u;
|
||||
return *this;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Gets the upper case flag.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR bool is_upper_case() const
|
||||
{
|
||||
return upper_case_;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Sets the fill character.
|
||||
/// \return A reference to the basic_format_spec.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 basic_format_spec& fill(typename TString::value_type c)
|
||||
{
|
||||
fill_ = c;
|
||||
return *this;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Gets the fill character.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR typename TString::value_type get_fill() const
|
||||
{
|
||||
return fill_;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Sets the left justify flag.
|
||||
/// \return A reference to the basic_format_spec.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 basic_format_spec& left()
|
||||
{
|
||||
left_justified_ = true;
|
||||
return *this;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Gets the left justify flag.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR bool is_left() const
|
||||
{
|
||||
return left_justified_;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Sets the right justify flag.
|
||||
/// \return A reference to the basic_format_spec.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 basic_format_spec& right()
|
||||
{
|
||||
left_justified_ = false;
|
||||
return *this;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Gets the right justify flag.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR bool is_right() const
|
||||
{
|
||||
return !left_justified_;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Sets the bool alpha flag.
|
||||
/// \return A reference to the basic_format_spec.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 basic_format_spec& boolalpha(bool b)
|
||||
{
|
||||
boolalpha_ = b;
|
||||
return *this;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Gets the boolalpha flag.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR bool is_boolalpha() const
|
||||
{
|
||||
return boolalpha_;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Equality operator.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR friend bool operator ==(const basic_format_spec& lhs, const basic_format_spec& rhs)
|
||||
{
|
||||
return (lhs.base_ == rhs.base_) &&
|
||||
(lhs.width_ == rhs.width_) &&
|
||||
(lhs.precision_ == rhs.precision_) &&
|
||||
(lhs.upper_case_ == rhs.upper_case_) &&
|
||||
(lhs.left_justified_ == rhs.left_justified_) &&
|
||||
(lhs.boolalpha_ == rhs.boolalpha_) &&
|
||||
(lhs.show_base_ == rhs.show_base_) &&
|
||||
(lhs.fill_ == rhs.fill_);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Inequality operator.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR friend bool operator !=(const basic_format_spec& lhs, const basic_format_spec& rhs)
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
uint_least8_t base_;
|
||||
uint_least8_t width_;
|
||||
uint_least8_t precision_;
|
||||
bool upper_case_;
|
||||
bool left_justified_;
|
||||
bool boolalpha_;
|
||||
bool show_base_;
|
||||
typename TString::value_type fill_;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
2652
Chapter18/cib/libs/etl/include/etl/basic_string.h
Normal file
2652
Chapter18/cib/libs/etl/include/etl/basic_string.h
Normal file
File diff suppressed because it is too large
Load Diff
281
Chapter18/cib/libs/etl/include/etl/basic_string_stream.h
Normal file
281
Chapter18/cib/libs/etl/include/etl/basic_string_stream.h
Normal file
@@ -0,0 +1,281 @@
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2020 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_BASIC_STRING_STREAM_INCLUDED
|
||||
#define ETL_BASIC_STRING_STREAM_INCLUDED
|
||||
|
||||
///\ingroup string
|
||||
|
||||
#include "platform.h"
|
||||
#include "to_string.h"
|
||||
|
||||
namespace etl
|
||||
{
|
||||
template <typename TFormat, typename TIString, typename TStringView>
|
||||
class basic_string_stream
|
||||
{
|
||||
public:
|
||||
|
||||
typedef TFormat format_spec_type;
|
||||
typedef TIString istring_type;
|
||||
typedef typename TIString::value_type value_type;
|
||||
typedef typename TIString::pointer pointer;
|
||||
typedef typename TIString::const_pointer const_pointer;
|
||||
|
||||
//*************************************************************************
|
||||
/// Construct from text.
|
||||
//*************************************************************************
|
||||
explicit basic_string_stream(TIString& text_)
|
||||
: text(text_)
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Construct from text and format fmt.
|
||||
//*************************************************************************
|
||||
basic_string_stream(TIString& text_, const TFormat& spec_)
|
||||
: text(text_)
|
||||
, format(spec_)
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Set the format fmt.
|
||||
//*************************************************************************
|
||||
void set_format(const TFormat& spec_)
|
||||
{
|
||||
format = spec_;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Get a const reference to the format fmt.
|
||||
//*************************************************************************
|
||||
const TFormat& get_format() const
|
||||
{
|
||||
return format;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Get a reference to the current string.
|
||||
//*************************************************************************
|
||||
TIString& str()
|
||||
{
|
||||
return text;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Get a const reference to the current string.
|
||||
//*************************************************************************
|
||||
const TIString& str() const
|
||||
{
|
||||
return text;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Resets the stream to the supplied string.
|
||||
//*************************************************************************
|
||||
void str(const value_type* p)
|
||||
{
|
||||
text.assign(p);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Resets the stream to the supplied string.
|
||||
//*************************************************************************
|
||||
void str(const TIString& is)
|
||||
{
|
||||
text.assign(is);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Stream operators.
|
||||
//*************************************************************************
|
||||
|
||||
//*********************************
|
||||
/// TFormat
|
||||
//*********************************
|
||||
friend basic_string_stream& operator <<(basic_string_stream& ss, const TFormat& fmt)
|
||||
{
|
||||
ss.format = fmt;
|
||||
return ss;
|
||||
}
|
||||
|
||||
//*********************************
|
||||
/// etl::base_spec from etl::setbase, etl::bin, etl::oct, etl::dec & etl::hex stream manipulators
|
||||
//*********************************
|
||||
friend basic_string_stream& operator <<(basic_string_stream& ss, etl::private_basic_format_spec::base_spec fmt)
|
||||
{
|
||||
ss.format.base(fmt.base);
|
||||
return ss;
|
||||
}
|
||||
|
||||
//*********************************
|
||||
/// etl::width_spec from etl::setw stream manipulator
|
||||
//*********************************
|
||||
friend basic_string_stream& operator <<(basic_string_stream& ss, etl::private_basic_format_spec::width_spec fmt)
|
||||
{
|
||||
ss.format.width(fmt.width);
|
||||
return ss;
|
||||
}
|
||||
|
||||
//*********************************
|
||||
/// etl::fill_spec from etl::setfill stream manipulator
|
||||
//*********************************
|
||||
template <typename TChar>
|
||||
friend basic_string_stream& operator <<(basic_string_stream& ss, etl::private_basic_format_spec::fill_spec<TChar> fmt)
|
||||
{
|
||||
ss.format.fill(fmt.fill);
|
||||
return ss;
|
||||
}
|
||||
|
||||
//*********************************
|
||||
/// etl::precision_spec from etl::setprecision stream manipulator
|
||||
//*********************************
|
||||
friend basic_string_stream& operator <<(basic_string_stream& ss, etl::private_basic_format_spec::precision_spec fmt)
|
||||
{
|
||||
ss.format.precision(fmt.precision);
|
||||
return ss;
|
||||
}
|
||||
|
||||
//*********************************
|
||||
/// etl::boolalpha_spec from etl::boolalpha & etl::noboolalpha stream manipulators
|
||||
//*********************************
|
||||
friend basic_string_stream& operator <<(basic_string_stream& ss, etl::private_basic_format_spec::boolalpha_spec fmt)
|
||||
{
|
||||
ss.format.boolalpha(fmt.boolalpha);
|
||||
return ss;
|
||||
}
|
||||
|
||||
//*********************************
|
||||
/// etl::uppercase_spec from etl::uppercase & etl::nouppercase stream manipulators
|
||||
//*********************************
|
||||
friend basic_string_stream& operator <<(basic_string_stream& ss, etl::private_basic_format_spec::uppercase_spec fmt)
|
||||
{
|
||||
ss.format.upper_case(fmt.upper_case);
|
||||
return ss;
|
||||
}
|
||||
|
||||
//*********************************
|
||||
/// etl::showbase_spec from etl::showbase & etl::noshowbase stream manipulators
|
||||
//*********************************
|
||||
friend basic_string_stream& operator <<(basic_string_stream& ss, etl::private_basic_format_spec::showbase_spec fmt)
|
||||
{
|
||||
ss.format.show_base(fmt.show_base);
|
||||
return ss;
|
||||
}
|
||||
|
||||
//*********************************
|
||||
/// etl::left_spec from etl::left stream manipulator
|
||||
//*********************************
|
||||
friend basic_string_stream& operator <<(basic_string_stream& ss, etl::private_basic_format_spec::left_spec /*fmt*/)
|
||||
{
|
||||
ss.format.left();
|
||||
return ss;
|
||||
}
|
||||
|
||||
//*********************************
|
||||
/// etl::right_spec from etl::left stream manipulator
|
||||
//*********************************
|
||||
friend basic_string_stream& operator <<(basic_string_stream& ss, etl::private_basic_format_spec::right_spec /*fmt*/)
|
||||
{
|
||||
ss.format.right();
|
||||
return ss;
|
||||
}
|
||||
|
||||
//*********************************
|
||||
/// From a string view
|
||||
//*********************************
|
||||
friend basic_string_stream& operator <<(basic_string_stream& ss, TStringView view)
|
||||
{
|
||||
etl::to_string(view, ss.text, ss.format, true);
|
||||
return ss;
|
||||
}
|
||||
|
||||
//*********************************
|
||||
/// From a character pointer to a string
|
||||
//*********************************
|
||||
friend basic_string_stream& operator <<(basic_string_stream& ss, pointer p)
|
||||
{
|
||||
TStringView view(p);
|
||||
ss << view;
|
||||
return ss;
|
||||
}
|
||||
|
||||
//*********************************
|
||||
/// From a const character pointer to a string
|
||||
//*********************************
|
||||
friend basic_string_stream& operator <<(basic_string_stream& ss, const_pointer p)
|
||||
{
|
||||
TStringView view(p);
|
||||
ss << view;
|
||||
return ss;
|
||||
}
|
||||
|
||||
//*********************************
|
||||
/// From a string interface
|
||||
//*********************************
|
||||
friend basic_string_stream& operator <<(basic_string_stream& ss, const TIString& t)
|
||||
{
|
||||
etl::to_string(t, ss.text, ss.format, true);
|
||||
return ss;
|
||||
}
|
||||
|
||||
//*********************************
|
||||
/// From a string
|
||||
//*********************************
|
||||
template <template <size_t> class TString, size_t SIZE>
|
||||
friend basic_string_stream& operator <<(basic_string_stream& ss, const TString<SIZE>& t)
|
||||
{
|
||||
const TIString& itext = t;
|
||||
etl::to_string(itext, ss.str(), ss.get_format(), true);
|
||||
return ss;
|
||||
}
|
||||
|
||||
//*********************************
|
||||
/// From anything else
|
||||
//*********************************
|
||||
template <typename T>
|
||||
friend basic_string_stream& operator <<(basic_string_stream& ss, const T& value)
|
||||
{
|
||||
etl::to_string(value, ss.text, ss.format, true);
|
||||
return ss;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
TIString& text;
|
||||
TFormat format;
|
||||
|
||||
basic_string_stream(const basic_string_stream&) ETL_DELETE;
|
||||
basic_string_stream& operator =(const basic_string_stream&) ETL_DELETE;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
2689
Chapter18/cib/libs/etl/include/etl/binary.h
Normal file
2689
Chapter18/cib/libs/etl/include/etl/binary.h
Normal file
File diff suppressed because it is too large
Load Diff
541
Chapter18/cib/libs/etl/include/etl/bip_buffer_spsc_atomic.h
Normal file
541
Chapter18/cib/libs/etl/include/etl/bip_buffer_spsc_atomic.h
Normal file
@@ -0,0 +1,541 @@
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2021 Benedek Kupper, 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.
|
||||
******************************************************************************/
|
||||
|
||||
/**
|
||||
* @note Based on the works of Andrea Lattuada and James Munns:
|
||||
* https://blog.systems.ethz.ch/blog/2019/the-design-and-implementation-of-a-lock-free-ring-buffer-with-contiguous-reservations.html
|
||||
* Whose design was inspired by Simon Cooke:
|
||||
* https://www.codeproject.com/Articles/3479/The-Bip-Buffer-The-Circular-Buffer-with-a-Twist
|
||||
*/
|
||||
|
||||
#ifndef ETL_BIP_BUFFER_SPSC_ATOMIC_INCLUDED
|
||||
#define ETL_BIP_BUFFER_SPSC_ATOMIC_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "alignment.h"
|
||||
#include "parameter_type.h"
|
||||
#include "atomic.h"
|
||||
#include "memory.h"
|
||||
#include "memory_model.h"
|
||||
#include "integral_limits.h"
|
||||
#include "utility.h"
|
||||
#include "error_handler.h"
|
||||
#include "span.h"
|
||||
#include "file_error_numbers.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#if ETL_HAS_ATOMIC
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
/// Base exception for a bip buffer.
|
||||
//***************************************************************************
|
||||
class bip_buffer_exception: public exception
|
||||
{
|
||||
public:
|
||||
|
||||
bip_buffer_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
|
||||
: exception(reason_, file_name_, line_number_)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Exception for an invalid reserve.
|
||||
//***************************************************************************
|
||||
class bip_buffer_reserve_invalid: public bip_buffer_exception
|
||||
{
|
||||
public:
|
||||
|
||||
bip_buffer_reserve_invalid(string_type file_name_, numeric_type line_number_)
|
||||
: bip_buffer_exception(ETL_ERROR_TEXT("bip_buffer:reserve", ETL_BIP_BUFFER_SPSC_ATOMIC_FILE_ID"A"), file_name_, line_number_)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// The common base for a bip_buffer_spsc_atomic_base.
|
||||
//***************************************************************************
|
||||
template <size_t MEMORY_MODEL = etl::memory_model::MEMORY_MODEL_LARGE>
|
||||
class bip_buffer_spsc_atomic_base
|
||||
{
|
||||
public:
|
||||
|
||||
/// The type used for determining the size of buffer.
|
||||
typedef typename etl::size_type_lookup<MEMORY_MODEL>::type size_type;
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns true if the buffer is empty.
|
||||
//*************************************************************************
|
||||
bool empty() const
|
||||
{
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns true if the buffer is full.
|
||||
//*************************************************************************
|
||||
bool full() const
|
||||
{
|
||||
return available() == 0;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns the total used size, which may be split in two blocks
|
||||
/// so the size will always be smaller after a read commit.
|
||||
//*************************************************************************
|
||||
size_type size() const
|
||||
{
|
||||
size_type write_index = write.load(etl::memory_order_acquire);
|
||||
size_type read_index = read.load(etl::memory_order_acquire);
|
||||
|
||||
// no wraparound
|
||||
if (write_index >= read_index)
|
||||
{
|
||||
// size is distance between read and write
|
||||
return write_index - read_index;
|
||||
}
|
||||
else
|
||||
{
|
||||
size_type last_index = last.load(etl::memory_order_acquire);
|
||||
|
||||
// size is distance between beginning and write, plus read and last
|
||||
return (write_index - 0) + (last_index - read_index);
|
||||
}
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns the largest contiguous available block size.
|
||||
//*************************************************************************
|
||||
size_type available() const
|
||||
{
|
||||
size_type write_index = write.load(etl::memory_order_acquire);
|
||||
size_type read_index = read.load(etl::memory_order_acquire);
|
||||
|
||||
// no wraparound
|
||||
if (write_index >= read_index)
|
||||
{
|
||||
size_type forward_size = capacity() - write_index;
|
||||
|
||||
// check if there's more space if wrapping around
|
||||
if (read_index > (forward_size + 1))
|
||||
{
|
||||
return read_index - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return forward_size;
|
||||
}
|
||||
}
|
||||
else // read_index > write_index
|
||||
{
|
||||
return read_index - write_index - 1;
|
||||
}
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns the maximum capacity of the buffer.
|
||||
//*************************************************************************
|
||||
size_type capacity() const
|
||||
{
|
||||
return RESERVED;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Returns the maximum size of the buffer.
|
||||
//*************************************************************************
|
||||
size_type max_size() const
|
||||
{
|
||||
return RESERVED;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructs the buffer.
|
||||
//*************************************************************************
|
||||
bip_buffer_spsc_atomic_base(size_type reserved_)
|
||||
: read(0)
|
||||
, write(0)
|
||||
, last(0)
|
||||
, RESERVED(reserved_)
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
void reset()
|
||||
{
|
||||
read.store(0, etl::memory_order_release);
|
||||
write.store(0, etl::memory_order_release);
|
||||
last.store(0, etl::memory_order_release);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
size_type get_write_reserve(size_type* psize, size_type fallback_size = numeric_limits<size_type>::max())
|
||||
{
|
||||
size_type write_index = write.load(etl::memory_order_relaxed);
|
||||
size_type read_index = read.load(etl::memory_order_acquire);
|
||||
|
||||
// No wraparound
|
||||
if (write_index >= read_index)
|
||||
{
|
||||
size_type forward_size = capacity() - write_index;
|
||||
|
||||
// We still fit in linearly
|
||||
if (*psize <= forward_size)
|
||||
{
|
||||
return write_index;
|
||||
}
|
||||
// There isn't more space even when wrapping around,
|
||||
// or the linear size is good enough as fallback
|
||||
else if ((read_index <= (forward_size + 1)) || (fallback_size <= forward_size))
|
||||
{
|
||||
*psize = forward_size;
|
||||
return write_index;
|
||||
}
|
||||
// Better wrap around now
|
||||
else
|
||||
{
|
||||
// Check if size fits.
|
||||
// When wrapping, the write index cannot reach read index,
|
||||
// then we'd not be able to distinguish wrapped situation from linear.
|
||||
if (*psize >= read_index)
|
||||
{
|
||||
if (read_index > 0)
|
||||
{
|
||||
*psize = read_index - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
*psize = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else // read_index > write_index
|
||||
{
|
||||
// Doesn't fit
|
||||
if ((write_index + *psize) >= read_index)
|
||||
{
|
||||
*psize = read_index - write_index - 1;
|
||||
}
|
||||
|
||||
return write_index;
|
||||
}
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
void apply_write_reserve(size_type windex, size_type wsize)
|
||||
{
|
||||
if (wsize > 0)
|
||||
{
|
||||
size_type write_index = write.load(etl::memory_order_relaxed);
|
||||
size_type read_index = read.load(etl::memory_order_acquire);
|
||||
|
||||
// Wrapped around already
|
||||
if (write_index < read_index)
|
||||
{
|
||||
ETL_ASSERT_OR_RETURN((windex == write_index) && ((wsize + 1) <= read_index), ETL_ERROR(bip_buffer_reserve_invalid));
|
||||
}
|
||||
// No wraparound so far, also not wrapping around with this block
|
||||
else if (windex == write_index)
|
||||
{
|
||||
ETL_ASSERT_OR_RETURN(wsize <= (capacity() - write_index), ETL_ERROR(bip_buffer_reserve_invalid));
|
||||
|
||||
// Move both indexes forward
|
||||
last.store(windex + wsize, etl::memory_order_release);
|
||||
}
|
||||
// Wrapping around now
|
||||
else
|
||||
{
|
||||
ETL_ASSERT_OR_RETURN((windex == 0) && ((wsize + 1) <= read_index), ETL_ERROR(bip_buffer_reserve_invalid));
|
||||
}
|
||||
|
||||
// Always update write index
|
||||
write.store(windex + wsize, etl::memory_order_release);
|
||||
}
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
size_type get_read_reserve(size_type* psize)
|
||||
{
|
||||
size_type read_index = read.load(etl::memory_order_relaxed);
|
||||
size_type write_index = write.load(etl::memory_order_acquire);
|
||||
|
||||
if (read_index > write_index)
|
||||
{
|
||||
// Writer has wrapped around
|
||||
size_type last_index = last.load(etl::memory_order_relaxed);
|
||||
|
||||
if (read_index == last_index)
|
||||
{
|
||||
// Reader reached the end, start read from 0
|
||||
read_index = 0;
|
||||
}
|
||||
else // (read_index < last_index)
|
||||
{
|
||||
// Use the remaining buffer at the end
|
||||
write_index = last_index;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// No wraparound, nothing to adjust
|
||||
}
|
||||
|
||||
// Limit to max available size
|
||||
if ((write_index - read_index) < *psize)
|
||||
{
|
||||
*psize = write_index - read_index;
|
||||
}
|
||||
|
||||
return read_index;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
void apply_read_reserve(size_type rindex, size_type rsize)
|
||||
{
|
||||
if (rsize > 0)
|
||||
{
|
||||
size_type rsize_checker = rsize;
|
||||
ETL_ASSERT_OR_RETURN((rindex == get_read_reserve(&rsize_checker)) && (rsize == rsize_checker), ETL_ERROR(bip_buffer_reserve_invalid));
|
||||
|
||||
read.store(rindex + rsize, etl::memory_order_release);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
etl::atomic<size_type> read;
|
||||
etl::atomic<size_type> write;
|
||||
etl::atomic<size_type> last;
|
||||
const size_type RESERVED;
|
||||
|
||||
#if defined(ETL_POLYMORPHIC_SPSC_BIP_BUFFER_ATOMIC) || defined(ETL_POLYMORPHIC_CONTAINERS)
|
||||
public:
|
||||
|
||||
virtual ~bip_buffer_spsc_atomic_base()
|
||||
{
|
||||
}
|
||||
#else
|
||||
protected:
|
||||
|
||||
~bip_buffer_spsc_atomic_base()
|
||||
{
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// A fixed capacity bipartite buffer.
|
||||
//***************************************************************************
|
||||
template <typename T, const size_t MEMORY_MODEL = etl::memory_model::MEMORY_MODEL_LARGE>
|
||||
class ibip_buffer_spsc_atomic : public bip_buffer_spsc_atomic_base<MEMORY_MODEL>
|
||||
{
|
||||
private:
|
||||
|
||||
typedef typename etl::bip_buffer_spsc_atomic_base<MEMORY_MODEL> base_t;
|
||||
using base_t::reset;
|
||||
using base_t::get_read_reserve;
|
||||
using base_t::apply_read_reserve;
|
||||
using base_t::get_write_reserve;
|
||||
using base_t::apply_write_reserve;
|
||||
|
||||
public:
|
||||
|
||||
typedef T value_type; ///< The type stored in the buffer.
|
||||
typedef T& reference; ///< A reference to the type used in the buffer.
|
||||
typedef const T& const_reference; ///< A const reference to the type used in the buffer.
|
||||
#if ETL_USING_CPP11
|
||||
typedef T&& rvalue_reference;///< An rvalue_reference to the type used in the buffer.
|
||||
#endif
|
||||
typedef typename base_t::size_type size_type; ///< The type used for determining the size of the buffer.
|
||||
|
||||
using base_t::max_size;
|
||||
|
||||
//*************************************************************************
|
||||
// Reserves a memory area for reading (up to the max_reserve_size).
|
||||
//*************************************************************************
|
||||
span<T> read_reserve(size_type max_reserve_size = numeric_limits<size_type>::max())
|
||||
{
|
||||
size_type reserve_size = max_reserve_size;
|
||||
size_type rindex = get_read_reserve(&reserve_size);
|
||||
|
||||
return span<T>(p_buffer + rindex, reserve_size);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
// Commits the previously reserved read memory area
|
||||
// the reserve can be trimmed at the end before committing.
|
||||
// Throws bip_buffer_reserve_invalid
|
||||
//*************************************************************************
|
||||
void read_commit(const span<T> &reserve)
|
||||
{
|
||||
size_type rindex = etl::distance(p_buffer, reserve.data());
|
||||
apply_read_reserve(rindex, reserve.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
// Reserves a memory area for writing up to the max_reserve_size.
|
||||
//*************************************************************************
|
||||
span<T> write_reserve(size_type max_reserve_size)
|
||||
{
|
||||
size_type reserve_size = max_reserve_size;
|
||||
size_type windex = get_write_reserve(&reserve_size);
|
||||
|
||||
return span<T>(p_buffer + windex, reserve_size);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
// Reserves an optimal memory area for writing. The buffer will only wrap
|
||||
// around if the available forward space is less than min_reserve_size.
|
||||
//*************************************************************************
|
||||
span<T> write_reserve_optimal(size_type min_reserve_size = 1U)
|
||||
{
|
||||
size_type reserve_size = numeric_limits<size_type>::max();
|
||||
size_type windex = get_write_reserve(&reserve_size, min_reserve_size);
|
||||
|
||||
return span<T>(p_buffer + windex, reserve_size);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
// Commits the previously reserved write memory area
|
||||
// the reserve can be trimmed at the end before committing.
|
||||
// Throws bip_buffer_reserve_invalid
|
||||
//*************************************************************************
|
||||
void write_commit(const span<T> &reserve)
|
||||
{
|
||||
size_type windex = etl::distance(p_buffer, reserve.data());
|
||||
apply_write_reserve(windex, reserve.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Clears the buffer, destructing any elements that haven't been read.
|
||||
//*************************************************************************
|
||||
void clear()
|
||||
{
|
||||
// the buffer might be split into two contiguous blocks
|
||||
for (span<T> reader = read_reserve(); reader.size() > 0; reader = read_reserve())
|
||||
{
|
||||
destroy(reader.begin(), reader.end());
|
||||
read_commit(reader);
|
||||
}
|
||||
// now the buffer is already empty
|
||||
// resetting the buffer here is beneficial to have
|
||||
// the whole buffer available for a single block,
|
||||
// but it requires synchronization between the writer and reader threads
|
||||
reset();
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
//*************************************************************************
|
||||
ibip_buffer_spsc_atomic(T* p_buffer_, size_type reserved_)
|
||||
: base_t(reserved_)
|
||||
, p_buffer(p_buffer_)
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
// Disable copy construction and assignment.
|
||||
ibip_buffer_spsc_atomic(const ibip_buffer_spsc_atomic&) ETL_DELETE;
|
||||
ibip_buffer_spsc_atomic& operator =(const ibip_buffer_spsc_atomic&) ETL_DELETE;
|
||||
|
||||
#if ETL_USING_CPP11
|
||||
ibip_buffer_spsc_atomic(ibip_buffer_spsc_atomic&&) = delete;
|
||||
ibip_buffer_spsc_atomic& operator =(ibip_buffer_spsc_atomic&&) = delete;
|
||||
#endif
|
||||
|
||||
T* const p_buffer;
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// A fixed capacity bipartite buffer.
|
||||
/// This buffer supports concurrent access by one producer and one consumer.
|
||||
/// \tparam T The type this buffer should support.
|
||||
/// \tparam SIZE The maximum capacity of the buffer.
|
||||
/// \tparam MEMORY_MODEL The memory model for the buffer. Determines the type of the internal counter variables.
|
||||
//***************************************************************************
|
||||
template <typename T, const size_t SIZE, const size_t MEMORY_MODEL = etl::memory_model::MEMORY_MODEL_LARGE>
|
||||
class bip_buffer_spsc_atomic : public ibip_buffer_spsc_atomic<T, MEMORY_MODEL>
|
||||
{
|
||||
private:
|
||||
|
||||
typedef typename etl::ibip_buffer_spsc_atomic<T, MEMORY_MODEL> base_t;
|
||||
|
||||
public:
|
||||
|
||||
typedef typename base_t::size_type size_type;
|
||||
|
||||
private:
|
||||
|
||||
static ETL_CONSTANT size_type RESERVED_SIZE = size_type(SIZE);
|
||||
|
||||
public:
|
||||
|
||||
ETL_STATIC_ASSERT((SIZE <= (etl::integral_limits<size_type>::max)), "Size too large for memory model");
|
||||
|
||||
static ETL_CONSTANT size_type MAX_SIZE = size_type(SIZE);
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
bip_buffer_spsc_atomic()
|
||||
: base_t(reinterpret_cast<T*>(buffer.raw), RESERVED_SIZE)
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Destructor.
|
||||
//*************************************************************************
|
||||
~bip_buffer_spsc_atomic()
|
||||
{
|
||||
base_t::clear();
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
/// The uninitialised buffer of T used in the bip_buffer_spsc.
|
||||
etl::uninitialized_buffer_of<T, RESERVED_SIZE> buffer;
|
||||
};
|
||||
|
||||
template <typename T, const size_t SIZE, const size_t MEMORY_MODEL>
|
||||
ETL_CONSTANT typename bip_buffer_spsc_atomic<T, SIZE, MEMORY_MODEL>::size_type bip_buffer_spsc_atomic<T, SIZE, MEMORY_MODEL>::RESERVED_SIZE;
|
||||
}
|
||||
|
||||
#endif /* ETL_HAS_ATOMIC && ETL_USING_CPP11 */
|
||||
|
||||
#endif /* ETL_BIP_BUFFER_SPSC_ATOMIC_INCLUDED */
|
||||
252
Chapter18/cib/libs/etl/include/etl/bit.h
Normal file
252
Chapter18/cib/libs/etl/include/etl/bit.h
Normal file
@@ -0,0 +1,252 @@
|
||||
///\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_BIT_INCLUDED
|
||||
#define ETL_BIT_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "type_traits.h"
|
||||
#include "binary.h"
|
||||
#include "integral_limits.h"
|
||||
#include "endianness.h"
|
||||
#include "type_traits.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#if ETL_USING_CPP20 && ETL_USING_STL
|
||||
#include <bit>
|
||||
#endif
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
/// bit_cast - Type to different type.
|
||||
//***************************************************************************
|
||||
template <typename TDestination, typename TSource>
|
||||
ETL_NODISCARD
|
||||
typename etl::enable_if<!(etl::is_integral<TDestination>::value&& etl::is_integral<TSource>::value) &&
|
||||
(sizeof(TDestination) == sizeof(TSource)) &&
|
||||
etl::is_trivially_copyable<TSource>::value &&
|
||||
etl::is_trivially_copyable<TDestination>::value, TDestination>::type
|
||||
bit_cast(const TSource& source) ETL_NOEXCEPT
|
||||
{
|
||||
TDestination destination;
|
||||
|
||||
memcpy(&destination, &source, sizeof(TDestination));
|
||||
|
||||
return destination;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// bit_cast - Integral to integral
|
||||
//***************************************************************************
|
||||
template <typename TDestination, typename TSource>
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR14
|
||||
typename etl::enable_if<(etl::is_integral<TDestination>::value && etl::is_integral<TSource>::value) &&
|
||||
(sizeof(TDestination) == sizeof(TSource)), TDestination>::type
|
||||
bit_cast(const TSource& source) ETL_NOEXCEPT
|
||||
{
|
||||
return static_cast<TDestination>(source);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// byteswap
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
ETL_CONSTEXPR14
|
||||
typename etl::enable_if<etl::is_integral<T>::value, T>::type
|
||||
byteswap(T value) ETL_NOEXCEPT
|
||||
{
|
||||
return etl::reverse_bytes(value);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// has_single_bit
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
ETL_NODISCARD ETL_CONSTEXPR14
|
||||
typename etl::enable_if<etl::is_unsigned<T>::value, bool>::type has_single_bit(T value) ETL_NOEXCEPT
|
||||
{
|
||||
return (value & (value - 1)) == 0;
|
||||
}
|
||||
//***************************************************************************
|
||||
/// countl_zero
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
ETL_NODISCARD ETL_CONSTEXPR14
|
||||
typename etl::enable_if<etl::is_unsigned<T>::value, int>::type
|
||||
countl_zero(T value) ETL_NOEXCEPT
|
||||
{
|
||||
return etl::count_leading_zeros(value);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// countl_one
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
ETL_NODISCARD ETL_CONSTEXPR14
|
||||
typename etl::enable_if<etl::is_unsigned<T>::value, int>::type
|
||||
countl_one(T value) ETL_NOEXCEPT
|
||||
{
|
||||
return etl::count_leading_ones(value);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// countr_zero
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
ETL_NODISCARD ETL_CONSTEXPR14
|
||||
typename etl::enable_if<etl::is_unsigned<T>::value, int>::type
|
||||
countr_zero(T value) ETL_NOEXCEPT
|
||||
{
|
||||
return etl::count_trailing_zeros(value);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// countr_one
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
ETL_NODISCARD ETL_CONSTEXPR14
|
||||
typename etl::enable_if<etl::is_unsigned<T>::value, int>::type
|
||||
countr_one(T value) ETL_NOEXCEPT
|
||||
{
|
||||
return etl::count_trailing_ones(value);;
|
||||
}
|
||||
|
||||
|
||||
//***************************************************************************
|
||||
/// bit_width
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
ETL_CONSTEXPR14
|
||||
typename etl::enable_if<etl::is_unsigned<T>::value, T>::type
|
||||
bit_width(T value) ETL_NOEXCEPT
|
||||
{
|
||||
#if ETL_USING_CPP20 && ETL_USING_STL
|
||||
return std::bit_width(value);
|
||||
#else
|
||||
return etl::integral_limits<T>::bits - etl::countl_zero(value);
|
||||
#endif
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// bit_ceil
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
ETL_NODISCARD ETL_CONSTEXPR14
|
||||
typename etl::enable_if<etl::is_unsigned<T>::value, T>::type
|
||||
bit_ceil(T value)
|
||||
{
|
||||
#if ETL_USING_CPP20 && ETL_USING_STL
|
||||
return std::bit_ceil(value);
|
||||
#else
|
||||
if (value == T(0))
|
||||
{
|
||||
return T(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
return T(1) << etl::bit_width(T(value - T(1)));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// bit_floor
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
ETL_NODISCARD ETL_CONSTEXPR14
|
||||
typename etl::enable_if<etl::is_unsigned<T>::value, T>::type
|
||||
bit_floor(T value) ETL_NOEXCEPT
|
||||
{
|
||||
#if ETL_USING_CPP20 && ETL_USING_STL
|
||||
return std::bit_floor(value);
|
||||
#else
|
||||
if (value == T(0))
|
||||
{
|
||||
return T(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
return T(1) << (etl::bit_width(value) - T(1));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// rotl
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
ETL_NODISCARD ETL_CONSTEXPR14
|
||||
typename etl::enable_if<etl::is_unsigned<T>::value, T>::type
|
||||
rotl(T value, int n) ETL_NOEXCEPT
|
||||
{
|
||||
if (n < 0)
|
||||
{
|
||||
return etl::rotate_right(value, -n);
|
||||
}
|
||||
else
|
||||
{
|
||||
return etl::rotate_left(value, n);
|
||||
}
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// rotr
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
ETL_NODISCARD ETL_CONSTEXPR14
|
||||
typename etl::enable_if<etl::is_unsigned<T>::value, T>::type
|
||||
rotr(T value, int n) ETL_NOEXCEPT
|
||||
{
|
||||
if (n < 0)
|
||||
{
|
||||
return etl::rotate_left(value, -n);
|
||||
}
|
||||
else
|
||||
{
|
||||
return etl::rotate_right(value, n);
|
||||
}
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// popcount
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
ETL_NODISCARD ETL_CONSTEXPR14
|
||||
typename etl::enable_if<etl::is_unsigned<T>::value, int>::type
|
||||
popcount(T value) ETL_NOEXCEPT
|
||||
{
|
||||
return etl::count_bits(value);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
1409
Chapter18/cib/libs/etl/include/etl/bit_stream.h
Normal file
1409
Chapter18/cib/libs/etl/include/etl/bit_stream.h
Normal file
File diff suppressed because it is too large
Load Diff
42
Chapter18/cib/libs/etl/include/etl/bitset.h
Normal file
42
Chapter18/cib/libs/etl/include/etl/bitset.h
Normal file
@@ -0,0 +1,42 @@
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2014 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_BITSET_INCLUDED
|
||||
#define ETL_BITSET_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
|
||||
#if defined(ETL_USE_LEGACY_BITSET)
|
||||
#include "private/bitset_legacy.h"
|
||||
#else
|
||||
#include "private/bitset_new.h"
|
||||
#endif
|
||||
|
||||
#endif
|
||||
190
Chapter18/cib/libs/etl/include/etl/bloom_filter.h
Normal file
190
Chapter18/cib/libs/etl/include/etl/bloom_filter.h
Normal file
@@ -0,0 +1,190 @@
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2014 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_BLOOM_FILTER_INCLUDED
|
||||
#define ETL_BLOOM_FILTER_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "parameter_type.h"
|
||||
#include "bitset.h"
|
||||
#include "type_traits.h"
|
||||
#include "binary.h"
|
||||
#include "log.h"
|
||||
#include "power.h"
|
||||
|
||||
///\defgroup bloom_filter bloom_filter
|
||||
/// A Bloom filter
|
||||
///\ingroup containers
|
||||
|
||||
namespace etl
|
||||
{
|
||||
namespace private_bloom_filter
|
||||
{
|
||||
// Placeholder null hash for defaulted template parameters.
|
||||
struct null_hash
|
||||
{
|
||||
template <typename T>
|
||||
size_t operator ()(T)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// An implementation of a bloom filter.
|
||||
/// Allows up to three hashes to be defined.
|
||||
/// Hashes must support the () operator and define 'argument_type'.
|
||||
///\tparam Desired_Width The desired number of hash results that can be stored. Rounded up to best fit the underlying bitset.
|
||||
///\tparam THash1 The first hash generator class.
|
||||
///\tparam THash2 The second hash generator class. If omitted, uses the null hash.
|
||||
///\tparam THash3 The third hash generator class. If omitted, uses the null hash.
|
||||
/// The hash classes must define <b>argument_type</b>.
|
||||
///\ingroup bloom_filter
|
||||
//***************************************************************************
|
||||
template <size_t Desired_Width,
|
||||
typename THash1,
|
||||
typename THash2 = private_bloom_filter::null_hash,
|
||||
typename THash3 = private_bloom_filter::null_hash>
|
||||
class bloom_filter
|
||||
{
|
||||
private:
|
||||
|
||||
typedef typename etl::parameter_type<typename THash1::argument_type>::type parameter_t;
|
||||
typedef private_bloom_filter::null_hash null_hash;
|
||||
|
||||
public:
|
||||
|
||||
enum
|
||||
{
|
||||
// Make the most efficient use of the bitset.
|
||||
WIDTH = etl::bitset<Desired_Width>::Allocated_Bits
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Clears the bloom filter of all entries.
|
||||
//***************************************************************************
|
||||
void clear()
|
||||
{
|
||||
flags.reset();
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Adds a key to the filter.
|
||||
///\param key The key to add.
|
||||
//***************************************************************************
|
||||
void add(parameter_t key)
|
||||
{
|
||||
flags.set(get_hash<THash1>(key));
|
||||
|
||||
if (!etl::is_same<THash2, null_hash>::value)
|
||||
{
|
||||
flags.set(get_hash<THash2>(key));
|
||||
}
|
||||
|
||||
if (!etl::is_same<THash3, null_hash>::value)
|
||||
{
|
||||
flags.set(get_hash<THash3>(key));
|
||||
}
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Tests a key to see if it exists in the filter.
|
||||
///\param key The key to test.
|
||||
///\return <b>true</b> if the key exists in the filter.
|
||||
//***************************************************************************
|
||||
bool exists(parameter_t key) const
|
||||
{
|
||||
bool exists1 = flags[get_hash<THash1>(key)];
|
||||
bool exists2 = true;
|
||||
bool exists3 = true;
|
||||
|
||||
// Do we have a second hash?
|
||||
if (!etl::is_same<THash2, null_hash>::value)
|
||||
{
|
||||
exists2 = flags[get_hash<THash2>(key)];
|
||||
}
|
||||
|
||||
// Do we have a third hash?
|
||||
if (!etl::is_same<THash3, null_hash>::value)
|
||||
{
|
||||
exists3 = flags[get_hash<THash3>(key)];
|
||||
}
|
||||
|
||||
return exists1 && exists2 && exists3;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Returns the width of the Bloom filter.
|
||||
//***************************************************************************
|
||||
size_t width() const
|
||||
{
|
||||
return WIDTH;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Returns the percentage of usage. Range 0 to 100.
|
||||
//***************************************************************************
|
||||
size_t usage() const
|
||||
{
|
||||
return (100 * count()) / WIDTH;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Returns the number of filter flags set.
|
||||
//***************************************************************************
|
||||
size_t count() const
|
||||
{
|
||||
return flags.count();
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
//***************************************************************************
|
||||
/// Gets the hash for the key.
|
||||
///\param key The key.
|
||||
///\return The hash value.
|
||||
//***************************************************************************
|
||||
template <typename THash>
|
||||
size_t get_hash(parameter_t key) const
|
||||
{
|
||||
size_t hash = THash()(key);
|
||||
|
||||
// Fold the hash down to fit the width.
|
||||
return fold_bits<size_t, etl::log2<WIDTH>::value>(hash);
|
||||
}
|
||||
|
||||
/// The Bloom filter flags.
|
||||
etl::bitset<WIDTH> flags;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
361
Chapter18/cib/libs/etl/include/etl/bresenham_line.h
Normal file
361
Chapter18/cib/libs/etl/include/etl/bresenham_line.h
Normal file
@@ -0,0 +1,361 @@
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2020 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_BRESENHAM_LINE_INCLUDED
|
||||
#define ETL_BRESENHAM_LINE_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "iterator.h"
|
||||
#include "static_assert.h"
|
||||
#include "type_traits.h"
|
||||
#include "utility.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
/// A pseudo-container that generates points on a line, using Bresenham's
|
||||
/// line algorithm.
|
||||
/// T is the type for the etl::coordinate_2d value type.
|
||||
/// TWork is the internal working variable type. Default is int16_t.
|
||||
//***************************************************************************
|
||||
template <typename T, typename TWork = int16_t>
|
||||
class bresenham_line
|
||||
{
|
||||
public:
|
||||
|
||||
//***************************************************
|
||||
/// Standard container types.
|
||||
//***************************************************
|
||||
typedef etl::coordinate_2d<T> value_type;
|
||||
typedef size_t size_type;
|
||||
typedef ptrdiff_t difference_type;
|
||||
typedef value_type& reference;
|
||||
typedef const value_type& const_reference;
|
||||
typedef value_type* pointer;
|
||||
typedef const value_type* const_pointer;
|
||||
|
||||
//***************************************************
|
||||
/// Const Iterator
|
||||
//***************************************************
|
||||
class const_iterator : public etl::iterator<ETL_OR_STD::forward_iterator_tag, const value_type>
|
||||
{
|
||||
public:
|
||||
|
||||
friend class bresenham_line;
|
||||
|
||||
//***************************************************
|
||||
/// Default constructor
|
||||
//***************************************************
|
||||
const_iterator()
|
||||
: p_bresenham_line(ETL_NULLPTR)
|
||||
{
|
||||
}
|
||||
|
||||
//***************************************************
|
||||
/// Copy constructor
|
||||
//***************************************************
|
||||
const_iterator(const const_iterator& other)
|
||||
: p_bresenham_line(other.p_bresenham_line)
|
||||
{
|
||||
}
|
||||
|
||||
//***************************************************
|
||||
/// Assignment operator
|
||||
//***************************************************
|
||||
const_iterator& operator =(const const_iterator& rhs)
|
||||
{
|
||||
p_bresenham_line = rhs.p_bresenham_line;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
//***************************************************
|
||||
/// Pre-increment operator
|
||||
//***************************************************
|
||||
const_iterator& operator ++()
|
||||
{
|
||||
// Has the end of the series has been reached?
|
||||
if (p_bresenham_line->get_coordinate() == p_bresenham_line->back())
|
||||
{
|
||||
// Mark it as an end iterator.
|
||||
p_bresenham_line = ETL_NULLPTR;
|
||||
}
|
||||
else
|
||||
{
|
||||
p_bresenham_line->next();
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
//***************************************************
|
||||
/// De-reference operator
|
||||
//***************************************************
|
||||
value_type operator *() const
|
||||
{
|
||||
return p_bresenham_line->get_coordinate();
|
||||
}
|
||||
|
||||
//***************************************************
|
||||
/// Equality operator
|
||||
//***************************************************
|
||||
friend bool operator ==(const const_iterator& lhs, const const_iterator& rhs)
|
||||
{
|
||||
return lhs.p_bresenham_line == rhs.p_bresenham_line;
|
||||
}
|
||||
|
||||
//***************************************************
|
||||
/// Inequality operator
|
||||
//***************************************************
|
||||
friend bool operator !=(const const_iterator& lhs, const const_iterator& rhs)
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
//***************************************************
|
||||
/// Constructor for use by bresenham_line
|
||||
//***************************************************
|
||||
const_iterator(bresenham_line<T>* pb)
|
||||
: p_bresenham_line(pb)
|
||||
{
|
||||
}
|
||||
|
||||
bresenham_line<T>* p_bresenham_line;
|
||||
};
|
||||
|
||||
//***************************************************
|
||||
/// Constructor.
|
||||
//***************************************************
|
||||
bresenham_line()
|
||||
{
|
||||
initialise(T(0), T(0), T(0), T(0));
|
||||
}
|
||||
|
||||
//***************************************************
|
||||
/// Constructor.
|
||||
/// Supplied first and last coordinates
|
||||
//***************************************************
|
||||
bresenham_line(etl::coordinate_2d<T> first_, etl::coordinate_2d<T> last_)
|
||||
{
|
||||
initialise(first_.x, first_.y, last_.x, last_.y);
|
||||
}
|
||||
|
||||
//***************************************************
|
||||
/// Constructor.
|
||||
/// Supplied first and last coordinates
|
||||
//***************************************************
|
||||
bresenham_line(T first_x, T first_y, T last_x, T last_y)
|
||||
{
|
||||
initialise(first_x, first_y, last_x, last_y);
|
||||
}
|
||||
|
||||
//***************************************************
|
||||
/// Resets the line.
|
||||
/// Supplied first and last coordinates
|
||||
//***************************************************
|
||||
void reset(etl::coordinate_2d<T> first_, etl::coordinate_2d<T> last_)
|
||||
{
|
||||
initialise(first_.x, first_.y, last_.x, last_.y);
|
||||
}
|
||||
|
||||
//***************************************************
|
||||
/// Resets the line.
|
||||
/// Supplied first and last coordinates
|
||||
//***************************************************
|
||||
void reset(T first_x, T first_y, T last_x, T last_y)
|
||||
{
|
||||
initialise(first_x, first_y, last_x, last_y);
|
||||
}
|
||||
|
||||
//***************************************************
|
||||
/// Get a const_iterator to the first coordinate.
|
||||
/// Resets the Bresenham line.
|
||||
//***************************************************
|
||||
const_iterator begin()
|
||||
{
|
||||
coordinate = first;
|
||||
|
||||
return const_iterator(this);
|
||||
}
|
||||
|
||||
//***************************************************
|
||||
/// Get a const_iterator to one past the last coordinate.
|
||||
//***************************************************
|
||||
const_iterator end() const
|
||||
{
|
||||
return const_iterator();
|
||||
}
|
||||
|
||||
//***************************************************
|
||||
/// Get the first coordinate.
|
||||
//***************************************************
|
||||
const_reference front() const
|
||||
{
|
||||
return first;
|
||||
}
|
||||
|
||||
//***************************************************
|
||||
/// Get the last coordinate.
|
||||
//***************************************************
|
||||
const_reference back() const
|
||||
{
|
||||
return last;
|
||||
}
|
||||
|
||||
//***************************************************
|
||||
/// Get the size of the series.
|
||||
//***************************************************
|
||||
size_t size() const
|
||||
{
|
||||
if (y_is_major_axis())
|
||||
{
|
||||
return (dy / 2) + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return (dx / 2) + 1;
|
||||
}
|
||||
}
|
||||
|
||||
//***************************************************
|
||||
/// Equality operator
|
||||
//***************************************************
|
||||
friend bool operator ==(const bresenham_line& lhs, const bresenham_line& rhs)
|
||||
{
|
||||
return (lhs.front() == rhs.front()) && (lhs.back() == rhs.back());
|
||||
}
|
||||
|
||||
//***************************************************
|
||||
/// Inequality operator
|
||||
//***************************************************
|
||||
friend bool operator !=(const bresenham_line& lhs, const bresenham_line& rhs)
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
//***************************************************
|
||||
/// Get the current number of generated points.
|
||||
//***************************************************
|
||||
void initialise(T first_x, T first_y, T last_x, T last_y)
|
||||
{
|
||||
first = value_type(first_x, first_y);
|
||||
last = value_type(last_x, last_y);
|
||||
coordinate = first;
|
||||
x_increment = (last_x < first_x) ? -1 : 1;
|
||||
y_increment = (last_y < first_y) ? -1 : 1;
|
||||
dx = (last_x < first_x) ? first_x - last_x : last_x - first_x;
|
||||
dy = (last_y < first_y) ? first_y - last_y : last_y - first_y;
|
||||
do_minor_increment = false;
|
||||
|
||||
if (y_is_major_axis())
|
||||
{
|
||||
dx *= 2;
|
||||
balance = dx - dy;
|
||||
dy *= 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
dy *= 2;
|
||||
balance = dy - dx;
|
||||
dx *= 2;
|
||||
}
|
||||
}
|
||||
|
||||
//***************************************************
|
||||
/// Returns true if Y is the major axis.
|
||||
//***************************************************
|
||||
bool y_is_major_axis() const
|
||||
{
|
||||
return dx < dy;
|
||||
}
|
||||
|
||||
//***************************************************
|
||||
/// Calculate the next point.
|
||||
//***************************************************
|
||||
void next()
|
||||
{
|
||||
if (y_is_major_axis())
|
||||
{
|
||||
// Y is major axis.
|
||||
if (do_minor_increment)
|
||||
{
|
||||
coordinate.x = T(coordinate.x + x_increment);
|
||||
balance -= dy;
|
||||
}
|
||||
|
||||
coordinate.y = T(coordinate.y + y_increment);
|
||||
balance += dx;
|
||||
}
|
||||
else
|
||||
{
|
||||
// X is major axis.
|
||||
if (do_minor_increment)
|
||||
{
|
||||
coordinate.y = T(coordinate.y + y_increment);
|
||||
balance -= dx;
|
||||
}
|
||||
|
||||
coordinate.x = T(coordinate.x + x_increment);
|
||||
balance += dy;
|
||||
}
|
||||
|
||||
do_minor_increment = (balance >= 0);
|
||||
}
|
||||
|
||||
//***************************************************
|
||||
/// Get the current coordinate.
|
||||
//***************************************************
|
||||
value_type get_coordinate() const
|
||||
{
|
||||
return coordinate;
|
||||
}
|
||||
|
||||
typedef TWork work_t;
|
||||
|
||||
value_type first;
|
||||
value_type last;
|
||||
value_type coordinate;
|
||||
work_t x_increment;
|
||||
work_t y_increment;
|
||||
work_t dx;
|
||||
work_t dy;
|
||||
work_t balance;
|
||||
bool do_minor_increment;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
304
Chapter18/cib/libs/etl/include/etl/buffer_descriptors.h
Normal file
304
Chapter18/cib/libs/etl/include/etl/buffer_descriptors.h
Normal file
@@ -0,0 +1,304 @@
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2020 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_BUFFER_DESCRIPTORS_INCLUDED
|
||||
#define ETL_BUFFER_DESCRIPTORS_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "array.h"
|
||||
#include "delegate.h"
|
||||
#include "type_traits.h"
|
||||
#include "static_assert.h"
|
||||
#include "cyclic_value.h"
|
||||
#include "algorithm.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#if ETL_USING_CPP11
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
/// buffer_descriptors
|
||||
//***************************************************************************
|
||||
template <typename TBuffer, size_t BUFFER_SIZE_, size_t N_BUFFERS_, typename TFlag = bool>
|
||||
class buffer_descriptors
|
||||
{
|
||||
private:
|
||||
|
||||
struct descriptor_item;
|
||||
|
||||
public:
|
||||
|
||||
typedef TBuffer value_type;
|
||||
typedef value_type* pointer;
|
||||
typedef size_t size_type;
|
||||
typedef TFlag flag_type;
|
||||
|
||||
static ETL_CONSTANT size_type N_BUFFERS = N_BUFFERS_;
|
||||
static ETL_CONSTANT size_type BUFFER_SIZE = BUFFER_SIZE_;
|
||||
|
||||
//*********************************
|
||||
/// Describes a buffer.
|
||||
//*********************************
|
||||
class descriptor
|
||||
{
|
||||
public:
|
||||
|
||||
friend class buffer_descriptors;
|
||||
|
||||
static ETL_CONSTANT size_type MAX_SIZE = buffer_descriptors::BUFFER_SIZE;
|
||||
|
||||
//*********************************
|
||||
descriptor()
|
||||
: pdesc_item(ETL_NULLPTR)
|
||||
{
|
||||
}
|
||||
|
||||
//*********************************
|
||||
descriptor(const descriptor& other)
|
||||
: pdesc_item(other.pdesc_item)
|
||||
{
|
||||
}
|
||||
|
||||
//*********************************
|
||||
descriptor& operator =(const descriptor& other)
|
||||
{
|
||||
pdesc_item = other.pdesc_item;
|
||||
return *this;
|
||||
}
|
||||
|
||||
//*********************************
|
||||
pointer data() const
|
||||
{
|
||||
assert(pdesc_item != ETL_NULLPTR);
|
||||
return pdesc_item->pbuffer;
|
||||
}
|
||||
|
||||
//*********************************
|
||||
ETL_NODISCARD
|
||||
ETL_CONSTEXPR size_type max_size() const
|
||||
{
|
||||
return BUFFER_SIZE;
|
||||
}
|
||||
|
||||
//*********************************
|
||||
ETL_NODISCARD
|
||||
bool is_allocated() const
|
||||
{
|
||||
return bool(pdesc_item->in_use);
|
||||
}
|
||||
|
||||
//*********************************
|
||||
ETL_NODISCARD
|
||||
bool is_released() const
|
||||
{
|
||||
return bool(!pdesc_item->in_use);
|
||||
}
|
||||
|
||||
//*********************************
|
||||
ETL_NODISCARD
|
||||
bool is_valid() const
|
||||
{
|
||||
return pdesc_item != ETL_NULLPTR;
|
||||
}
|
||||
|
||||
//*********************************
|
||||
void release()
|
||||
{
|
||||
pdesc_item->in_use = false;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
//*********************************
|
||||
descriptor(descriptor_item* pdesc_item_)
|
||||
: pdesc_item(pdesc_item_)
|
||||
{
|
||||
}
|
||||
|
||||
//*********************************
|
||||
void allocate()
|
||||
{
|
||||
pdesc_item->in_use = true;;
|
||||
}
|
||||
|
||||
/// The pointer to the buffer descriptor.
|
||||
descriptor_item* pdesc_item;
|
||||
};
|
||||
|
||||
//*********************************
|
||||
/// Describes a notification.
|
||||
//*********************************
|
||||
class notification
|
||||
{
|
||||
public:
|
||||
|
||||
//*********************************
|
||||
notification()
|
||||
: desc()
|
||||
, count(0U)
|
||||
{
|
||||
}
|
||||
|
||||
//*********************************
|
||||
notification(descriptor desc_, size_t count_)
|
||||
: desc(desc_)
|
||||
, count(count_)
|
||||
{
|
||||
}
|
||||
|
||||
//*********************************
|
||||
ETL_NODISCARD
|
||||
descriptor get_descriptor() const
|
||||
{
|
||||
return desc;
|
||||
}
|
||||
|
||||
//*********************************
|
||||
ETL_NODISCARD
|
||||
size_t get_count() const
|
||||
{
|
||||
return count;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
descriptor desc;
|
||||
size_t count;
|
||||
};
|
||||
|
||||
// The type of the callback function.
|
||||
typedef etl::delegate<void(notification)> callback_type;
|
||||
|
||||
//*********************************
|
||||
buffer_descriptors(TBuffer* pbuffers_, callback_type callback_ = callback_type())
|
||||
: callback(callback_)
|
||||
{
|
||||
for (size_t i = 0UL; i < N_BUFFERS; ++i)
|
||||
{
|
||||
descriptor_items[i].pbuffer = pbuffers_ + (i * BUFFER_SIZE);
|
||||
descriptor_items[i].in_use = false;
|
||||
}
|
||||
}
|
||||
|
||||
//*********************************
|
||||
void set_callback(const callback_type& callback_)
|
||||
{
|
||||
callback = callback_;
|
||||
}
|
||||
|
||||
//*********************************
|
||||
void clear()
|
||||
{
|
||||
for (size_t i = 0UL; i < N_BUFFERS; ++i)
|
||||
{
|
||||
descriptor_items[i].in_use = false;
|
||||
}
|
||||
|
||||
next.to_first();
|
||||
}
|
||||
|
||||
//*********************************
|
||||
ETL_NODISCARD
|
||||
bool is_valid() const
|
||||
{
|
||||
return callback.is_valid();
|
||||
}
|
||||
|
||||
//*********************************
|
||||
void notify(notification n)
|
||||
{
|
||||
// Do we have a valid callback?
|
||||
if (callback.is_valid())
|
||||
{
|
||||
callback(n);
|
||||
}
|
||||
}
|
||||
|
||||
//*********************************
|
||||
ETL_NODISCARD
|
||||
descriptor allocate()
|
||||
{
|
||||
descriptor desc(&descriptor_items[next]);
|
||||
|
||||
if (desc.is_released())
|
||||
{
|
||||
++next;
|
||||
|
||||
desc.allocate();
|
||||
|
||||
return desc;
|
||||
}
|
||||
else
|
||||
{
|
||||
return descriptor();
|
||||
}
|
||||
}
|
||||
|
||||
//*********************************
|
||||
ETL_NODISCARD
|
||||
descriptor allocate(value_type fill_)
|
||||
{
|
||||
descriptor desc = allocate();
|
||||
|
||||
if (desc.is_valid())
|
||||
{
|
||||
etl::fill_n(desc.data(), BUFFER_SIZE, fill_);
|
||||
}
|
||||
|
||||
return desc;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
//*********************************
|
||||
struct descriptor_item
|
||||
{
|
||||
pointer pbuffer;
|
||||
volatile flag_type in_use;
|
||||
};
|
||||
|
||||
callback_type callback;
|
||||
etl::array<descriptor_item, N_BUFFERS> descriptor_items;
|
||||
etl::cyclic_value<uint_least8_t, 0U, N_BUFFERS - 1> next;
|
||||
};
|
||||
|
||||
template <typename TBuffer, size_t BUFFER_SIZE_, size_t N_BUFFERS_, typename TFlag>
|
||||
ETL_CONSTANT typename buffer_descriptors<TBuffer, BUFFER_SIZE_, N_BUFFERS_, TFlag>::size_type buffer_descriptors<TBuffer, BUFFER_SIZE_, N_BUFFERS_, TFlag>::N_BUFFERS;
|
||||
|
||||
template <typename TBuffer, size_t BUFFER_SIZE_, size_t N_BUFFERS_, typename TFlag>
|
||||
ETL_CONSTANT typename buffer_descriptors<TBuffer, BUFFER_SIZE_, N_BUFFERS_, TFlag>::size_type buffer_descriptors<TBuffer, BUFFER_SIZE_, N_BUFFERS_, TFlag>::BUFFER_SIZE;
|
||||
|
||||
template <typename TBuffer, size_t BUFFER_SIZE_, size_t N_BUFFERS_, typename TFlag>
|
||||
ETL_CONSTANT typename buffer_descriptors<TBuffer, BUFFER_SIZE_, N_BUFFERS_, TFlag>::size_type buffer_descriptors<TBuffer, BUFFER_SIZE_, N_BUFFERS_, TFlag>::descriptor::MAX_SIZE;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
323
Chapter18/cib/libs/etl/include/etl/byte.h
Normal file
323
Chapter18/cib/libs/etl/include/etl/byte.h
Normal file
@@ -0,0 +1,323 @@
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
Copyright(c) 2022 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_BYTE_INCLUDED
|
||||
#define ETL_BYTE_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "type_traits.h"
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11 && !defined(ETL_BYTE_FORCE_CPP03_IMPLEMENTATION)
|
||||
|
||||
enum class byte : unsigned char {};
|
||||
|
||||
//*************************************************************************
|
||||
/// To integer.
|
||||
//*************************************************************************
|
||||
template <typename TInteger>
|
||||
constexpr
|
||||
typename etl::enable_if<etl::is_integral<TInteger>::value, TInteger>::type
|
||||
to_integer(etl::byte b) noexcept
|
||||
{
|
||||
return TInteger(b);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Shift left.
|
||||
//*************************************************************************
|
||||
template <typename TInteger>
|
||||
constexpr
|
||||
typename etl::enable_if<etl::is_integral<TInteger>::value, etl::byte>::type
|
||||
operator <<(etl::byte b, TInteger shift) noexcept
|
||||
{
|
||||
return etl::byte(static_cast<unsigned int>(b) << shift);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Shift right
|
||||
//*************************************************************************
|
||||
template <typename TInteger>
|
||||
constexpr
|
||||
typename etl::enable_if<etl::is_integral<TInteger>::value, etl::byte>::type
|
||||
operator >>(etl::byte b, TInteger shift) noexcept
|
||||
{
|
||||
return etl::byte(static_cast<unsigned int>(b) >> shift);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Shift left equals.
|
||||
//*************************************************************************
|
||||
template <typename TInteger>
|
||||
constexpr
|
||||
typename etl::enable_if<etl::is_integral<TInteger>::value, etl::byte&>::type
|
||||
operator <<=(etl::byte& b, TInteger shift) noexcept
|
||||
{
|
||||
return b = b << shift;;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Shift right equals.
|
||||
//*************************************************************************
|
||||
template <typename TInteger>
|
||||
constexpr
|
||||
typename etl::enable_if<etl::is_integral<TInteger>::value, etl::byte&>::type
|
||||
operator >>=(etl::byte& b, TInteger shift) noexcept
|
||||
{
|
||||
return b = b >> shift;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Or.
|
||||
//*************************************************************************
|
||||
inline constexpr etl::byte operator |(etl::byte lhs, etl::byte rhs) noexcept
|
||||
{
|
||||
return etl::byte(static_cast<unsigned int>(lhs) | static_cast<unsigned int>(rhs));
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// And.
|
||||
//*************************************************************************
|
||||
inline constexpr etl::byte operator &(etl::byte lhs, etl::byte rhs) noexcept
|
||||
{
|
||||
return etl::byte(static_cast<unsigned int>(lhs) & static_cast<unsigned int>(rhs));
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Exclusive Or.
|
||||
//*************************************************************************
|
||||
inline constexpr etl::byte operator ^(etl::byte lhs, etl::byte rhs) noexcept
|
||||
{
|
||||
return etl::byte(static_cast<unsigned int>(lhs) ^ static_cast<unsigned int>(rhs));
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Or equals.
|
||||
//*************************************************************************
|
||||
inline ETL_CONSTEXPR14 etl::byte& operator |=(etl::byte& lhs, etl::byte rhs) noexcept
|
||||
{
|
||||
return lhs = lhs | rhs;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// And equals
|
||||
//*************************************************************************
|
||||
inline ETL_CONSTEXPR14 etl::byte& operator &=(etl::byte& lhs, etl::byte rhs) noexcept
|
||||
{
|
||||
return lhs = lhs & rhs;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Exclusive or equals.
|
||||
//*************************************************************************
|
||||
inline ETL_CONSTEXPR14 etl::byte& operator ^=(etl::byte& lhs, etl::byte rhs) noexcept
|
||||
{
|
||||
return lhs = lhs ^ rhs;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Not.
|
||||
//*************************************************************************
|
||||
inline constexpr etl::byte operator ~(etl::byte b) noexcept
|
||||
{
|
||||
return etl::byte(~static_cast<unsigned int>(b));
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
//*************************************************************************
|
||||
/// The byte class.
|
||||
//*************************************************************************
|
||||
class byte
|
||||
{
|
||||
public:
|
||||
|
||||
// Friend functions
|
||||
template <typename TInteger>
|
||||
friend
|
||||
typename etl::enable_if<etl::is_integral<TInteger>::value, TInteger>::type
|
||||
to_integer(etl::byte b);
|
||||
|
||||
friend bool operator ==(etl::byte lhs, etl::byte rhs);
|
||||
|
||||
// Default constructor
|
||||
byte()
|
||||
: value(0U)
|
||||
{
|
||||
}
|
||||
|
||||
// Construct from a value castable to unsigned char
|
||||
template <typename T>
|
||||
explicit byte(T v)
|
||||
: value(static_cast<unsigned char>(v))
|
||||
{
|
||||
}
|
||||
|
||||
// Cast to a T
|
||||
template <typename T>
|
||||
operator T() const
|
||||
{
|
||||
return static_cast<T>(value);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
// The byte value
|
||||
unsigned char value;
|
||||
};
|
||||
|
||||
//*************************************************************************
|
||||
/// Equality test
|
||||
//*************************************************************************
|
||||
inline bool operator ==(etl::byte lhs, etl::byte rhs)
|
||||
{
|
||||
return (lhs.value == rhs.value);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Inequality test
|
||||
//*************************************************************************
|
||||
inline bool operator !=(etl::byte lhs, etl::byte rhs)
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// To integer.
|
||||
//*************************************************************************
|
||||
template <typename TInteger>
|
||||
typename etl::enable_if<etl::is_integral<TInteger>::value, TInteger>::type
|
||||
to_integer(etl::byte b)
|
||||
{
|
||||
return TInteger(b);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Shift left.
|
||||
//*************************************************************************
|
||||
template <typename TInteger>
|
||||
typename etl::enable_if<etl::is_integral<TInteger>::value, etl::byte>::type
|
||||
operator <<(etl::byte b, TInteger shift)
|
||||
{
|
||||
return etl::byte(to_integer<unsigned int>(b) << shift);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Shift right
|
||||
//*************************************************************************
|
||||
template <typename TInteger>
|
||||
typename etl::enable_if<etl::is_integral<TInteger>::value, etl::byte>::type
|
||||
operator >>(etl::byte b, TInteger shift)
|
||||
{
|
||||
return etl::byte(to_integer<unsigned int>(b) >> shift);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Shift left equals.
|
||||
//*************************************************************************
|
||||
template <typename TInteger>
|
||||
typename etl::enable_if<etl::is_integral<TInteger>::value, etl::byte&>::type
|
||||
operator <<=(etl::byte& b, TInteger shift)
|
||||
{
|
||||
b = b << shift;
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Shift right equals.
|
||||
//*************************************************************************
|
||||
template <typename TInteger>
|
||||
typename etl::enable_if<etl::is_integral<TInteger>::value, etl::byte&>::type
|
||||
operator >>=(etl::byte& b, TInteger shift)
|
||||
{
|
||||
b = b >> shift;
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Or.
|
||||
//*************************************************************************
|
||||
inline etl::byte operator |(etl::byte lhs, etl::byte rhs)
|
||||
{
|
||||
return etl::byte(to_integer<unsigned int>(lhs) | to_integer<unsigned int>(rhs));
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// And.
|
||||
//*************************************************************************
|
||||
inline etl::byte operator &(etl::byte lhs, etl::byte rhs)
|
||||
{
|
||||
return etl::byte(to_integer<unsigned int>(lhs) & to_integer<unsigned int>(rhs));
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Exclusive Or.
|
||||
//*************************************************************************
|
||||
inline etl::byte operator ^(etl::byte lhs, etl::byte rhs)
|
||||
{
|
||||
return etl::byte(to_integer<unsigned int>(lhs) ^ to_integer<unsigned int>(rhs));
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Or equals.
|
||||
//*************************************************************************
|
||||
inline etl::byte& operator |=(etl::byte& lhs, etl::byte rhs)
|
||||
{
|
||||
return lhs = lhs | rhs;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// And equals
|
||||
//*************************************************************************
|
||||
inline etl::byte& operator &=(etl::byte& lhs, etl::byte rhs)
|
||||
{
|
||||
return lhs = lhs & rhs;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Exclusive or equals.
|
||||
//*************************************************************************
|
||||
inline etl::byte& operator ^=(etl::byte& lhs, etl::byte rhs)
|
||||
{
|
||||
return lhs = lhs ^ rhs;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Not.
|
||||
//*************************************************************************
|
||||
inline etl::byte operator ~(etl::byte b)
|
||||
{
|
||||
return etl::byte(~to_integer<unsigned char>(b));
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
903
Chapter18/cib/libs/etl/include/etl/byte_stream.h
Normal file
903
Chapter18/cib/libs/etl/include/etl/byte_stream.h
Normal file
@@ -0,0 +1,903 @@
|
||||
///\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_BYTE_STREAM_INCLUDED
|
||||
#define ETL_BYTE_STREAM_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "type_traits.h"
|
||||
#include "nullptr.h"
|
||||
#include "endianness.h"
|
||||
#include "integral_limits.h"
|
||||
#include "algorithm.h"
|
||||
#include "iterator.h"
|
||||
#include "memory.h"
|
||||
#include "span.h"
|
||||
#include "iterator.h"
|
||||
#include "optional.h"
|
||||
#include "delegate.h"
|
||||
#include "exception.h"
|
||||
#include "error_handler.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <limits.h>
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
/// Encodes a byte stream.
|
||||
//***************************************************************************
|
||||
class byte_stream_writer
|
||||
{
|
||||
public:
|
||||
|
||||
typedef char* iterator;
|
||||
typedef const char* const_iterator;
|
||||
typedef etl::span<char> callback_parameter_type;
|
||||
typedef etl::delegate<void(callback_parameter_type)> callback_type;
|
||||
|
||||
//***************************************************************************
|
||||
/// Construct from span.
|
||||
//***************************************************************************
|
||||
byte_stream_writer(etl::span<char> span_, etl::endian stream_endianness_, callback_type callback_ = callback_type())
|
||||
: pdata(span_.begin())
|
||||
, pcurrent(span_.begin())
|
||||
, stream_length(span_.size_bytes())
|
||||
, stream_endianness(stream_endianness_)
|
||||
, callback(callback_)
|
||||
{
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Construct from span.
|
||||
//***************************************************************************
|
||||
byte_stream_writer(etl::span<unsigned char> span_, etl::endian stream_endianness_, callback_type callback_ = callback_type())
|
||||
: pdata(reinterpret_cast<char*>(span_.begin()))
|
||||
, pcurrent(reinterpret_cast<char*>(span_.begin()))
|
||||
, stream_length(span_.size_bytes())
|
||||
, stream_endianness(stream_endianness_)
|
||||
, callback(callback_)
|
||||
{
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Construct from range.
|
||||
//***************************************************************************
|
||||
byte_stream_writer(void* begin_, void* end_, etl::endian stream_endianness_, callback_type callback_ = callback_type())
|
||||
: pdata(reinterpret_cast<char*>(begin_))
|
||||
, pcurrent(reinterpret_cast<char*>(begin_))
|
||||
, stream_length(etl::distance(reinterpret_cast<char*>(begin_), reinterpret_cast<char*>(end_)))
|
||||
, stream_endianness(stream_endianness_)
|
||||
, callback(callback_)
|
||||
{
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Construct from begin and length.
|
||||
//***************************************************************************
|
||||
byte_stream_writer(void* begin_, size_t length_, etl::endian stream_endianness_, callback_type callback_ = callback_type())
|
||||
: pdata(reinterpret_cast<char*>(begin_))
|
||||
, pcurrent(reinterpret_cast<char*>(begin_))
|
||||
, stream_length(length_)
|
||||
, stream_endianness(stream_endianness_)
|
||||
, callback(callback_)
|
||||
{
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Construct from array.
|
||||
//***************************************************************************
|
||||
template <typename T, size_t Size>
|
||||
byte_stream_writer(T(&begin_)[Size], etl::endian stream_endianness_, callback_type callback_ = callback_type())
|
||||
: pdata(begin_)
|
||||
, pcurrent(begin_)
|
||||
, stream_length(begin_ + (Size * sizeof(T)))
|
||||
, stream_endianness(stream_endianness_)
|
||||
, callback(callback_)
|
||||
{
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Writes a boolean to the stream
|
||||
//***************************************************************************
|
||||
void write_unchecked(bool value)
|
||||
{
|
||||
*pcurrent++ = value ? 1 : 0;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Writes a boolean to the stream
|
||||
//***************************************************************************
|
||||
bool write(bool value)
|
||||
{
|
||||
bool success = (available<bool>() > 0U);
|
||||
|
||||
if (success)
|
||||
{
|
||||
write_unchecked(value);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Write a value to the stream.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
typename etl::enable_if<etl::is_integral<T>::value || etl::is_floating_point<T>::value, void>::type
|
||||
write_unchecked(T value)
|
||||
{
|
||||
to_bytes<T>(value);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Write a value to the stream.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
typename etl::enable_if<etl::is_integral<T>::value || etl::is_floating_point<T>::value, bool>::type
|
||||
write(T value)
|
||||
{
|
||||
bool success = (available<T>() > 0U);
|
||||
|
||||
if (success)
|
||||
{
|
||||
write_unchecked(value);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Write a range of T to the stream.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
typename etl::enable_if<etl::is_integral<T>::value || etl::is_floating_point<T>::value, void>::type
|
||||
write_unchecked(const etl::span<T>& range)
|
||||
{
|
||||
typename etl::span<T>::iterator itr = range.begin();
|
||||
|
||||
while (itr != range.end())
|
||||
{
|
||||
to_bytes(*itr);
|
||||
++itr;
|
||||
}
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Write a range of T to the stream.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
typename etl::enable_if<etl::is_integral<T>::value || etl::is_floating_point<T>::value, bool>::type
|
||||
write(const etl::span<T>& range)
|
||||
{
|
||||
bool success = (available<T>() >= range.size());
|
||||
|
||||
if (success)
|
||||
{
|
||||
write_unchecked(range);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Write a range of T to the stream.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
typename etl::enable_if<etl::is_integral<T>::value || etl::is_floating_point<T>::value, void>::type
|
||||
write_unchecked(const T* start, size_t length)
|
||||
{
|
||||
while (length-- != 0U)
|
||||
{
|
||||
to_bytes(*start);
|
||||
++start;
|
||||
}
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Write a range of T to the stream.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
typename etl::enable_if<etl::is_integral<T>::value || etl::is_floating_point<T>::value, bool>::type
|
||||
write(const T* start, size_t length)
|
||||
{
|
||||
bool success = (available<T>() >= length);
|
||||
|
||||
if (success)
|
||||
{
|
||||
write_unchecked(start, length);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Skip n items of T, if the total space is available.
|
||||
/// Returns <b>true</b> if the skip was possible.
|
||||
/// Returns <b>false</b> if the skip size was not possible.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
bool skip(size_t n)
|
||||
{
|
||||
bool success = (available<T>() >= n);
|
||||
|
||||
if (success)
|
||||
{
|
||||
step(n * sizeof(T));
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Sets the index back to the position in the stream. Default = 0.
|
||||
//***************************************************************************
|
||||
void restart(size_t n = 0U)
|
||||
{
|
||||
pcurrent = pdata + n;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Returns start of the stream.
|
||||
//***************************************************************************
|
||||
iterator begin()
|
||||
{
|
||||
return pdata;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Returns start of the stream.
|
||||
//***************************************************************************
|
||||
const_iterator begin() const
|
||||
{
|
||||
return pdata;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Returns end of the stream.
|
||||
//***************************************************************************
|
||||
iterator end()
|
||||
{
|
||||
return pcurrent;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Returns end of the stream.
|
||||
//***************************************************************************
|
||||
const_iterator end() const
|
||||
{
|
||||
return pcurrent;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Returns start of the stream.
|
||||
//***************************************************************************
|
||||
const_iterator cbegin() const
|
||||
{
|
||||
return pdata;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Returns end of the stream.
|
||||
//***************************************************************************
|
||||
const_iterator cend() const
|
||||
{
|
||||
return pcurrent;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Returns a span of the used portion of the stream.
|
||||
//***************************************************************************
|
||||
etl::span<char> used_data()
|
||||
{
|
||||
return etl::span<char>(pdata, pcurrent);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Returns a span of the used portion of the stream.
|
||||
//***************************************************************************
|
||||
etl::span<const char> used_data() const
|
||||
{
|
||||
return etl::span<const char>(pdata, pcurrent);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Returns a span of the free portion of the stream.
|
||||
//***************************************************************************
|
||||
etl::span<char> free_data()
|
||||
{
|
||||
return etl::span<char>(pcurrent, pdata + stream_length);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Returns a span of the free portion of the stream.
|
||||
//***************************************************************************
|
||||
etl::span<const char> free_data() const
|
||||
{
|
||||
return etl::span<const char>(pcurrent, pdata + stream_length);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Returns a span of whole the stream.
|
||||
//***************************************************************************
|
||||
etl::span<char> data()
|
||||
{
|
||||
return etl::span<char>(pdata, pdata + stream_length);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Returns a span of whole the stream.
|
||||
//***************************************************************************
|
||||
etl::span<const char> data() const
|
||||
{
|
||||
return etl::span<const char>(pdata, pdata + stream_length);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Returns <b>true</b> if the byte stream index has reached the end.
|
||||
//***************************************************************************
|
||||
bool full() const
|
||||
{
|
||||
return size_bytes() == capacity();
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Returns <b>true</b> if the byte stream is empty.
|
||||
//***************************************************************************
|
||||
bool empty() const
|
||||
{
|
||||
return size_bytes() == 0U;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Returns the number of bytes used in the stream.
|
||||
//***************************************************************************
|
||||
size_t size_bytes() const
|
||||
{
|
||||
return etl::distance(pdata, pcurrent);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Returns the maximum number of bytes in the stream.
|
||||
//***************************************************************************
|
||||
size_t capacity() const
|
||||
{
|
||||
return stream_length;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// The number of T left in the stream.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
size_t available() const
|
||||
{
|
||||
return (capacity() - size_bytes()) / sizeof(T);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// The number of bytes left in the stream.
|
||||
//***************************************************************************
|
||||
size_t available_bytes() const
|
||||
{
|
||||
return available<char>();
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Sets the function to call after every write.
|
||||
//***************************************************************************
|
||||
void set_callback(callback_type callback_)
|
||||
{
|
||||
callback = callback_;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Gets the function to call after every write.
|
||||
//***************************************************************************
|
||||
callback_type get_callback() const
|
||||
{
|
||||
return callback;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Gets the endianness of the stream.
|
||||
//***************************************************************************
|
||||
etl::endian get_endianness() const
|
||||
{
|
||||
return stream_endianness;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
//***************************************************************************
|
||||
/// to_bytes
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
typename etl::enable_if<sizeof(T) == 1U, void>::type
|
||||
to_bytes(const T value)
|
||||
{
|
||||
*pcurrent = static_cast<char>(value);
|
||||
step(1U);
|
||||
}
|
||||
|
||||
//*********************************
|
||||
template <typename T>
|
||||
typename etl::enable_if<sizeof(T) != 1U, void>::type
|
||||
to_bytes(const T value)
|
||||
{
|
||||
const char* pv = reinterpret_cast<const char*>(&value);
|
||||
copy_value(pv, pcurrent, sizeof(T));
|
||||
step(sizeof(T));
|
||||
}
|
||||
|
||||
//*********************************
|
||||
void step(size_t n)
|
||||
{
|
||||
callback.call_if(etl::span<char>(pcurrent, pcurrent + n));
|
||||
|
||||
pcurrent += n;
|
||||
}
|
||||
|
||||
//*********************************
|
||||
void copy_value(const char* source, char* destination, size_t length) const
|
||||
{
|
||||
const etl::endian platform_endianness = etl::endianness::value();
|
||||
|
||||
if (stream_endianness == platform_endianness)
|
||||
{
|
||||
etl::copy(source, source + length, destination);
|
||||
}
|
||||
else
|
||||
{
|
||||
etl::reverse_copy(source, source + length, destination);
|
||||
}
|
||||
}
|
||||
|
||||
char* const pdata; ///< The start of the byte stream buffer.
|
||||
char* pcurrent; ///< The current position in the byte stream buffer.
|
||||
const size_t stream_length; ///< The length of the byte stream buffer.
|
||||
const etl::endian stream_endianness; ///< The endianness of the stream data.
|
||||
callback_type callback; ///< An optional callback on every step on the write buffer.
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Decodes byte streams.
|
||||
/// Data must be stored in the stream in network order.
|
||||
//***************************************************************************
|
||||
class byte_stream_reader
|
||||
{
|
||||
public:
|
||||
|
||||
typedef char* iterator;
|
||||
typedef const char* const_iterator;
|
||||
|
||||
//***************************************************************************
|
||||
/// Construct from span.
|
||||
//***************************************************************************
|
||||
byte_stream_reader(etl::span<char> span_, etl::endian stream_endianness_)
|
||||
: pdata(span_.begin())
|
||||
, pcurrent(span_.begin())
|
||||
, stream_length(span_.size_bytes())
|
||||
, stream_endianness(stream_endianness_)
|
||||
{
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Construct from span.
|
||||
//***************************************************************************
|
||||
byte_stream_reader(etl::span<const char> span_, etl::endian stream_endianness_)
|
||||
: pdata(span_.begin())
|
||||
, pcurrent(span_.begin())
|
||||
, stream_length(span_.size_bytes())
|
||||
, stream_endianness(stream_endianness_)
|
||||
{
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Construct from range.
|
||||
//***************************************************************************
|
||||
byte_stream_reader(const void* begin_, const void* end_, etl::endian stream_endianness_)
|
||||
: pdata(reinterpret_cast<const char*>(begin_))
|
||||
, pcurrent(reinterpret_cast<const char*>(begin_))
|
||||
, stream_length(etl::distance(reinterpret_cast<const char*>(begin_), reinterpret_cast<const char*>(end_)))
|
||||
, stream_endianness(stream_endianness_)
|
||||
{
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Construct from begin and length.
|
||||
//***************************************************************************
|
||||
byte_stream_reader(const void* begin_, size_t length_, etl::endian stream_endianness_)
|
||||
: pdata(reinterpret_cast<const char*>(begin_))
|
||||
, pcurrent(reinterpret_cast<const char*>(begin_))
|
||||
, stream_length(length_)
|
||||
, stream_endianness(stream_endianness_)
|
||||
{
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Construct from array.
|
||||
//***************************************************************************
|
||||
template <typename T, size_t Size>
|
||||
byte_stream_reader(T(&begin_)[Size], etl::endian stream_endianness_)
|
||||
: pdata(begin_)
|
||||
, pcurrent(begin_)
|
||||
, stream_length(begin_ + (Size * sizeof(T)))
|
||||
, stream_endianness(stream_endianness_)
|
||||
{
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Construct from const array.
|
||||
//***************************************************************************
|
||||
template <typename T, size_t Size>
|
||||
byte_stream_reader(const T(&begin_)[Size], etl::endian stream_endianness_)
|
||||
: pdata(begin_)
|
||||
, pcurrent(begin_)
|
||||
, stream_length(begin_ + (Size * sizeof(T)))
|
||||
, stream_endianness(stream_endianness_)
|
||||
{
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Read a value from the stream.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
typename etl::enable_if<etl::is_integral<T>::value || etl::is_floating_point<T>::value, T>::type
|
||||
read_unchecked()
|
||||
{
|
||||
return from_bytes<T>();
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Read a value from the stream.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
typename etl::enable_if<etl::is_integral<T>::value || etl::is_floating_point<T>::value, etl::optional<T> >::type
|
||||
read()
|
||||
{
|
||||
etl::optional<T> result;
|
||||
|
||||
// Do we have enough room?
|
||||
if (available<T>() > 0U)
|
||||
{
|
||||
result = read_unchecked<T>();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Read a byte range from the stream.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
typename etl::enable_if<sizeof(T) == 1U, etl::span<const T> >::type
|
||||
read_unchecked(size_t n)
|
||||
{
|
||||
etl::span<const T> result;
|
||||
|
||||
const char* pend = pcurrent + (n * sizeof(T));
|
||||
|
||||
result = etl::span<const T>(reinterpret_cast<const T*>(pcurrent), reinterpret_cast<const T*>(pend));
|
||||
pcurrent = pend;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Read a byte range from the stream.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
typename etl::enable_if<sizeof(T) == 1U, etl::optional<etl::span<const T> > >::type
|
||||
read(size_t n)
|
||||
{
|
||||
etl::optional<etl::span<const T> > result;
|
||||
|
||||
// Do we have enough room?
|
||||
if (available<T>() >= n)
|
||||
{
|
||||
result = read_unchecked<T>(n);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Read a range of T from the stream.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
typename etl::enable_if<etl::is_integral<T>::value || etl::is_floating_point<T>::value, etl::span<const T> >::type
|
||||
read_unchecked(etl::span<T> range)
|
||||
{
|
||||
typename etl::span<T>::iterator destination = range.begin();
|
||||
|
||||
while (destination != range.end())
|
||||
{
|
||||
*destination++ = from_bytes<T>();
|
||||
}
|
||||
|
||||
return etl::span<const T>(range.begin(), range.end());
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Read a range of T from the stream.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
typename etl::enable_if<etl::is_integral<T>::value || etl::is_floating_point<T>::value, etl::optional<etl::span<const T> > >::type
|
||||
read(etl::span<T> range)
|
||||
{
|
||||
// Do we have enough room?
|
||||
if (available<T>() >= range.size())
|
||||
{
|
||||
return etl::optional<etl::span<const T> >(read_unchecked<T>(range));
|
||||
}
|
||||
|
||||
return etl::optional<etl::span<const T> >();
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Read a range of T from the stream.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
typename etl::enable_if<etl::is_integral<T>::value || etl::is_floating_point<T>::value, etl::span<const T> >::type
|
||||
read_unchecked(T* start, size_t length)
|
||||
{
|
||||
T* destination = start;
|
||||
|
||||
for (size_t i = 0; i < length; ++i)
|
||||
{
|
||||
*destination++ = from_bytes<T>();
|
||||
}
|
||||
|
||||
return etl::span<const T>(start, length);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Read a range of T from the stream.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
typename etl::enable_if<etl::is_integral<T>::value || etl::is_floating_point<T>::value, etl::optional<etl::span<const T> > >::type
|
||||
read(T* start, size_t length)
|
||||
{
|
||||
// Do we have enough room?
|
||||
if (available<T>() >= length)
|
||||
{
|
||||
return etl::optional<etl::span<const T> >(read_unchecked<T>(start, length));
|
||||
}
|
||||
|
||||
return etl::optional<etl::span<const T> >();
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Skip n items of T, up to the maximum space available.
|
||||
/// Returns <b>true</b> if the skip was possible.
|
||||
/// Returns <b>false</b> if the full skip size was not possible.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
bool skip(size_t n)
|
||||
{
|
||||
if (n <= available<T>())
|
||||
{
|
||||
pcurrent += (n * sizeof(T));
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Sets the index back to the position in the stream. Default = 0.
|
||||
//***************************************************************************
|
||||
void restart(size_t n = 0U)
|
||||
{
|
||||
pcurrent = pdata + n;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Returns start of the stream.
|
||||
//***************************************************************************
|
||||
const_iterator begin() const
|
||||
{
|
||||
return pdata;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Returns end of the stream.
|
||||
//***************************************************************************
|
||||
const_iterator end() const
|
||||
{
|
||||
return pcurrent;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Returns start of the stream.
|
||||
//***************************************************************************
|
||||
const_iterator cbegin() const
|
||||
{
|
||||
return pdata;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Returns end of the stream.
|
||||
//***************************************************************************
|
||||
const_iterator cend() const
|
||||
{
|
||||
return pcurrent;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Returns a span of the used portion of the stream.
|
||||
//***************************************************************************
|
||||
etl::span<const char> used_data() const
|
||||
{
|
||||
return etl::span<const char>(pdata, pcurrent);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Returns a span of the free portion of the stream.
|
||||
//***************************************************************************
|
||||
etl::span<const char> free_data() const
|
||||
{
|
||||
return etl::span<const char>(pcurrent, pdata + stream_length);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Returns a span of whole the stream.
|
||||
//***************************************************************************
|
||||
etl::span<const char> data() const
|
||||
{
|
||||
return etl::span<const char>(pdata, pdata + stream_length);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Returns <b>true</b> if the byte stream is empty.
|
||||
//***************************************************************************
|
||||
bool empty() const
|
||||
{
|
||||
return available<char>() == 0U;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Returns the number of bytes used in the stream.
|
||||
//***************************************************************************
|
||||
size_t size_bytes() const
|
||||
{
|
||||
return stream_length;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// The number of T left in the stream.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
size_t available() const
|
||||
{
|
||||
size_t used = etl::distance(pdata, pcurrent);
|
||||
|
||||
return (stream_length - used) / sizeof(T);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// The number of bytes left in the stream.
|
||||
//***************************************************************************
|
||||
size_t available_bytes() const
|
||||
{
|
||||
return available<char>();
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
//***************************************************************************
|
||||
/// from_bytes
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
typename etl::enable_if<sizeof(T) == 1U, T>::type
|
||||
from_bytes()
|
||||
{
|
||||
return static_cast<T>(*pcurrent++);
|
||||
}
|
||||
|
||||
//*********************************
|
||||
template <typename T>
|
||||
typename etl::enable_if<sizeof(T) != 1U, T>::type
|
||||
from_bytes()
|
||||
{
|
||||
T value;
|
||||
|
||||
char* pv = reinterpret_cast<char*>(&value);
|
||||
copy_value(pcurrent, pv, sizeof(T));
|
||||
pcurrent += sizeof(T);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
//*********************************
|
||||
void copy_value(const char* source, char* destination, size_t length) const
|
||||
{
|
||||
const etl::endian platform_endianness = etl::endianness::value();
|
||||
|
||||
if (stream_endianness == platform_endianness)
|
||||
{
|
||||
etl::copy(source, source + length, destination);
|
||||
}
|
||||
else
|
||||
{
|
||||
etl::reverse_copy(source, source + length, destination);
|
||||
}
|
||||
}
|
||||
|
||||
const char* const pdata; ///< The start of the byte stream buffer.
|
||||
const char* pcurrent; ///< The current position in the byte stream buffer.
|
||||
const size_t stream_length; ///< The length of the byte stream buffer.
|
||||
const etl::endian stream_endianness; ///< The endianness of the stream data.
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Default implementation of the write function.
|
||||
/// For integral and floating point types only.
|
||||
/// Overload this to support custom types.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
void write_unchecked(etl::byte_stream_writer& stream, const T& value)
|
||||
{
|
||||
stream.write_unchecked(value);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Implementation of the write function.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
bool write(etl::byte_stream_writer& stream, const T& value)
|
||||
{
|
||||
return stream.write(value);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Default implementation of the read function.
|
||||
/// For integral and floating point types only.
|
||||
/// Overload this to support custom types.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
T read_unchecked(etl::byte_stream_reader& stream)
|
||||
{
|
||||
return stream.read_unchecked<T>();
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Implementation of the read function.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
etl::optional<T> read(etl::byte_stream_reader& stream)
|
||||
{
|
||||
return stream.read<T>();
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
81
Chapter18/cib/libs/etl/include/etl/callback.h
Normal file
81
Chapter18/cib/libs/etl/include/etl/callback.h
Normal file
@@ -0,0 +1,81 @@
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
http://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2015 jwellbelove
|
||||
|
||||
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__
|
||||
#define __ETL_CALLBACK__
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
/// A callback class designed to be multiply inherited by other client classes.
|
||||
/// The class is parametrised with a callback parameter type and a unique id.
|
||||
/// The unique id allows multiple callbacks with the same parameter type.
|
||||
///\tparam TParameter The callback parameter type.
|
||||
///\tparam ID The unique id for this callback.
|
||||
//***************************************************************************
|
||||
template <typename TParameter, const int ID>
|
||||
class callback
|
||||
{
|
||||
private:
|
||||
|
||||
// Creates a parameter type unique to this ID.
|
||||
template <typename T, const int I>
|
||||
struct parameter
|
||||
{
|
||||
parameter(T value_)
|
||||
: value(value_)
|
||||
{
|
||||
}
|
||||
|
||||
typedef T value_type;
|
||||
|
||||
T value;
|
||||
|
||||
private:
|
||||
|
||||
parameter();
|
||||
};
|
||||
|
||||
// Specialisation for void.
|
||||
template <const int I>
|
||||
struct parameter<void, I>
|
||||
{
|
||||
typedef void value_type;
|
||||
};
|
||||
|
||||
public:
|
||||
|
||||
typedef parameter<TParameter, ID> type;
|
||||
|
||||
virtual void etl_callback(type p = type()) = 0;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
159
Chapter18/cib/libs/etl/include/etl/callback_service.h
Normal file
159
Chapter18/cib/libs/etl/include/etl/callback_service.h
Normal file
@@ -0,0 +1,159 @@
|
||||
///\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 "nullptr.h"
|
||||
#include "static_assert.h"
|
||||
#include "function.h"
|
||||
#include "array.h"
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
/// An indexed callback service.
|
||||
/// \tparam RANGE The number of callbacks to handle.
|
||||
/// \tparam OFFSET The lowest callback id value.
|
||||
/// The callback ids must range between OFFSET and OFFSET + RANGE - 1.
|
||||
//***************************************************************************
|
||||
template <size_t RANGE, size_t OFFSET = 0U>
|
||||
class callback_service
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Reset the callback service.
|
||||
/// Sets all callbacks to the internal default.
|
||||
//*************************************************************************
|
||||
callback_service()
|
||||
: unhandled_callback(*this),
|
||||
p_unhandled(ETL_NULLPTR)
|
||||
{
|
||||
lookup.fill(&unhandled_callback);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Registers a callback for the specified id.
|
||||
/// Compile time assert if the id is out of range.
|
||||
/// \tparam ID The id of the callback.
|
||||
/// \param callback Reference to the callback.
|
||||
//*************************************************************************
|
||||
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;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Registers a callback for the specified id.
|
||||
/// No action if the id is out of range.
|
||||
/// \param id Id of the callback.
|
||||
/// \param callback Reference to the callback.
|
||||
//*************************************************************************
|
||||
void register_callback(size_t id, etl::ifunction<size_t>& callback)
|
||||
{
|
||||
if ((id >= OFFSET) && (id < (OFFSET + RANGE)))
|
||||
{
|
||||
lookup[id - OFFSET] = &callback;
|
||||
}
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Registers an alternative callback for unhandled ids.
|
||||
/// \param callback A reference to the user supplied 'unhandled' callback.
|
||||
//*************************************************************************
|
||||
void register_unhandled_callback(etl::ifunction<size_t>& callback)
|
||||
{
|
||||
p_unhandled = &callback;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Executes the callback function for the index.
|
||||
/// Compile time assert if the id is out of range.
|
||||
/// \tparam ID The id of the 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);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Executes the callback function for the index.
|
||||
/// \param id Id of the callback.
|
||||
//*************************************************************************
|
||||
void callback(size_t id)
|
||||
{
|
||||
if ((id >= OFFSET) && (id < (OFFSET + RANGE)))
|
||||
{
|
||||
(*lookup[id - OFFSET])(id);
|
||||
}
|
||||
else
|
||||
{
|
||||
unhandled(id);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
//*************************************************************************
|
||||
/// The default callback function.
|
||||
/// Calls the user defined 'unhandled' callback if it exists.
|
||||
//*************************************************************************
|
||||
void unhandled(size_t id)
|
||||
{
|
||||
if (p_unhandled != ETL_NULLPTR)
|
||||
{
|
||||
(*p_unhandled)(id);
|
||||
}
|
||||
}
|
||||
|
||||
/// The default callback for unhandled ids.
|
||||
etl::function_mp<callback_service<RANGE, OFFSET>,
|
||||
size_t,
|
||||
&callback_service<RANGE, OFFSET>::unhandled> unhandled_callback;
|
||||
|
||||
/// Pointer to the user defined 'unhandled' callback.
|
||||
etl::ifunction<size_t>* p_unhandled;
|
||||
|
||||
/// Lookup table of callbacks.
|
||||
etl::array<etl::ifunction<size_t>*, RANGE> lookup;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
842
Chapter18/cib/libs/etl/include/etl/callback_timer.h
Normal file
842
Chapter18/cib/libs/etl/include/etl/callback_timer.h
Normal file
@@ -0,0 +1,842 @@
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2017 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_TIMER_INCLUDED
|
||||
#define ETL_CALLBACK_TIMER_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "algorithm.h"
|
||||
#include "nullptr.h"
|
||||
#include "function.h"
|
||||
#include "static_assert.h"
|
||||
#include "timer.h"
|
||||
#include "atomic.h"
|
||||
#include "error_handler.h"
|
||||
#include "placement_new.h"
|
||||
#include "delegate.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#if defined(ETL_IN_UNIT_TEST) && ETL_NOT_USING_STL
|
||||
#define ETL_DISABLE_TIMER_UPDATES
|
||||
#define ETL_ENABLE_TIMER_UPDATES
|
||||
#define ETL_TIMER_UPDATES_ENABLED true
|
||||
|
||||
#undef ETL_CALLBACK_TIMER_USE_ATOMIC_LOCK
|
||||
#undef ETL_CALLBACK_TIMER_USE_INTERRUPT_LOCK
|
||||
#else
|
||||
#if !defined(ETL_CALLBACK_TIMER_USE_ATOMIC_LOCK) && !defined(ETL_CALLBACK_TIMER_USE_INTERRUPT_LOCK)
|
||||
#error ETL_CALLBACK_TIMER_USE_ATOMIC_LOCK or ETL_CALLBACK_TIMER_USE_INTERRUPT_LOCK not defined
|
||||
#endif
|
||||
|
||||
#if defined(ETL_CALLBACK_TIMER_USE_ATOMIC_LOCK) && defined(ETL_CALLBACK_TIMER_USE_INTERRUPT_LOCK)
|
||||
#error Only define one of ETL_CALLBACK_TIMER_USE_ATOMIC_LOCK or ETL_CALLBACK_TIMER_USE_INTERRUPT_LOCK
|
||||
#endif
|
||||
|
||||
#if defined(ETL_CALLBACK_TIMER_USE_ATOMIC_LOCK)
|
||||
#define ETL_DISABLE_TIMER_UPDATES (++process_semaphore)
|
||||
#define ETL_ENABLE_TIMER_UPDATES (--process_semaphore)
|
||||
#define ETL_TIMER_UPDATES_ENABLED (process_semaphore.load() == 0)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(ETL_CALLBACK_TIMER_USE_INTERRUPT_LOCK)
|
||||
#if !defined(ETL_CALLBACK_TIMER_DISABLE_INTERRUPTS) || !defined(ETL_CALLBACK_TIMER_ENABLE_INTERRUPTS)
|
||||
#error ETL_CALLBACK_TIMER_DISABLE_INTERRUPTS and/or ETL_CALLBACK_TIMER_ENABLE_INTERRUPTS not defined
|
||||
#endif
|
||||
|
||||
#define ETL_DISABLE_TIMER_UPDATES ETL_CALLBACK_TIMER_DISABLE_INTERRUPTS
|
||||
#define ETL_ENABLE_TIMER_UPDATES ETL_CALLBACK_TIMER_ENABLE_INTERRUPTS
|
||||
#define ETL_TIMER_UPDATES_ENABLED true
|
||||
#endif
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//*************************************************************************
|
||||
/// The configuration of a timer.
|
||||
struct callback_timer_data
|
||||
{
|
||||
typedef etl::delegate<void(void)> callback_type;
|
||||
|
||||
enum callback_type_id
|
||||
{
|
||||
C_CALLBACK,
|
||||
IFUNCTION,
|
||||
DELEGATE
|
||||
};
|
||||
|
||||
//*******************************************
|
||||
callback_timer_data()
|
||||
: p_callback(ETL_NULLPTR),
|
||||
period(0),
|
||||
delta(etl::timer::state::Inactive),
|
||||
id(etl::timer::id::NO_TIMER),
|
||||
previous(etl::timer::id::NO_TIMER),
|
||||
next(etl::timer::id::NO_TIMER),
|
||||
repeating(true),
|
||||
cbk_type(IFUNCTION)
|
||||
{
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// C function callback
|
||||
//*******************************************
|
||||
callback_timer_data(etl::timer::id::type id_,
|
||||
void (*p_callback_)(),
|
||||
uint32_t period_,
|
||||
bool repeating_)
|
||||
: p_callback(reinterpret_cast<void*>(p_callback_)),
|
||||
period(period_),
|
||||
delta(etl::timer::state::Inactive),
|
||||
id(id_),
|
||||
previous(etl::timer::id::NO_TIMER),
|
||||
next(etl::timer::id::NO_TIMER),
|
||||
repeating(repeating_),
|
||||
cbk_type(C_CALLBACK)
|
||||
{
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// ETL function callback
|
||||
//*******************************************
|
||||
callback_timer_data(etl::timer::id::type id_,
|
||||
etl::ifunction<void>& callback_,
|
||||
uint32_t period_,
|
||||
bool repeating_)
|
||||
: p_callback(reinterpret_cast<void*>(&callback_)),
|
||||
period(period_),
|
||||
delta(etl::timer::state::Inactive),
|
||||
id(id_),
|
||||
previous(etl::timer::id::NO_TIMER),
|
||||
next(etl::timer::id::NO_TIMER),
|
||||
repeating(repeating_),
|
||||
cbk_type(IFUNCTION)
|
||||
{
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// ETL delegate callback
|
||||
//*******************************************
|
||||
callback_timer_data(etl::timer::id::type id_,
|
||||
callback_type& callback_,
|
||||
uint32_t period_,
|
||||
bool repeating_)
|
||||
: p_callback(reinterpret_cast<void*>(&callback_)),
|
||||
period(period_),
|
||||
delta(etl::timer::state::Inactive),
|
||||
id(id_),
|
||||
previous(etl::timer::id::NO_TIMER),
|
||||
next(etl::timer::id::NO_TIMER),
|
||||
repeating(repeating_),
|
||||
cbk_type(DELEGATE)
|
||||
{
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Returns true if the timer is active.
|
||||
//*******************************************
|
||||
bool is_active() const
|
||||
{
|
||||
return delta != etl::timer::state::Inactive;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Sets the timer to the inactive state.
|
||||
//*******************************************
|
||||
void set_inactive()
|
||||
{
|
||||
delta = etl::timer::state::Inactive;
|
||||
}
|
||||
|
||||
void* p_callback;
|
||||
uint32_t period;
|
||||
uint32_t delta;
|
||||
etl::timer::id::type id;
|
||||
uint_least8_t previous;
|
||||
uint_least8_t next;
|
||||
bool repeating;
|
||||
callback_type_id cbk_type;
|
||||
|
||||
private:
|
||||
|
||||
// Disabled.
|
||||
callback_timer_data(const callback_timer_data& other);
|
||||
callback_timer_data& operator =(const callback_timer_data& other);
|
||||
};
|
||||
|
||||
namespace private_callback_timer
|
||||
{
|
||||
//*************************************************************************
|
||||
/// A specialised intrusive linked list for timer data.
|
||||
//*************************************************************************
|
||||
class list
|
||||
{
|
||||
public:
|
||||
|
||||
//*******************************
|
||||
list(etl::callback_timer_data* ptimers_)
|
||||
: head(etl::timer::id::NO_TIMER),
|
||||
tail(etl::timer::id::NO_TIMER),
|
||||
current(etl::timer::id::NO_TIMER),
|
||||
ptimers(ptimers_)
|
||||
{
|
||||
}
|
||||
|
||||
//*******************************
|
||||
bool empty() const
|
||||
{
|
||||
return head == etl::timer::id::NO_TIMER;
|
||||
}
|
||||
|
||||
//*******************************
|
||||
// Inserts the timer at the correct delta position
|
||||
//*******************************
|
||||
void insert(etl::timer::id::type id_)
|
||||
{
|
||||
etl::callback_timer_data& timer = ptimers[id_];
|
||||
|
||||
if (head == etl::timer::id::NO_TIMER)
|
||||
{
|
||||
// No entries yet.
|
||||
head = id_;
|
||||
tail = id_;
|
||||
timer.previous = etl::timer::id::NO_TIMER;
|
||||
timer.next = etl::timer::id::NO_TIMER;
|
||||
}
|
||||
else
|
||||
{
|
||||
// We already have entries.
|
||||
etl::timer::id::type test_id = begin();
|
||||
|
||||
while (test_id != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
etl::callback_timer_data& test = ptimers[test_id];
|
||||
|
||||
// Find the correct place to insert.
|
||||
if (timer.delta <= test.delta)
|
||||
{
|
||||
if (test.id == head)
|
||||
{
|
||||
head = timer.id;
|
||||
}
|
||||
|
||||
// Insert before test.
|
||||
timer.previous = test.previous;
|
||||
test.previous = timer.id;
|
||||
timer.next = test.id;
|
||||
|
||||
// Adjust the next delta to compensate.
|
||||
test.delta -= timer.delta;
|
||||
|
||||
if (timer.previous != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
ptimers[timer.previous].next = timer.id;
|
||||
}
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
timer.delta -= test.delta;
|
||||
}
|
||||
|
||||
test_id = next(test_id);
|
||||
}
|
||||
|
||||
// Reached the end?
|
||||
if (test_id == etl::timer::id::NO_TIMER)
|
||||
{
|
||||
// Tag on to the tail.
|
||||
ptimers[tail].next = timer.id;
|
||||
timer.previous = tail;
|
||||
timer.next = etl::timer::id::NO_TIMER;
|
||||
tail = timer.id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//*******************************
|
||||
void remove(etl::timer::id::type id_, bool has_expired)
|
||||
{
|
||||
etl::callback_timer_data& timer = ptimers[id_];
|
||||
|
||||
if (head == id_)
|
||||
{
|
||||
head = timer.next;
|
||||
}
|
||||
else
|
||||
{
|
||||
ptimers[timer.previous].next = timer.next;
|
||||
}
|
||||
|
||||
if (tail == id_)
|
||||
{
|
||||
tail = timer.previous;
|
||||
}
|
||||
else
|
||||
{
|
||||
ptimers[timer.next].previous = timer.previous;
|
||||
}
|
||||
|
||||
if (!has_expired)
|
||||
{
|
||||
// Adjust the next delta.
|
||||
if (timer.next != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
ptimers[timer.next].delta += timer.delta;
|
||||
}
|
||||
}
|
||||
|
||||
timer.previous = etl::timer::id::NO_TIMER;
|
||||
timer.next = etl::timer::id::NO_TIMER;
|
||||
timer.delta = etl::timer::state::Inactive;
|
||||
}
|
||||
|
||||
//*******************************
|
||||
etl::callback_timer_data& front()
|
||||
{
|
||||
return ptimers[head];
|
||||
}
|
||||
|
||||
//*******************************
|
||||
const etl::callback_timer_data& front() const
|
||||
{
|
||||
return ptimers[head];
|
||||
}
|
||||
|
||||
//*******************************
|
||||
etl::timer::id::type begin()
|
||||
{
|
||||
current = head;
|
||||
return current;
|
||||
}
|
||||
|
||||
//*******************************
|
||||
etl::timer::id::type previous(etl::timer::id::type last)
|
||||
{
|
||||
current = ptimers[last].previous;
|
||||
return current;
|
||||
}
|
||||
|
||||
//*******************************
|
||||
etl::timer::id::type next(etl::timer::id::type last)
|
||||
{
|
||||
current = ptimers[last].next;
|
||||
return current;
|
||||
}
|
||||
|
||||
//*******************************
|
||||
void clear()
|
||||
{
|
||||
etl::timer::id::type id = begin();
|
||||
|
||||
while (id != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
etl::callback_timer_data& timer = ptimers[id];
|
||||
id = next(id);
|
||||
timer.next = etl::timer::id::NO_TIMER;
|
||||
}
|
||||
|
||||
head = etl::timer::id::NO_TIMER;
|
||||
tail = etl::timer::id::NO_TIMER;
|
||||
current = etl::timer::id::NO_TIMER;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
etl::timer::id::type head;
|
||||
etl::timer::id::type tail;
|
||||
etl::timer::id::type current;
|
||||
|
||||
etl::callback_timer_data* const ptimers;
|
||||
};
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Interface for callback timer
|
||||
//***************************************************************************
|
||||
class icallback_timer
|
||||
{
|
||||
public:
|
||||
|
||||
typedef etl::delegate<void(void)> callback_type;
|
||||
|
||||
//*******************************************
|
||||
/// Register a timer.
|
||||
//*******************************************
|
||||
etl::timer::id::type register_timer(void (*p_callback_)(),
|
||||
uint32_t period_,
|
||||
bool repeating_)
|
||||
{
|
||||
etl::timer::id::type id = etl::timer::id::NO_TIMER;
|
||||
|
||||
bool is_space = (registered_timers < MAX_TIMERS);
|
||||
|
||||
if (is_space)
|
||||
{
|
||||
// Search for the free space.
|
||||
for (uint_least8_t i = 0U; i < MAX_TIMERS; ++i)
|
||||
{
|
||||
etl::callback_timer_data& timer = timer_array[i];
|
||||
|
||||
if (timer.id == etl::timer::id::NO_TIMER)
|
||||
{
|
||||
// Create in-place.
|
||||
new (&timer) callback_timer_data(i, p_callback_, period_, repeating_);
|
||||
++registered_timers;
|
||||
id = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Register a timer.
|
||||
//*******************************************
|
||||
etl::timer::id::type register_timer(etl::ifunction<void>& callback_,
|
||||
uint32_t period_,
|
||||
bool repeating_)
|
||||
{
|
||||
etl::timer::id::type id = etl::timer::id::NO_TIMER;
|
||||
|
||||
bool is_space = (registered_timers < MAX_TIMERS);
|
||||
|
||||
if (is_space)
|
||||
{
|
||||
// Search for the free space.
|
||||
for (uint_least8_t i = 0U; i < MAX_TIMERS; ++i)
|
||||
{
|
||||
etl::callback_timer_data& timer = timer_array[i];
|
||||
|
||||
if (timer.id == etl::timer::id::NO_TIMER)
|
||||
{
|
||||
// Create in-place.
|
||||
new (&timer) callback_timer_data(i, callback_, period_, repeating_);
|
||||
++registered_timers;
|
||||
id = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Register a timer.
|
||||
//*******************************************
|
||||
#if ETL_USING_CPP11
|
||||
etl::timer::id::type register_timer(callback_type& callback_,
|
||||
uint32_t period_,
|
||||
bool repeating_)
|
||||
{
|
||||
etl::timer::id::type id = etl::timer::id::NO_TIMER;
|
||||
|
||||
bool is_space = (registered_timers < MAX_TIMERS);
|
||||
|
||||
if (is_space)
|
||||
{
|
||||
// Search for the free space.
|
||||
for (uint_least8_t i = 0U; i < MAX_TIMERS; ++i)
|
||||
{
|
||||
etl::callback_timer_data& timer = timer_array[i];
|
||||
|
||||
if (timer.id == etl::timer::id::NO_TIMER)
|
||||
{
|
||||
// Create in-place.
|
||||
new (&timer) callback_timer_data(i, callback_, period_, repeating_);
|
||||
++registered_timers;
|
||||
id = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
#endif
|
||||
|
||||
//*******************************************
|
||||
/// Unregister a timer.
|
||||
//*******************************************
|
||||
bool unregister_timer(etl::timer::id::type id_)
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
if (id_ != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
etl::callback_timer_data& timer = timer_array[id_];
|
||||
|
||||
if (timer.id != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
if (timer.is_active())
|
||||
{
|
||||
ETL_DISABLE_TIMER_UPDATES;
|
||||
active_list.remove(timer.id, false);
|
||||
ETL_ENABLE_TIMER_UPDATES;
|
||||
}
|
||||
|
||||
// Reset in-place.
|
||||
new (&timer) callback_timer_data();
|
||||
--registered_timers;
|
||||
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Enable/disable the timer.
|
||||
//*******************************************
|
||||
void enable(bool state_)
|
||||
{
|
||||
enabled = state_;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Get the enable/disable state.
|
||||
//*******************************************
|
||||
bool is_running() const
|
||||
{
|
||||
return enabled;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Clears the timer of data.
|
||||
//*******************************************
|
||||
void clear()
|
||||
{
|
||||
ETL_DISABLE_TIMER_UPDATES;
|
||||
active_list.clear();
|
||||
ETL_ENABLE_TIMER_UPDATES;
|
||||
|
||||
for (int i = 0; i < MAX_TIMERS; ++i)
|
||||
{
|
||||
::new (&timer_array[i]) callback_timer_data();
|
||||
}
|
||||
|
||||
registered_timers = 0;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
// Called by the timer service to indicate the
|
||||
// amount of time that has elapsed since the last successful call to 'tick'.
|
||||
// Returns true if the tick was processed,
|
||||
// false if not.
|
||||
//*******************************************
|
||||
bool tick(uint32_t count)
|
||||
{
|
||||
if (enabled)
|
||||
{
|
||||
if (ETL_TIMER_UPDATES_ENABLED)
|
||||
{
|
||||
// We have something to do?
|
||||
bool has_active = !active_list.empty();
|
||||
|
||||
if (has_active)
|
||||
{
|
||||
while (has_active && (count >= active_list.front().delta))
|
||||
{
|
||||
etl::callback_timer_data& timer = active_list.front();
|
||||
|
||||
count -= timer.delta;
|
||||
|
||||
active_list.remove(timer.id, true);
|
||||
|
||||
if (timer.repeating)
|
||||
{
|
||||
// Reinsert the timer.
|
||||
timer.delta = timer.period;
|
||||
active_list.insert(timer.id);
|
||||
}
|
||||
|
||||
if (timer.p_callback != ETL_NULLPTR)
|
||||
{
|
||||
if (timer.cbk_type == callback_timer_data::C_CALLBACK)
|
||||
{
|
||||
// Call the C callback.
|
||||
reinterpret_cast<void(*)()>(timer.p_callback)();
|
||||
}
|
||||
else if(timer.cbk_type == callback_timer_data::IFUNCTION)
|
||||
{
|
||||
// Call the function wrapper callback.
|
||||
(*reinterpret_cast<etl::ifunction<void>*>(timer.p_callback))();
|
||||
}
|
||||
else if(timer.cbk_type == callback_timer_data::DELEGATE)
|
||||
{
|
||||
// Call the delegate callback.
|
||||
(*reinterpret_cast<callback_type*>(timer.p_callback))();
|
||||
}
|
||||
}
|
||||
|
||||
has_active = !active_list.empty();
|
||||
}
|
||||
|
||||
if (has_active)
|
||||
{
|
||||
// Subtract any remainder from the next due timeout.
|
||||
active_list.front().delta -= count;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Starts a timer.
|
||||
//*******************************************
|
||||
bool start(etl::timer::id::type id_, bool immediate_ = false)
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
// Valid timer id?
|
||||
if (id_ != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
etl::callback_timer_data& timer = timer_array[id_];
|
||||
|
||||
// Registered timer?
|
||||
if (timer.id != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
// Has a valid period.
|
||||
if (timer.period != etl::timer::state::Inactive)
|
||||
{
|
||||
ETL_DISABLE_TIMER_UPDATES;
|
||||
if (timer.is_active())
|
||||
{
|
||||
active_list.remove(timer.id, false);
|
||||
}
|
||||
|
||||
timer.delta = immediate_ ? 0 : timer.period;
|
||||
active_list.insert(timer.id);
|
||||
ETL_ENABLE_TIMER_UPDATES;
|
||||
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Stops a timer.
|
||||
//*******************************************
|
||||
bool stop(etl::timer::id::type id_)
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
// Valid timer id?
|
||||
if (id_ != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
etl::callback_timer_data& timer = timer_array[id_];
|
||||
|
||||
// Registered timer?
|
||||
if (timer.id != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
if (timer.is_active())
|
||||
{
|
||||
ETL_DISABLE_TIMER_UPDATES;
|
||||
active_list.remove(timer.id, false);
|
||||
ETL_ENABLE_TIMER_UPDATES;
|
||||
}
|
||||
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Sets a timer's period.
|
||||
//*******************************************
|
||||
bool set_period(etl::timer::id::type id_, uint32_t period_)
|
||||
{
|
||||
if (stop(id_))
|
||||
{
|
||||
timer_array[id_].period = period_;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Sets a timer's mode.
|
||||
//*******************************************
|
||||
bool set_mode(etl::timer::id::type id_, bool repeating_)
|
||||
{
|
||||
if (stop(id_))
|
||||
{
|
||||
timer_array[id_].repeating = repeating_;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Check if there is an active timer.
|
||||
//*******************************************
|
||||
bool has_active_timer() const
|
||||
{
|
||||
return !active_list.empty();
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Get the time to the next timer event.
|
||||
/// Returns etl::timer::interval::No_Active_Interval if there is no active timer.
|
||||
//*******************************************
|
||||
uint32_t time_to_next() const
|
||||
{
|
||||
uint32_t delta = static_cast<uint32_t>(etl::timer::interval::No_Active_Interval);
|
||||
|
||||
if (has_active_timer())
|
||||
{
|
||||
delta = active_list.front().delta;
|
||||
}
|
||||
|
||||
return delta;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Checks if a timer is currently active.
|
||||
/// Returns <b>true</b> if the timer is active, otherwise <b>false</b>.
|
||||
//*******************************************
|
||||
bool is_active(etl::timer::id::type id_) const
|
||||
{
|
||||
// Valid timer id?
|
||||
if (is_valid_timer_id(id_))
|
||||
{
|
||||
if (has_active_timer())
|
||||
{
|
||||
const etl::callback_timer_data& timer = timer_array[id_];
|
||||
|
||||
// Registered timer?
|
||||
if (timer.id != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
return timer.is_active();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
//*******************************************
|
||||
/// Constructor.
|
||||
//*******************************************
|
||||
icallback_timer(callback_timer_data* const timer_array_, const uint_least8_t MAX_TIMERS_)
|
||||
: timer_array(timer_array_),
|
||||
active_list(timer_array_),
|
||||
enabled(false),
|
||||
#if defined(ETL_CALLBACK_TIMER_USE_ATOMIC_LOCK)
|
||||
process_semaphore(0),
|
||||
#endif
|
||||
registered_timers(0),
|
||||
MAX_TIMERS(MAX_TIMERS_)
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
//*******************************************
|
||||
/// Check that the timer id is valid.
|
||||
//*******************************************
|
||||
bool is_valid_timer_id(etl::timer::id::type id_) const
|
||||
{
|
||||
return (id_ < MAX_TIMERS);
|
||||
}
|
||||
|
||||
// The array of timer data structures.
|
||||
callback_timer_data* const timer_array;
|
||||
|
||||
// The list of active timers.
|
||||
private_callback_timer::list active_list;
|
||||
|
||||
volatile bool enabled;
|
||||
#if defined(ETL_CALLBACK_TIMER_USE_ATOMIC_LOCK)
|
||||
|
||||
#if defined(ETL_TIMER_SEMAPHORE_TYPE)
|
||||
typedef ETL_TIMER_SEMAPHORE_TYPE timer_semaphore_t;
|
||||
#else
|
||||
#if ETL_HAS_ATOMIC
|
||||
typedef etl::atomic_uint16_t timer_semaphore_t;
|
||||
#else
|
||||
#error No atomic type available
|
||||
#endif
|
||||
#endif
|
||||
|
||||
mutable etl::timer_semaphore_t process_semaphore;
|
||||
#endif
|
||||
uint_least8_t registered_timers;
|
||||
|
||||
public:
|
||||
|
||||
const uint_least8_t MAX_TIMERS;
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// The callback timer
|
||||
//***************************************************************************
|
||||
template <const uint_least8_t MAX_TIMERS_>
|
||||
class callback_timer : public etl::icallback_timer
|
||||
{
|
||||
public:
|
||||
|
||||
ETL_STATIC_ASSERT(MAX_TIMERS_ <= 254, "No more than 254 timers are allowed");
|
||||
|
||||
//*******************************************
|
||||
/// Constructor.
|
||||
//*******************************************
|
||||
callback_timer()
|
||||
: icallback_timer(timer_array, MAX_TIMERS_)
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
callback_timer_data timer_array[MAX_TIMERS_];
|
||||
};
|
||||
}
|
||||
|
||||
#undef ETL_DISABLE_TIMER_UPDATES
|
||||
#undef ETL_ENABLE_TIMER_UPDATES
|
||||
#undef ETL_TIMER_UPDATES_ENABLED
|
||||
|
||||
#endif
|
||||
670
Chapter18/cib/libs/etl/include/etl/callback_timer_atomic.h
Normal file
670
Chapter18/cib/libs/etl/include/etl/callback_timer_atomic.h
Normal file
@@ -0,0 +1,670 @@
|
||||
/******************************************************************************
|
||||
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_CALLBACK_TIMER_ATOMIC_INCLUDED
|
||||
#define ETL_CALLBACK_TIMER_ATOMIC_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "algorithm.h"
|
||||
#include "nullptr.h"
|
||||
#include "function.h"
|
||||
#include "static_assert.h"
|
||||
#include "timer.h"
|
||||
#include "error_handler.h"
|
||||
#include "placement_new.h"
|
||||
#include "delegate.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
/// Interface for callback timer
|
||||
//***************************************************************************
|
||||
template <typename TSemaphore>
|
||||
class icallback_timer_atomic
|
||||
{
|
||||
public:
|
||||
|
||||
typedef etl::delegate<void(void)> callback_type;
|
||||
|
||||
//*******************************************
|
||||
/// Register a timer.
|
||||
//*******************************************
|
||||
etl::timer::id::type register_timer(callback_type callback_,
|
||||
uint32_t period_,
|
||||
bool repeating_)
|
||||
{
|
||||
etl::timer::id::type id = etl::timer::id::NO_TIMER;
|
||||
|
||||
bool is_space = (number_of_registered_timers < MAX_TIMERS);
|
||||
|
||||
if (is_space)
|
||||
{
|
||||
// Search for the free space.
|
||||
for (uint_least8_t i = 0U; i < MAX_TIMERS; ++i)
|
||||
{
|
||||
timer_data& timer = timer_array[i];
|
||||
|
||||
if (timer.id == etl::timer::id::NO_TIMER)
|
||||
{
|
||||
// Create in-place.
|
||||
new (&timer) timer_data(i, callback_, period_, repeating_);
|
||||
++number_of_registered_timers;
|
||||
id = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Unregister a timer.
|
||||
//*******************************************
|
||||
bool unregister_timer(etl::timer::id::type id_)
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
if (id_ != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
timer_data& timer = timer_array[id_];
|
||||
|
||||
if (timer.id != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
if (timer.is_active())
|
||||
{
|
||||
++process_semaphore;
|
||||
active_list.remove(timer.id, false);
|
||||
--process_semaphore;
|
||||
}
|
||||
|
||||
// Reset in-place.
|
||||
new (&timer) timer_data();
|
||||
--number_of_registered_timers;
|
||||
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Enable/disable the timer.
|
||||
//*******************************************
|
||||
void enable(bool state_)
|
||||
{
|
||||
enabled = state_;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Get the enable/disable state.
|
||||
//*******************************************
|
||||
bool is_running() const
|
||||
{
|
||||
return enabled;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Clears the timer of data.
|
||||
//*******************************************
|
||||
void clear()
|
||||
{
|
||||
++process_semaphore;
|
||||
active_list.clear();
|
||||
--process_semaphore;
|
||||
|
||||
for (uint8_t i = 0U; i < MAX_TIMERS; ++i)
|
||||
{
|
||||
::new (&timer_array[i]) timer_data();
|
||||
}
|
||||
|
||||
number_of_registered_timers = 0;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
// Called by the timer service to indicate the
|
||||
// amount of time that has elapsed since the last successful call to 'tick'.
|
||||
// Returns true if the tick was processed,
|
||||
// false if not.
|
||||
//*******************************************
|
||||
bool tick(uint32_t count)
|
||||
{
|
||||
if (enabled)
|
||||
{
|
||||
if (process_semaphore == 0U)
|
||||
{
|
||||
// We have something to do?
|
||||
bool has_active = !active_list.empty();
|
||||
|
||||
if (has_active)
|
||||
{
|
||||
while (has_active && (count >= active_list.front().delta))
|
||||
{
|
||||
timer_data& timer = active_list.front();
|
||||
|
||||
count -= timer.delta;
|
||||
|
||||
active_list.remove(timer.id, true);
|
||||
|
||||
if (timer.callback.is_valid())
|
||||
{
|
||||
// Call the delegate callback.
|
||||
timer.callback();
|
||||
}
|
||||
|
||||
if (timer.repeating)
|
||||
{
|
||||
// Reinsert the timer.
|
||||
timer.delta = timer.period;
|
||||
active_list.insert(timer.id);
|
||||
}
|
||||
|
||||
has_active = !active_list.empty();
|
||||
}
|
||||
|
||||
if (has_active)
|
||||
{
|
||||
// Subtract any remainder from the next due timeout.
|
||||
active_list.front().delta -= count;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Starts a timer.
|
||||
//*******************************************
|
||||
bool start(etl::timer::id::type id_, bool immediate_ = false)
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
// Valid timer id?
|
||||
if (id_ != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
timer_data& timer = timer_array[id_];
|
||||
|
||||
// Registered timer?
|
||||
if (timer.id != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
// Has a valid period.
|
||||
if (timer.period != etl::timer::state::Inactive)
|
||||
{
|
||||
++process_semaphore;
|
||||
if (timer.is_active())
|
||||
{
|
||||
active_list.remove(timer.id, false);
|
||||
}
|
||||
|
||||
timer.delta = immediate_ ? 0U : timer.period;
|
||||
active_list.insert(timer.id);
|
||||
--process_semaphore;
|
||||
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Stops a timer.
|
||||
//*******************************************
|
||||
bool stop(etl::timer::id::type id_)
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
// Valid timer id?
|
||||
if (id_ != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
timer_data& timer = timer_array[id_];
|
||||
|
||||
// Registered timer?
|
||||
if (timer.id != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
if (timer.is_active())
|
||||
{
|
||||
++process_semaphore;
|
||||
active_list.remove(timer.id, false);
|
||||
--process_semaphore;
|
||||
}
|
||||
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Sets a timer's period.
|
||||
//*******************************************
|
||||
bool set_period(etl::timer::id::type id_, uint32_t period_)
|
||||
{
|
||||
if (stop(id_))
|
||||
{
|
||||
timer_array[id_].period = period_;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Sets a timer's mode.
|
||||
//*******************************************
|
||||
bool set_mode(etl::timer::id::type id_, bool repeating_)
|
||||
{
|
||||
if (stop(id_))
|
||||
{
|
||||
timer_array[id_].repeating = repeating_;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Check if there is an active timer.
|
||||
//*******************************************
|
||||
bool has_active_timer() const
|
||||
{
|
||||
++process_semaphore;
|
||||
bool result = !active_list.empty();
|
||||
--process_semaphore;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Get the time to the next timer event.
|
||||
/// Returns etl::timer::interval::No_Active_Interval if there is no active timer.
|
||||
//*******************************************
|
||||
uint32_t time_to_next() const
|
||||
{
|
||||
uint32_t delta = static_cast<uint32_t>(etl::timer::interval::No_Active_Interval);
|
||||
|
||||
++process_semaphore;
|
||||
if (!active_list.empty())
|
||||
{
|
||||
delta = active_list.front().delta;
|
||||
}
|
||||
--process_semaphore;
|
||||
|
||||
return delta;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Checks if a timer is currently active.
|
||||
/// Returns <b>true</b> if the timer is active, otherwise <b>false</b>.
|
||||
//*******************************************
|
||||
bool is_active(etl::timer::id::type id_) const
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
// Valid timer id?
|
||||
if (is_valid_timer_id(id_))
|
||||
{
|
||||
++process_semaphore;
|
||||
if (has_active_timer())
|
||||
{
|
||||
const timer_data& timer = timer_array[id_];
|
||||
|
||||
// Registered timer?
|
||||
if (timer.id != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
result = timer.is_active();
|
||||
}
|
||||
}
|
||||
--process_semaphore;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
//*************************************************************************
|
||||
/// The configuration of a timer.
|
||||
struct timer_data
|
||||
{
|
||||
//*******************************************
|
||||
timer_data()
|
||||
: callback()
|
||||
, period(0U)
|
||||
, delta(etl::timer::state::Inactive)
|
||||
, id(etl::timer::id::NO_TIMER)
|
||||
, previous(etl::timer::id::NO_TIMER)
|
||||
, next(etl::timer::id::NO_TIMER)
|
||||
, repeating(true)
|
||||
{
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// ETL delegate callback
|
||||
//*******************************************
|
||||
timer_data(etl::timer::id::type id_,
|
||||
callback_type callback_,
|
||||
uint32_t period_,
|
||||
bool repeating_)
|
||||
: callback(callback_)
|
||||
, period(period_)
|
||||
, delta(etl::timer::state::Inactive)
|
||||
, id(id_)
|
||||
, previous(etl::timer::id::NO_TIMER)
|
||||
, next(etl::timer::id::NO_TIMER)
|
||||
, repeating(repeating_)
|
||||
{
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Returns true if the timer is active.
|
||||
//*******************************************
|
||||
bool is_active() const
|
||||
{
|
||||
return delta != etl::timer::state::Inactive;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Sets the timer to the inactive state.
|
||||
//*******************************************
|
||||
void set_inactive()
|
||||
{
|
||||
delta = etl::timer::state::Inactive;
|
||||
}
|
||||
|
||||
callback_type callback;
|
||||
uint32_t period;
|
||||
uint32_t delta;
|
||||
etl::timer::id::type id;
|
||||
uint_least8_t previous;
|
||||
uint_least8_t next;
|
||||
bool repeating;
|
||||
|
||||
private:
|
||||
|
||||
// Disabled.
|
||||
timer_data(const timer_data& other) ETL_DELETE;
|
||||
timer_data& operator =(const timer_data& other) ETL_DELETE;
|
||||
};
|
||||
|
||||
//*******************************************
|
||||
/// Constructor.
|
||||
//*******************************************
|
||||
icallback_timer_atomic(timer_data* const timer_array_, const uint_least8_t MAX_TIMERS_)
|
||||
: timer_array(timer_array_)
|
||||
, active_list(timer_array_)
|
||||
, enabled(false)
|
||||
, process_semaphore(0U)
|
||||
, number_of_registered_timers(0U)
|
||||
, MAX_TIMERS(MAX_TIMERS_)
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
//*******************************************
|
||||
/// Check that the timer id is valid.
|
||||
//*******************************************
|
||||
bool is_valid_timer_id(etl::timer::id::type id_) const
|
||||
{
|
||||
return (id_ < MAX_TIMERS);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// A specialised intrusive linked list for timer data.
|
||||
//*************************************************************************
|
||||
class timer_list
|
||||
{
|
||||
public:
|
||||
|
||||
//*******************************
|
||||
timer_list(timer_data* ptimers_)
|
||||
: head(etl::timer::id::NO_TIMER)
|
||||
, tail(etl::timer::id::NO_TIMER)
|
||||
, current(etl::timer::id::NO_TIMER)
|
||||
, ptimers(ptimers_)
|
||||
{
|
||||
}
|
||||
|
||||
//*******************************
|
||||
bool empty() const
|
||||
{
|
||||
return head == etl::timer::id::NO_TIMER;
|
||||
}
|
||||
|
||||
//*******************************
|
||||
// Inserts the timer at the correct delta position
|
||||
//*******************************
|
||||
void insert(etl::timer::id::type id_)
|
||||
{
|
||||
timer_data& timer = ptimers[id_];
|
||||
|
||||
if (head == etl::timer::id::NO_TIMER)
|
||||
{
|
||||
// No entries yet.
|
||||
head = id_;
|
||||
tail = id_;
|
||||
timer.previous = etl::timer::id::NO_TIMER;
|
||||
timer.next = etl::timer::id::NO_TIMER;
|
||||
}
|
||||
else
|
||||
{
|
||||
// We already have entries.
|
||||
etl::timer::id::type test_id = begin();
|
||||
|
||||
while (test_id != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
timer_data& test = ptimers[test_id];
|
||||
|
||||
// Find the correct place to insert.
|
||||
if (timer.delta <= test.delta)
|
||||
{
|
||||
if (test.id == head)
|
||||
{
|
||||
head = timer.id;
|
||||
}
|
||||
|
||||
// Insert before test.
|
||||
timer.previous = test.previous;
|
||||
test.previous = timer.id;
|
||||
timer.next = test.id;
|
||||
|
||||
// Adjust the next delta to compensate.
|
||||
test.delta -= timer.delta;
|
||||
|
||||
if (timer.previous != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
ptimers[timer.previous].next = timer.id;
|
||||
}
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
timer.delta -= test.delta;
|
||||
}
|
||||
|
||||
test_id = next(test_id);
|
||||
}
|
||||
|
||||
// Reached the end?
|
||||
if (test_id == etl::timer::id::NO_TIMER)
|
||||
{
|
||||
// Tag on to the tail.
|
||||
ptimers[tail].next = timer.id;
|
||||
timer.previous = tail;
|
||||
timer.next = etl::timer::id::NO_TIMER;
|
||||
tail = timer.id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//*******************************
|
||||
void remove(etl::timer::id::type id_, bool has_expired)
|
||||
{
|
||||
timer_data& timer = ptimers[id_];
|
||||
|
||||
if (head == id_)
|
||||
{
|
||||
head = timer.next;
|
||||
}
|
||||
else
|
||||
{
|
||||
ptimers[timer.previous].next = timer.next;
|
||||
}
|
||||
|
||||
if (tail == id_)
|
||||
{
|
||||
tail = timer.previous;
|
||||
}
|
||||
else
|
||||
{
|
||||
ptimers[timer.next].previous = timer.previous;
|
||||
}
|
||||
|
||||
if (!has_expired)
|
||||
{
|
||||
// Adjust the next delta.
|
||||
if (timer.next != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
ptimers[timer.next].delta += timer.delta;
|
||||
}
|
||||
}
|
||||
|
||||
timer.previous = etl::timer::id::NO_TIMER;
|
||||
timer.next = etl::timer::id::NO_TIMER;
|
||||
timer.delta = etl::timer::state::Inactive;
|
||||
}
|
||||
|
||||
//*******************************
|
||||
timer_data& front()
|
||||
{
|
||||
return ptimers[head];
|
||||
}
|
||||
|
||||
//*******************************
|
||||
const timer_data& front() const
|
||||
{
|
||||
return ptimers[head];
|
||||
}
|
||||
|
||||
//*******************************
|
||||
etl::timer::id::type begin()
|
||||
{
|
||||
current = head;
|
||||
return current;
|
||||
}
|
||||
|
||||
//*******************************
|
||||
etl::timer::id::type previous(etl::timer::id::type last)
|
||||
{
|
||||
current = ptimers[last].previous;
|
||||
return current;
|
||||
}
|
||||
|
||||
//*******************************
|
||||
etl::timer::id::type next(etl::timer::id::type last)
|
||||
{
|
||||
current = ptimers[last].next;
|
||||
return current;
|
||||
}
|
||||
|
||||
//*******************************
|
||||
void clear()
|
||||
{
|
||||
etl::timer::id::type id = begin();
|
||||
|
||||
while (id != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
timer_data& timer = ptimers[id];
|
||||
id = next(id);
|
||||
timer.next = etl::timer::id::NO_TIMER;
|
||||
}
|
||||
|
||||
head = etl::timer::id::NO_TIMER;
|
||||
tail = etl::timer::id::NO_TIMER;
|
||||
current = etl::timer::id::NO_TIMER;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
etl::timer::id::type head;
|
||||
etl::timer::id::type tail;
|
||||
etl::timer::id::type current;
|
||||
|
||||
timer_data* const ptimers;
|
||||
};
|
||||
|
||||
// The array of timer data structures.
|
||||
timer_data* const timer_array;
|
||||
|
||||
// The list of active timers.
|
||||
timer_list active_list;
|
||||
|
||||
bool enabled;
|
||||
mutable TSemaphore process_semaphore;
|
||||
uint_least8_t number_of_registered_timers;
|
||||
|
||||
public:
|
||||
|
||||
const uint_least8_t MAX_TIMERS;
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// The callback timer
|
||||
//***************************************************************************
|
||||
template <uint_least8_t MAX_TIMERS_, typename TSemaphore>
|
||||
class callback_timer_atomic : public etl::icallback_timer_atomic<TSemaphore>
|
||||
{
|
||||
public:
|
||||
|
||||
ETL_STATIC_ASSERT(MAX_TIMERS_ <= 254U, "No more than 254 timers are allowed");
|
||||
|
||||
//*******************************************
|
||||
/// Constructor.
|
||||
//*******************************************
|
||||
callback_timer_atomic()
|
||||
: icallback_timer_atomic<TSemaphore>(timer_array, MAX_TIMERS_)
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
typename etl::icallback_timer_atomic<TSemaphore>::timer_data timer_array[MAX_TIMERS_];
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
671
Chapter18/cib/libs/etl/include/etl/callback_timer_interrupt.h
Normal file
671
Chapter18/cib/libs/etl/include/etl/callback_timer_interrupt.h
Normal file
@@ -0,0 +1,671 @@
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2022 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_TIMER_INTERRUPT_INCLUDED
|
||||
#define ETL_CALLBACK_TIMER_INTERRUPT_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "algorithm.h"
|
||||
#include "nullptr.h"
|
||||
#include "delegate.h"
|
||||
#include "static_assert.h"
|
||||
#include "timer.h"
|
||||
#include "error_handler.h"
|
||||
#include "placement_new.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
/// Interface for callback timer
|
||||
//***************************************************************************
|
||||
template <typename TInterruptGuard>
|
||||
class icallback_timer_interrupt
|
||||
{
|
||||
public:
|
||||
|
||||
typedef etl::delegate<void(void)> callback_type;
|
||||
|
||||
//*******************************************
|
||||
/// Register a timer.
|
||||
//*******************************************
|
||||
etl::timer::id::type register_timer(const callback_type& callback_,
|
||||
uint32_t period_,
|
||||
bool repeating_)
|
||||
{
|
||||
etl::timer::id::type id = etl::timer::id::NO_TIMER;
|
||||
|
||||
bool is_space = (number_of_registered_timers < MAX_TIMERS);
|
||||
|
||||
if (is_space)
|
||||
{
|
||||
// Search for the free space.
|
||||
for (uint_least8_t i = 0U; i < MAX_TIMERS; ++i)
|
||||
{
|
||||
timer_data& timer = timer_array[i];
|
||||
|
||||
if (timer.id == etl::timer::id::NO_TIMER)
|
||||
{
|
||||
TInterruptGuard guard;
|
||||
(void)guard; // Silence 'unused variable warnings.
|
||||
|
||||
// Create in-place.
|
||||
new (&timer) timer_data(i, callback_, period_, repeating_);
|
||||
++number_of_registered_timers;
|
||||
id = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Unregister a timer.
|
||||
//*******************************************
|
||||
bool unregister_timer(etl::timer::id::type id_)
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
if (id_ != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
timer_data& timer = timer_array[id_];
|
||||
|
||||
if (timer.id != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
if (timer.is_active())
|
||||
{
|
||||
TInterruptGuard guard;
|
||||
(void)guard; // Silence 'unused variable warnings.
|
||||
|
||||
active_list.remove(timer.id, false);
|
||||
}
|
||||
|
||||
// Reset in-place.
|
||||
new (&timer) timer_data();
|
||||
--number_of_registered_timers;
|
||||
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Enable/disable the timer.
|
||||
//*******************************************
|
||||
void enable(bool state_)
|
||||
{
|
||||
enabled = state_;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Get the enable/disable state.
|
||||
//*******************************************
|
||||
bool is_running() const
|
||||
{
|
||||
return enabled;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Clears the timer of data.
|
||||
//*******************************************
|
||||
void clear()
|
||||
{
|
||||
{
|
||||
TInterruptGuard guard;
|
||||
(void)guard; // Silence 'unused variable warnings.
|
||||
|
||||
active_list.clear();
|
||||
number_of_registered_timers = 0;
|
||||
}
|
||||
|
||||
for (uint8_t i = 0U; i < MAX_TIMERS; ++i)
|
||||
{
|
||||
::new (&timer_array[i]) timer_data();
|
||||
}
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
// Called by the timer service to indicate the
|
||||
// amount of time that has elapsed since the last successful call to 'tick'.
|
||||
// Returns true if the tick was processed,
|
||||
// false if not.
|
||||
//*******************************************
|
||||
bool tick(uint32_t count)
|
||||
{
|
||||
if (enabled)
|
||||
{
|
||||
// We have something to do?
|
||||
bool has_active = !active_list.empty();
|
||||
|
||||
if (has_active)
|
||||
{
|
||||
while (has_active && (count >= active_list.front().delta))
|
||||
{
|
||||
timer_data& timer = active_list.front();
|
||||
|
||||
count -= timer.delta;
|
||||
|
||||
active_list.remove(timer.id, true);
|
||||
|
||||
if (timer.callback.is_valid())
|
||||
{
|
||||
timer.callback();
|
||||
}
|
||||
|
||||
if (timer.repeating)
|
||||
{
|
||||
// Reinsert the timer.
|
||||
timer.delta = timer.period;
|
||||
active_list.insert(timer.id);
|
||||
}
|
||||
|
||||
has_active = !active_list.empty();
|
||||
}
|
||||
|
||||
if (has_active)
|
||||
{
|
||||
// Subtract any remainder from the next due timeout.
|
||||
active_list.front().delta -= count;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Starts a timer.
|
||||
//*******************************************
|
||||
bool start(etl::timer::id::type id_, bool immediate_ = false)
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
// Valid timer id?
|
||||
if (id_ != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
timer_data& timer = timer_array[id_];
|
||||
|
||||
// Registered timer?
|
||||
if (timer.id != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
// Has a valid period.
|
||||
if (timer.period != etl::timer::state::Inactive)
|
||||
{
|
||||
TInterruptGuard guard;
|
||||
(void)guard; // Silence 'unused variable warnings.
|
||||
|
||||
if (timer.is_active())
|
||||
{
|
||||
active_list.remove(timer.id, false);
|
||||
}
|
||||
|
||||
timer.delta = immediate_ ? 0U : timer.period;
|
||||
active_list.insert(timer.id);
|
||||
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Stops a timer.
|
||||
//*******************************************
|
||||
bool stop(etl::timer::id::type id_)
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
// Valid timer id?
|
||||
if (id_ != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
timer_data& timer = timer_array[id_];
|
||||
|
||||
// Registered timer?
|
||||
if (timer.id != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
if (timer.is_active())
|
||||
{
|
||||
TInterruptGuard guard;
|
||||
(void)guard; // Silence 'unused variable warnings.
|
||||
|
||||
active_list.remove(timer.id, false);
|
||||
}
|
||||
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Sets a timer's period.
|
||||
//*******************************************
|
||||
bool set_period(etl::timer::id::type id_, uint32_t period_)
|
||||
{
|
||||
if (stop(id_))
|
||||
{
|
||||
timer_array[id_].period = period_;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Sets a timer's mode.
|
||||
//*******************************************
|
||||
bool set_mode(etl::timer::id::type id_, bool repeating_)
|
||||
{
|
||||
if (stop(id_))
|
||||
{
|
||||
timer_array[id_].repeating = repeating_;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Check if there is an active timer.
|
||||
//*******************************************
|
||||
bool has_active_timer() const
|
||||
{
|
||||
TInterruptGuard guard;
|
||||
(void)guard; // Silence 'unused variable warnings.
|
||||
return !active_list.empty();
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Get the time to the next timer event.
|
||||
/// Returns etl::timer::interval::No_Active_Interval if there is no active timer.
|
||||
//*******************************************
|
||||
uint32_t time_to_next() const
|
||||
{
|
||||
uint32_t delta = static_cast<uint32_t>(etl::timer::interval::No_Active_Interval);
|
||||
|
||||
TInterruptGuard guard;
|
||||
(void)guard; // Silence 'unused variable warnings.
|
||||
|
||||
if (!active_list.empty())
|
||||
{
|
||||
delta = active_list.front().delta;
|
||||
}
|
||||
|
||||
return delta;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Checks if a timer is currently active.
|
||||
/// Returns <b>true</b> if the timer is active, otherwise <b>false</b>.
|
||||
//*******************************************
|
||||
bool is_active(etl::timer::id::type id_) const
|
||||
{
|
||||
// Valid timer id?
|
||||
if (is_valid_timer_id(id_))
|
||||
{
|
||||
if (has_active_timer())
|
||||
{
|
||||
TInterruptGuard guard;
|
||||
(void)guard; // Silence 'unused variable warnings.
|
||||
|
||||
const timer_data& timer = timer_array[id_];
|
||||
|
||||
// Registered timer?
|
||||
if (timer.id != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
return timer.is_active();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
//*************************************************************************
|
||||
/// The configuration of a timer.
|
||||
struct timer_data
|
||||
{
|
||||
//*******************************************
|
||||
timer_data()
|
||||
: callback()
|
||||
, period(0U)
|
||||
, delta(etl::timer::state::Inactive)
|
||||
, id(etl::timer::id::NO_TIMER)
|
||||
, previous(etl::timer::id::NO_TIMER)
|
||||
, next(etl::timer::id::NO_TIMER)
|
||||
, repeating(true)
|
||||
{
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// ETL delegate callback
|
||||
//*******************************************
|
||||
timer_data(etl::timer::id::type id_,
|
||||
callback_type callback_,
|
||||
uint32_t period_,
|
||||
bool repeating_)
|
||||
: callback(callback_)
|
||||
, period(period_)
|
||||
, delta(etl::timer::state::Inactive)
|
||||
, id(id_)
|
||||
, previous(etl::timer::id::NO_TIMER)
|
||||
, next(etl::timer::id::NO_TIMER)
|
||||
, repeating(repeating_)
|
||||
{
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Returns true if the timer is active.
|
||||
//*******************************************
|
||||
bool is_active() const
|
||||
{
|
||||
return delta != etl::timer::state::Inactive;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Sets the timer to the inactive state.
|
||||
//*******************************************
|
||||
void set_inactive()
|
||||
{
|
||||
delta = etl::timer::state::Inactive;
|
||||
}
|
||||
|
||||
callback_type callback;
|
||||
uint32_t period;
|
||||
uint32_t delta;
|
||||
etl::timer::id::type id;
|
||||
uint_least8_t previous;
|
||||
uint_least8_t next;
|
||||
bool repeating;
|
||||
|
||||
private:
|
||||
|
||||
// Disabled.
|
||||
timer_data(const timer_data& other) ETL_DELETE;
|
||||
timer_data& operator =(const timer_data& other) ETL_DELETE;
|
||||
};
|
||||
|
||||
//*******************************************
|
||||
/// Constructor.
|
||||
//*******************************************
|
||||
icallback_timer_interrupt(timer_data* const timer_array_, const uint_least8_t MAX_TIMERS_)
|
||||
: timer_array(timer_array_)
|
||||
, active_list(timer_array_)
|
||||
, enabled(false)
|
||||
, number_of_registered_timers(0U)
|
||||
, MAX_TIMERS(MAX_TIMERS_)
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
//*******************************************
|
||||
/// Check that the timer id is valid.
|
||||
//*******************************************
|
||||
bool is_valid_timer_id(etl::timer::id::type id_) const
|
||||
{
|
||||
return (id_ < MAX_TIMERS);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// A specialised intrusive linked list for timer data.
|
||||
//*************************************************************************
|
||||
class timer_list
|
||||
{
|
||||
public:
|
||||
|
||||
//*******************************
|
||||
timer_list(timer_data* ptimers_)
|
||||
: head(etl::timer::id::NO_TIMER)
|
||||
, tail(etl::timer::id::NO_TIMER)
|
||||
, current(etl::timer::id::NO_TIMER)
|
||||
, ptimers(ptimers_)
|
||||
{
|
||||
}
|
||||
|
||||
//*******************************
|
||||
bool empty() const
|
||||
{
|
||||
return head == etl::timer::id::NO_TIMER;
|
||||
}
|
||||
|
||||
//*******************************
|
||||
// Inserts the timer at the correct delta position
|
||||
//*******************************
|
||||
void insert(etl::timer::id::type id_)
|
||||
{
|
||||
timer_data& timer = ptimers[id_];
|
||||
|
||||
if (head == etl::timer::id::NO_TIMER)
|
||||
{
|
||||
// No entries yet.
|
||||
head = id_;
|
||||
tail = id_;
|
||||
timer.previous = etl::timer::id::NO_TIMER;
|
||||
timer.next = etl::timer::id::NO_TIMER;
|
||||
}
|
||||
else
|
||||
{
|
||||
// We already have entries.
|
||||
etl::timer::id::type test_id = begin();
|
||||
|
||||
while (test_id != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
timer_data& test = ptimers[test_id];
|
||||
|
||||
// Find the correct place to insert.
|
||||
if (timer.delta <= test.delta)
|
||||
{
|
||||
if (test.id == head)
|
||||
{
|
||||
head = timer.id;
|
||||
}
|
||||
|
||||
// Insert before test.
|
||||
timer.previous = test.previous;
|
||||
test.previous = timer.id;
|
||||
timer.next = test.id;
|
||||
|
||||
// Adjust the next delta to compensate.
|
||||
test.delta -= timer.delta;
|
||||
|
||||
if (timer.previous != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
ptimers[timer.previous].next = timer.id;
|
||||
}
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
timer.delta -= test.delta;
|
||||
}
|
||||
|
||||
test_id = next(test_id);
|
||||
}
|
||||
|
||||
// Reached the end?
|
||||
if (test_id == etl::timer::id::NO_TIMER)
|
||||
{
|
||||
// Tag on to the tail.
|
||||
ptimers[tail].next = timer.id;
|
||||
timer.previous = tail;
|
||||
timer.next = etl::timer::id::NO_TIMER;
|
||||
tail = timer.id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//*******************************
|
||||
void remove(etl::timer::id::type id_, bool has_expired)
|
||||
{
|
||||
timer_data& timer = ptimers[id_];
|
||||
|
||||
if (head == id_)
|
||||
{
|
||||
head = timer.next;
|
||||
}
|
||||
else
|
||||
{
|
||||
ptimers[timer.previous].next = timer.next;
|
||||
}
|
||||
|
||||
if (tail == id_)
|
||||
{
|
||||
tail = timer.previous;
|
||||
}
|
||||
else
|
||||
{
|
||||
ptimers[timer.next].previous = timer.previous;
|
||||
}
|
||||
|
||||
if (!has_expired)
|
||||
{
|
||||
// Adjust the next delta.
|
||||
if (timer.next != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
ptimers[timer.next].delta += timer.delta;
|
||||
}
|
||||
}
|
||||
|
||||
timer.previous = etl::timer::id::NO_TIMER;
|
||||
timer.next = etl::timer::id::NO_TIMER;
|
||||
timer.delta = etl::timer::state::Inactive;
|
||||
}
|
||||
|
||||
//*******************************
|
||||
timer_data& front()
|
||||
{
|
||||
return ptimers[head];
|
||||
}
|
||||
|
||||
//*******************************
|
||||
const timer_data& front() const
|
||||
{
|
||||
return ptimers[head];
|
||||
}
|
||||
|
||||
//*******************************
|
||||
etl::timer::id::type begin()
|
||||
{
|
||||
current = head;
|
||||
return current;
|
||||
}
|
||||
|
||||
//*******************************
|
||||
etl::timer::id::type previous(etl::timer::id::type last)
|
||||
{
|
||||
current = ptimers[last].previous;
|
||||
return current;
|
||||
}
|
||||
|
||||
//*******************************
|
||||
etl::timer::id::type next(etl::timer::id::type last)
|
||||
{
|
||||
current = ptimers[last].next;
|
||||
return current;
|
||||
}
|
||||
|
||||
//*******************************
|
||||
void clear()
|
||||
{
|
||||
etl::timer::id::type id = begin();
|
||||
|
||||
while (id != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
timer_data& timer = ptimers[id];
|
||||
id = next(id);
|
||||
timer.next = etl::timer::id::NO_TIMER;
|
||||
}
|
||||
|
||||
head = etl::timer::id::NO_TIMER;
|
||||
tail = etl::timer::id::NO_TIMER;
|
||||
current = etl::timer::id::NO_TIMER;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
etl::timer::id::type head;
|
||||
etl::timer::id::type tail;
|
||||
etl::timer::id::type current;
|
||||
|
||||
timer_data* const ptimers;
|
||||
};
|
||||
|
||||
// The array of timer data structures.
|
||||
timer_data* const timer_array;
|
||||
|
||||
// The list of active timers.
|
||||
timer_list active_list;
|
||||
|
||||
bool enabled;
|
||||
uint_least8_t number_of_registered_timers;
|
||||
|
||||
public:
|
||||
|
||||
const uint_least8_t MAX_TIMERS;
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// The callback timer
|
||||
//***************************************************************************
|
||||
template <uint_least8_t MAX_TIMERS_, typename TInterruptGuard>
|
||||
class callback_timer_interrupt : public etl::icallback_timer_interrupt<TInterruptGuard>
|
||||
{
|
||||
public:
|
||||
|
||||
ETL_STATIC_ASSERT(MAX_TIMERS_ <= 254U, "No more than 254 timers are allowed");
|
||||
|
||||
typedef typename icallback_timer_interrupt<TInterruptGuard>::callback_type callback_type;
|
||||
|
||||
//*******************************************
|
||||
/// Constructor.
|
||||
//*******************************************
|
||||
callback_timer_interrupt()
|
||||
: icallback_timer_interrupt<TInterruptGuard>(timer_array, MAX_TIMERS_)
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
typename icallback_timer_interrupt<TInterruptGuard>::timer_data timer_array[MAX_TIMERS_];
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
698
Chapter18/cib/libs/etl/include/etl/callback_timer_locked.h
Normal file
698
Chapter18/cib/libs/etl/include/etl/callback_timer_locked.h
Normal file
@@ -0,0 +1,698 @@
|
||||
/******************************************************************************
|
||||
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_CALLBACK_TIMER_LOCKED_INCLUDED
|
||||
#define ETL_CALLBACK_TIMER_LOCKED_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "algorithm.h"
|
||||
#include "nullptr.h"
|
||||
#include "delegate.h"
|
||||
#include "static_assert.h"
|
||||
#include "timer.h"
|
||||
#include "error_handler.h"
|
||||
#include "placement_new.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
/// Interface for callback timer
|
||||
//***************************************************************************
|
||||
class icallback_timer_locked
|
||||
{
|
||||
public:
|
||||
|
||||
typedef etl::delegate<void(void)> callback_type;
|
||||
typedef etl::delegate<bool(void)> try_lock_type;
|
||||
typedef etl::delegate<void(void)> lock_type;
|
||||
typedef etl::delegate<void(void)> unlock_type;
|
||||
|
||||
//*******************************************
|
||||
/// Register a timer.
|
||||
//*******************************************
|
||||
etl::timer::id::type register_timer(const callback_type& callback_,
|
||||
uint32_t period_,
|
||||
bool repeating_)
|
||||
{
|
||||
etl::timer::id::type id = etl::timer::id::NO_TIMER;
|
||||
|
||||
bool is_space = (number_of_registered_timers < MAX_TIMERS);
|
||||
|
||||
if (is_space)
|
||||
{
|
||||
// Search for the free space.
|
||||
for (uint_least8_t i = 0U; i < MAX_TIMERS; ++i)
|
||||
{
|
||||
timer_data& timer = timer_array[i];
|
||||
|
||||
if (timer.id == etl::timer::id::NO_TIMER)
|
||||
{
|
||||
// Create in-place.
|
||||
new (&timer) timer_data(i, callback_, period_, repeating_);
|
||||
++number_of_registered_timers;
|
||||
id = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Unregister a timer.
|
||||
//*******************************************
|
||||
bool unregister_timer(etl::timer::id::type id_)
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
if (id_ != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
timer_data& timer = timer_array[id_];
|
||||
|
||||
if (timer.id != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
if (timer.is_active())
|
||||
{
|
||||
lock();
|
||||
active_list.remove(timer.id, false);
|
||||
unlock();
|
||||
}
|
||||
|
||||
// Reset in-place.
|
||||
new (&timer) timer_data();
|
||||
--number_of_registered_timers;
|
||||
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Enable/disable the timer.
|
||||
//*******************************************
|
||||
void enable(bool state_)
|
||||
{
|
||||
enabled = state_;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Get the enable/disable state.
|
||||
//*******************************************
|
||||
bool is_running() const
|
||||
{
|
||||
return enabled;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Clears the timer of data.
|
||||
//*******************************************
|
||||
void clear()
|
||||
{
|
||||
lock();
|
||||
active_list.clear();
|
||||
unlock();
|
||||
|
||||
for (uint8_t i = 0U; i < MAX_TIMERS; ++i)
|
||||
{
|
||||
::new (&timer_array[i]) timer_data();
|
||||
}
|
||||
|
||||
number_of_registered_timers = 0;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
// Called by the timer service to indicate the
|
||||
// amount of time that has elapsed since the last successful call to 'tick'.
|
||||
// Returns true if the tick was processed,
|
||||
// false if not.
|
||||
//*******************************************
|
||||
bool tick(uint32_t count)
|
||||
{
|
||||
if (enabled)
|
||||
{
|
||||
if (try_lock())
|
||||
{
|
||||
// We have something to do?
|
||||
bool has_active = !active_list.empty();
|
||||
|
||||
if (has_active)
|
||||
{
|
||||
while (has_active && (count >= active_list.front().delta))
|
||||
{
|
||||
timer_data& timer = active_list.front();
|
||||
|
||||
count -= timer.delta;
|
||||
|
||||
active_list.remove(timer.id, true);
|
||||
|
||||
if (timer.callback.is_valid())
|
||||
{
|
||||
timer.callback();
|
||||
}
|
||||
|
||||
if (timer.repeating)
|
||||
{
|
||||
// Reinsert the timer.
|
||||
timer.delta = timer.period;
|
||||
active_list.insert(timer.id);
|
||||
}
|
||||
|
||||
has_active = !active_list.empty();
|
||||
}
|
||||
|
||||
if (has_active)
|
||||
{
|
||||
// Subtract any remainder from the next due timeout.
|
||||
active_list.front().delta -= count;
|
||||
}
|
||||
}
|
||||
|
||||
unlock();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Starts a timer.
|
||||
//*******************************************
|
||||
bool start(etl::timer::id::type id_, bool immediate_ = false)
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
// Valid timer id?
|
||||
if (id_ != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
timer_data& timer = timer_array[id_];
|
||||
|
||||
// Registered timer?
|
||||
if (timer.id != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
// Has a valid period.
|
||||
if (timer.period != etl::timer::state::Inactive)
|
||||
{
|
||||
lock();
|
||||
if (timer.is_active())
|
||||
{
|
||||
active_list.remove(timer.id, false);
|
||||
}
|
||||
|
||||
timer.delta = immediate_ ? 0U : timer.period;
|
||||
active_list.insert(timer.id);
|
||||
unlock();
|
||||
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Stops a timer.
|
||||
//*******************************************
|
||||
bool stop(etl::timer::id::type id_)
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
// Valid timer id?
|
||||
if (id_ != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
timer_data& timer = timer_array[id_];
|
||||
|
||||
// Registered timer?
|
||||
if (timer.id != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
if (timer.is_active())
|
||||
{
|
||||
lock();
|
||||
active_list.remove(timer.id, false);
|
||||
unlock();
|
||||
}
|
||||
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Sets a timer's period.
|
||||
//*******************************************
|
||||
bool set_period(etl::timer::id::type id_, uint32_t period_)
|
||||
{
|
||||
if (stop(id_))
|
||||
{
|
||||
timer_array[id_].period = period_;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Sets a timer's mode.
|
||||
//*******************************************
|
||||
bool set_mode(etl::timer::id::type id_, bool repeating_)
|
||||
{
|
||||
if (stop(id_))
|
||||
{
|
||||
timer_array[id_].repeating = repeating_;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Sets the lock and unlock delegates.
|
||||
//*******************************************
|
||||
void set_locks(try_lock_type try_lock_, lock_type lock_, lock_type unlock_)
|
||||
{
|
||||
try_lock = try_lock_;
|
||||
lock = lock_;
|
||||
unlock = unlock_;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Check if there is an active timer.
|
||||
//*******************************************
|
||||
bool has_active_timer() const
|
||||
{
|
||||
lock();
|
||||
bool result = !active_list.empty();
|
||||
unlock();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Get the time to the next timer event.
|
||||
/// Returns etl::timer::interval::No_Active_Interval if there is no active timer.
|
||||
//*******************************************
|
||||
uint32_t time_to_next() const
|
||||
{
|
||||
uint32_t delta = static_cast<uint32_t>(etl::timer::interval::No_Active_Interval);
|
||||
|
||||
lock();
|
||||
if (!active_list.empty())
|
||||
{
|
||||
delta = active_list.front().delta;
|
||||
}
|
||||
unlock();
|
||||
|
||||
return delta;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Checks if a timer is currently active.
|
||||
/// Returns <b>true</b> if the timer is active, otherwise <b>false</b>.
|
||||
//*******************************************
|
||||
bool is_active(etl::timer::id::type id_) const
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
// Valid timer id?
|
||||
if (is_valid_timer_id(id_))
|
||||
{
|
||||
if (has_active_timer())
|
||||
{
|
||||
lock();
|
||||
const timer_data& timer = timer_array[id_];
|
||||
|
||||
// Registered timer?
|
||||
if (timer.id != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
result = timer.is_active();
|
||||
}
|
||||
unlock();
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
//*************************************************************************
|
||||
/// The configuration of a timer.
|
||||
struct timer_data
|
||||
{
|
||||
//*******************************************
|
||||
timer_data()
|
||||
: callback()
|
||||
, period(0U)
|
||||
, delta(etl::timer::state::Inactive)
|
||||
, id(etl::timer::id::NO_TIMER)
|
||||
, previous(etl::timer::id::NO_TIMER)
|
||||
, next(etl::timer::id::NO_TIMER)
|
||||
, repeating(true)
|
||||
{
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// ETL delegate callback
|
||||
//*******************************************
|
||||
timer_data(etl::timer::id::type id_,
|
||||
callback_type callback_,
|
||||
uint32_t period_,
|
||||
bool repeating_)
|
||||
: callback(callback_)
|
||||
, period(period_)
|
||||
, delta(etl::timer::state::Inactive)
|
||||
, id(id_)
|
||||
, previous(etl::timer::id::NO_TIMER)
|
||||
, next(etl::timer::id::NO_TIMER)
|
||||
, repeating(repeating_)
|
||||
{
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Returns true if the timer is active.
|
||||
//*******************************************
|
||||
bool is_active() const
|
||||
{
|
||||
return delta != etl::timer::state::Inactive;
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Sets the timer to the inactive state.
|
||||
//*******************************************
|
||||
void set_inactive()
|
||||
{
|
||||
delta = etl::timer::state::Inactive;
|
||||
}
|
||||
|
||||
callback_type callback;
|
||||
uint32_t period;
|
||||
uint32_t delta;
|
||||
etl::timer::id::type id;
|
||||
uint_least8_t previous;
|
||||
uint_least8_t next;
|
||||
bool repeating;
|
||||
|
||||
private:
|
||||
|
||||
// Disabled.
|
||||
timer_data(const timer_data& other) ETL_DELETE;
|
||||
timer_data& operator =(const timer_data& other) ETL_DELETE;
|
||||
};
|
||||
|
||||
//*******************************************
|
||||
/// Constructor.
|
||||
//*******************************************
|
||||
icallback_timer_locked(timer_data* const timer_array_, const uint_least8_t MAX_TIMERS_)
|
||||
: timer_array(timer_array_),
|
||||
active_list(timer_array_),
|
||||
enabled(false),
|
||||
number_of_registered_timers(0U),
|
||||
MAX_TIMERS(MAX_TIMERS_)
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
//*************************************************************************
|
||||
/// A specialised intrusive linked list for timer data.
|
||||
//*************************************************************************
|
||||
class timer_list
|
||||
{
|
||||
public:
|
||||
|
||||
//*******************************
|
||||
timer_list(timer_data* ptimers_)
|
||||
: head(etl::timer::id::NO_TIMER)
|
||||
, tail(etl::timer::id::NO_TIMER)
|
||||
, current(etl::timer::id::NO_TIMER)
|
||||
, ptimers(ptimers_)
|
||||
{
|
||||
}
|
||||
|
||||
//*******************************
|
||||
bool empty() const
|
||||
{
|
||||
return head == etl::timer::id::NO_TIMER;
|
||||
}
|
||||
|
||||
//*******************************
|
||||
// Inserts the timer at the correct delta position
|
||||
//*******************************
|
||||
void insert(etl::timer::id::type id_)
|
||||
{
|
||||
timer_data& timer = ptimers[id_];
|
||||
|
||||
if (head == etl::timer::id::NO_TIMER)
|
||||
{
|
||||
// No entries yet.
|
||||
head = id_;
|
||||
tail = id_;
|
||||
timer.previous = etl::timer::id::NO_TIMER;
|
||||
timer.next = etl::timer::id::NO_TIMER;
|
||||
}
|
||||
else
|
||||
{
|
||||
// We already have entries.
|
||||
etl::timer::id::type test_id = begin();
|
||||
|
||||
while (test_id != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
timer_data& test = ptimers[test_id];
|
||||
|
||||
// Find the correct place to insert.
|
||||
if (timer.delta <= test.delta)
|
||||
{
|
||||
if (test.id == head)
|
||||
{
|
||||
head = timer.id;
|
||||
}
|
||||
|
||||
// Insert before test.
|
||||
timer.previous = test.previous;
|
||||
test.previous = timer.id;
|
||||
timer.next = test.id;
|
||||
|
||||
// Adjust the next delta to compensate.
|
||||
test.delta -= timer.delta;
|
||||
|
||||
if (timer.previous != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
ptimers[timer.previous].next = timer.id;
|
||||
}
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
timer.delta -= test.delta;
|
||||
}
|
||||
|
||||
test_id = next(test_id);
|
||||
}
|
||||
|
||||
// Reached the end?
|
||||
if (test_id == etl::timer::id::NO_TIMER)
|
||||
{
|
||||
// Tag on to the tail.
|
||||
ptimers[tail].next = timer.id;
|
||||
timer.previous = tail;
|
||||
timer.next = etl::timer::id::NO_TIMER;
|
||||
tail = timer.id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//*******************************
|
||||
void remove(etl::timer::id::type id_, bool has_expired)
|
||||
{
|
||||
timer_data& timer = ptimers[id_];
|
||||
|
||||
if (head == id_)
|
||||
{
|
||||
head = timer.next;
|
||||
}
|
||||
else
|
||||
{
|
||||
ptimers[timer.previous].next = timer.next;
|
||||
}
|
||||
|
||||
if (tail == id_)
|
||||
{
|
||||
tail = timer.previous;
|
||||
}
|
||||
else
|
||||
{
|
||||
ptimers[timer.next].previous = timer.previous;
|
||||
}
|
||||
|
||||
if (!has_expired)
|
||||
{
|
||||
// Adjust the next delta.
|
||||
if (timer.next != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
ptimers[timer.next].delta += timer.delta;
|
||||
}
|
||||
}
|
||||
|
||||
timer.previous = etl::timer::id::NO_TIMER;
|
||||
timer.next = etl::timer::id::NO_TIMER;
|
||||
timer.delta = etl::timer::state::Inactive;
|
||||
}
|
||||
|
||||
//*******************************
|
||||
timer_data& front()
|
||||
{
|
||||
return ptimers[head];
|
||||
}
|
||||
|
||||
//*******************************
|
||||
const timer_data& front() const
|
||||
{
|
||||
return ptimers[head];
|
||||
}
|
||||
|
||||
//*******************************
|
||||
etl::timer::id::type begin()
|
||||
{
|
||||
current = head;
|
||||
return current;
|
||||
}
|
||||
|
||||
//*******************************
|
||||
etl::timer::id::type previous(etl::timer::id::type last)
|
||||
{
|
||||
current = ptimers[last].previous;
|
||||
return current;
|
||||
}
|
||||
|
||||
//*******************************
|
||||
etl::timer::id::type next(etl::timer::id::type last)
|
||||
{
|
||||
current = ptimers[last].next;
|
||||
return current;
|
||||
}
|
||||
|
||||
//*******************************
|
||||
void clear()
|
||||
{
|
||||
etl::timer::id::type id = begin();
|
||||
|
||||
while (id != etl::timer::id::NO_TIMER)
|
||||
{
|
||||
timer_data& timer = ptimers[id];
|
||||
id = next(id);
|
||||
timer.next = etl::timer::id::NO_TIMER;
|
||||
}
|
||||
|
||||
head = etl::timer::id::NO_TIMER;
|
||||
tail = etl::timer::id::NO_TIMER;
|
||||
current = etl::timer::id::NO_TIMER;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
etl::timer::id::type head;
|
||||
etl::timer::id::type tail;
|
||||
etl::timer::id::type current;
|
||||
|
||||
timer_data* const ptimers;
|
||||
};
|
||||
|
||||
//*******************************************
|
||||
/// Check that the timer id is valid.
|
||||
//*******************************************
|
||||
bool is_valid_timer_id(etl::timer::id::type id_) const
|
||||
{
|
||||
return (id_ < MAX_TIMERS);
|
||||
}
|
||||
|
||||
// The array of timer data structures.
|
||||
timer_data* const timer_array;
|
||||
|
||||
// The list of active timers.
|
||||
timer_list active_list;
|
||||
|
||||
bool enabled;
|
||||
uint_least8_t number_of_registered_timers;
|
||||
|
||||
try_lock_type try_lock; ///< The callback that tries to lock.
|
||||
lock_type lock; ///< The callback that locks.
|
||||
unlock_type unlock; ///< The callback that unlocks.
|
||||
|
||||
public:
|
||||
|
||||
const uint_least8_t MAX_TIMERS;
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// The callback timer
|
||||
//***************************************************************************
|
||||
template <uint_least8_t MAX_TIMERS_>
|
||||
class callback_timer_locked : public etl::icallback_timer_locked
|
||||
{
|
||||
public:
|
||||
|
||||
ETL_STATIC_ASSERT(MAX_TIMERS_ <= 254U, "No more than 254 timers are allowed");
|
||||
|
||||
typedef icallback_timer_locked::callback_type callback_type;
|
||||
typedef icallback_timer_locked::try_lock_type try_lock_type;
|
||||
typedef icallback_timer_locked::lock_type lock_type;
|
||||
typedef icallback_timer_locked::unlock_type unlock_type;
|
||||
|
||||
//*******************************************
|
||||
/// Constructor.
|
||||
//*******************************************
|
||||
callback_timer_locked()
|
||||
: icallback_timer_locked(timer_array, MAX_TIMERS_)
|
||||
{
|
||||
}
|
||||
|
||||
//*******************************************
|
||||
/// Constructor.
|
||||
//*******************************************
|
||||
callback_timer_locked(try_lock_type try_lock_, lock_type lock_, unlock_type unlock_)
|
||||
: icallback_timer_locked(timer_array, MAX_TIMERS_)
|
||||
{
|
||||
this->set_locks(try_lock_, lock_, unlock_);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
timer_data timer_array[MAX_TIMERS_];
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
387
Chapter18/cib/libs/etl/include/etl/char_traits.h
Normal file
387
Chapter18/cib/libs/etl/include/etl/char_traits.h
Normal file
@@ -0,0 +1,387 @@
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2016 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_CHAR_TRAITS_INCLUDED
|
||||
#define ETL_CHAR_TRAITS_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "algorithm.h"
|
||||
#include "iterator.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
//*****************************************************************************
|
||||
///\defgroup char_traits char_traits
|
||||
/// Character traits
|
||||
///\ingroup string
|
||||
//*****************************************************************************
|
||||
|
||||
namespace etl
|
||||
{
|
||||
template<typename T> struct char_traits_types;
|
||||
|
||||
template<> struct char_traits_types<char>
|
||||
{
|
||||
typedef char char_type;
|
||||
typedef int int_type;
|
||||
typedef long long off_type;
|
||||
typedef size_t pos_type;
|
||||
typedef char state_type;
|
||||
};
|
||||
|
||||
template<> struct char_traits_types<signed char>
|
||||
{
|
||||
typedef signed char char_type;
|
||||
typedef int int_type;
|
||||
typedef long long off_type;
|
||||
typedef size_t pos_type;
|
||||
typedef signed char state_type;
|
||||
};
|
||||
|
||||
template<> struct char_traits_types<unsigned char>
|
||||
{
|
||||
typedef unsigned char char_type;
|
||||
typedef int int_type;
|
||||
typedef long long off_type;
|
||||
typedef size_t pos_type;
|
||||
typedef unsigned char state_type;
|
||||
};
|
||||
|
||||
template<> struct char_traits_types<wchar_t>
|
||||
{
|
||||
typedef wchar_t char_type;
|
||||
typedef uint_least16_t int_type;
|
||||
typedef long long off_type;
|
||||
typedef size_t pos_type;
|
||||
typedef char state_type;
|
||||
};
|
||||
|
||||
#if ETL_USING_CPP20
|
||||
template<> struct char_traits_types<char8_t>
|
||||
{
|
||||
typedef char8_t char_type;
|
||||
typedef unsigned int int_type;
|
||||
typedef long long off_type;
|
||||
typedef size_t pos_type;
|
||||
typedef char state_type;
|
||||
};
|
||||
#endif
|
||||
|
||||
template<> struct char_traits_types<char16_t>
|
||||
{
|
||||
typedef char16_t char_type;
|
||||
typedef uint_least16_t int_type;
|
||||
typedef long long off_type;
|
||||
typedef size_t pos_type;
|
||||
typedef char state_type;
|
||||
};
|
||||
|
||||
template<> struct char_traits_types<char32_t>
|
||||
{
|
||||
typedef char32_t char_type;
|
||||
typedef uint_least32_t int_type;
|
||||
typedef long long off_type;
|
||||
typedef size_t pos_type;
|
||||
typedef char state_type;
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Character traits for any character type.
|
||||
//***************************************************************************
|
||||
template<typename T>
|
||||
struct char_traits : public char_traits_types<T>
|
||||
{
|
||||
typedef typename char_traits_types<T>::char_type char_type;
|
||||
typedef typename char_traits_types<T>::int_type int_type;
|
||||
typedef typename char_traits_types<T>::off_type off_type;
|
||||
typedef typename char_traits_types<T>::pos_type pos_type;
|
||||
typedef typename char_traits_types<T>::state_type state_type;
|
||||
|
||||
//*************************************************************************
|
||||
static ETL_CONSTEXPR bool eq(char_type a, char_type b)
|
||||
{
|
||||
return a == b;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
static ETL_CONSTEXPR bool lt(char_type a, char_type b)
|
||||
{
|
||||
return a < b;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
static ETL_CONSTEXPR14 size_t length(const char_type* str)
|
||||
{
|
||||
size_t count = 0UL;
|
||||
|
||||
if (str != 0)
|
||||
{
|
||||
while (*str++ != 0)
|
||||
{
|
||||
++count;
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
static ETL_CONSTEXPR14 size_t length(const char_type* str, size_t max_length)
|
||||
{
|
||||
size_t count = 0UL;
|
||||
|
||||
if (str != 0)
|
||||
{
|
||||
while ((count < max_length) && (*str++ != 0))
|
||||
{
|
||||
++count;
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
static ETL_CONSTEXPR14 void assign(char_type& r, const char_type& c)
|
||||
{
|
||||
r = c;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
static ETL_CONSTEXPR14 char_type* assign(char_type* p, size_t n, char_type c)
|
||||
{
|
||||
if (p != ETL_NULLPTR)
|
||||
{
|
||||
etl::fill_n(p, n, c);
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
static ETL_CONSTEXPR14 char_type* move(char_type* dst, const char_type* src, size_t count)
|
||||
{
|
||||
if ((dst < src) || (dst > (src + count)))
|
||||
{
|
||||
etl::copy_n(src, count, dst);
|
||||
}
|
||||
else
|
||||
{
|
||||
etl::copy_n(ETL_OR_STD::reverse_iterator<const char_type*>(src + count),
|
||||
count,
|
||||
ETL_OR_STD::reverse_iterator<char_type*>(dst + count));
|
||||
}
|
||||
|
||||
return dst;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
static ETL_CONSTEXPR14 char_type* copy(char_type* dst, const char_type* src, size_t count)
|
||||
{
|
||||
etl::copy_n(src, count, dst);
|
||||
|
||||
return dst;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
static ETL_CONSTEXPR14 int compare(const char_type* s1, const char_type* s2, size_t count)
|
||||
{
|
||||
for (size_t i = 0UL; i < count; ++i)
|
||||
{
|
||||
const char_type c1 = *s1++;
|
||||
const char_type c2 = *s2++;
|
||||
|
||||
if (c1 < c2)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else if (c1 > c2)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
static ETL_CONSTEXPR14 const char_type* find(const char_type* p, size_t count, const char_type& ch)
|
||||
{
|
||||
for (size_t i = 0UL; i < count; ++i)
|
||||
{
|
||||
if (*p == ch)
|
||||
{
|
||||
return p;
|
||||
}
|
||||
|
||||
++p;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
static ETL_CONSTEXPR char_type to_char_type(int_type c)
|
||||
{
|
||||
return static_cast<char_type>(c);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
static ETL_CONSTEXPR int_type to_int_type(char_type c)
|
||||
{
|
||||
return static_cast<int_type>(c);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
static ETL_CONSTEXPR bool eq_int_type(int_type c1, int_type c2)
|
||||
{
|
||||
return (c1 == c2);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
static ETL_CONSTEXPR int_type eof()
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
static ETL_CONSTEXPR int_type not_eof(int_type e)
|
||||
{
|
||||
return (e == eof()) ? eof() - 1 : e;
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Alternative strlen for all character types.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
ETL_CONSTEXPR14 size_t strlen(const T* t)
|
||||
{
|
||||
return etl::char_traits<T>::length(t);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Alternative strlen for all character types, with maximum length.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
ETL_CONSTEXPR14 size_t strlen(const T* t, size_t max_length)
|
||||
{
|
||||
return etl::char_traits<T>::length(t, max_length);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Alternative strcmp for all character types.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
ETL_CONSTEXPR14 int strcmp(const T* t1, const T* t2)
|
||||
{
|
||||
while ((*t1 != 0) || (*t2 != 0))
|
||||
{
|
||||
if (*t1 > *t2)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (*t1 < *t2)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
++t1;
|
||||
++t2;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Alternative strncmp for all character types.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
ETL_CONSTEXPR14 int strncmp(const T* t1, const T* t2, size_t n)
|
||||
{
|
||||
while (((*t1 != 0) || (*t2 != 0)) && (n != 0))
|
||||
{
|
||||
if (*t1 < *t2)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else if (*t1 > *t2)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
++t1;
|
||||
++t2;
|
||||
--n;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Alternative strcpy for all character types.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
ETL_CONSTEXPR14 T* strcpy(T* dst, const T* src)
|
||||
{
|
||||
T* result = dst;
|
||||
|
||||
while (*src != 0)
|
||||
{
|
||||
*dst++ = *src++;
|
||||
}
|
||||
|
||||
*dst = 0;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Alternative strncpy for all character types.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
ETL_CONSTEXPR14 T* strncpy(T* dst, const T* src, size_t n)
|
||||
{
|
||||
T* result = dst;
|
||||
|
||||
while ((*src != 0) && (n != 0))
|
||||
{
|
||||
*dst++ = *src++;
|
||||
--n;
|
||||
}
|
||||
|
||||
*dst = 0;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
307
Chapter18/cib/libs/etl/include/etl/checksum.h
Normal file
307
Chapter18/cib/libs/etl/include/etl/checksum.h
Normal file
@@ -0,0 +1,307 @@
|
||||
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
Copyright(c) 2014 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_CHECKSUM_INCLUDED
|
||||
#define ETL_CHECKSUM_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "binary.h"
|
||||
#include "frame_check_sequence.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
///\defgroup checksum Checksum calculation
|
||||
///\ingroup maths
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
/// Standard addition checksum policy.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
struct checksum_policy_sum
|
||||
{
|
||||
typedef T value_type;
|
||||
|
||||
T initial() const
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
T add(T sum, uint8_t value) const
|
||||
{
|
||||
return sum + value;
|
||||
}
|
||||
|
||||
T final(T sum) const
|
||||
{
|
||||
return sum;
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// BSD checksum policy.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
struct checksum_policy_bsd
|
||||
{
|
||||
typedef T value_type;
|
||||
|
||||
T initial() const
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
T add(T sum, uint8_t value) const
|
||||
{
|
||||
return etl::rotate_right(sum) + value;
|
||||
}
|
||||
|
||||
T final(T sum) const
|
||||
{
|
||||
return sum;
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Standard XOR checksum policy.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
struct checksum_policy_xor
|
||||
{
|
||||
typedef T value_type;
|
||||
|
||||
T initial() const
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
T add(T sum, uint8_t value) const
|
||||
{
|
||||
return sum ^ value;
|
||||
}
|
||||
|
||||
T final(T sum) const
|
||||
{
|
||||
return sum;
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// XOR-rotate checksum policy.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
struct checksum_policy_xor_rotate
|
||||
{
|
||||
typedef T value_type;
|
||||
|
||||
T initial() const
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
T add(T sum, uint8_t value) const
|
||||
{
|
||||
return etl::rotate_left(sum) ^ value;
|
||||
}
|
||||
|
||||
T final(T sum) const
|
||||
{
|
||||
return sum;
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Parity checksum policy.
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
struct checksum_policy_parity
|
||||
{
|
||||
typedef T value_type;
|
||||
|
||||
T initial() const
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
T add(T sum, uint8_t value) const
|
||||
{
|
||||
return sum ^ etl::parity(value);
|
||||
}
|
||||
|
||||
T final(T sum) const
|
||||
{
|
||||
return sum;
|
||||
}
|
||||
};
|
||||
|
||||
//*************************************************************************
|
||||
/// Standard Checksum.
|
||||
//*************************************************************************
|
||||
template <typename T>
|
||||
class checksum : public etl::frame_check_sequence<etl::checksum_policy_sum<T> >
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
checksum()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
checksum(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
|
||||
//*************************************************************************
|
||||
/// BSD Checksum.
|
||||
//*************************************************************************
|
||||
template <typename T>
|
||||
class bsd_checksum : public etl::frame_check_sequence<etl::checksum_policy_bsd<T> >
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
bsd_checksum()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
bsd_checksum(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
|
||||
//*************************************************************************
|
||||
/// XOR Checksum.
|
||||
//*************************************************************************
|
||||
template <typename T>
|
||||
class xor_checksum : public etl::frame_check_sequence<etl::checksum_policy_xor<T> >
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
xor_checksum()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
xor_checksum(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
|
||||
//*************************************************************************
|
||||
/// XOR-shift Checksum.
|
||||
//*************************************************************************
|
||||
template <typename T>
|
||||
class xor_rotate_checksum : public etl::frame_check_sequence<etl::checksum_policy_xor_rotate<T> >
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
xor_rotate_checksum()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
xor_rotate_checksum(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
|
||||
//*************************************************************************
|
||||
/// Parity Checksum.
|
||||
//*************************************************************************
|
||||
template <typename T>
|
||||
class parity_checksum : public etl::frame_check_sequence<etl::checksum_policy_parity<T> >
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
parity_checksum()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
parity_checksum(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
1461
Chapter18/cib/libs/etl/include/etl/circular_buffer.h
Normal file
1461
Chapter18/cib/libs/etl/include/etl/circular_buffer.h
Normal file
File diff suppressed because it is too large
Load Diff
753
Chapter18/cib/libs/etl/include/etl/circular_iterator.h
Normal file
753
Chapter18/cib/libs/etl/include/etl/circular_iterator.h
Normal file
@@ -0,0 +1,753 @@
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2022 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_CIRCULAR_ITERATOR_INCLUDED
|
||||
#define ETL_CIRCULAR_ITERATOR_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "iterator.h"
|
||||
#include "static_assert.h"
|
||||
|
||||
///\defgroup iterator Iterator types
|
||||
|
||||
namespace etl
|
||||
{
|
||||
namespace private_circular_iterator
|
||||
{
|
||||
//***************************************************************************
|
||||
/// Common circular iterator implementation.
|
||||
//***************************************************************************
|
||||
template <typename TIterator>
|
||||
class circular_iterator_common
|
||||
: public etl::iterator<typename etl::iterator_traits<TIterator>::iterator_category, typename etl::iterator_traits<TIterator>::value_type>
|
||||
{
|
||||
public:
|
||||
|
||||
typedef typename etl::iterator_traits<TIterator>::value_type value_type;
|
||||
typedef typename etl::iterator_traits<TIterator>::difference_type difference_type;
|
||||
typedef typename etl::iterator_traits<TIterator>::pointer pointer;
|
||||
typedef typename etl::iterator_traits<TIterator>::reference reference;
|
||||
typedef typename etl::iterator_traits<TIterator>::iterator_category iterator_category;
|
||||
|
||||
//***************************************************************************
|
||||
/// Default constructor.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 circular_iterator_common()
|
||||
: itr_begin(TIterator())
|
||||
, itr_end(TIterator())
|
||||
, itr(TIterator())
|
||||
{
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Construct from iterators.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 circular_iterator_common(TIterator itr_begin_, TIterator itr_end_, TIterator start_)
|
||||
: itr_begin(itr_begin_)
|
||||
, itr_end(itr_end_)
|
||||
, itr(start_)
|
||||
{
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Copy constructor
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 circular_iterator_common(const circular_iterator_common& other)
|
||||
: itr_begin(other.itr_begin)
|
||||
, itr_end(other.itr_end)
|
||||
, itr(other.itr)
|
||||
{
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Assignment
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 circular_iterator_common& operator =(const circular_iterator_common& other)
|
||||
{
|
||||
itr_begin = other.itr_begin;
|
||||
itr_end = other.itr_end;
|
||||
itr = other.itr;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Beginning of the range.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 TIterator begin() const
|
||||
{
|
||||
return itr_begin;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// End of the range.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 TIterator end() const
|
||||
{
|
||||
return itr_end;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// How long is the range?
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 size_t size() const
|
||||
{
|
||||
return etl::distance(itr_begin, itr_end);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Is there nothing to iterate over?
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 bool empty() const
|
||||
{
|
||||
return (itr_begin == itr_end);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Dereference operator.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 value_type operator *()
|
||||
{
|
||||
return *itr;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Dereference operator.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 const value_type operator *() const
|
||||
{
|
||||
return *itr;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// -> operator.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 TIterator operator ->()
|
||||
{
|
||||
return itr;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// -> operator.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 const TIterator operator ->() const
|
||||
{
|
||||
return itr;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Conversion operator.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 operator TIterator() const
|
||||
{
|
||||
return itr;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Conversion to base iterator type.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 TIterator current() const
|
||||
{
|
||||
return itr;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
TIterator itr_begin; ///< The underlying begin iterator.
|
||||
TIterator itr_end; ///< The underlying end iterator.
|
||||
TIterator itr; ///< The underlying iterator.
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// A circular iterator class.
|
||||
/// This iterator can be given a pair of iterator values, which will loop if the start or end of the range is reached.
|
||||
///\ingroup iterator
|
||||
//***************************************************************************
|
||||
template <typename TIterator, typename TTag = typename etl::iterator_traits<TIterator>::iterator_category>
|
||||
class circular_iterator_impl
|
||||
{
|
||||
ETL_STATIC_ASSERT((etl::is_same<TTag, ETL_OR_STD::input_iterator_tag>::value_type), "input_iterator_catagory is not supported by circular_iterator");
|
||||
ETL_STATIC_ASSERT((etl::is_same<TTag, ETL_OR_STD::output_iterator_tag>::value_type), "output_iterator_catagory is not supported by circular_iterator");
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// A circular iterator class.
|
||||
/// Specialisation for forward iterators.
|
||||
///\ingroup iterator
|
||||
/// //***************************************************************************
|
||||
template <typename TIterator>
|
||||
class circular_iterator_impl<TIterator, ETL_OR_STD::forward_iterator_tag>
|
||||
: public circular_iterator_common<TIterator>
|
||||
{
|
||||
private:
|
||||
|
||||
typedef circular_iterator_common<TIterator> common_t;
|
||||
|
||||
public:
|
||||
|
||||
using common_t::operator=;
|
||||
|
||||
typedef typename common_t::value_type value_type;
|
||||
typedef typename common_t::difference_type difference_type;
|
||||
typedef typename common_t::pointer pointer;
|
||||
typedef typename common_t::reference reference;
|
||||
typedef typename common_t::iterator_category iterator_category;
|
||||
|
||||
//***************************************************************************
|
||||
/// Default constructor.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 circular_iterator_impl()
|
||||
: common_t()
|
||||
{
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Construct from iterators.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 circular_iterator_impl(TIterator itr_begin_, TIterator itr_end_)
|
||||
: common_t(itr_begin_, itr_end_, itr_begin_)
|
||||
{
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Construct from start + iterators.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 circular_iterator_impl(TIterator itr_begin_, TIterator itr_end_, TIterator start_)
|
||||
: common_t(itr_begin_, itr_end_, start_)
|
||||
{
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Copy constructor
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 circular_iterator_impl(const circular_iterator_impl& other)
|
||||
: common_t(other)
|
||||
{
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Assignment
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 circular_iterator_impl& operator =(const circular_iterator_impl& other)
|
||||
{
|
||||
common_t::operator=(other);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Increment.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 circular_iterator_impl& operator ++()
|
||||
{
|
||||
if (++this->itr == this->itr_end)
|
||||
{
|
||||
this->itr = this->itr_begin;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Increment.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 circular_iterator_impl operator ++(int)
|
||||
{
|
||||
circular_iterator_impl original(*this);
|
||||
|
||||
++(*this);
|
||||
|
||||
return (original);
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// A circular iterator class.
|
||||
/// Specialisation for random access iterators.
|
||||
///\ingroup iterator
|
||||
/// //***************************************************************************
|
||||
template <typename TIterator>
|
||||
class circular_iterator_impl<TIterator, ETL_OR_STD::bidirectional_iterator_tag>
|
||||
: public circular_iterator_common<TIterator>
|
||||
{
|
||||
private:
|
||||
|
||||
typedef circular_iterator_common<TIterator> common_t;
|
||||
|
||||
public:
|
||||
|
||||
using common_t::operator=;
|
||||
|
||||
typedef typename common_t::value_type value_type;
|
||||
typedef typename common_t::difference_type difference_type;
|
||||
typedef typename common_t::pointer pointer;
|
||||
typedef typename common_t::reference reference;
|
||||
typedef typename common_t::iterator_category iterator_category;
|
||||
|
||||
//***************************************************************************
|
||||
/// Default constructor.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 circular_iterator_impl()
|
||||
: common_t()
|
||||
{
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Construct from iterators.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 circular_iterator_impl(TIterator itr_begin_, TIterator itr_end_)
|
||||
: common_t(itr_begin_, itr_end_, itr_begin_)
|
||||
{
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Construct from start + iterators.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 circular_iterator_impl(TIterator itr_begin_, TIterator itr_end_, TIterator start_)
|
||||
: common_t(itr_begin_, itr_end_, start_)
|
||||
{
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Copy constructor
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 circular_iterator_impl(const circular_iterator_impl& other)
|
||||
: common_t(other)
|
||||
{
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Assignment
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 circular_iterator_impl& operator =(const circular_iterator_impl& other)
|
||||
{
|
||||
common_t::operator=(other);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Increment.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 circular_iterator_impl& operator ++()
|
||||
{
|
||||
if (++this->itr == this->itr_end)
|
||||
{
|
||||
this->itr = this->itr_begin;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Increment.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 circular_iterator_impl operator ++(int)
|
||||
{
|
||||
circular_iterator_impl original(*this);
|
||||
|
||||
++(*this);
|
||||
|
||||
return (original);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Decrement.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 circular_iterator_impl& operator --()
|
||||
{
|
||||
if (this->itr == this->itr_begin)
|
||||
{
|
||||
typename etl::reverse_iterator<TIterator> ritr(this->itr_end);
|
||||
++ritr;
|
||||
this->itr = ritr.base();
|
||||
}
|
||||
else
|
||||
{
|
||||
--this->itr;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Decrement.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 circular_iterator_impl operator --(int)
|
||||
{
|
||||
circular_iterator_impl original(*this);
|
||||
|
||||
--(*this);
|
||||
|
||||
return (original);
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// A circular iterator class.
|
||||
/// Specialisation for random access iterators.
|
||||
///\ingroup iterator
|
||||
//***************************************************************************
|
||||
template <typename TIterator>
|
||||
class circular_iterator_impl<TIterator, ETL_OR_STD::random_access_iterator_tag>
|
||||
: public etl::private_circular_iterator::circular_iterator_common<TIterator>
|
||||
{
|
||||
private:
|
||||
|
||||
typedef etl::private_circular_iterator::circular_iterator_common<TIterator> common_t;
|
||||
|
||||
public:
|
||||
|
||||
using common_t::operator=;
|
||||
|
||||
typedef typename common_t::value_type value_type;
|
||||
typedef typename common_t::difference_type difference_type;
|
||||
typedef typename common_t::pointer pointer;
|
||||
typedef typename common_t::reference reference;
|
||||
typedef typename common_t::iterator_category iterator_category;
|
||||
|
||||
//***************************************************************************
|
||||
/// Default constructor.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 circular_iterator_impl()
|
||||
: common_t()
|
||||
{
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Construct from iterators.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 circular_iterator_impl(TIterator itr_begin_, TIterator itr_end_)
|
||||
: common_t(itr_begin_, itr_end_, itr_begin_)
|
||||
{
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Construct from start + iterators.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 circular_iterator_impl(TIterator itr_begin_, TIterator itr_end_, TIterator start_)
|
||||
: common_t(itr_begin_, itr_end_, start_)
|
||||
{
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Copy constructor
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 circular_iterator_impl(const circular_iterator_impl& other)
|
||||
: common_t(other)
|
||||
{
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Assignment
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 circular_iterator_impl& operator =(const circular_iterator_impl& other)
|
||||
{
|
||||
common_t::operator=(other);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Increment.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 circular_iterator_impl& operator ++()
|
||||
{
|
||||
if (++this->itr == this->itr_end)
|
||||
{
|
||||
this->itr = this->itr_begin;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Increment.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 circular_iterator_impl operator ++(int)
|
||||
{
|
||||
circular_iterator_impl original(*this);
|
||||
|
||||
++(*this);
|
||||
|
||||
return (original);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Decrement.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 circular_iterator_impl& operator --()
|
||||
{
|
||||
if (this->itr == this->itr_begin)
|
||||
{
|
||||
typename etl::reverse_iterator<TIterator> ritr(this->itr_end);
|
||||
++ritr;
|
||||
this->itr = ritr.base();
|
||||
}
|
||||
else
|
||||
{
|
||||
--this->itr;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Decrement.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 circular_iterator_impl operator --(int)
|
||||
{
|
||||
circular_iterator_impl original(*this);
|
||||
|
||||
--(*this);
|
||||
|
||||
return (original);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// += operator.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 circular_iterator_impl& operator +=(difference_type offset)
|
||||
{
|
||||
const difference_type length = difference_type(this->size());
|
||||
offset %= length;
|
||||
|
||||
if (offset != 0)
|
||||
{
|
||||
const difference_type distance_from_begin = etl::distance(this->itr_begin, this->itr);
|
||||
const difference_type distance_to_end = etl::distance(this->itr, this->itr_end);
|
||||
|
||||
if (offset > 0)
|
||||
{
|
||||
if (distance_to_end > offset)
|
||||
{
|
||||
offset = distance_from_begin + offset;
|
||||
}
|
||||
else
|
||||
{
|
||||
offset = offset - distance_to_end;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
offset = -offset;
|
||||
|
||||
if (distance_from_begin >= offset)
|
||||
{
|
||||
offset = distance_from_begin - offset;
|
||||
}
|
||||
else
|
||||
{
|
||||
offset = offset - distance_from_begin;
|
||||
offset = length - offset;
|
||||
}
|
||||
}
|
||||
|
||||
this->itr = this->itr_begin + offset;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// -= operator.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 circular_iterator_impl& operator -=(typename etl::iterator_traits<TIterator>::difference_type offset)
|
||||
{
|
||||
return operator +=(-offset);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// A circular iterator class.
|
||||
/// This iterator can be given a pair of iterator values, which will loop if the start or end of the range is reached.
|
||||
///\ingroup iterator
|
||||
//**************************************************************************
|
||||
template <typename TIterator>
|
||||
class circular_iterator ETL_FINAL
|
||||
: public etl::private_circular_iterator::circular_iterator_impl<TIterator, typename etl::iterator_traits<TIterator>::iterator_category>
|
||||
{
|
||||
private:
|
||||
|
||||
typedef typename etl::private_circular_iterator::circular_iterator_impl<TIterator, typename etl::iterator_traits<TIterator>::iterator_category> impl_t;
|
||||
|
||||
public:
|
||||
|
||||
using impl_t::operator=;
|
||||
|
||||
typedef typename impl_t::value_type value_type;
|
||||
typedef typename impl_t::difference_type difference_type;
|
||||
typedef typename impl_t::pointer pointer;
|
||||
typedef typename impl_t::reference reference;
|
||||
typedef typename impl_t::iterator_category iterator_category;
|
||||
|
||||
//***************************************************************************
|
||||
/// Default constructor.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 circular_iterator()
|
||||
: impl_t()
|
||||
{
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Construct from iterators.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 circular_iterator(TIterator itr_begin_, TIterator itr_end_)
|
||||
: impl_t(itr_begin_, itr_end_, itr_begin_)
|
||||
{
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Construct from start + iterators.
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 circular_iterator(TIterator itr_begin_, TIterator itr_end_, TIterator start_)
|
||||
: impl_t(itr_begin_, itr_end_, start_)
|
||||
{
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Copy constructor
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 circular_iterator(const circular_iterator& other)
|
||||
: impl_t(other)
|
||||
{
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Assignment
|
||||
//***************************************************************************
|
||||
ETL_CONSTEXPR14 circular_iterator& operator =(const circular_iterator& other)
|
||||
{
|
||||
impl_t::operator=(other);
|
||||
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
//*****************************************************************************
|
||||
/// + addition operator.
|
||||
//*****************************************************************************
|
||||
template <typename TIterator>
|
||||
ETL_CONSTEXPR14 etl::circular_iterator<TIterator> operator +(etl::circular_iterator<TIterator>& lhs,
|
||||
typename etl::iterator_traits<TIterator>::difference_type offset)
|
||||
{
|
||||
etl::circular_iterator<TIterator> result(lhs);
|
||||
result += offset;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
/// - offset operator.
|
||||
//*****************************************************************************
|
||||
template <typename TIterator>
|
||||
ETL_CONSTEXPR14 etl::circular_iterator<TIterator> operator -(etl::circular_iterator<TIterator>& lhs,
|
||||
typename etl::iterator_traits<TIterator>::difference_type offset)
|
||||
{
|
||||
etl::circular_iterator<TIterator> result(lhs);
|
||||
result -= offset;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
/// - circular_iterator difference operator.
|
||||
//*****************************************************************************
|
||||
template <typename TIterator>
|
||||
ETL_CONSTEXPR14 typename etl::iterator_traits<TIterator>::difference_type operator -(etl::circular_iterator<TIterator>& lhs,
|
||||
etl::circular_iterator<TIterator>& rhs)
|
||||
{
|
||||
return TIterator(lhs) - TIterator(rhs);
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
/// Equality operator. circular_iterator == circular_iterator.
|
||||
//*****************************************************************************
|
||||
template <typename TIterator>
|
||||
ETL_CONSTEXPR14 bool operator ==(const etl::circular_iterator<TIterator>& lhs,
|
||||
const etl::circular_iterator<TIterator>& rhs)
|
||||
{
|
||||
return TIterator(lhs) == TIterator(rhs);
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
/// Equality operator. circular_iterator == iterator.
|
||||
//*****************************************************************************
|
||||
template <typename TIterator>
|
||||
ETL_CONSTEXPR14 bool operator ==(const etl::circular_iterator<TIterator>& lhs,
|
||||
TIterator rhs)
|
||||
{
|
||||
return TIterator(lhs) == rhs;
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
/// Equality operator. iterator == circular_iterator.
|
||||
//*****************************************************************************
|
||||
template <typename TIterator>
|
||||
ETL_CONSTEXPR14 bool operator ==(TIterator lhs,
|
||||
const etl::circular_iterator<TIterator>& rhs)
|
||||
{
|
||||
return lhs == TIterator(rhs);
|
||||
}
|
||||
|
||||
|
||||
//*****************************************************************************
|
||||
/// Inequality operator. circular_iterator == circular_iterator.
|
||||
//*****************************************************************************
|
||||
template <typename TIterator>
|
||||
ETL_CONSTEXPR14 bool operator !=(const etl::circular_iterator<TIterator>& lhs,
|
||||
const etl::circular_iterator<TIterator>& rhs)
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
/// Inequality operator. circular_iterator == iterator.
|
||||
//*****************************************************************************
|
||||
template <typename TIterator>
|
||||
ETL_CONSTEXPR14 bool operator !=(const etl::circular_iterator<TIterator>& lhs,
|
||||
TIterator rhs)
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
/// Inequality operator. iterator == circular_iterator.
|
||||
//*****************************************************************************
|
||||
template <typename TIterator>
|
||||
ETL_CONSTEXPR14 bool operator !=(TIterator& lhs,
|
||||
const etl::circular_iterator<TIterator>& rhs)
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
63
Chapter18/cib/libs/etl/include/etl/combinations.h
Normal file
63
Chapter18/cib/libs/etl/include/etl/combinations.h
Normal file
@@ -0,0 +1,63 @@
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2018 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_COMBINATIONS_INCLUDED
|
||||
#define ETL_COMBINATIONS_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "permutations.h"
|
||||
#include "factorial.h"
|
||||
|
||||
///\defgroup combinations combinations
|
||||
/// combinations<N, K> : Calculates K combinations from N.
|
||||
///\ingroup maths
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
///\ingroup combinations
|
||||
/// Calculates combinations.
|
||||
//***************************************************************************
|
||||
template <size_t NV, size_t KV>
|
||||
struct combinations
|
||||
{
|
||||
static ETL_CONSTANT size_t value = etl::permutations<NV, KV>::value / etl::factorial<KV>::value;
|
||||
};
|
||||
|
||||
template <size_t NV, size_t KV>
|
||||
ETL_CONSTANT size_t combinations<NV, KV>::value;
|
||||
|
||||
#if ETL_USING_CPP17
|
||||
template <size_t NV, size_t KV>
|
||||
inline constexpr size_t combinations_v = combinations<NV, KV>::value;
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
112
Chapter18/cib/libs/etl/include/etl/compare.h
Normal file
112
Chapter18/cib/libs/etl/include/etl/compare.h
Normal file
@@ -0,0 +1,112 @@
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2017 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_COMPARE_INCLUDED
|
||||
#define ETL_COMPARE_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "parameter_type.h"
|
||||
#include "functional.h"
|
||||
|
||||
//*****************************************************************************
|
||||
///\defgroup compare compare
|
||||
/// Comparisons only using less than operator
|
||||
///\ingroup utilities
|
||||
//*****************************************************************************
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
/// Defines <=, >, >=, ==, !=, <=> in terms of <
|
||||
/// Default implementation of TLess is etl::less
|
||||
//***************************************************************************
|
||||
template <typename T, typename TLess = etl::less<T> >
|
||||
struct compare
|
||||
{
|
||||
enum cmp_result
|
||||
{
|
||||
Less = -1,
|
||||
Equal = 0,
|
||||
Greater = 1
|
||||
};
|
||||
|
||||
typedef typename etl::parameter_type<T>::type first_argument_type;
|
||||
typedef typename etl::parameter_type<T>::type second_argument_type;
|
||||
|
||||
static ETL_CONSTEXPR bool lt(first_argument_type lhs, second_argument_type rhs)
|
||||
{
|
||||
return TLess()(lhs, rhs);
|
||||
}
|
||||
|
||||
static ETL_CONSTEXPR bool gt(first_argument_type lhs, second_argument_type rhs)
|
||||
{
|
||||
return TLess()(rhs, lhs);
|
||||
}
|
||||
|
||||
static ETL_CONSTEXPR bool lte(first_argument_type lhs, second_argument_type rhs)
|
||||
{
|
||||
return !gt(lhs, rhs);
|
||||
}
|
||||
|
||||
static ETL_CONSTEXPR bool gte(first_argument_type lhs, second_argument_type rhs)
|
||||
{
|
||||
return !lt(lhs, rhs);
|
||||
}
|
||||
|
||||
static ETL_CONSTEXPR bool eq(first_argument_type lhs, second_argument_type rhs)
|
||||
{
|
||||
return gte(lhs, rhs) && lte(lhs, rhs);
|
||||
}
|
||||
|
||||
static ETL_CONSTEXPR bool ne(first_argument_type lhs, second_argument_type rhs)
|
||||
{
|
||||
return !eq(lhs, rhs);
|
||||
}
|
||||
|
||||
static ETL_CONSTEXPR cmp_result cmp(first_argument_type lhs, second_argument_type rhs)
|
||||
{
|
||||
return lt(lhs, rhs) ? Less : gt(lhs, rhs) ? Greater : Equal;
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Default implementation of TLess is etl::less
|
||||
//***************************************************************************
|
||||
#if ETL_USING_CPP11
|
||||
template <typename T, typename TLess = etl::less<T> >
|
||||
#else
|
||||
template <typename T, typename TLess>
|
||||
#endif
|
||||
ETL_CONSTEXPR14 int three_way_compare(const T& lhs, const T& rhs)
|
||||
{
|
||||
return etl::compare<T>::cmp(lhs, rhs);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
57
Chapter18/cib/libs/etl/include/etl/constant.h
Normal file
57
Chapter18/cib/libs/etl/include/etl/constant.h
Normal file
@@ -0,0 +1,57 @@
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2017 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_CONSTANT_INCLUDED
|
||||
#define ETL_CONSTANT_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
|
||||
//*****************************************************************************
|
||||
///\defgroup constant constant
|
||||
///\ingroup utilities
|
||||
//*****************************************************************************
|
||||
|
||||
namespace etl
|
||||
{
|
||||
template <typename T, const T VALUE>
|
||||
class constant
|
||||
{
|
||||
public:
|
||||
|
||||
typedef T value_type;
|
||||
|
||||
static ETL_CONSTANT T value = VALUE;
|
||||
};
|
||||
|
||||
template <typename T, const T VALUE>
|
||||
const T constant<T, VALUE>::value;
|
||||
}
|
||||
|
||||
#endif
|
||||
40
Chapter18/cib/libs/etl/include/etl/container.h
Normal file
40
Chapter18/cib/libs/etl/include/etl/container.h
Normal file
@@ -0,0 +1,40 @@
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2014 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_CONTAINER_INCLUDED
|
||||
#define ETL_CONTAINER_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "iterator.h"
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#endif
|
||||
|
||||
292
Chapter18/cib/libs/etl/include/etl/correlation.h
Normal file
292
Chapter18/cib/libs/etl/include/etl/correlation.h
Normal file
@@ -0,0 +1,292 @@
|
||||
///\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_CORRELATION_INCLUDED
|
||||
#define ETL_CORRELATION_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "functional.h"
|
||||
#include "type_traits.h"
|
||||
|
||||
#include <math.h>
|
||||
#include <stdint.h>
|
||||
|
||||
namespace etl
|
||||
{
|
||||
namespace private_correlation
|
||||
{
|
||||
//***************************************************************************
|
||||
/// Types for generic correlation.
|
||||
//***************************************************************************
|
||||
template <typename TInput, typename TCalc>
|
||||
struct correlation_traits
|
||||
{
|
||||
typedef TCalc calc_t;
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Types for float correlation.
|
||||
//***************************************************************************
|
||||
template <typename TCalc>
|
||||
struct correlation_traits<float, TCalc>
|
||||
{
|
||||
typedef float calc_t;
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Types for double correlation.
|
||||
//***************************************************************************
|
||||
template <typename TCalc>
|
||||
struct correlation_traits<double, TCalc>
|
||||
{
|
||||
typedef double calc_t;
|
||||
};
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Correlation Type.
|
||||
//***************************************************************************
|
||||
namespace private_correlation
|
||||
{
|
||||
template<typename T = void>
|
||||
struct correlation_type_statics
|
||||
{
|
||||
static ETL_CONSTANT bool Sample = false;
|
||||
static ETL_CONSTANT bool Population = true;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
ETL_CONSTANT bool correlation_type_statics<T>::Sample;
|
||||
|
||||
template<typename T>
|
||||
ETL_CONSTANT bool correlation_type_statics<T>::Population;
|
||||
}
|
||||
|
||||
struct correlation_type : public private_correlation::correlation_type_statics<>
|
||||
{
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Correlation.
|
||||
//***************************************************************************
|
||||
template <bool Correlation_Type, typename TInput, typename TCalc = TInput>
|
||||
class correlation
|
||||
: public private_correlation::correlation_traits<TInput, TCalc>
|
||||
, public etl::binary_function<TInput, TInput, void>
|
||||
{
|
||||
private:
|
||||
|
||||
static ETL_CONSTANT int Adjustment = (Correlation_Type == correlation_type::Population) ? 0 : 1;
|
||||
|
||||
typedef typename private_correlation::correlation_traits<TInput, TCalc>::calc_t calc_t;
|
||||
|
||||
public:
|
||||
|
||||
//*********************************
|
||||
/// Constructor.
|
||||
//*********************************
|
||||
correlation()
|
||||
{
|
||||
clear();
|
||||
}
|
||||
|
||||
//*********************************
|
||||
/// Constructor.
|
||||
//*********************************
|
||||
template <typename TIterator>
|
||||
correlation(TIterator first1, TIterator last1, TIterator first2)
|
||||
{
|
||||
clear();
|
||||
add(first1, last1, first2);
|
||||
}
|
||||
|
||||
//*********************************
|
||||
/// Add a pair of values.
|
||||
//*********************************
|
||||
void add(TInput value1, TInput value2)
|
||||
{
|
||||
inner_product += TCalc(value1 * value2);
|
||||
sum_of_squares1 += TCalc(value1 * value1);
|
||||
sum_of_squares2 += TCalc(value2 * value2);
|
||||
sum1 += TCalc(value1);
|
||||
sum2 += TCalc(value2);
|
||||
++counter;
|
||||
recalculate = true;
|
||||
}
|
||||
|
||||
//*********************************
|
||||
/// Add a range.
|
||||
//*********************************
|
||||
template <typename TIterator>
|
||||
void add(TIterator first1, TIterator last1, TIterator first2)
|
||||
{
|
||||
while (first1 != last1)
|
||||
{
|
||||
add(*first1, *first2);
|
||||
++first1;
|
||||
++first2;
|
||||
}
|
||||
}
|
||||
|
||||
//*********************************
|
||||
/// operator ()
|
||||
/// Add a pair of values.
|
||||
//*********************************
|
||||
void operator ()(TInput value1, TInput value2)
|
||||
{
|
||||
add(value1, value2);
|
||||
}
|
||||
|
||||
//*********************************
|
||||
/// operator ()
|
||||
/// Add a range.
|
||||
//*********************************
|
||||
template <typename TIterator>
|
||||
void operator ()(TIterator first1, TIterator last1, TIterator first2)
|
||||
{
|
||||
add(first1, last1, first2);
|
||||
}
|
||||
|
||||
//*********************************
|
||||
/// Get the correlation.
|
||||
//*********************************
|
||||
double get_covariance() const
|
||||
{
|
||||
calculate();
|
||||
|
||||
return covariance_value;
|
||||
}
|
||||
|
||||
//*********************************
|
||||
/// Get the correlation.
|
||||
//*********************************
|
||||
double get_correlation() const
|
||||
{
|
||||
calculate();
|
||||
|
||||
return correlation_value;
|
||||
}
|
||||
|
||||
//*********************************
|
||||
/// Get the correlation.
|
||||
//*********************************
|
||||
operator double() const
|
||||
{
|
||||
return get_correlation();
|
||||
}
|
||||
|
||||
//*********************************
|
||||
/// Get the total number added entries.
|
||||
//*********************************
|
||||
size_t count() const
|
||||
{
|
||||
return size_t(counter);
|
||||
}
|
||||
|
||||
//*********************************
|
||||
/// Clear the correlation.
|
||||
//*********************************
|
||||
void clear()
|
||||
{
|
||||
inner_product = calc_t(0);
|
||||
sum_of_squares1 = calc_t(0);
|
||||
sum_of_squares2 = calc_t(0);
|
||||
sum1 = calc_t(0);
|
||||
sum2 = calc_t(0);
|
||||
counter = 0U;
|
||||
covariance_value = 0.0;
|
||||
correlation_value = 0.0;
|
||||
recalculate = true;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
//*********************************
|
||||
/// Do the calculation.
|
||||
//*********************************
|
||||
void calculate() const
|
||||
{
|
||||
if (recalculate)
|
||||
{
|
||||
correlation_value = 0.0;
|
||||
covariance_value = 0.0;
|
||||
|
||||
if (counter != 0)
|
||||
{
|
||||
double n = double(counter);
|
||||
double adjustment = 1.0 / (n * (n - Adjustment));
|
||||
|
||||
double square_of_sum1 = (sum1 * sum1);
|
||||
double square_of_sum2 = (sum2 * sum2);
|
||||
|
||||
double variance1 = ((n * sum_of_squares1) - square_of_sum1) * adjustment;
|
||||
double variance2 = ((n * sum_of_squares2) - square_of_sum2) * adjustment;
|
||||
|
||||
double stddev1 = 0.0;
|
||||
double stddev2 = 0.0;
|
||||
|
||||
if (variance1 > 0)
|
||||
{
|
||||
stddev1 = sqrt(variance1);
|
||||
}
|
||||
|
||||
if (variance2 > 0)
|
||||
{
|
||||
stddev2 = sqrt(variance2);
|
||||
}
|
||||
|
||||
covariance_value = ((n * inner_product) - (sum1 * sum2)) * adjustment;
|
||||
|
||||
if ((stddev1 > 0.0) && (stddev2 > 0.0))
|
||||
{
|
||||
correlation_value = covariance_value / (stddev1 * stddev2);
|
||||
}
|
||||
}
|
||||
|
||||
recalculate = false;
|
||||
}
|
||||
}
|
||||
|
||||
calc_t inner_product;
|
||||
calc_t sum_of_squares1;
|
||||
calc_t sum_of_squares2;
|
||||
calc_t sum1;
|
||||
calc_t sum2;
|
||||
uint32_t counter;
|
||||
mutable double covariance_value;
|
||||
mutable double correlation_value;
|
||||
mutable bool recalculate;
|
||||
};
|
||||
|
||||
template <bool Correlation_Type, typename TInput, typename TCalc>
|
||||
ETL_CONSTANT int correlation<Correlation_Type, TInput, TCalc>::Adjustment;
|
||||
}
|
||||
|
||||
#endif
|
||||
240
Chapter18/cib/libs/etl/include/etl/covariance.h
Normal file
240
Chapter18/cib/libs/etl/include/etl/covariance.h
Normal file
@@ -0,0 +1,240 @@
|
||||
///\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_COVARIANCE_INCLUDED
|
||||
#define ETL_COVARIANCE_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "functional.h"
|
||||
#include "type_traits.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
namespace etl
|
||||
{
|
||||
namespace private_covariance
|
||||
{
|
||||
//***************************************************************************
|
||||
/// Types for generic covariance.
|
||||
//***************************************************************************
|
||||
template <typename TInput, typename TCalc>
|
||||
struct covariance_traits
|
||||
{
|
||||
typedef TCalc calc_t;
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Types for float covariance.
|
||||
//***************************************************************************
|
||||
template <typename TCalc>
|
||||
struct covariance_traits<float, TCalc>
|
||||
{
|
||||
typedef float calc_t;
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Types for double covariance.
|
||||
//***************************************************************************
|
||||
template <typename TCalc>
|
||||
struct covariance_traits<double, TCalc>
|
||||
{
|
||||
typedef double calc_t;
|
||||
};
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Covariance Type.
|
||||
//***************************************************************************
|
||||
namespace private_covariance
|
||||
{
|
||||
template<typename T = void>
|
||||
struct covariance_type_statics
|
||||
{
|
||||
static ETL_CONSTANT bool Sample = false;
|
||||
static ETL_CONSTANT bool Population = true;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
ETL_CONSTANT bool covariance_type_statics<T>::Sample;
|
||||
|
||||
template<typename T>
|
||||
ETL_CONSTANT bool covariance_type_statics<T>::Population;
|
||||
}
|
||||
|
||||
struct covariance_type : public private_covariance::covariance_type_statics<>
|
||||
{
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Covariance.
|
||||
//***************************************************************************
|
||||
template <bool Covariance_Type, typename TInput, typename TCalc = TInput>
|
||||
class covariance
|
||||
: public private_covariance::covariance_traits<TInput, TCalc>
|
||||
, public etl::binary_function<TInput, TInput, void>
|
||||
{
|
||||
private:
|
||||
|
||||
static ETL_CONSTANT int Adjustment = (Covariance_Type == covariance_type::Population) ? 0 : 1;
|
||||
|
||||
typedef typename private_covariance::covariance_traits<TInput, TCalc>::calc_t calc_t;
|
||||
|
||||
public:
|
||||
|
||||
//*********************************
|
||||
/// Constructor.
|
||||
//*********************************
|
||||
covariance()
|
||||
{
|
||||
clear();
|
||||
}
|
||||
|
||||
//*********************************
|
||||
/// Constructor.
|
||||
//*********************************
|
||||
template <typename TIterator>
|
||||
covariance(TIterator first1, TIterator last1, TIterator first2)
|
||||
{
|
||||
clear();
|
||||
add(first1, last1, first2);
|
||||
}
|
||||
|
||||
//*********************************
|
||||
/// Add a pair of values.
|
||||
//*********************************
|
||||
void add(TInput value1, TInput value2)
|
||||
{
|
||||
inner_product += TCalc(value1 * value2);
|
||||
sum1 += TCalc(value1);
|
||||
sum2 += TCalc(value2);
|
||||
++counter;
|
||||
recalculate = true;
|
||||
}
|
||||
|
||||
//*********************************
|
||||
/// Add a range.
|
||||
//*********************************
|
||||
template <typename TIterator>
|
||||
void add(TIterator first1, TIterator last1, TIterator first2)
|
||||
{
|
||||
while (first1 != last1)
|
||||
{
|
||||
add(*first1, *first2);
|
||||
++first1;
|
||||
++first2;
|
||||
}
|
||||
}
|
||||
|
||||
//*********************************
|
||||
/// operator ()
|
||||
/// Add a pair of values.
|
||||
//*********************************
|
||||
void operator ()(TInput value1, TInput value2)
|
||||
{
|
||||
add(value1, value2);
|
||||
}
|
||||
|
||||
//*********************************
|
||||
/// operator ()
|
||||
/// Add a range.
|
||||
//*********************************
|
||||
template <typename TIterator>
|
||||
void operator ()(TIterator first1, TIterator last1, TIterator first2)
|
||||
{
|
||||
add(first1, last1, first2);
|
||||
}
|
||||
|
||||
//*********************************
|
||||
/// Get the covariance.
|
||||
//*********************************
|
||||
double get_covariance() const
|
||||
{
|
||||
if (recalculate)
|
||||
{
|
||||
covariance_value = 0.0;
|
||||
|
||||
if (counter != 0)
|
||||
{
|
||||
double n = double(counter);
|
||||
double adjustment = 1.0 / (n * (n - Adjustment));
|
||||
|
||||
covariance_value = ((n * inner_product) - (sum1 * sum2)) * adjustment;
|
||||
|
||||
recalculate = false;
|
||||
}
|
||||
}
|
||||
|
||||
return covariance_value;
|
||||
}
|
||||
|
||||
//*********************************
|
||||
/// Get the covariance.
|
||||
//*********************************
|
||||
operator double() const
|
||||
{
|
||||
return get_covariance();
|
||||
}
|
||||
|
||||
//*********************************
|
||||
/// Get the total number added entries.
|
||||
//*********************************
|
||||
size_t count() const
|
||||
{
|
||||
return size_t(counter);
|
||||
}
|
||||
|
||||
//*********************************
|
||||
/// Clear the covariance.
|
||||
//*********************************
|
||||
void clear()
|
||||
{
|
||||
inner_product = calc_t(0);
|
||||
sum1 = calc_t(0);
|
||||
sum2 = calc_t(0);
|
||||
counter = 0U;
|
||||
covariance_value = 0.0;
|
||||
recalculate = true;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
calc_t inner_product;
|
||||
calc_t sum1;
|
||||
calc_t sum2;
|
||||
uint32_t counter;
|
||||
mutable double covariance_value;
|
||||
mutable bool recalculate;
|
||||
};
|
||||
|
||||
template <bool Covariance_Type, typename TInput, typename TCalc>
|
||||
ETL_CONSTANT int covariance<Covariance_Type, TInput, TCalc>::Adjustment;
|
||||
}
|
||||
|
||||
#endif
|
||||
88
Chapter18/cib/libs/etl/include/etl/crc.h
Normal file
88
Chapter18/cib/libs/etl/include/etl/crc.h
Normal file
@@ -0,0 +1,88 @@
|
||||
///\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_CRC_INCLUDED
|
||||
#define ETL_CRC_INCLUDED
|
||||
|
||||
#include "crc1.h"
|
||||
|
||||
#include "crc8_ccitt.h"
|
||||
#include "crc8_cdma2000.h"
|
||||
#include "crc8_darc.h"
|
||||
#include "crc8_dvbs2.h"
|
||||
#include "crc8_ebu.h"
|
||||
#include "crc8_icode.h"
|
||||
#include "crc8_itu.h"
|
||||
#include "crc8_j1850_zero.h"
|
||||
#include "crc8_j1850.h"
|
||||
#include "crc8_maxim.h"
|
||||
#include "crc8_rohc.h"
|
||||
#include "crc8_wcdma.h"
|
||||
|
||||
#include "crc16.h"
|
||||
#include "crc16_a.h"
|
||||
#include "crc16_arc.h"
|
||||
#include "crc16_aug_ccitt.h"
|
||||
#include "crc16_buypass.h"
|
||||
#include "crc16_ccitt.h"
|
||||
#include "crc16_cdma2000.h"
|
||||
#include "crc16_dds110.h"
|
||||
#include "crc16_dectr.h"
|
||||
#include "crc16_dectx.h"
|
||||
#include "crc16_dnp.h"
|
||||
#include "crc16_en13757.h"
|
||||
#include "crc16_genibus.h"
|
||||
#include "crc16_kermit.h"
|
||||
#include "crc16_maxim.h"
|
||||
#include "crc16_mcrf4xx.h"
|
||||
#include "crc16_modbus.h"
|
||||
#include "crc16_profibus.h"
|
||||
#include "crc16_riello.h"
|
||||
#include "crc16_t10dif.h"
|
||||
#include "crc16_teledisk.h"
|
||||
#include "crc16_tms37157.h"
|
||||
#include "crc16_usb.h"
|
||||
#include "crc16_x25.h"
|
||||
#include "crc16_xmodem.h"
|
||||
#include "crc16_m17.h"
|
||||
|
||||
#include "crc32.h"
|
||||
#include "crc32_bzip2.h"
|
||||
#include "crc32_c.h"
|
||||
#include "crc32_d.h"
|
||||
#include "crc32_jamcrc.h"
|
||||
#include "crc32_mpeg2.h"
|
||||
#include "crc32_posix.h"
|
||||
#include "crc32_q.h"
|
||||
#include "crc32_xfer.h"
|
||||
|
||||
#include "crc64_ecma.h"
|
||||
|
||||
#endif
|
||||
105
Chapter18/cib/libs/etl/include/etl/crc1.h
Normal file
105
Chapter18/cib/libs/etl/include/etl/crc1.h
Normal file
@@ -0,0 +1,105 @@
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2023 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_CRC1_INCLUDED
|
||||
#define ETL_CRC1_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "frame_check_sequence.h"
|
||||
#include "binary.h"
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
/// fnv_1 policy.
|
||||
/// Calculates FNV1.
|
||||
//***************************************************************************
|
||||
struct crc1_policy
|
||||
{
|
||||
typedef uint8_t value_type;
|
||||
|
||||
enum
|
||||
{
|
||||
odd_parity = 1,
|
||||
even_parity = 0
|
||||
};
|
||||
|
||||
//*********************************
|
||||
value_type initial() const
|
||||
{
|
||||
return even_parity;
|
||||
}
|
||||
|
||||
//*********************************
|
||||
uint8_t add(int parity, uint8_t value) const
|
||||
{
|
||||
return parity ^ etl::parity(value);
|
||||
}
|
||||
|
||||
//*********************************
|
||||
uint8_t final(uint8_t parity) const
|
||||
{
|
||||
return parity;
|
||||
}
|
||||
};
|
||||
|
||||
class crc1 : public etl::frame_check_sequence<crc1_policy>
|
||||
{
|
||||
public:
|
||||
|
||||
enum
|
||||
{
|
||||
odd_parity = crc1_policy::odd_parity,
|
||||
even_parity = crc1_policy::even_parity
|
||||
};
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc1()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc1(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
78
Chapter18/cib/libs/etl/include/etl/crc16.h
Normal file
78
Chapter18/cib/libs/etl/include/etl/crc16.h
Normal file
@@ -0,0 +1,78 @@
|
||||
///\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_CRC16_INCLUDED
|
||||
#define ETL_CRC16_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup crc16 16 bit CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11 && !defined(ETL_CRC_FORCE_CPP03_IMPLEMENTATION)
|
||||
template <size_t Table_Size>
|
||||
using crc16_t = etl::crc_type<etl::private_crc::crc16_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc16_t : public etl::crc_type<etl::private_crc::crc16_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc16_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc16_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc16_t<256U> crc16_t256;
|
||||
typedef etl::crc16_t<16U> crc16_t16;
|
||||
typedef etl::crc16_t<4U> crc16_t4;
|
||||
typedef crc16_t256 crc16;
|
||||
}
|
||||
#endif
|
||||
78
Chapter18/cib/libs/etl/include/etl/crc16_a.h
Normal file
78
Chapter18/cib/libs/etl/include/etl/crc16_a.h
Normal file
@@ -0,0 +1,78 @@
|
||||
///\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_CRC16_A_INCLUDED
|
||||
#define ETL_CRC16_A_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup crc16_a 16 bit CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11 && !defined(ETL_CRC_FORCE_CPP03_IMPLEMENTATION)
|
||||
template <size_t Table_Size>
|
||||
using crc16_a_t = etl::crc_type<etl::private_crc::crc16_a_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc16_a_t : public etl::crc_type<etl::private_crc::crc16_a_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc16_a_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc16_a_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc16_a_t<256U> crc16_a_t256;
|
||||
typedef etl::crc16_a_t<16U> crc16_a_t16;
|
||||
typedef etl::crc16_a_t<4U> crc16_a_t4;
|
||||
typedef crc16_a_t256 crc16_a;
|
||||
}
|
||||
#endif
|
||||
78
Chapter18/cib/libs/etl/include/etl/crc16_arc.h
Normal file
78
Chapter18/cib/libs/etl/include/etl/crc16_arc.h
Normal file
@@ -0,0 +1,78 @@
|
||||
///\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_CRC16_ARC_INCLUDED
|
||||
#define ETL_CRC16_ARC_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup crc16_arc 16 bit CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11 && !defined(ETL_CRC_FORCE_CPP03_IMPLEMENTATION)
|
||||
template <size_t Table_Size>
|
||||
using crc16_arc_t = etl::crc_type<etl::private_crc::crc16_arc_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc16_arc_t : public etl::crc_type<etl::private_crc::crc16_arc_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc16_arc_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc16_arc_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc16_arc_t<256U> crc16_arc_t256;
|
||||
typedef etl::crc16_arc_t<16U> crc16_arc_t16;
|
||||
typedef etl::crc16_arc_t<4U> crc16_arc_t4;
|
||||
typedef crc16_arc_t256 crc16_arc;
|
||||
}
|
||||
#endif
|
||||
80
Chapter18/cib/libs/etl/include/etl/crc16_aug_ccitt.h
Normal file
80
Chapter18/cib/libs/etl/include/etl/crc16_aug_ccitt.h
Normal file
@@ -0,0 +1,80 @@
|
||||
///\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_CRC16_AUG_CCITT_INCLUDED
|
||||
#define ETL_CRC16_AUG_CCITT_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup crc16_aug_ccitt 16 bit CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11 && !defined(ETL_CRC_FORCE_CPP03_IMPLEMENTATION)
|
||||
template <size_t Table_Size>
|
||||
using crc16_aug_ccitt_t = etl::crc_type<etl::private_crc::crc16_aug_ccitt_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc16_aug_ccitt_t : public etl::crc_type<etl::private_crc::crc16_aug_ccitt_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc16_aug_ccitt_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc16_aug_ccitt_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc16_aug_ccitt_t<256U> crc16_aug_ccitt_t256;
|
||||
typedef etl::crc16_aug_ccitt_t<16U> crc16_aug_ccitt_t16;
|
||||
typedef etl::crc16_aug_ccitt_t<4U> crc16_aug_ccitt_t4;
|
||||
typedef crc16_aug_ccitt_t256 crc16_aug_ccitt;
|
||||
}
|
||||
#endif
|
||||
78
Chapter18/cib/libs/etl/include/etl/crc16_buypass.h
Normal file
78
Chapter18/cib/libs/etl/include/etl/crc16_buypass.h
Normal file
@@ -0,0 +1,78 @@
|
||||
///\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_CRC16_BUYPASS_INCLUDED
|
||||
#define ETL_CRC16_BUYPASS_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup crc16_buypass 16 bit CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11 && !defined(ETL_CRC_FORCE_CPP03_IMPLEMENTATION)
|
||||
template <size_t Table_Size>
|
||||
using crc16_buypass_t = etl::crc_type<etl::private_crc::crc16_buypass_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc16_buypass_t : public etl::crc_type<etl::private_crc::crc16_buypass_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc16_buypass_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc16_buypass_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc16_buypass_t<256U> crc16_buypass_t256;
|
||||
typedef etl::crc16_buypass_t<16U> crc16_buypass_t16;
|
||||
typedef etl::crc16_buypass_t<4U> crc16_buypass_t4;
|
||||
typedef crc16_buypass_t256 crc16_buypass;
|
||||
}
|
||||
#endif
|
||||
78
Chapter18/cib/libs/etl/include/etl/crc16_ccitt.h
Normal file
78
Chapter18/cib/libs/etl/include/etl/crc16_ccitt.h
Normal file
@@ -0,0 +1,78 @@
|
||||
///\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_CRC16_CCITT_INCLUDED
|
||||
#define ETL_CRC16_CCITT_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup crc16_ccitt 16 bit CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11 && !defined(ETL_CRC_FORCE_CPP03_IMPLEMENTATION)
|
||||
template <size_t Table_Size>
|
||||
using crc16_ccitt_t = etl::crc_type<etl::private_crc::crc16_ccitt_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc16_ccitt_t : public etl::crc_type<etl::private_crc::crc16_ccitt_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc16_ccitt_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc16_ccitt_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc16_ccitt_t<256U> crc16_ccitt_t256;
|
||||
typedef etl::crc16_ccitt_t<16U> crc16_ccitt_t16;
|
||||
typedef etl::crc16_ccitt_t<4U> crc16_ccitt_t4;
|
||||
typedef crc16_ccitt_t256 crc16_ccitt;
|
||||
}
|
||||
#endif
|
||||
78
Chapter18/cib/libs/etl/include/etl/crc16_cdma2000.h
Normal file
78
Chapter18/cib/libs/etl/include/etl/crc16_cdma2000.h
Normal file
@@ -0,0 +1,78 @@
|
||||
///\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_CRC16_CDMA2000_INCLUDED
|
||||
#define ETL_CRC16_CDMA2000_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup crc16_cdma2000 16 bit CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11 && !defined(ETL_CRC_FORCE_CPP03_IMPLEMENTATION)
|
||||
template <size_t Table_Size>
|
||||
using crc16_cdma2000_t = etl::crc_type<etl::private_crc::crc16_cdma2000_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc16_cdma2000_t : public etl::crc_type<etl::private_crc::crc16_cdma2000_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc16_cdma2000_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc16_cdma2000_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc16_cdma2000_t<256U> crc16_cdma2000_t256;
|
||||
typedef etl::crc16_cdma2000_t<16U> crc16_cdma2000_t16;
|
||||
typedef etl::crc16_cdma2000_t<4U> crc16_cdma2000_t4;
|
||||
typedef crc16_cdma2000_t256 crc16_cdma2000;
|
||||
}
|
||||
#endif
|
||||
78
Chapter18/cib/libs/etl/include/etl/crc16_dds110.h
Normal file
78
Chapter18/cib/libs/etl/include/etl/crc16_dds110.h
Normal file
@@ -0,0 +1,78 @@
|
||||
///\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_CRC16_DDS110_INCLUDED
|
||||
#define ETL_CRC16_DDS110_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup crc16_dds110 16 bit CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11 && !defined(ETL_CRC_FORCE_CPP03_IMPLEMENTATION)
|
||||
template <size_t Table_Size>
|
||||
using crc16_dds110_t = etl::crc_type<etl::private_crc::crc16_dds110_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc16_dds110_t : public etl::crc_type<etl::private_crc::crc16_dds110_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc16_dds110_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc16_dds110_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc16_dds110_t<256U> crc16_dds110_t256;
|
||||
typedef etl::crc16_dds110_t<16U> crc16_dds110_t16;
|
||||
typedef etl::crc16_dds110_t<4U> crc16_dds110_t4;
|
||||
typedef crc16_dds110_t256 crc16_dds110;
|
||||
}
|
||||
#endif
|
||||
78
Chapter18/cib/libs/etl/include/etl/crc16_dectr.h
Normal file
78
Chapter18/cib/libs/etl/include/etl/crc16_dectr.h
Normal file
@@ -0,0 +1,78 @@
|
||||
///\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_CRC16_DECT_R_INCLUDED
|
||||
#define ETL_CRC16_DECT_R_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup crc16_dectr 16 bit CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11 && !defined(ETL_CRC_FORCE_CPP03_IMPLEMENTATION)
|
||||
template <size_t Table_Size>
|
||||
using crc16_dect_r_t = etl::crc_type<etl::private_crc::crc16_dect_r_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc16_dect_r_t : public etl::crc_type<etl::private_crc::crc16_dect_r_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc16_dect_r_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc16_dect_r_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc16_dect_r_t<256U> crc16_dect_r_t256;
|
||||
typedef etl::crc16_dect_r_t<16U> crc16_dect_r_t16;
|
||||
typedef etl::crc16_dect_r_t<4U> crc16_dect_r_t4;
|
||||
typedef crc16_dect_r_t256 crc16_dectr;
|
||||
}
|
||||
#endif
|
||||
78
Chapter18/cib/libs/etl/include/etl/crc16_dectx.h
Normal file
78
Chapter18/cib/libs/etl/include/etl/crc16_dectx.h
Normal file
@@ -0,0 +1,78 @@
|
||||
///\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_CRC16_DECT_X_INCLUDED
|
||||
#define ETL_CRC16_DECT_X_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup crc16_dectx 16 bit CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11 && !defined(ETL_CRC_FORCE_CPP03_IMPLEMENTATION)
|
||||
template <size_t Table_Size>
|
||||
using crc16_dect_x_t = etl::crc_type<etl::private_crc::crc16_dect_x_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc16_dect_x_t : public etl::crc_type<etl::private_crc::crc16_dect_x_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc16_dect_x_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc16_dect_x_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc16_dect_x_t<256U> crc16_dect_x_t256;
|
||||
typedef etl::crc16_dect_x_t<16U> crc16_dect_x_t16;
|
||||
typedef etl::crc16_dect_x_t<4U> crc16_dect_x_t4;
|
||||
typedef crc16_dect_x_t256 crc16_dectx;
|
||||
}
|
||||
#endif
|
||||
78
Chapter18/cib/libs/etl/include/etl/crc16_dnp.h
Normal file
78
Chapter18/cib/libs/etl/include/etl/crc16_dnp.h
Normal file
@@ -0,0 +1,78 @@
|
||||
///\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_CRC16_DNP_INCLUDED
|
||||
#define ETL_CRC16_DNP_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup crc16_dnp 16 bit CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11 && !defined(ETL_CRC_FORCE_CPP03_IMPLEMENTATION)
|
||||
template <size_t Table_Size>
|
||||
using crc16_dnp_t = etl::crc_type<etl::private_crc::crc16_dnp_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc16_dnp_t : public etl::crc_type<etl::private_crc::crc16_dnp_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc16_dnp_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc16_dnp_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc16_dnp_t<256U> crc16_dnp_t256;
|
||||
typedef etl::crc16_dnp_t<16U> crc16_dnp_t16;
|
||||
typedef etl::crc16_dnp_t<4U> crc16_dnp_t4;
|
||||
typedef crc16_dnp_t256 crc16_dnp;
|
||||
}
|
||||
#endif
|
||||
78
Chapter18/cib/libs/etl/include/etl/crc16_en13757.h
Normal file
78
Chapter18/cib/libs/etl/include/etl/crc16_en13757.h
Normal file
@@ -0,0 +1,78 @@
|
||||
///\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_CRC16_EN13757_INCLUDED
|
||||
#define ETL_CRC16_EN13757_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup crc16_en13757 16 bit CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11 && !defined(ETL_CRC_FORCE_CPP03_IMPLEMENTATION)
|
||||
template <size_t Table_Size>
|
||||
using crc16_en13757_t = etl::crc_type<etl::private_crc::crc16_en13757_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc16_en13757_t : public etl::crc_type<etl::private_crc::crc16_en13757_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc16_en13757_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc16_en13757_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc16_en13757_t<256U> crc16_en13757_t256;
|
||||
typedef etl::crc16_en13757_t<16U> crc16_en13757_t16;
|
||||
typedef etl::crc16_en13757_t<4U> crc16_en13757_t4;
|
||||
typedef crc16_en13757_t256 crc16_en13757;
|
||||
}
|
||||
#endif
|
||||
78
Chapter18/cib/libs/etl/include/etl/crc16_genibus.h
Normal file
78
Chapter18/cib/libs/etl/include/etl/crc16_genibus.h
Normal file
@@ -0,0 +1,78 @@
|
||||
///\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_CRC16_GENIBUS_INCLUDED
|
||||
#define ETL_CRC16_GENIBUS_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup crc16_genibus 16 bit CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11 && !defined(ETL_CRC_FORCE_CPP03_IMPLEMENTATION)
|
||||
template <size_t Table_Size>
|
||||
using crc16_genibus_t = etl::crc_type<etl::private_crc::crc16_genibus_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc16_genibus_t : public etl::crc_type<etl::private_crc::crc16_genibus_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc16_genibus_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc16_genibus_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc16_genibus_t<256U> crc16_genibus_t256;
|
||||
typedef etl::crc16_genibus_t<16U> crc16_genibus_t16;
|
||||
typedef etl::crc16_genibus_t<4U> crc16_genibus_t4;
|
||||
typedef crc16_genibus_t256 crc16_genibus;
|
||||
}
|
||||
#endif
|
||||
78
Chapter18/cib/libs/etl/include/etl/crc16_kermit.h
Normal file
78
Chapter18/cib/libs/etl/include/etl/crc16_kermit.h
Normal file
@@ -0,0 +1,78 @@
|
||||
///\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_CRC16_KERMIT_INCLUDED
|
||||
#define ETL_CRC16_KERMIT_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup crc16_kermit 16 bit CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11 && !defined(ETL_CRC_FORCE_CPP03_IMPLEMENTATION)
|
||||
template <size_t Table_Size>
|
||||
using crc16_kermit_t = etl::crc_type<etl::private_crc::crc16_kermit_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc16_kermit_t : public etl::crc_type<etl::private_crc::crc16_kermit_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc16_kermit_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc16_kermit_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc16_kermit_t<256U> crc16_kermit_t256;
|
||||
typedef etl::crc16_kermit_t<16U> crc16_kermit_t16;
|
||||
typedef etl::crc16_kermit_t<4U> crc16_kermit_t4;
|
||||
typedef crc16_kermit_t256 crc16_kermit;
|
||||
}
|
||||
#endif
|
||||
78
Chapter18/cib/libs/etl/include/etl/crc16_m17.h
Normal file
78
Chapter18/cib/libs/etl/include/etl/crc16_m17.h
Normal file
@@ -0,0 +1,78 @@
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2023 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_CRC16_M17_INCLUDED
|
||||
#define ETL_CRC16_M17_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup crc16_m17 16 bit CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11 && !defined(ETL_CRC_FORCE_CPP03_IMPLEMENTATION)
|
||||
template <size_t Table_Size>
|
||||
using crc16_m17_t = etl::crc_type<etl::private_crc::crc16_m17_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc16_m17_t : public etl::crc_type<etl::private_crc::crc16_m17_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc16_m17_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc16_m17_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc16_m17_t<256U> crc16_m17_t256;
|
||||
typedef etl::crc16_m17_t<16U> crc16_m17_t16;
|
||||
typedef etl::crc16_m17_t<4U> crc16_m17_t4;
|
||||
typedef crc16_m17_t256 crc16_m17;
|
||||
}
|
||||
#endif
|
||||
78
Chapter18/cib/libs/etl/include/etl/crc16_maxim.h
Normal file
78
Chapter18/cib/libs/etl/include/etl/crc16_maxim.h
Normal file
@@ -0,0 +1,78 @@
|
||||
///\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_CRC16_MAXIM_INCLUDED
|
||||
#define ETL_CRC16_MAXIM_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
//\defgroup crc16_maxim 16 bit CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11 && !defined(ETL_CRC_FORCE_CPP03_IMPLEMENTATION)
|
||||
template <size_t Table_Size>
|
||||
using crc16_maxim_t = etl::crc_type<etl::private_crc::crc16_maxim_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc16_maxim_t : public etl::crc_type<etl::private_crc::crc16_maxim_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc16_maxim_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc16_maxim_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc16_maxim_t<256U> crc16_maxim_t256;
|
||||
typedef etl::crc16_maxim_t<16U> crc16_maxim_t16;
|
||||
typedef etl::crc16_maxim_t<4U> crc16_maxim_t4;
|
||||
typedef crc16_maxim_t256 crc16_maxim;
|
||||
}
|
||||
#endif
|
||||
78
Chapter18/cib/libs/etl/include/etl/crc16_mcrf4xx.h
Normal file
78
Chapter18/cib/libs/etl/include/etl/crc16_mcrf4xx.h
Normal file
@@ -0,0 +1,78 @@
|
||||
///\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_CRC16_MCRF4XX_INCLUDED
|
||||
#define ETL_CRC16_MCRF4XX_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup crc16_mcrf4xx 16 bit CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11 && !defined(ETL_CRC_FORCE_CPP03_IMPLEMENTATION)
|
||||
template <size_t Table_Size>
|
||||
using crc16_mcrf4xx_t = etl::crc_type<etl::private_crc::crc16_mcrf4xx_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc16_mcrf4xx_t : public etl::crc_type<etl::private_crc::crc16_mcrf4xx_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc16_mcrf4xx_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc16_mcrf4xx_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc16_mcrf4xx_t<256U> crc16_mcrf4xx_t256;
|
||||
typedef etl::crc16_mcrf4xx_t<16U> crc16_mcrf4xx_t16;
|
||||
typedef etl::crc16_mcrf4xx_t<4U> crc16_mcrf4xx_t4;
|
||||
typedef crc16_mcrf4xx_t256 crc16_mcrf4xx;
|
||||
}
|
||||
#endif
|
||||
78
Chapter18/cib/libs/etl/include/etl/crc16_modbus.h
Normal file
78
Chapter18/cib/libs/etl/include/etl/crc16_modbus.h
Normal file
@@ -0,0 +1,78 @@
|
||||
///\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_CRC16_MODBUS_INCLUDED
|
||||
#define ETL_CRC16_MODBUS_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
//\defgroup crc16_modbus 16 bit CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11 && !defined(ETL_CRC_FORCE_CPP03_IMPLEMENTATION)
|
||||
template <size_t Table_Size>
|
||||
using crc16_modbus_t = etl::crc_type<etl::private_crc::crc16_modbus_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc16_modbus_t : public etl::crc_type<etl::private_crc::crc16_modbus_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc16_modbus_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc16_modbus_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc16_modbus_t<256U> crc16_modbus_t256;
|
||||
typedef etl::crc16_modbus_t<16U> crc16_modbus_t16;
|
||||
typedef etl::crc16_modbus_t<4U> crc16_modbus_t4;
|
||||
typedef crc16_modbus_t256 crc16_modbus;
|
||||
}
|
||||
#endif
|
||||
78
Chapter18/cib/libs/etl/include/etl/crc16_profibus.h
Normal file
78
Chapter18/cib/libs/etl/include/etl/crc16_profibus.h
Normal file
@@ -0,0 +1,78 @@
|
||||
///\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_CRC16_PROFIBUS_INCLUDED
|
||||
#define ETL_CRC16_PROFIBUS_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup crc16_profibus 16 bit CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11 && !defined(ETL_CRC_FORCE_CPP03_IMPLEMENTATION)
|
||||
template <size_t Table_Size>
|
||||
using crc16_profibus_t = etl::crc_type<etl::private_crc::crc16_profibus_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc16_profibus_t : public etl::crc_type<etl::private_crc::crc16_profibus_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc16_profibus_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc16_profibus_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc16_profibus_t<256U> crc16_profibus_t256;
|
||||
typedef etl::crc16_profibus_t<16U> crc16_profibus_t16;
|
||||
typedef etl::crc16_profibus_t<4U> crc16_profibus_t4;
|
||||
typedef crc16_profibus_t256 crc16_profibus;
|
||||
}
|
||||
#endif
|
||||
78
Chapter18/cib/libs/etl/include/etl/crc16_riello.h
Normal file
78
Chapter18/cib/libs/etl/include/etl/crc16_riello.h
Normal file
@@ -0,0 +1,78 @@
|
||||
///\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_CRC16_RIELLO_INCLUDED
|
||||
#define ETL_CRC16_RIELLO_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup crc16_riello 16 bit CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11 && !defined(ETL_CRC_FORCE_CPP03_IMPLEMENTATION)
|
||||
template <size_t Table_Size>
|
||||
using crc16_riello_t = etl::crc_type<etl::private_crc::crc16_riello_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc16_riello_t : public etl::crc_type<etl::private_crc::crc16_riello_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc16_riello_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc16_riello_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc16_riello_t<256U> crc16_riello_t256;
|
||||
typedef etl::crc16_riello_t<16U> crc16_riello_t16;
|
||||
typedef etl::crc16_riello_t<4U> crc16_riello_t4;
|
||||
typedef crc16_riello_t256 crc16_riello;
|
||||
}
|
||||
#endif
|
||||
78
Chapter18/cib/libs/etl/include/etl/crc16_t10dif.h
Normal file
78
Chapter18/cib/libs/etl/include/etl/crc16_t10dif.h
Normal file
@@ -0,0 +1,78 @@
|
||||
///\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_CRC16_T10DIF_INCLUDED
|
||||
#define ETL_CRC16_T10DIF_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup crc16_t10dif 16 bit CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11 && !defined(ETL_CRC_FORCE_CPP03_IMPLEMENTATION)
|
||||
template <size_t Table_Size>
|
||||
using crc16_t10dif_t = etl::crc_type<etl::private_crc::crc16_t10dif_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc16_t10dif_t : public etl::crc_type<etl::private_crc::crc16_t10dif_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc16_t10dif_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc16_t10dif_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc16_t10dif_t<256U> crc16_t10dif_t256;
|
||||
typedef etl::crc16_t10dif_t<16U> crc16_t10dif_t16;
|
||||
typedef etl::crc16_t10dif_t<4U> crc16_t10dif_t4;
|
||||
typedef crc16_t10dif_t256 crc16_t10dif;
|
||||
}
|
||||
#endif
|
||||
78
Chapter18/cib/libs/etl/include/etl/crc16_teledisk.h
Normal file
78
Chapter18/cib/libs/etl/include/etl/crc16_teledisk.h
Normal file
@@ -0,0 +1,78 @@
|
||||
///\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_CRC16_TELEDISK_INCLUDED
|
||||
#define ETL_CRC16_TELEDISK_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
//\defgroup crc16_teledisk 16 bit CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11 && !defined(ETL_CRC_FORCE_CPP03_IMPLEMENTATION)
|
||||
template <size_t Table_Size>
|
||||
using crc16_teledisk_t = etl::crc_type<etl::private_crc::crc16_teledisk_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc16_teledisk_t : public etl::crc_type<etl::private_crc::crc16_teledisk_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc16_teledisk_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc16_teledisk_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc16_teledisk_t<256U> crc16_teledisk_t256;
|
||||
typedef etl::crc16_teledisk_t<16U> crc16_teledisk_t16;
|
||||
typedef etl::crc16_teledisk_t<4U> crc16_teledisk_t4;
|
||||
typedef crc16_teledisk_t256 crc16_teledisk;
|
||||
}
|
||||
#endif
|
||||
78
Chapter18/cib/libs/etl/include/etl/crc16_tms37157.h
Normal file
78
Chapter18/cib/libs/etl/include/etl/crc16_tms37157.h
Normal file
@@ -0,0 +1,78 @@
|
||||
///\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_CRC16_TMS37157_INCLUDED
|
||||
#define ETL_CRC16_TMS37157_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
//\defgroup crc16_tms37157 16 bit CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11 && !defined(ETL_CRC_FORCE_CPP03_IMPLEMENTATION)
|
||||
template <size_t Table_Size>
|
||||
using crc16_tms37157_t = etl::crc_type<etl::private_crc::crc16_tms37157_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc16_tms37157_t : public etl::crc_type<etl::private_crc::crc16_tms37157_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc16_tms37157_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc16_tms37157_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc16_tms37157_t<256U> crc16_tms37157_t256;
|
||||
typedef etl::crc16_tms37157_t<16U> crc16_tms37157_t16;
|
||||
typedef etl::crc16_tms37157_t<4U> crc16_tms37157_t4;
|
||||
typedef crc16_tms37157_t256 crc16_tms37157;
|
||||
}
|
||||
#endif
|
||||
78
Chapter18/cib/libs/etl/include/etl/crc16_usb.h
Normal file
78
Chapter18/cib/libs/etl/include/etl/crc16_usb.h
Normal file
@@ -0,0 +1,78 @@
|
||||
///\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_CRC16_USB_INCLUDED
|
||||
#define ETL_CRC16_USB_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup crc16_usb 16 bit CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11 && !defined(ETL_CRC_FORCE_CPP03_IMPLEMENTATION)
|
||||
template <size_t Table_Size>
|
||||
using crc16_usb_t = etl::crc_type<etl::private_crc::crc16_usb_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc16_usb_t : public etl::crc_type<etl::private_crc::crc16_usb_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc16_usb_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc16_usb_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc16_usb_t<256U> crc16_usb_t256;
|
||||
typedef etl::crc16_usb_t<16U> crc16_usb_t16;
|
||||
typedef etl::crc16_usb_t<4U> crc16_usb_t4;
|
||||
typedef crc16_usb_t256 crc16_usb;
|
||||
}
|
||||
#endif
|
||||
78
Chapter18/cib/libs/etl/include/etl/crc16_x25.h
Normal file
78
Chapter18/cib/libs/etl/include/etl/crc16_x25.h
Normal file
@@ -0,0 +1,78 @@
|
||||
///\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_CRC16_X25_INCLUDED
|
||||
#define ETL_CRC16_X25_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup crc16_x25 16 bit CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11 && !defined(ETL_CRC_FORCE_CPP03_IMPLEMENTATION)
|
||||
template <size_t Table_Size>
|
||||
using crc16_x25_t = etl::crc_type<etl::private_crc::crc16_x25_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc16_x25_t : public etl::crc_type<etl::private_crc::crc16_x25_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc16_x25_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc16_x25_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc16_x25_t<256U> crc16_x25_t256;
|
||||
typedef etl::crc16_x25_t<16U> crc16_x25_t16;
|
||||
typedef etl::crc16_x25_t<4U> crc16_x25_t4;
|
||||
typedef crc16_x25_t256 crc16_x25;
|
||||
}
|
||||
#endif
|
||||
78
Chapter18/cib/libs/etl/include/etl/crc16_xmodem.h
Normal file
78
Chapter18/cib/libs/etl/include/etl/crc16_xmodem.h
Normal file
@@ -0,0 +1,78 @@
|
||||
///\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_CRC16_XMODEM_INCLUDED
|
||||
#define ETL_CRC16_XMODEM_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup crc16_xmodem 16 bit CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11
|
||||
template <size_t Table_Size>
|
||||
using crc16_xmodem_t = etl::crc_type<etl::private_crc::crc16_xmodem_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc16_xmodem_t : public etl::crc_type<etl::private_crc::crc16_xmodem_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc16_xmodem_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc16_xmodem_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc16_xmodem_t<256U> crc16_xmodem_t256;
|
||||
typedef etl::crc16_xmodem_t<16U> crc16_xmodem_t16;
|
||||
typedef etl::crc16_xmodem_t<4U> crc16_xmodem_t4;
|
||||
typedef crc16_xmodem_t256 crc16_xmodem;
|
||||
}
|
||||
#endif
|
||||
78
Chapter18/cib/libs/etl/include/etl/crc32.h
Normal file
78
Chapter18/cib/libs/etl/include/etl/crc32.h
Normal file
@@ -0,0 +1,78 @@
|
||||
///\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_CRC32_INCLUDED
|
||||
#define ETL_CRC32_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup crc32 32 bit CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11
|
||||
template <size_t Table_Size>
|
||||
using crc32_t = etl::crc_type<etl::private_crc::crc32_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc32_t : public etl::crc_type<etl::private_crc::crc32_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc32_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc32_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc32_t<256U> crc32_t256;
|
||||
typedef etl::crc32_t<16U> crc32_t16;
|
||||
typedef etl::crc32_t<4U> crc32_t4;
|
||||
typedef crc32_t256 crc32;
|
||||
}
|
||||
#endif
|
||||
78
Chapter18/cib/libs/etl/include/etl/crc32_bzip2.h
Normal file
78
Chapter18/cib/libs/etl/include/etl/crc32_bzip2.h
Normal file
@@ -0,0 +1,78 @@
|
||||
///\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_CRC32_BZIP2_INCLUDED
|
||||
#define ETL_CRC32_BZIP2_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup crc32_bzip2 32 bit CRC BZIP2 calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11
|
||||
template <size_t Table_Size>
|
||||
using crc32_bzip2_t = etl::crc_type<etl::private_crc::crc32_bzip2_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc32_bzip2_t : public etl::crc_type<etl::private_crc::crc32_bzip2_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc32_bzip2_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc32_bzip2_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc32_bzip2_t<256U> crc32_bzip2_t256;
|
||||
typedef etl::crc32_bzip2_t<16U> crc32_bzip2_t16;
|
||||
typedef etl::crc32_bzip2_t<4U> crc32_bzip2_t4;
|
||||
typedef crc32_bzip2_t256 crc32_bzip2;
|
||||
}
|
||||
#endif
|
||||
78
Chapter18/cib/libs/etl/include/etl/crc32_c.h
Normal file
78
Chapter18/cib/libs/etl/include/etl/crc32_c.h
Normal file
@@ -0,0 +1,78 @@
|
||||
///\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_CRC32_C_INCLUDED
|
||||
#define ETL_CRC32_C_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup crc32_c 32 bit CRC_C calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11
|
||||
template <size_t Table_Size>
|
||||
using crc32_c_t = etl::crc_type<etl::private_crc::crc32_c_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc32_c_t : public etl::crc_type<etl::private_crc::crc32_c_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc32_c_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc32_c_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc32_c_t<256U> crc32_c_t256;
|
||||
typedef etl::crc32_c_t<16U> crc32_c_t16;
|
||||
typedef etl::crc32_c_t<4U> crc32_c_t4;
|
||||
typedef crc32_c_t256 crc32_c;
|
||||
}
|
||||
#endif
|
||||
78
Chapter18/cib/libs/etl/include/etl/crc32_d.h
Normal file
78
Chapter18/cib/libs/etl/include/etl/crc32_d.h
Normal file
@@ -0,0 +1,78 @@
|
||||
///\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_CRC32_D_INCLUDED
|
||||
#define ETL_CRC32_D_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup crc32_d 32 bit CRC_D calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11
|
||||
template <size_t Table_Size>
|
||||
using crc32_d_t = etl::crc_type<etl::private_crc::crc32_d_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc32_d_t : public etl::crc_type<etl::private_crc::crc32_d_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc32_d_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc32_d_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc32_d_t<256U> crc32_d_t256;
|
||||
typedef etl::crc32_d_t<16U> crc32_d_t16;
|
||||
typedef etl::crc32_d_t<4U> crc32_d_t4;
|
||||
typedef crc32_d_t256 crc32_d;
|
||||
}
|
||||
#endif
|
||||
78
Chapter18/cib/libs/etl/include/etl/crc32_jamcrc.h
Normal file
78
Chapter18/cib/libs/etl/include/etl/crc32_jamcrc.h
Normal file
@@ -0,0 +1,78 @@
|
||||
///\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_CRC32_JAMCRC_INCLUDED
|
||||
#define ETL_CRC32_JAMCRC_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup crc32_jamcrc 32 bit CRC_JAMCRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11
|
||||
template <size_t Table_Size>
|
||||
using crc32_jamcrc_t = etl::crc_type<etl::private_crc::crc32_jamcrc_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc32_jamcrc_t : public etl::crc_type<etl::private_crc::crc32_jamcrc_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc32_jamcrc_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc32_jamcrc_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc32_jamcrc_t<256U> crc32_jamcrc_t256;
|
||||
typedef etl::crc32_jamcrc_t<16U> crc32_jamcrc_t16;
|
||||
typedef etl::crc32_jamcrc_t<4U> crc32_jamcrc_t4;
|
||||
typedef crc32_jamcrc_t256 crc32_jamcrc;
|
||||
}
|
||||
#endif
|
||||
78
Chapter18/cib/libs/etl/include/etl/crc32_mpeg2.h
Normal file
78
Chapter18/cib/libs/etl/include/etl/crc32_mpeg2.h
Normal file
@@ -0,0 +1,78 @@
|
||||
///\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_CRC32_MPEG2_INCLUDED
|
||||
#define ETL_CRC32_MPEG2_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup crc32_mpeg2 32 bit CRC MPEG2 calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11
|
||||
template <size_t Table_Size>
|
||||
using crc32_mpeg2_t = etl::crc_type<etl::private_crc::crc32_mpeg2_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc32_mpeg2_t : public etl::crc_type<etl::private_crc::crc32_mpeg2_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc32_mpeg2_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc32_mpeg2_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc32_mpeg2_t<256U> crc32_mpeg2_t256;
|
||||
typedef etl::crc32_mpeg2_t<16U> crc32_mpeg2_t16;
|
||||
typedef etl::crc32_mpeg2_t<4U> crc32_mpeg2_t4;
|
||||
typedef crc32_mpeg2_t256 crc32_mpeg2;
|
||||
}
|
||||
#endif
|
||||
78
Chapter18/cib/libs/etl/include/etl/crc32_posix.h
Normal file
78
Chapter18/cib/libs/etl/include/etl/crc32_posix.h
Normal file
@@ -0,0 +1,78 @@
|
||||
///\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_CRC32_POSIX_INCLUDED
|
||||
#define ETL_CRC32_POSIX_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup crc32_posix 32 bit CRC POSIX calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11
|
||||
template <size_t Table_Size>
|
||||
using crc32_posix_t = etl::crc_type<etl::private_crc::crc32_posix_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc32_posix_t : public etl::crc_type<etl::private_crc::crc32_posix_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc32_posix_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc32_posix_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc32_posix_t<256U> crc32_posix_t256;
|
||||
typedef etl::crc32_posix_t<16U> crc32_posix_t16;
|
||||
typedef etl::crc32_posix_t<4U> crc32_posix_t4;
|
||||
typedef crc32_posix_t256 crc32_posix;
|
||||
}
|
||||
#endif
|
||||
78
Chapter18/cib/libs/etl/include/etl/crc32_q.h
Normal file
78
Chapter18/cib/libs/etl/include/etl/crc32_q.h
Normal file
@@ -0,0 +1,78 @@
|
||||
///\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_CRC32_D_INCLUDED
|
||||
#define ETL_CRC32_D_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup crc32_q 32 bit CRC_Q calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11
|
||||
template <size_t Table_Size>
|
||||
using crc32_q_t = etl::crc_type<etl::private_crc::crc32_q_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc32_q_t : public etl::crc_type<etl::private_crc::crc32_q_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc32_q_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc32_q_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc32_q_t<256U> crc32_q_t256;
|
||||
typedef etl::crc32_q_t<16U> crc32_q_t16;
|
||||
typedef etl::crc32_q_t<4U> crc32_q_t4;
|
||||
typedef crc32_q_t256 crc32_q;
|
||||
}
|
||||
#endif
|
||||
78
Chapter18/cib/libs/etl/include/etl/crc32_xfer.h
Normal file
78
Chapter18/cib/libs/etl/include/etl/crc32_xfer.h
Normal file
@@ -0,0 +1,78 @@
|
||||
///\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_CRC32_XFER_INCLUDED
|
||||
#define ETL_CRC32_XFER_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup crc32_xfer 32 bit CRC_Q calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11
|
||||
template <size_t Table_Size>
|
||||
using crc32_xfer_t = etl::crc_type<etl::private_crc::crc32_xfer_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc32_xfer_t : public etl::crc_type<etl::private_crc::crc32_xfer_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc32_xfer_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc32_xfer_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc32_xfer_t<256U> crc32_xfer_t256;
|
||||
typedef etl::crc32_xfer_t<16U> crc32_xfer_t16;
|
||||
typedef etl::crc32_xfer_t<4U> crc32_xfer_t4;
|
||||
typedef crc32_xfer_t256 crc32_xfer;
|
||||
}
|
||||
#endif
|
||||
78
Chapter18/cib/libs/etl/include/etl/crc64_ecma.h
Normal file
78
Chapter18/cib/libs/etl/include/etl/crc64_ecma.h
Normal file
@@ -0,0 +1,78 @@
|
||||
///\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_CRC64_ECMA_INCLUDED
|
||||
#define ETL_CRC64_ECMA_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup crc64_ecma 64 bit ECMA CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11
|
||||
template <size_t Table_Size>
|
||||
using crc64_ecma_t = etl::crc_type<etl::private_crc::crc64_ecma_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc64_ecma_t : public etl::crc_type<etl::private_crc::crc64_ecma_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc64_ecma_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc64_ecma_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc64_ecma_t<256U> crc64_ecma_t256;
|
||||
typedef etl::crc64_ecma_t<16U> crc64_ecma_t16;
|
||||
typedef etl::crc64_ecma_t<4U> crc64_ecma_t4;
|
||||
typedef crc64_ecma_t256 crc64_ecma;
|
||||
}
|
||||
#endif
|
||||
76
Chapter18/cib/libs/etl/include/etl/crc8_ccitt.h
Normal file
76
Chapter18/cib/libs/etl/include/etl/crc8_ccitt.h
Normal file
@@ -0,0 +1,76 @@
|
||||
///\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_CRC8_CCITT_INCLUDED
|
||||
#define ETL_CRC8_CCITT_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11 && !defined(ETL_CRC_FORCE_CPP03_IMPLEMENTATION)
|
||||
template <size_t Table_Size>
|
||||
using crc8_ccitt_t = etl::crc_type<etl::private_crc::crc8_ccitt_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc8_ccitt_t : public etl::crc_type<etl::private_crc::crc8_ccitt_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc8_ccitt_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc8_ccitt_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef crc8_ccitt_t<256U> crc8_ccitt_t256;
|
||||
typedef crc8_ccitt_t<16U> crc8_ccitt_t16;
|
||||
typedef crc8_ccitt_t<4U> crc8_ccitt_t4;
|
||||
typedef crc8_ccitt_t256 crc8_ccitt;
|
||||
}
|
||||
|
||||
#endif
|
||||
79
Chapter18/cib/libs/etl/include/etl/crc8_cdma2000.h
Normal file
79
Chapter18/cib/libs/etl/include/etl/crc8_cdma2000.h
Normal file
@@ -0,0 +1,79 @@
|
||||
///\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_CRC8_CDMA2000_INCLUDED
|
||||
#define ETL_CRC8_CDMA2000_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup cdma2000 8 bit CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11 && !defined(ETL_CRC_FORCE_CPP03_IMPLEMENTATION)
|
||||
template <size_t Table_Size>
|
||||
using crc8_cdma2000_t = etl::crc_type<etl::private_crc::crc8_cdma2000_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc8_cdma2000_t : public etl::crc_type<etl::private_crc::crc8_cdma2000_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc8_cdma2000_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc8_cdma2000_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc8_cdma2000_t<256U> crc8_cdma2000_t256;
|
||||
typedef etl::crc8_cdma2000_t<16U> crc8_cdma2000_t16;
|
||||
typedef etl::crc8_cdma2000_t<4U> crc8_cdma2000_t4;
|
||||
typedef crc8_cdma2000_t256 crc8_cdma2000;
|
||||
}
|
||||
|
||||
#endif
|
||||
79
Chapter18/cib/libs/etl/include/etl/crc8_darc.h
Normal file
79
Chapter18/cib/libs/etl/include/etl/crc8_darc.h
Normal file
@@ -0,0 +1,79 @@
|
||||
///\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_CRC8_DARC_INCLUDED
|
||||
#define ETL_CRC8_DARC_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup darc 8 bit CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11 && !defined(ETL_CRC_FORCE_CPP03_IMPLEMENTATION)
|
||||
template <size_t Table_Size>
|
||||
using crc8_darc_t = etl::crc_type<etl::private_crc::crc8_darc_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc8_darc_t : public etl::crc_type<etl::private_crc::crc8_darc_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc8_darc_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc8_darc_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc8_darc_t<256U> crc8_darc_t256;
|
||||
typedef etl::crc8_darc_t<16U> crc8_darc_t16;
|
||||
typedef etl::crc8_darc_t<4U> crc8_darc_t4;
|
||||
typedef crc8_darc_t256 crc8_darc;
|
||||
}
|
||||
|
||||
#endif
|
||||
79
Chapter18/cib/libs/etl/include/etl/crc8_dvbs2.h
Normal file
79
Chapter18/cib/libs/etl/include/etl/crc8_dvbs2.h
Normal file
@@ -0,0 +1,79 @@
|
||||
///\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_CRC8_DVBS2_INCLUDED
|
||||
#define ETL_CRC8_DVBS2_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup dvbs2 8 bit CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11 && !defined(ETL_CRC_FORCE_CPP03_IMPLEMENTATION)
|
||||
template <size_t Table_Size>
|
||||
using crc8_dvbs2_t = etl::crc_type<etl::private_crc::crc8_dvbs2_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc8_dvbs2_t : public etl::crc_type<etl::private_crc::crc8_dvbs2_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc8_dvbs2_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc8_dvbs2_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc8_dvbs2_t<256U> crc8_dvbs2_t256;
|
||||
typedef etl::crc8_dvbs2_t<16U> crc8_dvbs2_t16;
|
||||
typedef etl::crc8_dvbs2_t<4U> crc8_dvbs2_t4;
|
||||
typedef crc8_dvbs2_t256 crc8_dvbs2;
|
||||
}
|
||||
|
||||
#endif
|
||||
79
Chapter18/cib/libs/etl/include/etl/crc8_ebu.h
Normal file
79
Chapter18/cib/libs/etl/include/etl/crc8_ebu.h
Normal file
@@ -0,0 +1,79 @@
|
||||
///\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_CRC8_EBU_INCLUDED
|
||||
#define ETL_CRC8_EBU_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup ebu 8 bit CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11 && !defined(ETL_CRC_FORCE_CPP03_IMPLEMENTATION)
|
||||
template <size_t Table_Size>
|
||||
using crc8_ebu_t = etl::crc_type<etl::private_crc::crc8_ebu_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc8_ebu_t : public etl::crc_type<etl::private_crc::crc8_ebu_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc8_ebu_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc8_ebu_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc8_ebu_t<256U> crc8_ebu_t256;
|
||||
typedef etl::crc8_ebu_t<16U> crc8_ebu_t16;
|
||||
typedef etl::crc8_ebu_t<4U> crc8_ebu_t4;
|
||||
typedef crc8_ebu_t256 crc8_ebu;
|
||||
}
|
||||
|
||||
#endif
|
||||
79
Chapter18/cib/libs/etl/include/etl/crc8_icode.h
Normal file
79
Chapter18/cib/libs/etl/include/etl/crc8_icode.h
Normal file
@@ -0,0 +1,79 @@
|
||||
///\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_CRC8_ICODE_INCLUDED
|
||||
#define ETL_CRC8_ICODE_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup icode 8 bit CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11 && !defined(ETL_CRC_FORCE_CPP03_IMPLEMENTATION)
|
||||
template <size_t Table_Size>
|
||||
using crc8_icode_t = etl::crc_type<etl::private_crc::crc8_icode_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc8_icode_t : public etl::crc_type<etl::private_crc::crc8_icode_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc8_icode_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc8_icode_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc8_icode_t<256U> crc8_icode_t256;
|
||||
typedef etl::crc8_icode_t<16U> crc8_icode_t16;
|
||||
typedef etl::crc8_icode_t<4U> crc8_icode_t4;
|
||||
typedef crc8_icode_t256 crc8_icode;
|
||||
}
|
||||
|
||||
#endif
|
||||
79
Chapter18/cib/libs/etl/include/etl/crc8_itu.h
Normal file
79
Chapter18/cib/libs/etl/include/etl/crc8_itu.h
Normal file
@@ -0,0 +1,79 @@
|
||||
///\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_CRC8_ITU_INCLUDED
|
||||
#define ETL_CRC8_ITU_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup itu 8 bit CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11 && !defined(ETL_CRC_FORCE_CPP03_IMPLEMENTATION)
|
||||
template <size_t Table_Size>
|
||||
using crc8_itu_t = etl::crc_type<etl::private_crc::crc8_itu_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc8_itu_t : public etl::crc_type<etl::private_crc::crc8_itu_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc8_itu_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc8_itu_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc8_itu_t<256U> crc8_itu_t256;
|
||||
typedef etl::crc8_itu_t<16U> crc8_itu_t16;
|
||||
typedef etl::crc8_itu_t<4U> crc8_itu_t4;
|
||||
typedef crc8_itu_t256 crc8_itu;
|
||||
}
|
||||
|
||||
#endif
|
||||
79
Chapter18/cib/libs/etl/include/etl/crc8_j1850.h
Normal file
79
Chapter18/cib/libs/etl/include/etl/crc8_j1850.h
Normal file
@@ -0,0 +1,79 @@
|
||||
///\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_CRC8_J1850_INCLUDED
|
||||
#define ETL_CRC8_J1850_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup SAE J1850 8 bit CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11 && !defined(ETL_CRC_FORCE_CPP03_IMPLEMENTATION)
|
||||
template <size_t Table_Size>
|
||||
using crc8_j1850_t = etl::crc_type<etl::private_crc::crc8_j1850_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc8_j1850_t : public etl::crc_type<etl::private_crc::crc8_j1850_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc8_j1850_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc8_j1850_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc8_j1850_t<256U> crc8_j1850_t256;
|
||||
typedef etl::crc8_j1850_t<16U> crc8_j1850_t16;
|
||||
typedef etl::crc8_j1850_t<4U> crc8_j1850_t4;
|
||||
typedef crc8_j1850_t256 crc8_j1850;
|
||||
}
|
||||
|
||||
#endif
|
||||
79
Chapter18/cib/libs/etl/include/etl/crc8_j1850_zero.h
Normal file
79
Chapter18/cib/libs/etl/include/etl/crc8_j1850_zero.h
Normal file
@@ -0,0 +1,79 @@
|
||||
///\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_CRC8_J1850_ZERO_INCLUDED
|
||||
#define ETL_CRC8_J1850_ZERO_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup SAE J1850 Zero 8 bit CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11 && !defined(ETL_CRC_FORCE_CPP03_IMPLEMENTATION)
|
||||
template <size_t Table_Size>
|
||||
using crc8_j1850_zero_t = etl::crc_type<etl::private_crc::crc8_j1850_zero_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc8_j1850_zero_t : public etl::crc_type<etl::private_crc::crc8_j1850_zero_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc8_j1850_zero_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc8_j1850_zero_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc8_j1850_zero_t<256U> crc8_j1850_zero_t256;
|
||||
typedef etl::crc8_j1850_zero_t<16U> crc8_j1850_zero_t16;
|
||||
typedef etl::crc8_j1850_zero_t<4U> crc8_j1850_zero_t4;
|
||||
typedef crc8_j1850_zero_t256 crc8_j1850_zero;
|
||||
}
|
||||
|
||||
#endif
|
||||
79
Chapter18/cib/libs/etl/include/etl/crc8_maxim.h
Normal file
79
Chapter18/cib/libs/etl/include/etl/crc8_maxim.h
Normal file
@@ -0,0 +1,79 @@
|
||||
///\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_CRC8_MAXIM_INCLUDED
|
||||
#define ETL_CRC8_MAXIM_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup maxim 8 bit CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11 && !defined(ETL_CRC_FORCE_CPP03_IMPLEMENTATION)
|
||||
template <size_t Table_Size>
|
||||
using crc8_maxim_t = etl::crc_type<etl::private_crc::crc8_maxim_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc8_maxim_t : public etl::crc_type<etl::private_crc::crc8_maxim_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc8_maxim_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc8_maxim_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc8_maxim_t<256U> crc8_maxim_t256;
|
||||
typedef etl::crc8_maxim_t<16U> crc8_maxim_t16;
|
||||
typedef etl::crc8_maxim_t<4U> crc8_maxim_t4;
|
||||
typedef crc8_maxim_t256 crc8_maxim;
|
||||
}
|
||||
|
||||
#endif
|
||||
79
Chapter18/cib/libs/etl/include/etl/crc8_rohc.h
Normal file
79
Chapter18/cib/libs/etl/include/etl/crc8_rohc.h
Normal file
@@ -0,0 +1,79 @@
|
||||
///\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_CRC8_ROHC_INCLUDED
|
||||
#define ETL_CRC8_ROHC_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup rohc 8 bit CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11 && !defined(ETL_CRC_FORCE_CPP03_IMPLEMENTATION)
|
||||
template <size_t Table_Size>
|
||||
using crc8_rohc_t = etl::crc_type<etl::private_crc::crc8_rohc_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc8_rohc_t : public etl::crc_type<etl::private_crc::crc8_rohc_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc8_rohc_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc8_rohc_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc8_rohc_t<256U> crc8_rohc_t256;
|
||||
typedef etl::crc8_rohc_t<16U> crc8_rohc_t16;
|
||||
typedef etl::crc8_rohc_t<4U> crc8_rohc_t4;
|
||||
typedef crc8_rohc_t256 crc8_rohc;
|
||||
}
|
||||
|
||||
#endif
|
||||
79
Chapter18/cib/libs/etl/include/etl/crc8_wcdma.h
Normal file
79
Chapter18/cib/libs/etl/include/etl/crc8_wcdma.h
Normal file
@@ -0,0 +1,79 @@
|
||||
///\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_CRC8_WCDMA_INCLUDED
|
||||
#define ETL_CRC8_WCDMA_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "private/crc_implementation.h"
|
||||
|
||||
///\defgroup wcdma 8 bit CRC calculation
|
||||
///\ingroup crc
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#if ETL_USING_CPP11 && !defined(ETL_CRC_FORCE_CPP03_IMPLEMENTATION)
|
||||
template <size_t Table_Size>
|
||||
using crc8_wcdma_t = etl::crc_type<etl::private_crc::crc8_wcdma_parameters, Table_Size>;
|
||||
#else
|
||||
template <size_t Table_Size>
|
||||
class crc8_wcdma_t : public etl::crc_type<etl::private_crc::crc8_wcdma_parameters, Table_Size>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
crc8_wcdma_t()
|
||||
{
|
||||
this->reset();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor from range.
|
||||
/// \param begin Start of the range.
|
||||
/// \param end End of the range.
|
||||
//*************************************************************************
|
||||
template<typename TIterator>
|
||||
crc8_wcdma_t(TIterator begin, const TIterator end)
|
||||
{
|
||||
this->reset();
|
||||
this->add(begin, end);
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef etl::crc8_wcdma_t<256U> crc8_wcdma_t256;
|
||||
typedef etl::crc8_wcdma_t<16U> crc8_wcdma_t16;
|
||||
typedef etl::crc8_wcdma_t<4U> crc8_wcdma_t4;
|
||||
typedef crc8_wcdma_t256 crc8_wcdma;
|
||||
}
|
||||
|
||||
#endif
|
||||
37
Chapter18/cib/libs/etl/include/etl/cstring.h
Normal file
37
Chapter18/cib/libs/etl/include/etl/cstring.h
Normal file
@@ -0,0 +1,37 @@
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2020 jwellbelove
|
||||
|
||||
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.
|
||||
******************************************************************************/
|
||||
|
||||
//*****************************************************************************
|
||||
// This header is deprecated. Use "string.h"
|
||||
//*****************************************************************************
|
||||
|
||||
#pragma message ("This header is deprecated. Use 'etl/string.h'")
|
||||
|
||||
#include "string.h"
|
||||
617
Chapter18/cib/libs/etl/include/etl/cyclic_value.h
Normal file
617
Chapter18/cib/libs/etl/include/etl/cyclic_value.h
Normal file
@@ -0,0 +1,617 @@
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2014 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_CYCLIC_VALUE_INCLUDED
|
||||
#define ETL_CYCLIC_VALUE_INCLUDED
|
||||
|
||||
///\defgroup cyclic_value cyclic_value
|
||||
/// Provides a value that cycles between two limits.
|
||||
/// \ingroup utilities
|
||||
|
||||
#include "platform.h"
|
||||
#include "static_assert.h"
|
||||
#include "exception.h"
|
||||
#include "static_assert.h"
|
||||
#include "type_traits.h"
|
||||
#include "algorithm.h"
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
/// Provides a value that cycles between two limits.
|
||||
//***************************************************************************
|
||||
template <typename T, T First = 0, T Last = 0, bool EtlRuntimeSpecialisation = ((First == 0) && (Last == 0))>
|
||||
class cyclic_value;
|
||||
|
||||
//***************************************************************************
|
||||
/// Provides a value that cycles between two compile time limits.
|
||||
/// Supports incrementing and decrementing.
|
||||
///\tparam T The type of the variable.
|
||||
///\tparam First The first value of the range.
|
||||
///\tparam Last The last value of the range.
|
||||
///\ingroup cyclic_value
|
||||
//***************************************************************************
|
||||
template <typename T, T First, T Last>
|
||||
class cyclic_value<T, First, Last, false>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
/// The initial value is set to the first value.
|
||||
//*************************************************************************
|
||||
cyclic_value()
|
||||
: value(First)
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor.
|
||||
/// Set to an initial value.
|
||||
/// Clamped to the range.
|
||||
//*************************************************************************
|
||||
explicit cyclic_value(T initial)
|
||||
{
|
||||
set(initial);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Copy constructor.
|
||||
//*************************************************************************
|
||||
cyclic_value(const cyclic_value<T, First, Last>& other)
|
||||
: value(other.value)
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Assignment operator.
|
||||
//*************************************************************************
|
||||
cyclic_value& operator =(const cyclic_value<T, First, Last>& other)
|
||||
{
|
||||
value = other.value;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Sets the value.
|
||||
/// Truncates to the First/Last range.
|
||||
///\param value The value.
|
||||
//*************************************************************************
|
||||
void set(T value_)
|
||||
{
|
||||
if (value_ > Last)
|
||||
{
|
||||
value_ = Last;
|
||||
}
|
||||
else if (value_ < First)
|
||||
{
|
||||
value_ = First;
|
||||
}
|
||||
|
||||
value = value_;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Resets the value to the first in the range.
|
||||
//*************************************************************************
|
||||
void to_first()
|
||||
{
|
||||
value = First;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Resets the value to the last in the range.
|
||||
//*************************************************************************
|
||||
void to_last()
|
||||
{
|
||||
value = Last;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Advances to value by a number of steps.
|
||||
///\param n The number of steps to advance.
|
||||
//*************************************************************************
|
||||
void advance(int n)
|
||||
{
|
||||
if (n > 0)
|
||||
{
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
operator ++();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < -n; ++i)
|
||||
{
|
||||
operator --();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Conversion operator.
|
||||
/// \return The value of the underlying type.
|
||||
//*************************************************************************
|
||||
operator T()
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Const conversion operator.
|
||||
/// \return The value of the underlying type.
|
||||
//*************************************************************************
|
||||
operator const T() const
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// ++ operator.
|
||||
//*************************************************************************
|
||||
cyclic_value& operator ++()
|
||||
{
|
||||
if (value >= Last) ETL_UNLIKELY
|
||||
{
|
||||
value = First;
|
||||
}
|
||||
else
|
||||
{
|
||||
++value;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// ++ operator.
|
||||
//*************************************************************************
|
||||
cyclic_value operator ++(int)
|
||||
{
|
||||
cyclic_value temp(*this);
|
||||
|
||||
operator++();
|
||||
|
||||
return temp;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// -- operator.
|
||||
//*************************************************************************
|
||||
cyclic_value& operator --()
|
||||
{
|
||||
if (value <= First) ETL_UNLIKELY
|
||||
{
|
||||
value = Last;
|
||||
}
|
||||
else
|
||||
{
|
||||
--value;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// -- operator.
|
||||
//*************************************************************************
|
||||
cyclic_value operator --(int)
|
||||
{
|
||||
cyclic_value temp(*this);
|
||||
|
||||
operator--();
|
||||
|
||||
return temp;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// = operator.
|
||||
//*************************************************************************
|
||||
cyclic_value& operator =(T t)
|
||||
{
|
||||
set(t);
|
||||
return *this;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// = operator.
|
||||
//*************************************************************************
|
||||
template <const T FIRST2, const T LAST2>
|
||||
cyclic_value& operator =(const cyclic_value<T, FIRST2, LAST2>& other)
|
||||
{
|
||||
set(other.get());
|
||||
return *this;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Gets the value.
|
||||
//*************************************************************************
|
||||
T get() const
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Gets the first value.
|
||||
//*************************************************************************
|
||||
static ETL_CONSTEXPR T first()
|
||||
{
|
||||
return First;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Gets the last value.
|
||||
//*************************************************************************
|
||||
static ETL_CONSTEXPR T last()
|
||||
{
|
||||
return Last;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Swaps the values.
|
||||
//*************************************************************************
|
||||
void swap(cyclic_value<T, First, Last>& other)
|
||||
{
|
||||
using ETL_OR_STD::swap; // Allow ADL
|
||||
|
||||
swap(value, other.value);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Swaps the values.
|
||||
//*************************************************************************
|
||||
friend void swap(cyclic_value<T, First, Last>& lhs, cyclic_value<T, First, Last>& rhs)
|
||||
{
|
||||
lhs.swap(rhs);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Operator ==.
|
||||
//*************************************************************************
|
||||
friend bool operator == (const cyclic_value<T, First, Last>& lhs, const cyclic_value<T, First, Last>& rhs)
|
||||
{
|
||||
return lhs.value == rhs.value;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Operator !=.
|
||||
//*************************************************************************
|
||||
friend bool operator != (const cyclic_value<T, First, Last>& lhs, const cyclic_value<T, First, Last>& rhs)
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
T value; ///< The current value.
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Provides a value that cycles between two run time limits.
|
||||
/// Supports incrementing and decrementing.
|
||||
///\tparam T The type of the variable.
|
||||
///\tparam First The first value of the range.
|
||||
///\tparam Last The last value of the range.
|
||||
///\ingroup cyclic_value
|
||||
//***************************************************************************
|
||||
template <typename T, T First, T Last>
|
||||
class cyclic_value<T, First, Last, true>
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor.
|
||||
/// Sets 'first' and 'last' to the template parameter values.
|
||||
/// The initial value is set to the first value.
|
||||
//*************************************************************************
|
||||
cyclic_value()
|
||||
: value(First)
|
||||
, first_value(First)
|
||||
, last_value(Last)
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor.
|
||||
/// Sets the value to the first of the range.
|
||||
///\param first The first value in the range.
|
||||
///\param last The last value in the range.
|
||||
//*************************************************************************
|
||||
cyclic_value(T first_, T last_)
|
||||
: value(first_)
|
||||
, first_value(first_)
|
||||
, last_value(last_)
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor.
|
||||
/// Set to an initial value.
|
||||
/// Clamped to the range.
|
||||
///\param first The first value in the range.
|
||||
///\param last The last value in the range.
|
||||
//*************************************************************************
|
||||
cyclic_value(T first_, T last_, T initial)
|
||||
: first_value(first_)
|
||||
, last_value(last_)
|
||||
{
|
||||
set(initial);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Copy constructor.
|
||||
//*************************************************************************
|
||||
cyclic_value(const cyclic_value& other)
|
||||
: value(other.value)
|
||||
, first_value(other.first_value)
|
||||
, last_value(other.last_value)
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Sets the range.
|
||||
/// Sets the value to the first of the range.
|
||||
///\param first The first value in the range.
|
||||
///\param last The last value in the range.
|
||||
//*************************************************************************
|
||||
void set(T first_, T last_)
|
||||
{
|
||||
first_value = first_;
|
||||
last_value = last_;
|
||||
value = first_;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Sets the value.
|
||||
///\param value The value.
|
||||
//*************************************************************************
|
||||
void set(T value_)
|
||||
{
|
||||
if (value_ > last_value)
|
||||
{
|
||||
value_ = last_value;
|
||||
}
|
||||
else if (value_ < first_value)
|
||||
{
|
||||
value_ = first_value;
|
||||
}
|
||||
|
||||
value = value_;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Resets the value to the first in the range.
|
||||
//*************************************************************************
|
||||
void to_first()
|
||||
{
|
||||
value = first_value;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Resets the value to the last in the range.
|
||||
//*************************************************************************
|
||||
void to_last()
|
||||
{
|
||||
value = last_value;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Advances to value by a number of steps.
|
||||
///\param n The number of steps to advance.
|
||||
//*************************************************************************
|
||||
void advance(int n)
|
||||
{
|
||||
if (n > 0)
|
||||
{
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
operator ++();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < -n; ++i)
|
||||
{
|
||||
operator --();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Conversion operator.
|
||||
/// \return The value of the underlying type.
|
||||
//*************************************************************************
|
||||
operator T()
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Const conversion operator.
|
||||
/// \return The value of the underlying type.
|
||||
//*************************************************************************
|
||||
operator const T() const
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// ++ operator.
|
||||
//*************************************************************************
|
||||
cyclic_value& operator ++()
|
||||
{
|
||||
if (value >= last_value)
|
||||
{
|
||||
value = first_value;
|
||||
}
|
||||
else
|
||||
{
|
||||
++value;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// ++ operator.
|
||||
//*************************************************************************
|
||||
cyclic_value operator ++(int)
|
||||
{
|
||||
cyclic_value temp(*this);
|
||||
|
||||
operator++();
|
||||
|
||||
return temp;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// -- operator.
|
||||
//*************************************************************************
|
||||
cyclic_value& operator --()
|
||||
{
|
||||
if (value <= first_value)
|
||||
{
|
||||
value = last_value;
|
||||
}
|
||||
else
|
||||
{
|
||||
--value;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// -- operator.
|
||||
//*************************************************************************
|
||||
cyclic_value operator --(int)
|
||||
{
|
||||
cyclic_value temp(*this);
|
||||
|
||||
operator--();
|
||||
|
||||
return temp;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// = operator.
|
||||
//*************************************************************************
|
||||
cyclic_value& operator =(T t)
|
||||
{
|
||||
set(t);
|
||||
return *this;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// = operator.
|
||||
//*************************************************************************
|
||||
cyclic_value& operator =(const cyclic_value& other)
|
||||
{
|
||||
value = other.value;
|
||||
first_value = other.first_value;
|
||||
last_value = other.last_value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Gets the value.
|
||||
//*************************************************************************
|
||||
T get() const
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Gets the first value.
|
||||
//*************************************************************************
|
||||
T first() const
|
||||
{
|
||||
return first_value;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Gets the last value.
|
||||
//*************************************************************************
|
||||
T last() const
|
||||
{
|
||||
return last_value;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Swaps the values.
|
||||
//*************************************************************************
|
||||
void swap(cyclic_value<T, First, Last>& other)
|
||||
{
|
||||
using ETL_OR_STD::swap; // Allow ADL
|
||||
|
||||
swap(first_value, other.first_value);
|
||||
swap(last_value, other.last_value);
|
||||
swap(value, other.value);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Swaps the values.
|
||||
//*************************************************************************
|
||||
friend void swap(cyclic_value<T, First, Last>& lhs, cyclic_value<T, First, Last>& rhs)
|
||||
{
|
||||
lhs.swap(rhs);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Operator ==.
|
||||
//*************************************************************************
|
||||
friend bool operator == (const cyclic_value<T, First, Last>& lhs, const cyclic_value<T, First, Last>& rhs)
|
||||
{
|
||||
return (lhs.value == rhs.value) &&
|
||||
(lhs.first_value == rhs.first_value) &&
|
||||
(lhs.last_value == rhs.last_value);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Operator !=.
|
||||
//*************************************************************************
|
||||
friend bool operator != (const cyclic_value<T, First, Last>& lhs, const cyclic_value<T, First, Last>& rhs)
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
T value; ///< The current value.
|
||||
T first_value; ///< The first value in the range.
|
||||
T last_value; ///< The last value in the range.
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
587
Chapter18/cib/libs/etl/include/etl/debounce.h
Normal file
587
Chapter18/cib/libs/etl/include/etl/debounce.h
Normal file
@@ -0,0 +1,587 @@
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2016 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_DEBOUNCE_INCLUDED
|
||||
#define ETL_DEBOUNCE_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "static_assert.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
namespace etl
|
||||
{
|
||||
namespace private_debounce
|
||||
{
|
||||
class debounce_base
|
||||
{
|
||||
public:
|
||||
|
||||
typedef uint_least8_t flags_t;
|
||||
typedef uint16_t count_t;
|
||||
|
||||
//*************************************************************************
|
||||
/// Adds the new sample and clears the state change flag.
|
||||
/// If the sample has changed then the counter is reset.
|
||||
/// The last sample state is stored as a bit in the flags.
|
||||
//*************************************************************************
|
||||
void add_sample(bool sample)
|
||||
{
|
||||
// Changed from last time?
|
||||
if (sample != ((flags & Sample) != 0))
|
||||
{
|
||||
count = 0;
|
||||
flags = (flags & ~Sample) | (sample ? Sample : 0);
|
||||
}
|
||||
|
||||
flags &= ~Change;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Gets the current debouncer change state.
|
||||
///\return 'true' if the debouncer has changed state.
|
||||
//*************************************************************************
|
||||
bool has_changed() const
|
||||
{
|
||||
return (flags & Change) != 0;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Gets the current debouncer state.
|
||||
///\return 'true' if the debouncer is in the true state.
|
||||
//*************************************************************************
|
||||
bool is_set() const
|
||||
{
|
||||
return ((flags & State) > Off);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Gets the debouncer hold state.
|
||||
///\return 'true' if the debouncer is in the hold state.
|
||||
//*************************************************************************
|
||||
bool is_held() const
|
||||
{
|
||||
return (flags & State) > On;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Gets the debouncer repeat state.
|
||||
///\return 'true' if the debouncer is repeating.
|
||||
//*************************************************************************
|
||||
bool is_repeating() const
|
||||
{
|
||||
return ((flags & State) == Repeating);
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
enum states
|
||||
{
|
||||
Off = 0,
|
||||
On = 1,
|
||||
Held = 2,
|
||||
Repeating = 3,
|
||||
State = 0x03U,
|
||||
Sample = 4,
|
||||
Change = 8
|
||||
};
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor.
|
||||
//*************************************************************************
|
||||
debounce_base(bool initial_state)
|
||||
: flags(initial_state ? On : Off),
|
||||
count(0)
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Destructor.
|
||||
//*************************************************************************
|
||||
~debounce_base()
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Gets the next state based on the inputs.
|
||||
//*************************************************************************
|
||||
void get_next(bool sample, bool condition_set, bool condition_clear, const uint_least8_t state_table[][2])
|
||||
{
|
||||
int index1 = ((flags & State) * 2) + (sample ? 1 : 0);
|
||||
int index2 = (sample ? (condition_set ? 0 : 1) : (condition_clear ? 0 : 1));
|
||||
|
||||
flags_t next = flags;
|
||||
|
||||
next &= ~State;
|
||||
next |= state_table[index1][index2];
|
||||
|
||||
if (next != flags)
|
||||
{
|
||||
next |= Change;
|
||||
}
|
||||
else
|
||||
{
|
||||
next &= ~Change;
|
||||
}
|
||||
|
||||
flags = next;
|
||||
}
|
||||
|
||||
flags_t flags;
|
||||
count_t count;
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// State change logic for 2 state debounce.
|
||||
//***************************************************************************
|
||||
class debounce2 : public debounce_base
|
||||
{
|
||||
protected:
|
||||
|
||||
debounce2(bool initial_state)
|
||||
: debounce_base(initial_state)
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Destructor.
|
||||
//*************************************************************************
|
||||
~debounce2()
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
///
|
||||
//*************************************************************************
|
||||
void set_state(bool sample, bool condition_set, bool condition_clear)
|
||||
{
|
||||
static ETL_CONSTANT uint_least8_t state_table[4][2] =
|
||||
{
|
||||
/* Off 0 */{ debounce_base::Off, debounce_base::Off },
|
||||
/* Off 1 */{ debounce_base::On, debounce_base::Off },
|
||||
/* On 0 */{ debounce_base::Off, debounce_base::On },
|
||||
/* On 1 */{ debounce_base::On, debounce_base::On },
|
||||
};
|
||||
|
||||
get_next(sample, condition_set, condition_clear, state_table);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
///
|
||||
//*************************************************************************
|
||||
bool process(bool sample, count_t valid_count)
|
||||
{
|
||||
add_sample(sample);
|
||||
|
||||
if (count < UINT16_MAX)
|
||||
{
|
||||
++count;
|
||||
|
||||
bool valid = (count == valid_count);
|
||||
|
||||
switch (flags & State)
|
||||
{
|
||||
case Off:
|
||||
{
|
||||
set_state(sample, valid, valid);
|
||||
break;
|
||||
}
|
||||
|
||||
case On:
|
||||
{
|
||||
set_state(sample, valid, valid);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (flags & Change)
|
||||
{
|
||||
count = 0;
|
||||
}
|
||||
|
||||
return (flags & Change);
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// State change logic for 3 state debounce.
|
||||
//***************************************************************************
|
||||
class debounce3 : public debounce_base
|
||||
{
|
||||
protected:
|
||||
|
||||
debounce3(bool initial_state)
|
||||
: debounce_base(initial_state)
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Destructor.
|
||||
//*************************************************************************
|
||||
~debounce3()
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
///
|
||||
//*************************************************************************
|
||||
void set_state(bool sample, bool condition_set, bool condition_clear)
|
||||
{
|
||||
static ETL_CONSTANT uint_least8_t state_table[6][2] =
|
||||
{
|
||||
/* Off 0 */{ debounce_base::Off, debounce_base::Off },
|
||||
/* Off 1 */{ debounce_base::On, debounce_base::Off },
|
||||
/* On 0 */{ debounce_base::Off, debounce_base::On },
|
||||
/* On 1 */{ debounce_base::Held, debounce_base::On },
|
||||
/* Held 0 */{ debounce_base::Off, debounce_base::Held },
|
||||
/* Held 1 */{ debounce_base::Held, debounce_base::Held }
|
||||
};
|
||||
|
||||
get_next(sample, condition_set, condition_clear, state_table);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
///
|
||||
//*************************************************************************
|
||||
bool process(bool sample, count_t valid_count, count_t hold_count)
|
||||
{
|
||||
add_sample(sample);
|
||||
|
||||
if (count < UINT16_MAX)
|
||||
{
|
||||
++count;
|
||||
|
||||
bool valid = (count == valid_count);
|
||||
bool hold = (count == hold_count);
|
||||
|
||||
switch (flags & State)
|
||||
{
|
||||
case Off:
|
||||
{
|
||||
set_state(sample, valid, valid);
|
||||
break;
|
||||
}
|
||||
|
||||
case On:
|
||||
{
|
||||
set_state(sample, hold, valid);
|
||||
break;
|
||||
}
|
||||
|
||||
case Held:
|
||||
{
|
||||
set_state(sample, hold, valid);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (flags & Change)
|
||||
{
|
||||
count = 0;
|
||||
}
|
||||
|
||||
return (flags & Change);
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// State change logic for 4 state debounce.
|
||||
//***************************************************************************
|
||||
class debounce4 : public debounce_base
|
||||
{
|
||||
protected:
|
||||
|
||||
debounce4(bool initial_state)
|
||||
: debounce_base(initial_state)
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Destructor.
|
||||
//*************************************************************************
|
||||
~debounce4()
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
///
|
||||
//*************************************************************************
|
||||
void set_state(bool sample, bool condition_set, bool condition_clear)
|
||||
{
|
||||
static ETL_CONSTANT uint_least8_t state_table[8][2] =
|
||||
{
|
||||
/* Off 0 */{ debounce_base::Off, debounce_base::Off },
|
||||
/* Off 1 */{ debounce_base::On, debounce_base::Off },
|
||||
/* On 0 */{ debounce_base::Off, debounce_base::On },
|
||||
/* On 1 */{ debounce_base::Held, debounce_base::On },
|
||||
/* Held 0 */{ debounce_base::Off, debounce_base::Held },
|
||||
/* Held 1 */{ debounce_base::Repeating, debounce_base::Held },
|
||||
/* Repeating 0 */{ debounce_base::Off, debounce_base::Repeating },
|
||||
/* Repeating 1 */{ debounce_base::Repeating, debounce_base::Repeating }
|
||||
};
|
||||
|
||||
get_next(sample, condition_set, condition_clear, state_table);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
///
|
||||
//*************************************************************************
|
||||
bool process(bool sample, count_t valid_count, count_t hold_count, count_t repeat_count)
|
||||
{
|
||||
add_sample(sample);
|
||||
|
||||
if (count < UINT16_MAX)
|
||||
{
|
||||
++count;
|
||||
|
||||
bool valid = (count == valid_count);
|
||||
bool hold = (count == hold_count);
|
||||
bool repeat = (count == repeat_count);
|
||||
|
||||
switch (flags & State)
|
||||
{
|
||||
case Off:
|
||||
{
|
||||
set_state(sample, valid, valid);
|
||||
break;
|
||||
}
|
||||
|
||||
case On:
|
||||
{
|
||||
set_state(sample, hold, valid);
|
||||
break;
|
||||
}
|
||||
|
||||
case Held:
|
||||
{
|
||||
set_state(sample, repeat, valid);
|
||||
break;
|
||||
}
|
||||
|
||||
case Repeating:
|
||||
{
|
||||
set_state(sample, repeat, valid);
|
||||
|
||||
if (sample && repeat)
|
||||
{
|
||||
count = 0;
|
||||
flags |= Change;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (flags & Change)
|
||||
{
|
||||
count = 0;
|
||||
}
|
||||
|
||||
return (flags & Change);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// A class to debounce signals.
|
||||
/// Fixed Valid/Hold/Repeating values.
|
||||
//***************************************************************************
|
||||
template <const uint16_t VALID_COUNT = 0, const uint16_t HOLD_COUNT = 0, const uint16_t REPEAT_COUNT = 0>
|
||||
class debounce : public private_debounce::debounce4
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor.
|
||||
//*************************************************************************
|
||||
debounce(bool initial_state = false)
|
||||
: debounce4(initial_state)
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Adds a new sample.
|
||||
/// Returns 'true' if the debouncer changes state.
|
||||
///\param sample The new sample.
|
||||
///\return 'true' if the debouncer changed state.
|
||||
//*************************************************************************
|
||||
bool add(bool sample)
|
||||
{
|
||||
return process(sample, VALID_COUNT, HOLD_COUNT, REPEAT_COUNT);
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// A class to debounce signals.
|
||||
/// Fixed Valid/Hold values.
|
||||
//***************************************************************************
|
||||
template <const uint16_t VALID_COUNT, const uint16_t HOLD_COUNT>
|
||||
class debounce<VALID_COUNT, HOLD_COUNT, 0> : public private_debounce::debounce3
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor.
|
||||
//*************************************************************************
|
||||
debounce(bool initial_state = false)
|
||||
: debounce3(initial_state)
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Adds a new sample.
|
||||
/// Returns 'true' if the debouncer changes state from...
|
||||
/// 1. Clear to Set.
|
||||
/// 2. Set to Clear.
|
||||
/// 3. Not Held to Held.
|
||||
///\param sample The new sample.
|
||||
///\return 'true' if the debouncer changed state.
|
||||
//*************************************************************************
|
||||
bool add(bool sample)
|
||||
{
|
||||
return process(sample, VALID_COUNT, HOLD_COUNT);
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// A class to debounce signals.
|
||||
/// Fixed Valid value.
|
||||
//***************************************************************************
|
||||
template <const uint16_t VALID_COUNT>
|
||||
class debounce<VALID_COUNT, 0, 0> : public private_debounce::debounce2
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor.
|
||||
//*************************************************************************
|
||||
debounce(bool initial_state = false)
|
||||
: debounce2(initial_state)
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Adds a new sample.
|
||||
/// Returns 'true' if the debouncer changes state from...
|
||||
/// 1. Clear to Set.
|
||||
/// 2. Set to Clear.
|
||||
///\param sample The new sample.
|
||||
///\return 'true' if the debouncer changed state.
|
||||
//*************************************************************************
|
||||
bool add(bool sample)
|
||||
{
|
||||
return process(sample, VALID_COUNT);
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// A class to debounce signals.
|
||||
/// Variable Valid/Hold/Repeating values.
|
||||
//***************************************************************************
|
||||
template <>
|
||||
class debounce<0, 0, 0> : public private_debounce::debounce4
|
||||
{
|
||||
public:
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor.
|
||||
///\param initial_state The initial state. Default = false.
|
||||
//*************************************************************************
|
||||
debounce(bool initial_state = false)
|
||||
: debounce4(initial_state),
|
||||
valid_count(1),
|
||||
hold_count(0),
|
||||
repeat_count(0)
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor.
|
||||
///\param valid_count The count for a valid state..
|
||||
///\param hold_count The count after valid_count for a hold state. Default = 0.
|
||||
///\param repeat_count The count after hold_count for a key repeat. Default = 0.
|
||||
//*************************************************************************
|
||||
debounce(count_t valid, count_t hold = 0, count_t repeat = 0)
|
||||
: debounce4(false)
|
||||
{
|
||||
set(valid, hold, repeat);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Constructor.
|
||||
//*************************************************************************
|
||||
void set(count_t valid, count_t hold = 0, count_t repeat = 0)
|
||||
{
|
||||
valid_count = valid;
|
||||
hold_count = hold;
|
||||
repeat_count = repeat;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Adds a new sample.
|
||||
/// Returns 'true' if the debouncer changes state from...
|
||||
/// 1. Clear to Set.
|
||||
/// 2. Set to Clear.
|
||||
/// 3. Not Held to Held.
|
||||
/// 4. Key repeats.
|
||||
///\param sample The new sample.
|
||||
///\return 'true' if the debouncer changed state.
|
||||
//*************************************************************************
|
||||
bool add(bool sample)
|
||||
{
|
||||
return process(sample, valid_count, hold_count, repeat_count);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
count_t valid_count;
|
||||
count_t hold_count;
|
||||
count_t repeat_count;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
180
Chapter18/cib/libs/etl/include/etl/debug_count.h
Normal file
180
Chapter18/cib/libs/etl/include/etl/debug_count.h
Normal file
@@ -0,0 +1,180 @@
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2017 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_DEBUG_COUNT_INCLUDED
|
||||
#define ETL_DEBUG_COUNT_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "atomic.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
|
||||
///\defgroup debug_count debug count
|
||||
///\ingroup utilities
|
||||
|
||||
#if defined(ETL_DEBUG_COUNT)
|
||||
|
||||
#define ETL_DECLARE_DEBUG_COUNT etl::debug_count etl_debug_count
|
||||
#define ETL_SET_DEBUG_COUNT(n) etl_debug_count.set(n)
|
||||
#define ETL_GET_DEBUG_COUNT etl_debug_count.get()
|
||||
#define ETL_INCREMENT_DEBUG_COUNT ++etl_debug_count
|
||||
#define ETL_DECREMENT_DEBUG_COUNT --etl_debug_count
|
||||
#define ETL_ADD_DEBUG_COUNT(n) etl_debug_count += (n)
|
||||
#define ETL_SUBTRACT_DEBUG_COUNT(n) etl_debug_count -= (n)
|
||||
#define ETL_RESET_DEBUG_COUNT etl_debug_count.clear()
|
||||
#define ETL_OBJECT_RESET_DEBUG_COUNT(object) object.etl_debug_count.clear()
|
||||
#define ETL_OBJECT_GET_DEBUG_COUNT(object) object.etl_debug_count.get()
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
/// Used to count instances.
|
||||
/// Asserts if the count is decremented below zero.
|
||||
/// Asserts if the count is not zero when destructed.
|
||||
/// Does nothing in a non-debug build.
|
||||
///\ingroup reference
|
||||
//***************************************************************************
|
||||
class debug_count
|
||||
{
|
||||
public:
|
||||
debug_count()
|
||||
: count(0)
|
||||
{
|
||||
}
|
||||
|
||||
~debug_count()
|
||||
{
|
||||
assert(count == 0);
|
||||
}
|
||||
|
||||
debug_count& operator++()
|
||||
{
|
||||
++count;
|
||||
return *this;
|
||||
}
|
||||
|
||||
debug_count& operator--()
|
||||
{
|
||||
--count;
|
||||
assert(count >= 0);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
debug_count& operator+=(T n)
|
||||
{
|
||||
count += int32_t(n);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
debug_count& operator-=(T n)
|
||||
{
|
||||
count -= int32_t(n);
|
||||
return *this;
|
||||
}
|
||||
|
||||
debug_count& operator=(const debug_count& other)
|
||||
{
|
||||
count.store(other.count.load());
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
#if ETL_HAS_ATOMIC
|
||||
void swap(debug_count& other) ETL_NOEXCEPT // NOT ATOMIC
|
||||
{
|
||||
int32_t temp = other.count.load();
|
||||
other.count.store(count.load());
|
||||
count.store(temp);
|
||||
}
|
||||
#else
|
||||
void swap(debug_count& other) ETL_NOEXCEPT
|
||||
{
|
||||
swap(count, other.count);
|
||||
}
|
||||
#endif
|
||||
|
||||
operator int32_t() const
|
||||
{
|
||||
return count;
|
||||
}
|
||||
|
||||
int32_t get() const
|
||||
{
|
||||
return int32_t(count);
|
||||
}
|
||||
|
||||
void set(int32_t n)
|
||||
{
|
||||
count = n;
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
count = 0;
|
||||
}
|
||||
|
||||
private:
|
||||
#if ETL_HAS_ATOMIC
|
||||
etl::atomic_int32_t count;
|
||||
#else
|
||||
int32_t count;
|
||||
#endif
|
||||
};
|
||||
} // namespace etl
|
||||
|
||||
inline void swap(etl::debug_count& lhs, etl::debug_count& rhs)
|
||||
{
|
||||
lhs.swap(rhs);
|
||||
}
|
||||
|
||||
#else
|
||||
#define ETL_DECLARE_DEBUG_COUNT etl::debug_count etl_debug_count
|
||||
#define ETL_SET_DEBUG_COUNT(n) ETL_DO_NOTHING
|
||||
#define ETL_GET_DEBUG_COUNT ETL_DO_NOTHING
|
||||
#define ETL_INCREMENT_DEBUG_COUNT ETL_DO_NOTHING
|
||||
#define ETL_DECREMENT_DEBUG_COUNT ETL_DO_NOTHING
|
||||
#define ETL_ADD_DEBUG_COUNT(n) ETL_DO_NOTHING
|
||||
#define ETL_SUBTRACT_DEBUG_COUNT(n) ETL_DO_NOTHING
|
||||
#define ETL_RESET_DEBUG_COUNT ETL_DO_NOTHING
|
||||
#define ETL_OBJECT_RESET_DEBUG_COUNT(object) ETL_DO_NOTHING
|
||||
#define ETL_OBJECT_GET_DEBUG_COUNT(object) ETL_DO_NOTHING
|
||||
|
||||
namespace etl
|
||||
{
|
||||
class debug_count
|
||||
{
|
||||
};
|
||||
}
|
||||
#endif // ETL_DEBUG_COUNT
|
||||
|
||||
#endif
|
||||
42
Chapter18/cib/libs/etl/include/etl/delegate.h
Normal file
42
Chapter18/cib/libs/etl/include/etl/delegate.h
Normal file
@@ -0,0 +1,42 @@
|
||||
///\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_DELEGATE_INCLUDED
|
||||
#define ETL_DELEGATE_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
|
||||
#if ETL_USING_CPP11 && !defined(ETL_DELEGATE_FORCE_CPP03_IMPLEMENTATION)
|
||||
#include "private/delegate_cpp11.h"
|
||||
#else
|
||||
#include "private/delegate_cpp03.h"
|
||||
#endif
|
||||
|
||||
#endif
|
||||
262
Chapter18/cib/libs/etl/include/etl/delegate_observer.h
Normal file
262
Chapter18/cib/libs/etl/include/etl/delegate_observer.h
Normal file
@@ -0,0 +1,262 @@
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
https://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2023 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_DELEGATE_OBSERVER_INCLUDED
|
||||
#define ETL_DELEGATE_OBSERVER_INCLUDED
|
||||
|
||||
#include "algorithm.h"
|
||||
|
||||
#include "platform.h"
|
||||
#include "delegate.h"
|
||||
#include "vector.h"
|
||||
#include "exception.h"
|
||||
#include "error_handler.h"
|
||||
#include "utility.h"
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
///\ingroup observer
|
||||
/// The base class for delegate observer exceptions.
|
||||
//***************************************************************************
|
||||
class delegate_observer_exception : public exception
|
||||
{
|
||||
public:
|
||||
|
||||
delegate_observer_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
|
||||
: exception(reason_, file_name_, line_number_)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
///\ingroup observer
|
||||
/// The exception thrown when the delegate observer list is full.
|
||||
//***************************************************************************
|
||||
class delegate_observer_list_full : public delegate_observer_exception
|
||||
{
|
||||
public:
|
||||
|
||||
delegate_observer_list_full(string_type file_name_, numeric_type line_number_)
|
||||
: delegate_observer_exception(ETL_ERROR_TEXT("delegate_observable:full", ETL_DELEGATE_OBSERVER_FILE_ID"A"), file_name_, line_number_)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
//*********************************************************************
|
||||
/// The object that is being observed.
|
||||
///\tparam MAX_OBSERVERS The maximum number of observers that can be accommodated.
|
||||
///\ingroup observer
|
||||
//*********************************************************************
|
||||
template <typename TNotification, const size_t MAX_OBSERVERS>
|
||||
class delegate_observable
|
||||
{
|
||||
public:
|
||||
|
||||
typedef etl::delegate<void(TNotification)> observer_type;
|
||||
|
||||
private:
|
||||
|
||||
//***********************************
|
||||
// Item stored in the observer list.
|
||||
//***********************************
|
||||
struct observer_item
|
||||
{
|
||||
observer_item(observer_type observer_)
|
||||
: observer(observer_)
|
||||
, enabled(true)
|
||||
{
|
||||
}
|
||||
|
||||
observer_type observer;
|
||||
bool enabled;
|
||||
};
|
||||
|
||||
//***********************************
|
||||
// How to compare an observer with an observer list item.
|
||||
//***********************************
|
||||
struct compare_observers
|
||||
{
|
||||
compare_observers(observer_type& observer_)
|
||||
: observer(&observer_)
|
||||
{
|
||||
}
|
||||
|
||||
bool operator ()(const observer_item& item) const
|
||||
{
|
||||
return observer == item.observer;
|
||||
}
|
||||
|
||||
observer_type observer;
|
||||
};
|
||||
|
||||
typedef etl::vector<observer_item, MAX_OBSERVERS> Observer_List;
|
||||
|
||||
public:
|
||||
|
||||
typedef size_t size_type;
|
||||
typedef TNotification notification_type;
|
||||
|
||||
//*****************************************************************
|
||||
/// Add an observer to the list.
|
||||
/// If asserts or exceptions are enabled then an etl::observable_observer_list_full
|
||||
/// is emitted if the observer list is already full.
|
||||
///\param observer A reference to the observer.
|
||||
//*****************************************************************
|
||||
void add_observer(observer_type& observer)
|
||||
{
|
||||
// See if we already have it in our list.
|
||||
typename Observer_List::iterator i_observer_item = find_observer(observer);
|
||||
|
||||
// Not there?
|
||||
if (i_observer_item == observer_list.end())
|
||||
{
|
||||
// Is there enough room?
|
||||
ETL_ASSERT_OR_RETURN(!observer_list.full(), ETL_ERROR(etl::observer_list_full));
|
||||
|
||||
// Add it.
|
||||
observer_list.push_back(observer_item(observer));
|
||||
}
|
||||
}
|
||||
|
||||
//*****************************************************************
|
||||
/// Remove a particular observer from the list.
|
||||
///\param observer A reference to the observer.
|
||||
///\return <b>true</b> if the observer was removed, <b>false</b> if not.
|
||||
//*****************************************************************
|
||||
bool remove_observer(observer_type& observer)
|
||||
{
|
||||
// See if we have it in our list.
|
||||
typename Observer_List::iterator i_observer_item = find_observer(observer);
|
||||
|
||||
// Found it?
|
||||
if (i_observer_item != observer_list.end())
|
||||
{
|
||||
// Erase it.
|
||||
observer_list.erase(i_observer_item);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//*****************************************************************
|
||||
/// Enable an observer
|
||||
///\param observer A reference to the observer.
|
||||
///\param state <b>true</b> to enable, <b>false</b> to disable. Default is enable.
|
||||
//*****************************************************************
|
||||
void enable_observer(observer_type& observer, bool state = true)
|
||||
{
|
||||
// See if we have it in our list.
|
||||
typename Observer_List::iterator i_observer_item = find_observer(observer);
|
||||
|
||||
// Found it?
|
||||
if (i_observer_item != observer_list.end())
|
||||
{
|
||||
i_observer_item->enabled = state;
|
||||
}
|
||||
}
|
||||
|
||||
//*****************************************************************
|
||||
/// Disable an observer
|
||||
//*****************************************************************
|
||||
void disable_observer(observer_type& observer)
|
||||
{
|
||||
// See if we have it in our list.
|
||||
typename Observer_List::iterator i_observer_item = find_observer(observer);
|
||||
|
||||
// Found it?
|
||||
if (i_observer_item != observer_list.end())
|
||||
{
|
||||
i_observer_item->enabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
//*****************************************************************
|
||||
/// Clear all observers from the list.
|
||||
//*****************************************************************
|
||||
void clear_observers()
|
||||
{
|
||||
observer_list.clear();
|
||||
}
|
||||
|
||||
//*****************************************************************
|
||||
/// Returns the number of observers.
|
||||
//*****************************************************************
|
||||
size_type number_of_observers() const
|
||||
{
|
||||
return observer_list.size();
|
||||
}
|
||||
|
||||
//*****************************************************************
|
||||
/// Notify all of the observers, sending them the notification.
|
||||
///\tparam TNotification The notification type.
|
||||
///\param n The notification.
|
||||
//*****************************************************************
|
||||
void notify_observers(TNotification n)
|
||||
{
|
||||
typename Observer_List::iterator i_observer_item = observer_list.begin();
|
||||
|
||||
while (i_observer_item != observer_list.end())
|
||||
{
|
||||
if (i_observer_item->enabled)
|
||||
{
|
||||
i_observer_item->p_observer->notification(n);
|
||||
}
|
||||
|
||||
++i_observer_item;
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
~observable()
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
//*****************************************************************
|
||||
/// Find an observer in the list.
|
||||
/// Returns the end of the list if not found.
|
||||
//*****************************************************************
|
||||
typename Observer_List::iterator find_observer(observer_type& observer_)
|
||||
{
|
||||
return etl::find_if(observer_list.begin(), observer_list.end(), compare_observers(observer_));
|
||||
}
|
||||
|
||||
/// The list of observers.
|
||||
Observer_List observer_list;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
215
Chapter18/cib/libs/etl/include/etl/delegate_service.h
Normal file
215
Chapter18/cib/libs/etl/include/etl/delegate_service.h
Normal file
@@ -0,0 +1,215 @@
|
||||
///\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_DELEGATE_SERVICE_INCLUDED
|
||||
#define ETL_DELEGATE_SERVICE_INCLUDED
|
||||
|
||||
#include "platform.h"
|
||||
#include "nullptr.h"
|
||||
#include "static_assert.h"
|
||||
#include "delegate.h"
|
||||
#include "array.h"
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
/// An indexed delegate service.
|
||||
/// \tparam Range The number of delegates to handle.
|
||||
/// \tparam Offset The lowest delegate id value.
|
||||
/// \tparam Delegates Pointer to an array of delegate pointers.
|
||||
/// The delegate ids must range between Offset and Offset + Range - 1.
|
||||
//***************************************************************************
|
||||
#if ETL_USING_CPP11 && !defined(ETL_DELEGATE_FORCE_CPP03_IMPLEMENTATION)
|
||||
template <size_t Range,
|
||||
size_t Offset = 0U,
|
||||
const etl::delegate<void(size_t)>* Delegates = nullptr>
|
||||
class delegate_service
|
||||
{
|
||||
public:
|
||||
|
||||
typedef etl::delegate<void(size_t)> delegate_type;
|
||||
|
||||
//*************************************************************************
|
||||
/// Executes the delegate function for the index.
|
||||
/// Compile time assert if the id is out of range.
|
||||
/// \tparam Id The id of the delegate.
|
||||
//*************************************************************************
|
||||
template <size_t Id>
|
||||
void call() const
|
||||
{
|
||||
ETL_STATIC_ASSERT(Id < (Offset + Range), "Callback Id out of range");
|
||||
ETL_STATIC_ASSERT(Id >= Offset, "Callback Id out of range");
|
||||
|
||||
Delegates[Id - Offset](Id);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Executes the delegate function for the index.
|
||||
/// \param id Id of the delegate.
|
||||
//*************************************************************************
|
||||
void call(size_t id) const
|
||||
{
|
||||
if ((id >= Offset) && (id < (Offset + Range)))
|
||||
{
|
||||
// Call the delegate with the specified Id.
|
||||
Delegates[id - Offset](id);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Call the 'unhandled' delegate.
|
||||
Delegates[Range](id);
|
||||
}
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
//***************************************************************************
|
||||
/// An indexed delegate service.
|
||||
/// \tparam Range The number of delegates to handle.
|
||||
/// \tparam Offset The lowest delegate id value.
|
||||
/// The delegate ids must range between Offset and Offset + Range - 1.
|
||||
//***************************************************************************
|
||||
template <size_t Range,
|
||||
size_t Offset>
|
||||
#if ETL_USING_CPP11 && !defined(ETL_DELEGATE_FORCE_CPP03_IMPLEMENTATION)
|
||||
class delegate_service<Range, Offset, nullptr>
|
||||
#else
|
||||
class delegate_service
|
||||
#endif
|
||||
{
|
||||
public:
|
||||
|
||||
typedef etl::delegate<void(size_t)> delegate_type;
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
/// Sets all delegates to the internal default.
|
||||
//*************************************************************************
|
||||
delegate_service()
|
||||
{
|
||||
delegate_type default_delegate = delegate_type::create<delegate_service<Range, Offset>, &delegate_service<Range, Offset>::unhandled>(*this);
|
||||
|
||||
lookup.fill(default_delegate);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Registers a delegate for the specified id.
|
||||
/// Compile time assert if the id is out of range.
|
||||
/// \tparam Id The id of the delegate.
|
||||
/// \param delegate Reference to the delegate.
|
||||
//*************************************************************************
|
||||
template <size_t Id>
|
||||
void register_delegate(delegate_type 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;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Registers a delegate for the specified id.
|
||||
/// No action if the id is out of range.
|
||||
/// \param id Id of the delegate.
|
||||
/// \param delegate Reference to the delegate.
|
||||
//*************************************************************************
|
||||
void register_delegate(size_t id, delegate_type callback)
|
||||
{
|
||||
if ((id >= Offset) && (id < (Offset + Range)))
|
||||
{
|
||||
lookup[id - Offset] = callback;
|
||||
}
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Registers an alternative delegate for unhandled ids.
|
||||
/// \param delegate A reference to the user supplied 'unhandled' delegate.
|
||||
//*************************************************************************
|
||||
void register_unhandled_delegate(delegate_type callback)
|
||||
{
|
||||
unhandled_delegate = callback;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Executes the delegate function for the index.
|
||||
/// Compile time assert if the id is out of range.
|
||||
/// \tparam Id The id of the delegate.
|
||||
//*************************************************************************
|
||||
template <size_t Id>
|
||||
void call() const
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Executes the delegate function for the index.
|
||||
/// \param id Id of the delegate.
|
||||
//*************************************************************************
|
||||
void call(const size_t id) const
|
||||
{
|
||||
if ((id >= Offset) && (id < (Offset + Range)))
|
||||
{
|
||||
// Call the delegate with the specified Id.
|
||||
lookup[id - Offset](id);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Call the 'unhandled' delegate.
|
||||
unhandled(id);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
//*************************************************************************
|
||||
/// The default callback function.
|
||||
/// Calls the user defined 'unhandled' callback if it exists.
|
||||
//*************************************************************************
|
||||
void unhandled(size_t id) const
|
||||
{
|
||||
if (unhandled_delegate.is_valid())
|
||||
{
|
||||
unhandled_delegate(id);
|
||||
}
|
||||
}
|
||||
|
||||
/// The default delegate for unhandled ids.
|
||||
delegate_type unhandled_delegate;
|
||||
|
||||
/// Lookup table of delegates.
|
||||
etl::array<delegate_type, Range> lookup;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user