From f287df575b8af826c8794070879a5be72ee91f3d Mon Sep 17 00:00:00 2001 From: Christopher Lackner Date: Mon, 28 Jan 2019 11:02:46 +0100 Subject: [PATCH] archive enums --- libsrc/core/archive.hpp | 13 +++++++++++++ tests/catch/archive.cpp | 20 ++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/libsrc/core/archive.hpp b/libsrc/core/archive.hpp index 0ff17768..c1182b40 100644 --- a/libsrc/core/archive.hpp +++ b/libsrc/core/archive.hpp @@ -209,6 +209,19 @@ namespace ngcore return (*this); } + // archive implementation for enums + template + auto operator & (T& val) -> typename std::enable_if::value, Archive&>::type + { + int enumval; + if(Output()) + enumval = int(val); + *this & enumval; + if(Input()) + val = T(enumval); + return *this; + } + // vector has special implementation (like a bitarray) therefore // it needs a special overload (this could probably be more efficient, but we // don't use it that often anyway) diff --git a/tests/catch/archive.cpp b/tests/catch/archive.cpp index 60cd33f0..96c2087c 100644 --- a/tests/catch/archive.cpp +++ b/tests/catch/archive.cpp @@ -248,6 +248,22 @@ void testMap(Archive& in, Archive& out) CHECK(map2["netgen"] == "v6.2.1901"); } +enum MyEnum + { + CASE1, + CASE2 + }; + +void testEnum(Archive& in, Archive& out) + { + MyEnum en = CASE2; + out & en; + out.FlushBuffer(); + MyEnum enin; + in & enin; + CHECK(enin == CASE2); + } + void testArchive(Archive& in, Archive& out) { SECTION("Empty String") @@ -301,6 +317,10 @@ void testArchive(Archive& in, Archive& out) { testMap(in, out); } + SECTION("enum") + { + testEnum(in, out); + } } TEST_CASE("BinaryArchive")