#pragma once // 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 MATCH_BEGIN(expr) \ { \ auto&& _match_var = (expr); \ switch (tmwa::sexpr::VariantFriend::get_state(_match_var)) \ { \ { \ { \ /* }}}} */ #define MATCH_END() \ /* {{{{ */ \ } \ } \ } \ (void) _match_var; \ } #define MATCH_CASE(ty, v) \ /* {{{{ */ \ } \ break; \ } \ { \ using _match_case_type = std::remove_const::type>::type; \ case tmwa::sexpr::VariantFriend::get_state_for<_match_case_type, decltype(_match_var)>(): \ { \ ty v = tmwa::sexpr::VariantFriend::unchecked_get<_match_case_type>(_match_var); /* }}}} */ #define MATCH_DEFAULT() \ /* {{{{ */ \ } \ break; \ } \ { \ default: \ { \ /* }}}} */ 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 using sexpr::Variant; } // namespace tmwa #include "variant.tcc"