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
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075 #ifndef XIMOL_ASSIGNMENT_HPP_
00076 #define XIMOL_ASSIGNMENT_HPP_
00077
00078 #include <ximol/str_cast.hpp>
00079 #include <ximol/error.hpp>
00080 #include <ximol/translation.hpp>
00081
00082 #include <boost/mpl/if.hpp>
00083 #include <boost/type_traits.hpp>
00084
00085 XIMOL_BEGIN_NAMESPACE
00086
00087
00088
00089
00090
00091
00092 namespace assignment_private_helper {
00093
00094
00095 template<typename T>
00096 struct char_type_choice {
00097 typedef typename ::boost::mpl::if_c<
00098 ::boost::is_convertible<T, ::std::string const&>::value,
00099 ::std::string,
00100 ::std::wstring
00101 >::type type;
00102 };
00103
00104 };
00105
00106
00107
00108
00109
00110
00111 namespace assignment_test_equality_policy
00112 {
00113
00114 struct error_policy_throw {
00115 static void object_are_different(){
00116 XIMOL_THROW << _(L"The two value should be equal") << XIMOL_AS_ERROR;
00117 };
00118 };
00119
00120
00121 template < typename policy = error_policy_throw >
00122 struct standard {
00123 template<typename T, typename U>
00124 static void value(T const& t_, U const& u_)
00125 {
00126 if (! (u_==t_) )
00127 policy::object_are_different();
00128 };
00129 };
00130
00131
00132 template < typename policy = error_policy_throw >
00133 struct char_serialization {
00134 template<typename T, typename U>
00135 static void value(T const& t_, U const& u_)
00136 {
00137 if(str< ::std::string>::cast(u_)!=str< ::std::string>::cast(t_))
00138 policy::object_are_different();
00139 }
00140 };
00141
00142
00143 template < typename policy = error_policy_throw >
00144 struct wchar_serialization {
00145 template<typename T, typename U>
00146 static void value(T const& t_, U const& u_)
00147 {
00148 if(str< ::std::wstring>::cast(u_)!=str< ::std::wstring>::cast(t_))
00149 policy::object_are_different();
00150 }
00151 };
00152
00153
00154 template < typename policy = error_policy_throw >
00155 struct serialization {
00156 template<typename T, typename U>
00157 static void value(T const& t_, U const& u_)
00158 {
00159 using namespace assignment_private_helper;
00160 if(str< typename char_type_choice<T>::type >::cast(u_)!=str< typename char_type_choice<T>::type >::cast(t_))
00161 policy::object_are_different();
00162 }
00163 };
00164 };
00165
00166
00167
00168
00169
00170
00171 namespace assignment_equality_policy
00172 {
00173
00174 struct standard {
00175 template<typename T, typename U>
00176 static void value(T& t_, U const& u_)
00177 { t_ = u_ ; }
00178 };
00179
00180
00181 struct char_serialization {
00182 template<typename T, typename U>
00183 static void value(T& t_, U const& u_)
00184 {
00185 ::std::istringstream is(str<std::string>::cast(u_));
00186 is >> t_;
00187 }
00188 };
00189
00190
00191 struct wchar_serialization {
00192 template<typename T, typename U>
00193 static void value(T& t_, U const& u_)
00194 {
00195 ::std::wistringstream is(str<std::wstring>::cast(u_));
00196 is >> t_;
00197 }
00198 };
00199
00200
00201 struct to_string {
00202 template<typename T, typename U>
00203 static void value(T& t_, U const& u_)
00204 {
00205 using namespace assignment_private_helper;
00206 t_ = str< typename char_type_choice<T>::type >::cast(u_).c_str();
00207 }
00208 };
00209 };
00210
00211
00212
00213
00214
00215
00216
00217 template<typename assign, typename test_equality>
00218 struct assignment {
00219 template<typename T, typename U>
00220 static T& equality(T& t_, U const& u_) {
00221 ::boost::mpl::if_c<
00222 ::boost::is_const<T>::value ||
00223 ::boost::is_array<T>::value,
00224 test_equality,
00225 assign
00226 >::type::value(t_, u_);
00227 return t_;
00228 }
00229 };
00230
00231
00232
00233
00234
00235 typedef assignment<assignment_equality_policy::to_string, assignment_test_equality_policy::serialization<> > assignment_to_string;
00236
00237 typedef assignment<assignment_equality_policy::standard, assignment_test_equality_policy::standard<> > assignment_std;
00238
00239 typedef assignment<assignment_equality_policy::char_serialization, assignment_test_equality_policy::char_serialization<> > assignment_char_serialization;
00240
00241 typedef assignment<assignment_equality_policy::char_serialization, assignment_test_equality_policy::char_serialization<> > assignment_wchar_serialization;
00242
00243
00244
00245 XIMOL_END_NAMESPACE
00246
00247 #endif // #ifndef XIMOL_ASSIGNMENT_HPP_