Made this lib compatible with esp-idf components system

This commit is contained in:
2021-02-04 01:57:20 +01:00
parent 1c7e556e7e
commit 471b524df2
16 changed files with 36 additions and 502 deletions

15
CMakeLists.txt Normal file
View File

@ -0,0 +1,15 @@
set(headers
include/binary_semaphore.h
include/counting_semaphore.h
include/esprandom.h
include/event_group.h
include/http_client.h
include/lockhelper.h
include/lockingqueue.h
include/mutex_semaphore.h
include/recursivelockhelper.h
include/recursive_mutex_semaphore.h
include/websocket_client.h
)
idf_component_register(INCLUDE_DIRS include SRCS ${headers} REQUIRES freertos esp_system esp_http_client esp_websocket_client cpputils)

View File

@ -1,36 +0,0 @@
#pragma once
// system includes
#include <utility>
#include <optional>
namespace espcpputils {
template<typename T>
class CleanupHelper
{
public:
CleanupHelper(T && cleanup) :
m_cleanup{std::move(cleanup)}
{}
~CleanupHelper()
{
if (m_cleanup)
(*m_cleanup)();
}
void disarm()
{
m_cleanup = std::nullopt;
}
private:
std::optional<T> m_cleanup;
};
template<typename T>
CleanupHelper<T> makeCleanupHelper(T && cleanup)
{
return CleanupHelper<T>(std::move(cleanup));
}
} // namespace espcpputils

View File

@ -1,99 +0,0 @@
#pragma once
// system includes
#include <cassert>
#include <type_traits>
#include <utility>
// local includes
#include "esputils.h"
namespace espcpputils {
template<typename T>
class DelayedConstruction
{
ESP_DISABLE_COPY_MOVE(DelayedConstruction)
public:
DelayedConstruction() = default;
~DelayedConstruction()
{
if (m_constructed)
destruct();
}
template<typename ...Targs>
void construct(Targs &&...args)
{
assert(!m_constructed);
new (&helper.value) T {std::forward<Targs>(args)...};
m_constructed = true;
}
void destruct()
{
assert(m_constructed);
helper.value.~T();
m_constructed = false;
}
T &get()
{
assert(m_constructed);
return helper.value;
}
const T &get() const
{
assert(m_constructed);
return helper.value;
}
T *operator->()
{
assert(m_constructed);
return &helper.value;
}
const T *operator->() const
{
assert(m_constructed);
return &helper.value;
}
T &operator*()
{
assert(m_constructed);
return helper.value;
}
const T &operator*() const
{
assert(m_constructed);
return helper.value;
}
//! allows for getting the address before the object has been constructed
T &getUnsafe()
{
return helper.value;
}
//! allows for getting the address before the object has been constructed
const T &getUnsafe() const
{
return helper.value;
}
bool constructed() const { return m_constructed; }
private:
bool m_constructed{};
union Helper
{
Helper() {}
~Helper() {}
T value;
} helper;
};
} // namespace espcpputils

View File

