From 5c51e17156530402f7860a3bf82bd57ac87f49f7 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Tue, 12 Feb 2019 03:58:14 +0000 Subject: [PATCH] [ox/mc] Add Vector handler to DefWriter, add static_assert tests --- deps/ox/src/ox/mc/defwriter.hpp | 45 ++++++++++++++++++++++++--------- deps/ox/src/ox/std/vector.hpp | 7 +++++ 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/deps/ox/src/ox/mc/defwriter.hpp b/deps/ox/src/ox/mc/defwriter.hpp index fab61341..894d68e3 100644 --- a/deps/ox/src/ox/mc/defwriter.hpp +++ b/deps/ox/src/ox/mc/defwriter.hpp @@ -31,6 +31,21 @@ static constexpr int indirectionLevels(T *t) { return 1 + indirectionLevels(*t); } +static_assert([] { + int i = 0; + return indirectionLevels(&i) == 1; +}(), "indirectionLevels broken: indirectionLevels(int*)"); + +static_assert([] { + int i[2] = {}; + return indirectionLevels(i) == 1; +}(), "indirectionLevels broken: indirectionLevels(int[])"); + +static_assert([] { + int i[2][2] = {{}}; + return indirectionLevels(i) == 2; +}(), "indirectionLevels broken: indirectionLevels(int[][])"); + class MetalClawDefWriter { private: @@ -44,16 +59,16 @@ class MetalClawDefWriter { ~MetalClawDefWriter(); template - int op(const char *name, T *val, std::size_t valLen); - - template - int op(const char *name, const char *val); - - template - int op(const char *name, ox::BString *val); + ox::Error op(const char *name, T *val, std::size_t valLen); template - constexpr int op(const char *name, T *val); + ox::Error op(const char *name, ox::Vector *val); + + template + ox::Error op(const char *name, ox::BString *val); + + template + constexpr ox::Error op(const char *name, T *val); constexpr void setTypeInfo(const char *name, int fields); @@ -87,22 +102,28 @@ class MetalClawDefWriter { // array handler template -int MetalClawDefWriter::op(const char *name, T *val, std::size_t) { +ox::Error MetalClawDefWriter::op(const char *name, T *val, std::size_t) { if (m_type) { constexpr typename RemoveIndirection::type *p = nullptr; const auto t = type(p); m_type->fieldList.push_back(mc::Field{t, name, subscriptLevels(val)}); + return 0; } - return 0; + return OxError(1); +} + +template +ox::Error MetalClawDefWriter::op(const char *name, ox::Vector *val) { + return op(name, val->data(), val->size()); } template -int MetalClawDefWriter::op(const char *name, ox::BString *val) { +ox::Error MetalClawDefWriter::op(const char *name, ox::BString *val) { return op(name, val->c_str()); } template -constexpr int MetalClawDefWriter::op(const char *name, T *val) { +constexpr ox::Error MetalClawDefWriter::op(const char *name, T *val) { if (m_type) { const auto t = type(val); m_type->fieldList.push_back(mc::Field{t, name}); diff --git a/deps/ox/src/ox/std/vector.hpp b/deps/ox/src/ox/std/vector.hpp index 75ed5601..a6fd25ec 100644 --- a/deps/ox/src/ox/std/vector.hpp +++ b/deps/ox/src/ox/std/vector.hpp @@ -43,6 +43,8 @@ class Vector { void resize(std::size_t size) noexcept; + T *data(); + bool contains(T) const noexcept; void push_back(const T &item) noexcept; @@ -153,6 +155,11 @@ void Vector::resize(std::size_t size) noexcept { m_size = size; } +template +T *Vector::data() { + return m_items; +} + template bool Vector::contains(T v) const noexcept { for (std::size_t i = 0; i < m_size; i++) {