tinympl  0.2
mini MPL library for C++11
to_string.hpp
1 // Copyright (C) 2013, Ennio Barbaro.
2 //
3 // Use, modification, and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // See http://sbabbi.github.io/tinympl for documentation.
8 //
9 // You are welcome to contact the author at:
10 // enniobarbaro@gmail.com
11 //
12 
13 #ifndef TINYMPL_TO_STRING_HPP
14 #define TINYMPL_TO_STRING_HPP
15 
16 #include <tinympl/string.hpp>
17 
18 namespace tinympl
19 {
20 namespace detail
21 {
22 
23 //Handle generic numbers
24 template<class T,T value,T base = 10,class = void> struct to_string_impl
25 {
26  typedef typename to_string_impl<T,value / base,base>::type head;
27  typedef typename to_string_impl<T,value % base,base>::type tail;
28  typedef typename head::template append<tail>::type type;
29 };
30 
31 //Handle negative numbers
32 template<class T,T value,T base> struct to_string_impl<T,value,base,
33  typename std::enable_if<(value < 0)>::type>
34 {
35  typedef typename to_string_impl<T,-value,base>::type tail;
36  typedef typename tail::template insert_c<0,'-'>::type type;
37 };
38 
39 //Handle one digit numbers
40 template<class T,T value,T base> struct to_string_impl<T,value,base,
41  typename std::enable_if<(value >= 0 && value < base)>::type>
42 {
43  static_assert( value >= 0 && value < 16,"Base > 16 not supported");
44 
45  typedef basic_string<char,
46  (value < 10 ?
47  '0' + value :
48  'a' + value - 10)> type;
49 };
50 
51 }
52 
58 template<class T,T value> using to_string = detail::to_string_impl<T,value>;
60 template<class T,T value> using to_string_t = typename to_string<T,value>::type;
61 
63 template<int value> using to_string_i = detail::to_string_impl<int,value>;
64 template<int value> using to_string_i_t = typename to_string_i<value>::type;
65 
67 template<long value> using to_string_l = detail::to_string_impl<long,value>;
68 template<long value> using to_string_l_t = typename to_string_l<value>::type;
69 
71 template<unsigned value> using to_string_u = detail::to_string_impl<unsigned,value>;
72 template<unsigned value> using to_string_u_t = typename to_string_u<value>::type;
73 
75 template<long long value> using to_string_ll = detail::to_string_impl<long long,value>;
76 template<long long value> using to_string_ll_t = typename to_string_ll<value>::type;
77 
80 }
81 
82 #endif // TINYMPL_TO_STRING_HPP