summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMaxime Coste <mawww@kakoune.org>2017-11-12 13:01:18 +0800
committerMaxime Coste <mawww@kakoune.org>2017-11-12 13:01:18 +0800
commit00e06302722a5b15a292b10d165dff640a3d3ce0 (patch)
treea05cc739b011f5d2d697671b0a9e11662e8adaaa /src
parent5cfccad39cd4b215736f8560a55404f78bae4b7d (diff)
Move Array and ConstexprVector to a constexpr_utils.hh header
Diffstat (limited to 'src')
-rw-r--r--src/buffer.hh1
-rw-r--r--src/client.hh1
-rw-r--r--src/constexpr_utils.hh78
-rw-r--r--src/input_handler.hh1
-rw-r--r--src/meta.hh70
-rw-r--r--src/option.hh8
6 files changed, 86 insertions, 73 deletions
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 <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
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 <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
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<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" },
- } };
+ });
}
}