/*-----------------------------------------------------------------------------+ Copyright (c) 2007-2009: Joachim Faulhaber +------------------------------------------------------------------------------+ Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENCE.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +-----------------------------------------------------------------------------*/ #ifndef BOOST_ICL_TYPE_TO_STRING_HPP_JOFA_080416 #define BOOST_ICL_TYPE_TO_STRING_HPP_JOFA_080416 #include #include #include #include #include #include #include #include namespace boost{ namespace icl { // Primary template: note the additional defaulted Enable parameter so we can SFINAE template struct type_to_string { /** Convert the type to its typestring */ static std::string apply(); }; //-------------------------------------------------------------------------- // Explicit specializations for builtins (Enable defaults to void) template<> inline std::string type_to_string::apply() { return "bool"; } template<> inline std::string type_to_string::apply() { return "char"; } template<> inline std::string type_to_string::apply(){ return "short"; } template<> inline std::string type_to_string::apply() { return "int"; } template<> inline std::string type_to_string::apply() { return "long"; } template<> inline std::string type_to_string::apply(){ return "Long"; } template<> inline std::string type_to_string::apply(){ return "char+"; } template<> inline std::string type_to_string::apply(){ return "short+"; } template<> inline std::string type_to_string::apply() { return "int+"; } template<> inline std::string type_to_string::apply() { return "long+"; } template<> inline std::string type_to_string::apply(){ return "Long+"; } template<> inline std::string type_to_string::apply() { return "flt"; } template<> inline std::string type_to_string::apply() { return "dbl"; } //------------------------------------------------------------------------- template class Templ> struct unary_template_to_string { static std::string apply(); }; // --------------------------------------------------------------------------- template class Templ> struct binary_template_to_string { static std::string apply(); }; // --------------------------------------------------------------------------- template<> struct type_to_string { static std::string apply() { return "string"; } }; // ======================================================================= // Constrained partial specializations: // - unary: matches only when the template instantiation has exactly 1 type parameter // - binary: matches only when the template instantiation has exactly 2 type parameters // // These use template class ...> + sizeof...(Args) to SFINAE-out // matches for templates with different number of type-parameters (including those // that have additional defaulted parameters). // ======================================================================= // unary: exactly one type parameter (C++11-compatible enable_if) template< template class Unary, class... Args > struct type_to_string< Unary, typename std::enable_if< (sizeof...(Args) == 1), void >::type > { using First = typename std::tuple_element<0, std::tuple >::type; static std::string apply() { return unary_template_to_string::apply() + "<" + type_to_string::apply() + ">"; } }; // binary: exactly two type parameters (C++11-compatible enable_if) template< template class Binary, class... Args > struct type_to_string< Binary, typename std::enable_if< (sizeof...(Args) == 2), void >::type > { using First = typename std::tuple_element<0, std::tuple >::type; using Second = typename std::tuple_element<1, std::tuple >::type; static std::string apply() { return binary_template_to_string::apply() + "<" + type_to_string::apply() + "," + type_to_string::apply() + ">"; } }; }} // namespace boost icl #endif