Files
revng-revng/include/revng/ADT/KeyedObjectTraits.h
Alessandro Di Federico e62f888381 Drop KeyTraits
This commit drops the KeyTraits in favor of a std::any-like solution.
Basically, we type erase any key the user wants to employ, just exposing
a virtual version of the destructor, a comparison operator and a clone
primitive.
2021-05-06 15:42:00 +02:00

38 lines
919 B
C++

#pragma once
//
// This file is distributed under the MIT License. See LICENSE.md for details.
//
#include "revng/ADT/STLExtras.h"
#include "revng/Support/Concepts.h"
template<typename T, typename = void>
struct KeyedObjectTraits {
// static * key(const T &);
// static T fromKey(* Key);
};
// clang-format off
template<typename T>
concept HasKeyObjectTraits = requires(T a) {
{ KeyedObjectTraits<T>::key(a) };
{ KeyedObjectTraits<T>::fromKey(KeyedObjectTraits<T>::key(a)) } -> same_as<T>;
};
// clang-format on
/// Inherit if T is the key of itself
template<typename T>
struct IdentityKeyedObjectTraits {
static T key(const T &Obj) { return Obj; }
static T fromKey(T Obj) { return Obj; }
};
/// Trivial specialization for integral types
template<Integral T>
struct KeyedObjectTraits<T> : public IdentityKeyedObjectTraits<T> {};
static_assert(Integral<int>);
static_assert(HasKeyObjectTraits<int>);