From 63c2e854ed2c9778ea9225d86578b4e9b65900ee Mon Sep 17 00:00:00 2001 From: Ben Longbons Date: Fri, 27 Jun 2014 18:10:48 -0700 Subject: Port the Variant junk --- src/sexpr/variant.hpp | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 src/sexpr/variant.hpp (limited to 'src/sexpr/variant.hpp') diff --git a/src/sexpr/variant.hpp b/src/sexpr/variant.hpp new file mode 100644 index 0000000..ccbfeaa --- /dev/null +++ b/src/sexpr/variant.hpp @@ -0,0 +1,119 @@ +#ifndef TMWA_SEXPR_VARIANT_HPP +#define TMWA_SEXPR_VARIANT_HPP +// variant.hpp - A single value, multiple type container. Better than boost's. +// +// Copyright © 2012 Ben Longbons +// +// This file is part of The Mana World (Athena server) +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +# include +# include +# include +# include + +# include "union.hpp" +# include "void.hpp" + +# include "fwd.hpp" + + +namespace tmwa +{ +namespace sexpr +{ +# define JOIN(a, b) a##b + +# define WITH_VAR(ty, var, expr) \ + for (bool JOIN(var, _guard) = true; JOIN(var, _guard); ) \ + for (ty var = expr; JOIN(var, _guard); JOIN(var, _guard) = false) +# define MATCH(expr) \ + WITH_VAR(auto&&, _match_var, expr) \ + switch (tmwa::sexpr::VariantFriend::get_state(_match_var)) +# define CASE(ty, var) \ + break; \ + case tmwa::sexpr::VariantFriend::get_state_for(): \ + WITH_VAR(ty, var, tmwa::sexpr::VariantFriend::unchecked_get(_match_var)) + + template + class Variant + { + static_assert(sizeof...(T), "A variant must not be empty"); + }; + template + class Variant + { + constexpr static size_t state_count = 1 + sizeof...(T); + + // simplify things immensely + friend class VariantFriend; + + typedef Union DataType; + DataType data; + size_t state; + + void do_destruct(); + template + void do_construct(A&&... a); + public: + Variant(); + ~Variant(); + + void reset(); + template + void emplace(A&&... a); + + Variant(const Variant& r); + Variant(Variant&& r); + Variant& operator = (const Variant& r); + Variant& operator = (Variant&& r); + + template + Variant(E e) + { + do_construct(std::move(e)); + } + + template + Variant& operator = (E e) + { + emplace(std::move(e)); + return *this; + } + + // use these ONLY if only one type makes sense + // otherwise use apply + template + bool is() const; + + template + E *get_if(); + + template + const E *get_if() const; + }; + + template + void apply(R& r, F&& f); + template + void apply(R& r, F&& f, V1&& v1, V&&... v); + template + void apply(Void&& r, F&& f, V&&... v); +} // namespace sexpr +} // namespace tmwa + +# include "variant.tcc" + +#endif //TMWA_SEXPR_VARIANT_HPP -- cgit v1.2.3-70-g09d2