ximol/assignment.hpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002  *                                                                           *
00003  * library XiMoL                                                             *
00004  * Copyright (C) 2002, 2003, 2004 Florent Tournois                           *
00005  *                                                                           *
00006  * This library is free software; you can redistribute it and/or             *
00007  * modify it under the terms of the GNU Lesser General Public                *
00008  * License as published by the Free Software Foundation; either              *
00009  * version 2.1 of the License, or (at your option) any later version.        *
00010  *                                                                           *
00011  * This library is distributed in the hope that it will be useful,           *
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00014  * Lesser General Public License for more details.                           *
00015  *                                                                           *
00016  * You should have received a copy of the GNU Lesser General Public          *
00017  * License along with this library; if not, write to the Free Software       *
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA   *
00019  *                                                                           *
00020 \*****************************************************************************/
00021 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00022 /** \file 
00023         \brief Assignment declaration.
00024     In this file, we define all helper for the equality problem.
00025     In some cases, we need to assign a const string and then we just want to
00026     test the equality. All these helpers are here for this problem.
00027 
00028     \author Tournois Florent
00029         \version 1.0
00030 
00031     $Id: assignment.hpp,v 1.11 2004/02/25 18:58:39 tournois Exp $
00032     $Log: assignment.hpp,v $
00033     Revision 1.11  2004/02/25 18:58:39  tournois
00034     imporve the gcc compatibility.
00035 
00036     Revision 1.10  2004/02/25 15:57:06  tournois
00037     no message
00038 
00039     Revision 1.9  2004/02/22 22:36:23  hfp
00040     *** empty log message ***
00041 
00042     Revision 1.8  2004/02/22 10:27:32  tournois
00043     Add some doc.
00044 
00045     Revision 1.7  2004/02/22 09:54:19  tournois
00046     Change years on the copyright.
00047 
00048     Revision 1.6  2004/02/15 10:04:36  hfp
00049     * assignment_private_helper: all the things are already part of boost...
00050        * select -> mpl::if_c
00051        * equal -> now use of equal is more specified to boost::type_traits:
00052          is_const, is_convertible, ...
00053        * assignment (struct) is more special: test_equality is also selected
00054           in case of arrays
00055 
00056     Revision 1.5  2004/01/29 20:52:35  tournois
00057     doc and tutorial.
00058 
00059     Revision 1.4  2004/01/27 21:49:51  tournois
00060     Add docs and tutorial.
00061 
00062     Revision 1.3  2004/01/19 20:40:55  tournois
00063     Add min, max and digits facet.
00064     Create the control flow file.
00065 
00066     Revision 1.2  2004/01/18 11:40:58  tournois
00067     Add the pattern facet.
00068 
00069     Revision 1.1  2004/01/08 20:02:29  tournois
00070     Add XIMOL_XML_NAMESPACE_PATH::comment and assignment mainuplator.
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 /** A small namespace to hide the dirty stuff.
00089  *  In this namespace we have define some classic type manipulation.
00090  */
00091 //-----------------------------------------------------------------------------
00092 namespace assignment_private_helper {
00093 
00094     /// test the character type for the serialisation
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, // condition
00099                         ::std::string, // if true
00100                         ::std::wstring // else case
00101                 >::type type;
00102         };
00103 
00104 }; // end of the namespace assignment_private_helper
00105 
00106 //-----------------------------------------------------------------------------
00107 /** Namespace for the assignment test option.
00108  *  If we need to test the equality, here are some possibility. 
00109  */
00110 //-----------------------------------------------------------------------------
00111 namespace assignment_test_equality_policy 
00112 {
00113     /// throw error when test are false
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     /// standard equality (x==y)
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     /// test with small char serialisation
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     /// test with wide char serialisation
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     /// test with char (choose at compile time serialisation)
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 }; // end of namespace assignment_test_equality_policy
00165 
00166 //-----------------------------------------------------------------------------
00167 /** Namespace for the assignment option.
00168  *  If we need to assign something, here are some possibility. 
00169  */
00170 //-----------------------------------------------------------------------------
00171 namespace assignment_equality_policy 
00172 {
00173     /// standard assignment (x=y)
00174     struct standard {
00175         template<typename T, typename U>
00176         static void value(T& t_, U const& u_)
00177         { t_ = u_ ; }
00178     };
00179 
00180     /// assignment with small char serialization
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     /// assignment with wide char serialization
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     /// assignment to a string
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 }; // end of namespace assignment_equality_policy
00210 
00211 //=============================================================================
00212 /** Assignment struct with options.
00213  *  assign type is the assignment option
00214  *  test_equality is the test type option
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 ||  // condition A
00223                         ::boost::is_array<T>::value,    // condition B: an assign could be specialized for arrays, but...
00224             test_equality, // true
00225             assign // false
00226         >::type::value(t_, u_);
00227         return t_;
00228     }
00229 };
00230 
00231 ///@name assignment type defined
00232 //@{
00233 
00234 /// assignment for string type
00235 typedef assignment<assignment_equality_policy::to_string, assignment_test_equality_policy::serialization<> > assignment_to_string;
00236 /// assignment standard
00237 typedef assignment<assignment_equality_policy::standard, assignment_test_equality_policy::standard<> > assignment_std;
00238 /// assignment with small char serialization
00239 typedef assignment<assignment_equality_policy::char_serialization, assignment_test_equality_policy::char_serialization<> > assignment_char_serialization;
00240 /// assignment with wide char serialization
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_


Donate to the XiMoL project SourceForge.net Logo If you have any questions about XiMoL, you could write to tournois@users.sourceforge.net.