Skip to content
Snippets Groups Projects
Commit 072c5d2d authored by Jan Trávníček's avatar Jan Trávníček
Browse files

simplify variant

parent 7cd65352
No related branches found
No related tags found
No related merge requests found
......@@ -62,14 +62,6 @@ struct variant_helper<F, Ts...> {
variant_helper<Ts...>::move(old_t, old_v, new_v);
}
 
inline static bool compareEq(size_t this_t, const void * this_v, size_t other_t, const void * other_v)
{
if (this_t == typeid(F).hash_code())
return this_t == other_t && *(reinterpret_cast<const F*>(this_v)) == *(reinterpret_cast<const F*>(other_v));
else
return variant_helper<Ts...>::compareEq(this_t, this_v, other_t, other_v);
}
inline static void print(ostream& out, const size_t id, const void* data) {
if (id == typeid(F).hash_code())
out << *reinterpret_cast<const F*>(data);
......@@ -84,17 +76,6 @@ struct variant_helper<F, Ts...> {
return variant_helper<Ts...>::string(id, data);
}
 
inline static bool compareLess(size_t this_t, const void * this_v, size_t other_t, const void * other_v)
{
if (this_t == typeid(F).hash_code() && other_t != typeid(F).hash_code()) return true;
if (this_t != typeid(F).hash_code() && other_t == typeid(F).hash_code()) return false;
if (this_t == typeid(F).hash_code() && other_t == typeid(F).hash_code())
return *(reinterpret_cast<const F*>(this_v)) < *(reinterpret_cast<const F*>(other_v));
else
return variant_helper<Ts...>::compareLess(this_t, this_v, other_t, other_v);
}
inline static int compareHelper(size_t this_t, const void * this_v, size_t other_t, const void * other_v)
{
if (this_t == typeid(F).hash_code() && other_t != typeid(F).hash_code()) return -1;
......@@ -112,10 +93,8 @@ template<> struct variant_helper<> {
inline static void destroy(size_t, void *) { }
inline static void copy(size_t, const void *, void *) { }
inline static void move(size_t, void *, void *) { }
inline static bool compareEq(size_t, const void *, size_t, const void *) { return true; }
inline static void print(ostream&, const size_t, const void *) {}
inline static std::string string(const size_t, const void *) { return ""; }
inline static bool compareLess(size_t, const void *, size_t, const void *) { return false; }
inline static int compareHelper(size_t, const void *, size_t, const void *) { return 0; }
};
 
......@@ -180,7 +159,7 @@ public:
 
bool operator== (const variant<Ts...>& other) const
{
return helper_t::compareEq(this->type_id, &this->data, other.type_id, &other.data);
return helper_t::compareHelper(this->type_id, &this->data, other.type_id, &other.data) == 0;
}
 
bool operator!= (const variant<Ts...>& other) const
......@@ -190,7 +169,7 @@ public:
 
bool operator< (const variant<Ts...>& other) const
{
return helper_t::compareLess(this->type_id, &this->data, other.type_id, &other.data);
return helper_t::compareHelper(this->type_id, &this->data, other.type_id, &other.data) < 0;
}
 
bool operator> (const variant<Ts...>& other) const
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment