SigUtil  0.95
Utility modules for modern C++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
type_convert.hpp
Go to the documentation of this file.
1 /*
2 Copyright© 2014 Akihiro Nishimura
3 
4 This software is released under the MIT License.
5 http://opensource.org/licenses/mit-license.php
6 */
7 
8 #ifndef _SIG_UTIL_TYPE_CONVERT_HPP
9 #define _SIG_UTIL_TYPE_CONVERT_HPP
10 
11 #include "type_traits.hpp"
12 
13 namespace sig
14 {
15 namespace impl
16 {
17 
18 template <class T>
20 {
21  using type = typename std::remove_const<typename std::remove_reference<T>::type>::type;
22 };
23 
24 template <size_t N1, size_t N2>
25 struct plus_t
26 {
27  static const size_t value = N1 + N2;
28 };
29 
30 
31 // out/in type to stream (mainly filestream)
32 template <class FILE_STRING>
34  typedef std::ofstream ofstream;
35  typedef std::ifstream ifstream;
36  typedef std::ostreambuf_iterator<char> ostreambuf_iterator;
37  typedef std::istreambuf_iterator<char> istreambuf_iterator;
38 };
39 template<> struct FStreamSelector<std::wstring>{
40  typedef std::wofstream ofstream;
41  typedef std::wifstream ifstream;
42  typedef std::ostreambuf_iterator<wchar_t> ostreambuf_iterator;
43  typedef std::istreambuf_iterator<wchar_t> istreambuf_iterator;
44 };
45 template<> struct FStreamSelector<wchar_t const*>{
46  typedef std::wofstream ofstream;
47  typedef std::wifstream ifstream;
48  typedef std::ostreambuf_iterator<wchar_t> ostreambuf_iterator;
49  typedef std::istreambuf_iterator<wchar_t> istreambuf_iterator;
50 };
51 
52 // out/in type to stringstream
53 template <class T> struct SStreamSelector{
54  typedef std::ostringstream ostringstream;
55  typedef std::istringstream istringstream;
56  typedef std::string string;
57 };
58 template<> struct SStreamSelector<std::wstring>{
59  typedef std::wostringstream ostringstream;
60  typedef std::wistringstream istringstream;
61  typedef std::wstring string;
62 };
63 template<> struct SStreamSelector<wchar_t const*>{
64  typedef std::wostringstream ostringstream;
65  typedef std::wistringstream istringstream;
66  typedef std::wstring string;
67 };
68 
69 // string to each number type
70 template <class NUM> struct Str2NumSelector{};
71 template <> struct Str2NumSelector<int>{
72  int operator()(std::string s){ return std::stoi(s); }
73 };
74 template <> struct Str2NumSelector<long>{
75  long operator()(std::string s){ return std::stol(s); }
76 };
77 template <> struct Str2NumSelector<long long>{
78  long long operator()(std::string s){ return std::stoll(s); }
79 };
80 template <> struct Str2NumSelector<unsigned int>{
81  unsigned int operator()(std::string s){ return std::stoul(s); }
82 };
83 template <> struct Str2NumSelector<unsigned long>{
84  unsigned long operator()(std::string s){ return std::stoul(s); }
85 };
86 template <> struct Str2NumSelector<unsigned long long>{
87  unsigned long long operator()(std::string s){ return std::stoull(s); }
88 };
89 template <> struct Str2NumSelector<float>{
90  float operator()(std::string s){ return std::stof(s); }
91 };
92 template <> struct Str2NumSelector<double>{
93  double operator()(std::string s){ return std::stod(s); }
94 };
95 
96 // convert string-related type T into STL string type
97 template <class T>
98 struct StringId{ using type = void; static const bool value = false; };
99 template <>
100 struct StringId<std::string>{ using type = std::string; static const bool value = true; };
101 template <>
102 struct StringId<char*>{ using type = std::string; static const bool value = true; };
103 template <>
104 struct StringId<char const*>{ using type = std::string; static const bool value = true; };
105 template <>
106 struct StringId<std::wstring>{ using type = std::wstring; static const bool value = true; };
107 template <>
108 struct StringId<wchar_t*>{ using type = std::wstring; static const bool value = true; };
109 template <>
110 struct StringId<wchar_t const*>{ using type = std::wstring; static const bool value = true; };
111 
112 //
113 template <class T>
114 using string_t = typename StringId<
115  typename remove_const_reference<
116  typename std::decay<T>::type
117  >::type
118 >::type;
119 
120 template <class T>
121 struct is_string
122 {
123  static const bool value = StringId<
124  typename remove_const_reference<
125  typename std::decay<T>::type
126  >::type
127  >::value;
128 };
129 
130 // combine std::conditional and std::same
131 template <class T1, class T2, class TrueT, class FalseT>
132 struct SameIf{
133  using type = typename std::conditional<std::is_same<T1, T2>::value, TrueT, FalseT>::type;
134 };
135 
136 } // impl
137 
138 } // sig
139 #endif
std::istringstream istringstream
typename std::remove_const< typename std::remove_reference< T >::type >::type type
static const size_t value
std::ostreambuf_iterator< char > ostreambuf_iterator
long long operator()(std::string s)
unsigned long long operator()(std::string s)
static const bool value
std::istreambuf_iterator< wchar_t > istreambuf_iterator
std::ostreambuf_iterator< wchar_t > ostreambuf_iterator
std::ostreambuf_iterator< wchar_t > ostreambuf_iterator
typename std::conditional< std::is_same< T1, T2 >::value, TrueT, FalseT >::type type
unsigned long operator()(std::string s)
std::ostringstream ostringstream
unsigned int operator()(std::string s)
std::istreambuf_iterator< wchar_t > istreambuf_iterator
std::istreambuf_iterator< char > istreambuf_iterator
Definition: array.hpp:15
static const bool value
typename StringId< typename remove_const_reference< typename std::decay< T >::type >::type >::type string_t