From 00e06302722a5b15a292b10d165dff640a3d3ce0 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sun, 12 Nov 2017 13:01:18 +0800 Subject: [PATCH] Move Array and ConstexprVector to a constexpr_utils.hh header --- src/buffer.hh | 1 + src/client.hh | 1 + src/constexpr_utils.hh | 78 ++++++++++++++++++++++++++++++++++++++++++ src/input_handler.hh | 1 + src/meta.hh | 70 ------------------------------------- src/option.hh | 8 +++-- 6 files changed, 86 insertions(+), 73 deletions(-) create mode 100644 src/constexpr_utils.hh diff --git a/src/buffer.hh b/src/buffer.hh index d677647a..0eb6a4f4 100644 --- a/src/buffer.hh +++ b/src/buffer.hh @@ -3,6 +3,7 @@ #include "clock.hh" #include "coord.hh" +#include "constexpr_utils.hh" #include "enum.hh" #include "safe_ptr.hh" #include "scope.hh" diff --git a/src/client.hh b/src/client.hh index fd80ba24..b12bd854 100644 --- a/src/client.hh +++ b/src/client.hh @@ -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" diff --git a/src/constexpr_utils.hh b/src/constexpr_utils.hh new file mode 100644 index 00000000..f1d3ce81 --- /dev/null +++ b/src/constexpr_utils.hh @@ -0,0 +1,78 @@ +#ifndef constexpr_utils_hh_INCLUDED +#define constexpr_utils_hh_INCLUDED + +#include +#include +#include + +namespace Kakoune +{ + +template +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 +constexpr Array make_array(const T (&data)[N], std::index_sequence) +{ + static_assert(sizeof...(Indices) == N, "size mismatch"); + return {{data[Indices]...}}; +} + +template +constexpr Array make_array(const T (&data)[N]) +{ + return make_array(data, std::make_index_sequence()); +} + +template +struct ConstexprVector +{ + using iterator = T*; + using const_iterator = const T*; + + constexpr ConstexprVector() : m_size{0} {} + constexpr ConstexprVector(std::initializer_list 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 diff --git a/src/input_handler.hh b/src/input_handler.hh index 663fd81b..f4f9f1b6 100644 --- a/src/input_handler.hh +++ b/src/input_handler.hh @@ -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" diff --git a/src/meta.hh b/src/meta.hh index ce29a758..280f2b93 100644 --- a/src/meta.hh +++ b/src/meta.hh @@ -1,10 +1,6 @@ #ifndef meta_hh_INCLUDED #define meta_hh_INCLUDED -#include -#include -#include - namespace Kakoune { inline namespace Meta @@ -14,72 +10,6 @@ struct AnyType{}; template struct Type : AnyType {}; } - -template -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 -constexpr Array make_array(const T (&data)[N], std::index_sequence) -{ - static_assert(sizeof...(Indices) == N, "size mismatch"); - return {{data[Indices]...}}; -} - -template -constexpr Array make_array(const T (&data)[N]) -{ - return make_array(data, std::make_index_sequence()); -} - -template -struct ConstexprVector -{ - using iterator = T*; - using const_iterator = const T*; - - constexpr ConstexprVector() : m_size{0} {} - constexpr ConstexprVector(std::initializer_list 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 diff --git a/src/option.hh b/src/option.hh index c0d450d5..f4aafe3a 100644 --- a/src/option.hh +++ b/src/option.hh @@ -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) { return true; } -constexpr Array, 5> enum_desc(Meta::Type) +constexpr auto enum_desc(Meta::Type) { - return { { + return make_array, 5>({ { DebugFlags::Hooks, "hooks" }, { DebugFlags::Shell, "shell" }, { DebugFlags::Profile, "profile" }, { DebugFlags::Keys, "keys" }, { DebugFlags::Commands, "commands" }, - } }; + }); } }