00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef __CONVERTTO_HPP
00032 #define __CONVERTTO_HPP
00033
00034 #include <boost/static_assert.hpp>
00035 #include <boost/type_traits.hpp>
00036 #include <boost/mpl/if.hpp>
00037
00038 #include "libecs.hpp"
00039 #include "Util.hpp"
00040
00041 namespace libecs
00042 {
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 template< typename ToType, typename FromType >
00054 class ConvertTo
00055 {
00056 public:
00057 inline const ToType operator()( const FromType& aValue )
00058 {
00059
00060
00061
00062
00063
00064 typedef typename boost::mpl::if_c<
00065 ( boost::is_arithmetic<FromType>::value &&
00066 boost::is_arithmetic<ToType>::value ) &&
00067 ! boost::is_same<FromType,ToType>::value,
00068 NumericCaster<ToType,FromType>,
00069 StaticCaster<ToType,FromType>
00070 >::type
00071 Converter;
00072
00073 return Converter()( aValue );
00074 }
00075 };
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 template< typename ToType >
00086 class ConvertTo< ToType, String >
00087 {
00088 public:
00089 inline const ToType operator()( StringCref aValue )
00090 {
00091
00092
00093
00094
00095 typedef typename boost::mpl::if_<
00096 boost::is_arithmetic< ToType >,
00097 LexicalCaster< ToType, String >,
00098 StaticCaster< ToType, String >
00099 >::type
00100 Converter;
00101
00102 return Converter()( aValue );
00103 }
00104 };
00105
00106
00107
00108 template< typename FromType >
00109 class ConvertTo< String, FromType >
00110 {
00111 public:
00112 inline const String operator()( const FromType& aValue )
00113 {
00114
00115
00116
00117
00118 typedef typename boost::mpl::if_<
00119 boost::is_arithmetic< FromType >,
00120 LexicalCaster< String, FromType >,
00121 StaticCaster< String, FromType >
00122 >::type
00123 Converter;
00124
00125 return Converter()( aValue );
00126 }
00127 };
00128
00129
00130 template<>
00131 class ConvertTo< String, String >
00132 {
00133 public:
00134 inline const String operator()( const String& aValue )
00135 {
00136 return aValue;
00137 }
00138 };
00139
00140
00141
00142
00143
00144
00145 template< typename ToType, typename FromType >
00146 inline const ToType convertTo( const FromType& aValue )
00147 {
00148 return ConvertTo<ToType,FromType>()( aValue );
00149 }
00150
00151
00152
00153 }
00154
00155
00156 #endif