Created
November 13, 2014 11:08
-
-
Save thynson/8a03582d56664c13ff72 to your computer and use it in GitHub Desktop.
Type enumerator
This file contains 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
/* | |
* Copyright (C) 2014 LAN Xingcan | |
* All right reserved | |
* | |
* Permission to use, copy, modify, and/or distribute this software for any | |
* purpose with or without fee is hereby granted, provided that the above | |
* copyright notice and this permission notice appear in all copies. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
*/ | |
#ifndef LANXC_TYPE_NUMBERATOR_HPP_INCLUDED | |
#define LANXC_TYPE_NUMBERATOR_HPP_INCLUDED | |
#include <type_traits> | |
namespace lanxc | |
{ | |
template<typename ...Types> | |
struct type_enumerator; | |
template<> | |
struct type_enumerator<> | |
{ | |
template<typename> | |
struct unique_checker {}; | |
template<typename Type, typename ...Types> | |
struct value_evaluator; | |
template<unsigned Value, typename ...Types> | |
struct type_evaluator; | |
}; | |
template<typename Type, typename ...Remains> | |
struct type_enumerator<>::value_evaluator<Type, Type, Remains...> | |
{ | |
static constexpr unsigned value = 1; | |
}; | |
template<typename Type, typename First, typename ...Remains> | |
struct type_enumerator<>::value_evaluator<Type, First, Remains...> | |
{ | |
static_assert(sizeof...(Remains) > 0, "Type not in type list"); | |
static constexpr unsigned value = 1 + type_enumerator<>::value_evaluator<Type, Remains...>::value; | |
}; | |
template<typename First, typename ...Types> | |
struct type_enumerator<>::type_evaluator<1, First, Types...> | |
{ | |
using type = First; | |
}; | |
template<unsigned Value, typename First, typename ...Types> | |
struct type_enumerator<>::type_evaluator<Value, First, Types...> | |
{ | |
static_assert(Value <= sizeof...(Types) + 1, "Value is greater than length of type list"); | |
using type = typename type_enumerator<>::type_evaluator<Value - 1, Types...>; | |
}; | |
template<typename ...Args> | |
struct type_enumerator : type_enumerator<>::unique_checker<Args>... | |
{ | |
template<typename T> | |
static constexpr typename std::enable_if< | |
std::is_base_of<type_enumerator<>::unique_checker<T>, type_enumerator>::value, | |
unsigned>::type | |
value() | |
{ | |
return type_enumerator<>::value_evaluator<T, Args...>::value; | |
} | |
template<unsigned N> | |
using type = typename type_enumerator<>::type_evaluator<N, Args...>::type; | |
}; | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment