* Add phone call authentication method * Remove dynamic mem allocation from error handler * Add more error handling code * Move error handling to different file * Remove Socket IO dependency * cleanup in websocket code * Add rapidjson as cmake dependency * Added Dockerfiles as primary build system for packages * Changed policy in CMakeList to work with lower version of CMake * Fix opensuse builds * Link filesystem library in gcc 8.5 or older
93 lines
2.4 KiB
C++
93 lines
2.4 KiB
C++
#pragma once
|
|
|
|
#include <array>
|
|
#include <cstddef>
|
|
#include <cstring>
|
|
#include <string_view>
|
|
#include <utility>
|
|
|
|
namespace rublon {
|
|
template < typename T, std::size_t N, std::size_t... Idx >
|
|
constexpr std::array< T, N > toStdArray(T (&arr)[N], std::index_sequence< Idx... >) {
|
|
return {arr[Idx]...};
|
|
}
|
|
|
|
template < std::size_t NewSize, typename T, std::size_t OldSize, std::size_t... Indexes >
|
|
constexpr std::array< T, NewSize > resize(const std::array< T, OldSize > & arr, std::index_sequence< Indexes... >) {
|
|
return {arr[Indexes]...};
|
|
}
|
|
|
|
template < std::size_t NewSize, typename T, std::size_t OldSize >
|
|
constexpr std::array< T, NewSize > resize(const std::array< T, OldSize > & arr) {
|
|
constexpr std::size_t minSize = std::min(OldSize, NewSize);
|
|
return resize(arr, std::make_index_sequence< minSize >());
|
|
}
|
|
|
|
namespace details {
|
|
class StaticStringBase {};
|
|
} // namespace details
|
|
// statically allocates a string buffer of (N+1) chars
|
|
template < size_t N >
|
|
class StaticString : public details::StaticStringBase {
|
|
public:
|
|
constexpr StaticString() = default;
|
|
constexpr StaticString(const char (&chars)[N]) : m_str(toStdArray(chars)) {}
|
|
|
|
constexpr StaticString(std::array< const char, N > chars) : m_str(std::move(chars)) {}
|
|
constexpr StaticString(const char * str) {
|
|
std::strncpy(m_str.data(), str, N);
|
|
}
|
|
|
|
void operator=(const char * str) {
|
|
std::strncpy(m_str.data(), str, N);
|
|
}
|
|
|
|
void operator=(std::string_view str) {
|
|
std::strncpy(m_str.data(), str.data(), N);
|
|
}
|
|
|
|
const char * c_str() const noexcept {
|
|
return m_str.data();
|
|
}
|
|
|
|
const char * data() const noexcept {
|
|
return m_str.data();
|
|
}
|
|
|
|
char * data() noexcept {
|
|
return m_str.data();
|
|
}
|
|
|
|
constexpr char & operator[](size_t pos) {
|
|
return m_str[pos];
|
|
}
|
|
|
|
constexpr const char & operator[](size_t pos) const {
|
|
return m_str[pos];
|
|
}
|
|
|
|
std::size_t size() const {
|
|
return strlen(m_str.data());
|
|
}
|
|
|
|
constexpr std::size_t capacity() const noexcept {
|
|
return N - 1;
|
|
}
|
|
|
|
template < std::size_t M >
|
|
constexpr StaticString< N + M - 1 > operator+(const StaticString< M > & rhs) const {
|
|
return join(resize< N - 1 >(m_str), rhs.m_str);
|
|
}
|
|
|
|
template < std::size_t M >
|
|
friend class StaticString;
|
|
|
|
operator std::string_view() const {
|
|
return {m_str.data(), size()};
|
|
}
|
|
|
|
private:
|
|
std::array< char, N + 1 > m_str{};
|
|
};
|
|
} // namespace rublon
|