type_to_string.hpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*-----------------------------------------------------------------------------+
  2. Copyright (c) 2007-2009: Joachim Faulhaber
  3. +------------------------------------------------------------------------------+
  4. Distributed under the Boost Software License, Version 1.0.
  5. (See accompanying file LICENCE.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. +-----------------------------------------------------------------------------*/
  8. #ifndef BOOST_ICL_TYPE_TO_STRING_HPP_JOFA_080416
  9. #define BOOST_ICL_TYPE_TO_STRING_HPP_JOFA_080416
  10. #include <stdio.h>
  11. #include <string>
  12. #include <sstream>
  13. #include <boost/type_traits/is_integral.hpp>
  14. #include <boost/type_traits/is_float.hpp>
  15. #include <boost/mpl/if.hpp>
  16. namespace boost{ namespace icl
  17. {
  18. //--------------------------------------------------------------------------
  19. template<class Type>
  20. struct type_to_string
  21. {
  22. /** Convert the type to it's typestring */
  23. static std::string apply();
  24. };
  25. //--------------------------------------------------------------------------
  26. template<>inline std::string type_to_string<bool>::apply() { return "bool"; }
  27. template<>inline std::string type_to_string<char>::apply() { return "char"; }
  28. template<>inline std::string type_to_string<short>::apply(){ return "short"; }
  29. template<>inline std::string type_to_string<int>::apply() { return "int"; }
  30. template<>inline std::string type_to_string<long>::apply() { return "long"; }
  31. template<>inline std::string type_to_string<long long>::apply(){ return "Long"; }
  32. template<>inline std::string type_to_string<unsigned char>::apply(){ return "char+"; }
  33. template<>inline std::string type_to_string<unsigned short>::apply(){ return "short+"; }
  34. template<>inline std::string type_to_string<unsigned int>::apply() { return "int+"; }
  35. template<>inline std::string type_to_string<unsigned long>::apply() { return "long+"; }
  36. template<>inline std::string type_to_string<unsigned long long>::apply(){ return "Long+"; }
  37. template<>inline std::string type_to_string<float>::apply() { return "flt"; }
  38. template<>inline std::string type_to_string<double>::apply() { return "dbl"; }
  39. //-------------------------------------------------------------------------
  40. template<template<class> class Templ>
  41. struct unary_template_to_string
  42. {
  43. static std::string apply();
  44. };
  45. template <template<class>class Unary, class Type>
  46. struct type_to_string<Unary<Type> >
  47. {
  48. static std::string to_string()
  49. {
  50. return unary_template_to_string<Unary>::apply()+"<"+type_to_string<Type>::apply()+">";
  51. }
  52. };
  53. // ---------------------------------------------------------------------------
  54. template<template<class,class>class Templ>
  55. struct binary_template_to_string
  56. {
  57. static std::string apply();
  58. };
  59. template <template<class Type1, class Type2>class Binary, class Type1, class Type2>
  60. struct type_to_string<Binary<Type1, Type2> >
  61. {
  62. static std::string apply()
  63. {
  64. return binary_template_to_string<Binary>::apply()+
  65. "<"+type_to_string<Type1>::apply()+","+type_to_string<Type2>::apply()+">";
  66. }
  67. };
  68. // ---------------------------------------------------------------------------
  69. template<>
  70. struct type_to_string<std::string>
  71. {
  72. static std::string apply() { return "string"; }
  73. };
  74. }} // namespace boost icl
  75. #endif