#pragma once #ifndef TCG_TRAITS_H #define TCG_TRAITS_H // tcg includes #include "sfinae.h" // STD includes #include //-------------------------------------------------------------------------- namespace tcg { //**************************************************************************** // TCG traits for generic type concepts //**************************************************************************** template struct traits { typedef T *pointer_type; typedef T pointed_type; typedef T &reference_type; typedef T referenced_type; typedef T element_type; }; template struct traits { typedef T **pointer_type; typedef T pointed_type; typedef T *&reference_type; typedef T *referenced_type; typedef T *element_type; }; template struct traits : public traits { typedef T element_type; }; template struct traits : public traits {}; template <> struct traits { typedef void *pointer_type; typedef void pointed_type; typedef void reference_type; typedef void referenced_type; }; //**************************************************************************** // Qualifier removers //**************************************************************************** template struct remove_const { typedef T type; }; template struct remove_const { typedef T type; }; template struct remove_ref { typedef T type; }; template struct remove_ref { typedef T type; }; template struct remove_cref { typedef typename remove_const::type>::type type; }; //**************************************************************************** // TCG traits for function types //**************************************************************************** template class function_traits { template struct result; template struct result { typedef typename F::result_type type; }; template struct result { typedef struct { } type; }; template static typename enable_if_exists::type result_fun(Q *); static double result_fun(...); template struct argument; template struct argument { typedef typename F::argument_type type; }; template struct argument { typedef void type; }; template static typename enable_if_exists::type argument_fun(Q *); static double argument_fun(...); template struct first_arg; template struct first_arg { typedef typename F::first_argument_type type; }; template struct first_arg { typedef void type; }; template static typename enable_if_exists::type first_arg_fun(Q *); static double first_arg_fun(...); template struct second_arg; template struct second_arg { typedef typename F::second_argument_type type; }; template struct second_arg { typedef void type; }; template static typename enable_if_exists::type second_arg_fun(Q *); static double second_arg_fun(...); public: enum { has_result = (sizeof(result_fun(typename tcg::traits::pointer_type())) == sizeof(char)), has_argument = (sizeof(argument_fun(typename tcg::traits::pointer_type())) == sizeof(char)), has_first_arg = (sizeof(first_arg_fun(typename tcg::traits::pointer_type())) == sizeof(char)), has_second_arg = (sizeof(second_arg_fun(typename tcg::traits::pointer_type())) == sizeof(char)) }; typedef typename result::type ret_type; typedef typename argument::type arg_type; typedef typename first_arg::type arg1_type; typedef typename second_arg::type arg2_type; }; //----------------------------------------------------------------------- template struct function_traits { 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; }; template struct function_traits : public function_traits {}; template struct function_traits : public function_traits {}; //----------------------------------------------------------------------- template struct function_traits { 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; }; template struct function_traits : public function_traits {}; template struct function_traits : public function_traits {}; //----------------------------------------------------------------------- template struct function_traits { enum { has_result = true, has_first_arg = true, has_second_arg = true }; typedef Ret ret_type; typedef Arg1 arg1_type; typedef Arg2 arg2_type; }; template struct function_traits : public function_traits {}; template struct function_traits : public function_traits {}; //****************************************************************************** // TCG traits for output container readers //****************************************************************************** template struct container_reader_traits { 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(); } }; //************************************************************************************ // Notable Test traits //************************************************************************************ template struct is_floating_point { enum { value = false }; }; template <> struct is_floating_point { enum { value = true }; }; template <> struct is_floating_point { enum { value = true }; }; template <> struct is_floating_point { enum { value = true }; }; //----------------------------------------------------------------------- template struct is_function { enum { value = function_traits::has_result }; }; template struct is_functor { template static typename enable_if_exists::type result( Q *); static double result(...); enum { value = (sizeof(result(typename tcg::traits::pointer_type())) == sizeof(char)) }; }; } // namespace tcg #endif // TCG_TRAITS_H