Created
December 13, 2018 22:25
-
-
Save dolinenkov/addf6706f7532b4da0901ca48155c8a2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <algorithm> | |
#include <type_traits> | |
#include "TypeList.hh" | |
namespace SimplePhil { | |
template<class... Args> | |
class Variant { | |
private: | |
using Traits = TypeList<Args...>; | |
public: | |
/*template<class T> | |
struct Default | |
{ | |
using Type = T; | |
};*/ | |
struct IncorrectTypeException {}; | |
~Variant(); | |
Variant(); | |
Variant(const Variant &); | |
Variant(Variant &&); | |
Variant & operator=(const Variant &); | |
Variant & operator=(Variant &&); | |
template<class Sample, class... Arguments> | |
Sample & init(Arguments && ...); | |
bool empty() const; | |
template<class Sample> | |
bool is() const; | |
template<class Sample> | |
Sample & as(); // throws | |
template<class Sample> | |
const Sample & as() const; // throws | |
private: | |
template<class T> | |
static void constructDefault(Variant<Args...> * variant) { | |
if (Traits::constructByIndex(Traits::typeIndex<T>(), variant->placement)) | |
variant->typeIndex = Traits::typeIndex<T>(); | |
else | |
variant->typeIndex = Traits::invalidTypeIndex(); | |
} | |
static void destroyDefault(Variant<Args...> * variant) { | |
if (Traits::destroyByIndex(variant->typeIndex, (void *) variant->placement)) | |
variant->typeIndex = Traits::invalidTypeIndex(); | |
} | |
private: | |
size_t typeIndex; | |
char placement[Traits::maxInstanceSize()]; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment