Move Array and ConstexprVector to a constexpr_utils.hh header

This commit is contained in:
Maxime Coste 2017-11-12 13:01:18 +08:00
parent 5cfccad39c
commit 00e0630272
6 changed files with 86 additions and 73 deletions

View File

@ -3,6 +3,7 @@
#include "clock.hh"
#include "coord.hh"
#include "constexpr_utils.hh"
#include "enum.hh"
#include "safe_ptr.hh"
#include "scope.hh"

View File

@ -1,6 +1,7 @@
#ifndef client_hh_INCLUDED
#define client_hh_INCLUDED
#include "constexpr_utils.hh"
#include "display_buffer.hh"
#include "env_vars.hh"
#include "input_handler.hh"

78
src/constexpr_utils.hh Normal file
View File

@ -0,0 +1,78 @@
#ifndef constexpr_utils_hh_INCLUDED
#define constexpr_utils_hh_INCLUDED
#include <utility>
#include <initializer_list>
#include <stddef.h>
namespace Kakoune
{
template<typename T, size_t N>
struct Array
{
constexpr size_t size() const { return N; }
constexpr const T& operator[](int i) const { return m_data[i]; }
constexpr const T* begin() const { return m_data; }
constexpr const T* end() const { return m_data+N; }
T m_data[N];
};
template<typename T, size_t N, size_t... Indices>
constexpr Array<T, N> make_array(const T (&data)[N], std::index_sequence<Indices...>)
{
static_assert(sizeof...(Indices) == N, "size mismatch");
return {{data[Indices]...}};
}
template<typename T, size_t N>
constexpr Array<T, N> make_array(const T (&data)[N])
{
return make_array(data, std::make_index_sequence<N>());
}
template<typename T, size_t capacity>
struct ConstexprVector
{
using iterator = T*;
using const_iterator = const T*;
constexpr ConstexprVector() : m_size{0} {}
constexpr ConstexprVector(std::initializer_list<T> items)
: m_size{items.size()}
{
T* ptr = m_data;
for (auto& item : items)
*ptr++ = std::move(item);
}
constexpr bool empty() const { return m_size == 0; }
constexpr size_t size() const { return m_size; }
constexpr void resize(size_t n, const T& val = {})
{
if (n >= capacity)
throw "capacity exceeded";
for (int i = m_size; i < n; ++i)
m_data[i] = val;
m_size = n;
kak_assert(this->size() == m_size); // check for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79520
}
constexpr T& operator[](size_t i) { return m_data[i]; }
constexpr const T& operator[](size_t i) const { return m_data[i]; }
constexpr iterator begin() { return m_data; }
constexpr iterator end() { return m_data + m_size; }
constexpr const_iterator begin() const { return m_data; }
constexpr const_iterator end() const { return m_data + m_size; }
size_t m_size;
T m_data[capacity] = {};
};
}
#endif // constexpr_utils_hh_INCLUDED

View File

@ -2,6 +2,7 @@
#define input_handler_hh_INCLUDED
#include "completion.hh"
#include "constexpr_utils.hh"
#include "context.hh"
#include "face.hh"
#include "normal.hh"

View File

@ -1,10 +1,6 @@
#ifndef meta_hh_INCLUDED
#define meta_hh_INCLUDED
#include <utility>
#include <initializer_list>
#include <stddef.h>
namespace Kakoune
{
inline namespace Meta
@ -14,72 +10,6 @@ struct AnyType{};
template<typename T> struct Type : AnyType {};
}
template<typename T, size_t N>
struct Array
{
constexpr size_t size() const { return N; }
constexpr const T& operator[](int i) const { return m_data[i]; }
constexpr const T* begin() const { return m_data; }
constexpr const T* end() const { return m_data+N; }
T m_data[N];
};
template<typename T, size_t N, size_t... Indices>
constexpr Array<T, N> make_array(const T (&data)[N], std::index_sequence<Indices...>)
{
static_assert(sizeof...(Indices) == N, "size mismatch");
return {{data[Indices]...}};
}
template<typename T, size_t N>
constexpr Array<T, N> make_array(const T (&data)[N])
{
return make_array(data, std::make_index_sequence<N>());
}
template<typename T, size_t capacity>
struct ConstexprVector
{
using iterator = T*;
using const_iterator = const T*;
constexpr ConstexprVector() : m_size{0} {}
constexpr ConstexprVector(std::initializer_list<T> items)
: m_size{items.size()}
{
T* ptr = m_data;
for (auto& item : items)
*ptr++ = std::move(item);
}
constexpr bool empty() const { return m_size == 0; }
constexpr size_t size() const { return m_size; }
constexpr void resize(size_t n, const T& val = {})
{
if (n >= capacity)
throw "capacity exceeded";
for (int i = m_size; i < n; ++i)
m_data[i] = val;
m_size = n;
kak_assert(this->size() == m_size); // check for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79520
}
constexpr T& operator[](size_t i) { return m_data[i]; }
constexpr const T& operator[](size_t i) const { return m_data[i]; }
constexpr iterator begin() { return m_data; }
constexpr iterator end() { return m_data + m_size; }
constexpr const_iterator begin() const { return m_data; }
constexpr const_iterator end() const { return m_data + m_size; }
size_t m_size;
T m_data[capacity] = {};
};
}
#endif // meta_hh_INCLUDED

View File

@ -3,6 +3,8 @@
#include "enum.hh"
#include "meta.hh"
#include "vector.hh"
#include "constexpr_utils.hh"
namespace Kakoune
{
@ -46,15 +48,15 @@ enum class DebugFlags
constexpr bool with_bit_ops(Meta::Type<DebugFlags>) { return true; }
constexpr Array<EnumDesc<DebugFlags>, 5> enum_desc(Meta::Type<DebugFlags>)
constexpr auto enum_desc(Meta::Type<DebugFlags>)
{
return { {
return make_array<EnumDesc<DebugFlags>, 5>({
{ DebugFlags::Hooks, "hooks" },
{ DebugFlags::Shell, "shell" },
{ DebugFlags::Profile, "profile" },
{ DebugFlags::Keys, "keys" },
{ DebugFlags::Commands, "commands" },
} };
});
}
}