tahoma2d/toonz/sources/include/tcg/traits.h

309 lines
7.6 KiB
C
Raw Normal View History

2016-05-17 03:04:11 +12:00
#pragma once
2016-03-19 06:57:51 +13:00
#ifndef TCG_TRAITS_H
#define TCG_TRAITS_H
// tcg includes
#include "sfinae.h"
// STD includes
#include <iterator>
//--------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
namespace tcg {
2016-03-19 06:57:51 +13:00
//****************************************************************************
// TCG traits for generic type concepts
//****************************************************************************
template <typename T>
struct traits {
2016-06-15 18:43:10 +12:00
typedef T *pointer_type;
typedef T pointed_type;
typedef T &reference_type;
typedef T referenced_type;
typedef T element_type;
2016-03-19 06:57:51 +13:00
};
template <typename T>
struct traits<T *> {
2016-06-15 18:43:10 +12:00
typedef T **pointer_type;
typedef T pointed_type;
typedef T *&reference_type;
typedef T *referenced_type;
typedef T *element_type;
2016-03-19 06:57:51 +13:00
};
template <typename T>
struct traits<T[]> : public traits<T *> {
2016-06-15 18:43:10 +12:00
typedef T element_type;
2016-03-19 06:57:51 +13:00
};
template <typename T>
2016-06-15 18:43:10 +12:00
struct traits<T &> : public traits<T> {};
2016-03-19 06:57:51 +13:00
template <>
struct traits<void> {
2016-06-15 18:43:10 +12:00
typedef void *pointer_type;
typedef void pointed_type;
typedef void reference_type;
typedef void referenced_type;
2016-03-19 06:57:51 +13:00
};
//****************************************************************************
// Qualifier removers
//****************************************************************************
template <typename T>
struct remove_const {
2016-06-15 18:43:10 +12:00
typedef T type;
2016-03-19 06:57:51 +13:00
};
template <typename T>
struct remove_const<const T> {
2016-06-15 18:43:10 +12:00
typedef T type;
2016-03-19 06:57:51 +13:00
};
template <typename T>
struct remove_ref {
2016-06-15 18:43:10 +12:00
typedef T type;
2016-03-19 06:57:51 +13:00
};
template <typename T>
struct remove_ref<T &> {
2016-06-15 18:43:10 +12:00
typedef T type;
2016-03-19 06:57:51 +13:00
};
template <typename T>
struct remove_cref {
2016-06-15 18:43:10 +12:00
typedef typename remove_const<typename remove_ref<T>::type>::type type;
2016-03-19 06:57:51 +13:00
};
//****************************************************************************
// TCG traits for function types
//****************************************************************************
template <typename Func>
2016-06-15 18:43:10 +12:00
class function_traits {
template <typename F, bool>
struct result;
template <typename F>
struct result<F, true> {
typedef typename F::result_type type;
};
template <typename F>
struct result<F, false> {
typedef struct { } type; };
template <typename Q>
static typename enable_if_exists<typename Q::result_type, char>::type
result_fun(Q *);
static double result_fun(...);
template <typename F, bool>
struct argument;
template <typename F>
struct argument<F, true> {
typedef typename F::argument_type type;
};
template <typename F>
struct argument<F, false> {
typedef void type;
};
template <typename Q>
static typename enable_if_exists<typename Q::argument_type, char>::type
argument_fun(Q *);
static double argument_fun(...);
template <typename F, bool>
struct first_arg;
template <typename F>
struct first_arg<F, true> {
typedef typename F::first_argument_type type;
};
template <typename F>
struct first_arg<F, false> {
typedef void type;
};
template <typename Q>
static typename enable_if_exists<typename Q::first_argument_type, char>::type
first_arg_fun(Q *);
static double first_arg_fun(...);
template <typename F, bool>
struct second_arg;
template <typename F>
struct second_arg<F, true> {
typedef typename F::second_argument_type type;
};
template <typename F>
struct second_arg<F, false> {
typedef void type;
};
template <typename Q>
static typename enable_if_exists<typename Q::second_argument_type, char>::type
second_arg_fun(Q *);
static double second_arg_fun(...);
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
enum {
has_result =
(sizeof(result_fun(typename tcg::traits<Func>::pointer_type())) ==
sizeof(char)),
has_argument =
(sizeof(argument_fun(typename tcg::traits<Func>::pointer_type())) ==
sizeof(char)),
has_first_arg =
(sizeof(first_arg_fun(typename tcg::traits<Func>::pointer_type())) ==
sizeof(char)),
has_second_arg =
(sizeof(second_arg_fun(typename tcg::traits<Func>::pointer_type())) ==
sizeof(char))
};
typedef typename result<Func, has_result>::type ret_type;
typedef typename argument<Func, has_argument>::type arg_type;
typedef typename first_arg<Func, has_first_arg>::type arg1_type;
typedef typename second_arg<Func, has_second_arg>::type arg2_type;
2016-03-19 06:57:51 +13:00
};
//-----------------------------------------------------------------------
template <typename Ret>
struct function_traits<Ret()> {
2016-06-15 18:43:10 +12:00
enum {
has_result = true,
has_argument = false,
has_first_arg = false,
has_second_arg = false
};
typedef Ret ret_type;
typedef void arg_type;
typedef void arg1_type;
typedef void arg2_type;
2016-03-19 06:57:51 +13:00
};
template <typename Ret>
2016-06-15 18:43:10 +12:00
struct function_traits<Ret (*)()> : public function_traits<Ret()> {};
2016-03-19 06:57:51 +13:00
template <typename Ret>
2016-06-15 18:43:10 +12:00
struct function_traits<Ret (&)()> : public function_traits<Ret()> {};
2016-03-19 06:57:51 +13:00
//-----------------------------------------------------------------------
template <typename Ret, typename Arg>
struct function_traits<Ret(Arg)> {
2016-06-15 18:43:10 +12:00
enum {
has_result = true,
has_argument = true,
has_first_arg = false,
has_second_arg = false
};
typedef Ret ret_type;
typedef Arg arg_type;
typedef void arg1_type;
typedef void arg2_type;
2016-03-19 06:57:51 +13:00
};
template <typename Ret, typename Arg>
2016-06-15 18:43:10 +12:00
struct function_traits<Ret (*)(Arg)> : public function_traits<Ret(Arg)> {};
2016-03-19 06:57:51 +13:00
template <typename Ret, typename Arg>
2016-06-15 18:43:10 +12:00
struct function_traits<Ret (&)(Arg)> : public function_traits<Ret(Arg)> {};
2016-03-19 06:57:51 +13:00
//-----------------------------------------------------------------------
template <typename Ret, typename Arg1, typename Arg2>
struct function_traits<Ret(Arg1, Arg2)> {
2016-06-15 18:43:10 +12:00
enum { has_result = true, has_first_arg = true, has_second_arg = true };
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
typedef Ret ret_type;
typedef Arg1 arg1_type;
typedef Arg2 arg2_type;
2016-03-19 06:57:51 +13:00
};
template <typename Ret, typename Arg1, typename Arg2>
2016-06-15 18:43:10 +12:00
struct function_traits<Ret (*)(Arg1, Arg2)>
: public function_traits<Ret(Arg1, Arg2)> {};
2016-03-19 06:57:51 +13:00
template <typename Ret, typename Arg1, typename Arg2>
2016-06-15 18:43:10 +12:00
struct function_traits<Ret (&)(Arg1, Arg2)>
: public function_traits<Ret(Arg1, Arg2)> {};
2016-03-19 06:57:51 +13:00
//******************************************************************************
// TCG traits for output container readers
//******************************************************************************
template <typename Reader, typename OutputData = typename Reader::value_type>
struct container_reader_traits {
2016-06-15 18:43:10 +12:00
typedef Reader reader_type;
typedef OutputData value_type;
static void openContainer(reader_type &reader) { reader.openContainer(); }
static void addElement(reader_type &reader, const value_type &data) {
reader.addElement(data);
}
static void closeContainer(reader_type &reader) { reader.closeContainer(); }
2016-03-19 06:57:51 +13:00
};
//************************************************************************************
// Notable Test traits
//************************************************************************************
template <typename T>
struct is_floating_point {
2016-06-15 18:43:10 +12:00
enum { value = false };
2016-03-19 06:57:51 +13:00
};
template <>
struct is_floating_point<float> {
2016-06-15 18:43:10 +12:00
enum { value = true };
2016-03-19 06:57:51 +13:00
};
template <>
struct is_floating_point<double> {
2016-06-15 18:43:10 +12:00
enum { value = true };
2016-03-19 06:57:51 +13:00
};
template <>
struct is_floating_point<long double> {
2016-06-15 18:43:10 +12:00
enum { value = true };
2016-03-19 06:57:51 +13:00
};
//-----------------------------------------------------------------------
template <typename T>
struct is_function {
2016-06-15 18:43:10 +12:00
enum { value = function_traits<T>::has_result };
2016-03-19 06:57:51 +13:00
};
template <typename T>
struct is_functor {
2016-06-15 18:43:10 +12:00
template <typename Q>
static typename enable_if_exists<typename Q::result_type, char>::type result(
Q *);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
static double result(...);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
enum {
value = (sizeof(result(typename tcg::traits<T>::pointer_type())) ==
sizeof(char))
};
2016-03-19 06:57:51 +13:00
};
2016-06-15 18:43:10 +12:00
} // namespace tcg
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
#endif // TCG_TRAITS_H