@ -1,114 +0,0 @@
#pragma once
// system includes
#include <type_traits>
#include <cstddef>
#include <utility>
#include <cstdint>
namespace espcpputils {
class EspFlag
{
using uint = unsigned int;
using ushort = unsigned short;
int i;
public:
constexpr inline EspFlag(int value) noexcept : i(value) {}
constexpr inline operator int() const noexcept { return i; }
constexpr inline EspFlag(uint value) noexcept : i(int(value)) {}
constexpr inline EspFlag(short value) noexcept : i(int(value)) {}
constexpr inline EspFlag(ushort value) noexcept : i(int(uint(value))) {}
constexpr inline operator uint() const noexcept { return uint(i); }
};
template<typename Enum>
class EspFlags
{
static_assert((sizeof(Enum) <= sizeof(int)),
"EspFlags uses an int as storage, so an enum with underlying "
"long long will overflow.");
static_assert((std::is_enum<Enum>::value), "EspFlags is only usable on enumeration types.");
using uint = unsigned int;
using ushort = unsigned short;
public:
typedef typename std::conditional<
std::is_unsigned<typename std::underlying_type<Enum>::type>::value,
unsigned int,
signed int
>::type Int;
typedef Enum enum_type;
constexpr inline EspFlags() noexcept : i(0) {}
constexpr inline EspFlags(Enum flags) noexcept : i(Int(flags)) {}
constexpr inline EspFlags(EspFlag flag) noexcept : i(flag) {}
constexpr inline EspFlags(std::initializer_list<Enum> flags) noexcept
: i(initializer_list_helper(flags.begin(), flags.end())) {}
constexpr inline EspFlags &operator&=(int mask) noexcept { i &= mask; return *this; }
constexpr inline EspFlags &operator&=(uint mask) noexcept { i &= mask; return *this; }
constexpr inline EspFlags &operator&=(Enum mask) noexcept { i &= Int(mask); return *this; }
constexpr inline EspFlags &operator|=(EspFlags other) noexcept { i |= other.i; return *this; }
constexpr inline EspFlags &operator|=(Enum other) noexcept { i |= Int(other); return *this; }
constexpr inline EspFlags &operator^=(EspFlags other) noexcept { i ^= other.i; return *this; }
constexpr inline EspFlags &operator^=(Enum other) noexcept { i ^= Int(other); return *this; }
constexpr inline operator Int() const noexcept { return i; }
constexpr inline EspFlags operator|(EspFlags other) const noexcept { return EspFlags(EspFlag(i | other.i)); }
constexpr inline EspFlags operator|(Enum other) const noexcept { return EspFlags(EspFlag(i | Int(other))); }
constexpr inline EspFlags operator^(EspFlags other) const noexcept { return EspFlags(EspFlag(i ^ other.i)); }
constexpr inline EspFlags operator^(Enum other) const noexcept { return EspFlags(EspFlag(i ^ Int(other))); }
constexpr inline EspFlags operator&(int mask) const noexcept { return EspFlags(EspFlag(i & mask)); }
constexpr inline EspFlags operator&(uint mask) const noexcept { return EspFlags(EspFlag(i & mask)); }
constexpr inline EspFlags operator&(Enum other) const noexcept { return EspFlags(EspFlag(i & Int(other))); }
constexpr inline EspFlags operator~() const noexcept { return EspFlags(EspFlag(~i)); }
constexpr inline bool operator!() const noexcept { return !i; }
constexpr inline bool testFlag(Enum flag) const noexcept { return (i & Int(flag)) == Int(flag) && (Int(flag) != 0 || i == Int(flag) ); }
constexpr inline EspFlags &setFlag(Enum flag, bool on = true) noexcept
{
return on ? (*this |= flag) : (*this &= ~Int(flag));
}
private:
constexpr static inline Int initializer_list_helper(typename std::initializer_list<Enum>::const_iterator it,
typename std::initializer_list<Enum>::const_iterator end)
noexcept
{
return (it == end ? Int(0) : (Int(*it) | initializer_list_helper(it + 1, end)));
}
Int i;
};
class EspIncompatibleFlag
{
int i;
public:
constexpr inline explicit EspIncompatibleFlag(int i) noexcept;
constexpr inline operator int() const noexcept { return i; }
};
constexpr inline EspIncompatibleFlag::EspIncompatibleFlag(int value) noexcept : i(value) {}
} // namespace espcpputils
#define ESP_DECLARE_FLAGS(Flags, Enum)\
typedef ::espcpputils::EspFlags<Enum> Flags;
#define ESP_DECLARE_INCOMPATIBLE_FLAGS(Flags) \
constexpr inline ::espcpputils::EspIncompatibleFlag operator|(Flags::enum_type f1, int f2) noexcept \
{ return ::espcpputils::EspIncompatibleFlag(int(f1) | f2); }
#define ESP_DECLARE_OPERATORS_FOR_FLAGS(Flags) \
constexpr inline ::espcpputils::EspFlags<Flags::enum_type> operator|(Flags::enum_type f1, Flags::enum_type f2) noexcept \
{ return ::espcpputils::EspFlags<Flags::enum_type>(f1) | f2; } \
constexpr inline ::espcpputils::EspFlags<Flags::enum_type> operator|(Flags::enum_type f1, ::espcpputils::EspFlags<Flags::enum_type> f2) noexcept \
{ return f2 | f1; } ESP_DECLARE_INCOMPATIBLE_FLAGS(Flags)

View File

@ -1,232 +0,0 @@
#pragma once
// system includes
#include <algorithm>
#include <cstdint>
#include <functional>
#include <vector>
#include <cstring>
#include <string>
#include <string_view>
/*
Avoid "unused parameter" warnings
*/
#define ESP_UNUSED(x) (void)x;
/* These two macros make it possible to turn the builtin line expander into a
* string literal. */
#define ESP_STRINGIFY2(x) #x
#define ESP_STRINGIFY(x) ESP_STRINGIFY2(x)
/*
Some classes do not permit copies to be made of an object. These
classes contains a private copy constructor and assignment
operator to disable copying (the compiler gives an error message).
*/
#define ESP_DISABLE_COPY(Class) \
Class(const Class &) = delete;\
Class &operator=(const Class &) = delete;
#define ESP_DISABLE_MOVE(Class) \
Class(Class &&) = delete; \
Class &operator=(Class &&) = delete;
#define ESP_DISABLE_COPY_MOVE(Class) \
ESP_DISABLE_COPY(Class) \
ESP_DISABLE_MOVE(Class)
/* These two macros make it possible to define a typesafe enum with parse and
* toString methods */
#define DECLARE_TYPESAFE_ENUM_HELPER1(name) name,
#define DECLARE_TYPESAFE_ENUM_HELPER2(name) case TheEnum::name: return #name;
#define DECLARE_TYPESAFE_ENUM_HELPER3(name) else if (str == ESP_STRINGIFY(name)) return TheEnum::name;
#define DECLARE_TYPESAFE_ENUM(Name, Derivation, Values) \
enum class Name Derivation \
{ \
Values(DECLARE_TYPESAFE_ENUM_HELPER1) \
}; \
std::string toString(Name value); \
std::optional<Name> parse##Name(std::string_view str);
#define IMPLEMENT_TYPESAFE_ENUM(Name, Derivation, Values) \
std::string toString(Name value) \
{ \
switch (value) \
{ \
using TheEnum = Name; \
Values(DECLARE_TYPESAFE_ENUM_HELPER2) \
} \
return std::string{"Unknown " #Name "("} + std::to_string(int(value)) + ')'; \
} \
std::optional<Name> parse##Name(std::string_view str) \
{ \
using TheEnum = Name; \
if (false) {} \
Values(DECLARE_TYPESAFE_ENUM_HELPER3) \
return std::nullopt; \
}
namespace espcpputils {
template<typename... T>
class Signal
{
public:
using Slot = std::function<void(T...)>;
Signal &operator+=(Slot &&slot)
{
m_slots.emplace_back(std::move(slot));
return *this;
}
Signal &operator+=(const Slot &slot)
{
m_slots.emplace_back(slot);
return *this;
}
template<typename ...Targs>
void operator()(Targs && ...args) const
{
for (const auto &slot : m_slots)
slot(std::forward<Targs>(args)...);
}
private:
std::vector<Slot> m_slots;
};
namespace literals {
namespace {
/**
* User-defined Literals
* usage:
*
* uint32_t = test = 10_MHz; // --> 10000000
*/
constexpr unsigned long long operator"" _kHz(unsigned long long x)
{
return x * 1000;
}
constexpr unsigned long long operator"" _MHz(unsigned long long x)
{
return x * 1000 * 1000;
}
constexpr unsigned long long operator"" _GHz(unsigned long long x)
{
return x * 1000 * 1000 * 1000;
}
constexpr unsigned long long operator"" _kBit(unsigned long long x)
{
return x * 1024;
}
constexpr unsigned long long operator"" _MBit(unsigned long long x)
{
return x * 1024 * 1024;
}
constexpr unsigned long long operator"" _GBit(unsigned long long x)
{
return x * 1024 * 1024 * 1024;
}
constexpr unsigned long long operator"" _kB(unsigned long long x)
{
return x * 1024;
}
constexpr unsigned long long operator"" _MB(unsigned long long x)
{
return x * 1024 * 1024;
}
constexpr unsigned long long operator"" _GB(unsigned long long x)
{
return x * 1024 * 1024 * 1024;
}
} // namespace
} // namespace literals
namespace {
template<typename T>
T vmin(T&&t)
{
return std::forward<T>(t);
}
template<typename T0, typename T1, typename... Ts>
typename std::common_type<T0, T1, Ts...>::type vmin(T0&& val1, T1&& val2, Ts&&... vs)
{
if (val1 < val2)
return vmin(val1, std::forward<Ts>(vs)...);
else
return vmin(val2, std::forward<Ts>(vs)...);
}
template<class T>
constexpr const T& clamp( const T& v, const T& lo, const T& hi )
{
// assert( !(hi < lo) );
return (v < lo) ? lo : (hi < v) ? hi : v;
}
template<typename T>
T mapValue(T x, T in_min, T in_max, T out_min, T out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
template <typename... Args>
struct EspNonConstOverload
{
template <typename R, typename T>
constexpr auto operator()(R (T::*ptr)(Args...)) const noexcept -> decltype(ptr)
{ return ptr; }
template <typename R, typename T>
static constexpr auto of(R (T::*ptr)(Args...)) noexcept -> decltype(ptr)
{ return ptr; }
};
template <typename... Args>
struct EspConstOverload
{
template <typename R, typename T>
constexpr auto operator()(R (T::*ptr)(Args...) const) const noexcept -> decltype(ptr)
{ return ptr; }
template <typename R, typename T>
static constexpr auto of(R (T::*ptr)(Args...) const) noexcept -> decltype(ptr)
{ return ptr; }
};
template <typename... Args>
struct EspOverload : EspConstOverload<Args...>, EspNonConstOverload<Args...>
{
using EspConstOverload<Args...>::of;
using EspConstOverload<Args...>::operator();
using EspNonConstOverload<Args...>::of;
using EspNonConstOverload<Args...>::operator();
template <typename R>
constexpr auto operator()(R (*ptr)(Args...)) const noexcept -> decltype(ptr)
{ return ptr; }
template <typename R>
static constexpr auto of(R (*ptr)(Args...)) noexcept -> decltype(ptr)
{ return ptr; }
};
template <typename... Args> constexpr __attribute__((__unused__)) EspOverload<Args...> espOverload = {};
template <typename... Args> constexpr __attribute__((__unused__)) EspConstOverload<Args...> espConstOverload = {};
template <typename... Args> constexpr __attribute__((__unused__)) EspNonConstOverload<Args...> espNonConstOverload = {};
template<typename First, typename ... T>
bool is_in(First &&first, T && ... t)
{
return ((first == t) || ...);
}
} // namespace
} // namespace espcpputils

View File

@ -6,12 +6,12 @@
#include <freertos/task.h>
// local includes
#include "esputils.h"
#include "cppmacros.h"
namespace espcpputils {
class binary_semaphore
{
ESP_DISABLE_COPY_MOVE(binary_semaphore)
CPP_DISABLE_COPY_MOVE(binary_semaphore)
public:
binary_semaphore() :

View File

@ -6,12 +6,12 @@
#include <freertos/task.h>
// local includes
#include "esputils.h"
#include "cppmacros.h"
namespace espcpputils {
class counting_semaphore
{
ESP_DISABLE_COPY_MOVE(counting_semaphore)
CPP_DISABLE_COPY_MOVE(counting_semaphore)
public:
counting_semaphore(UBaseType_t uxMaxCount, UBaseType_t uxInitialCount) :

View File

@ -3,6 +3,7 @@
// system includes
#include <limits>
#include <random>
#include <iterator>
// esp-idf includes
#include <esp_system.h>
@ -24,14 +25,14 @@ public:
std::string randomString(std::size_t length)
{
static constexpr auto chars =
static constexpr const char chars[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
auto rng = esp_random_device{};
auto dist = std::uniform_int_distribution{{}, std::strlen(chars) - 1};
auto dist = std::uniform_int_distribution{{}, std::size(chars) - 1};
auto result = std::string(length, '\0');
std::generate_n(std::begin(result), length, [&]() { return chars[dist(rng)]; });

View File

@ -5,12 +5,12 @@
#include <freertos/event_groups.h>
// local includes
#include "esputils.h"
#include "cppmacros.h"
namespace espcpputils {
class event_group
{
ESP_DISABLE_COPY_MOVE(event_group)
CPP_DISABLE_COPY_MOVE(event_group)
public:
event_group() :

View File

@ -4,12 +4,12 @@
#include <esp_http_client.h>
// local includes
#include "esputils.h"
#include "cppmacros.h"
namespace espcpputils {
class http_client
{
ESP_DISABLE_COPY_MOVE(http_client)
CPP_DISABLE_COPY_MOVE(http_client)
public:
http_client(const esp_http_client_config_t *config) :

View File

@ -5,12 +5,12 @@
#include <freertos/semphr.h>
// local includes
#include "esputils.h"
#include "cppmacros.h"
namespace espcpputils {
class LockHelper
{
ESP_DISABLE_COPY_MOVE(LockHelper)
CPP_DISABLE_COPY_MOVE(LockHelper)
public:
LockHelper(QueueHandle_t _xMutex, TickType_t xTicksToWait=portMAX_DELAY) :

View File

@ -9,7 +9,6 @@
#include <freertos/semphr.h>
// local includes
#include "esputils.h"
#include "delayedconstruction.h"
#include "recursive_mutex_semaphore.h"
#include "recursivelockhelper.h"

View File

@ -6,12 +6,12 @@
#include <freertos/task.h>
// local includes
#include "esputils.h"
#include "cppmacros.h"
namespace espcpputils {
class mutex_semaphore
{
ESP_DISABLE_COPY_MOVE(mutex_semaphore)
CPP_DISABLE_COPY_MOVE(mutex_semaphore)
public:
mutex_semaphore() :

View File

@ -6,12 +6,12 @@
#include <freertos/task.h>
// local includes
#include "esputils.h"
#include "cppmacros.h"
namespace espcpputils {
class recursive_mutex_semaphore
{
ESP_DISABLE_COPY_MOVE(recursive_mutex_semaphore)
CPP_DISABLE_COPY_MOVE(recursive_mutex_semaphore)
public:
recursive_mutex_semaphore() :

View File

@ -5,12 +5,12 @@
#include <freertos/semphr.h>
// local includes
#include "esputils.h"
#include "cppmacros.h"
namespace espcpputils {
class RecursiveLockHelper
{
ESP_DISABLE_COPY_MOVE(RecursiveLockHelper)
CPP_DISABLE_COPY_MOVE(RecursiveLockHelper)
public:
RecursiveLockHelper(QueueHandle_t _xMutex, TickType_t xTicksToWait=portMAX_DELAY) :

View File

@ -4,12 +4,12 @@
#include <esp_websocket_client.h>
// local includes
#include "esputils.h"
#include "cppmacros.h"
namespace espcpputils {
class websocket_client
{
ESP_DISABLE_COPY_MOVE(websocket_client)
CPP_DISABLE_COPY_MOVE(websocket_client)
public:
websocket_client(const esp_websocket_client_config_t &config) :