ximol/traits.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 Traits.
00024 
00025     \author Tournois Florent
00026         \version 1.0
00027 
00028     $Id: traits.hpp,v 1.4 2004/02/22 10:27:33 tournois Exp $
00029     $Log: traits.hpp,v $
00030     Revision 1.4  2004/02/22 10:27:33  tournois
00031     Add some doc.
00032 
00033     Revision 1.3  2004/02/22 09:54:19  tournois
00034     Change years on the copyright.
00035 
00036     Revision 1.2  2003/12/03 13:21:58  hfp
00037     include dependencies and type forwarding
00038 
00039     Revision 1.1  2003/11/17 22:06:59  tournois
00040     Add traits for relation stream--object.
00041     Change cast.
00042 
00043   */
00044 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00045 #ifndef XIMOL_TRAITS_HPP_
00046 #define XIMOL_TRAITS_HPP_
00047 
00048 #include <iosfwd>
00049 
00050 
00051 XIMOL_BEGIN_NAMESPACE
00052 
00053 namespace private_detail_about_traits 
00054 {
00055     typedef char yes;
00056     struct no { char dummy[2]; };
00057 
00058     template <typename T>
00059     struct make { static T& type(); };
00060 
00061     template <typename U>
00062     struct is
00063     {
00064         static no   type(...);
00065         static yes  type(U);
00066     };
00067 
00068     struct does_not_exist { char dummy[17]; };
00069     template < typename T, typename U> does_not_exist operator<<(T&,U&);
00070     template < typename T, typename U> does_not_exist operator>>(T&,U&);
00071     template<typename T>
00072     T& insert_in_nslookup(T&,does_not_exist&);
00073 
00074     template < typename T, typename U>
00075     struct has_inserter
00076     {
00077         enum { value= (sizeof(is<does_not_exist>::type(operator<<(insert_in_nslookup(make<T>::type(),make<does_not_exist>::type()),make<U>::type()))) == sizeof(no)) };
00078     };
00079 
00080     template < typename T, typename U>
00081     struct has_extractor
00082     {
00083         enum { value= (sizeof(is<does_not_exist>::type(operator>>(insert_in_nslookup(make<T>::type(),make<does_not_exist>::type()),make<U>::type()))) == sizeof(no)) };
00084     };
00085 };
00086 
00087 template < typename Stream, typename Object >
00088 struct stream_and_object_traits
00089 {
00090     enum { is_extractible = private_detail_about_traits::has_extractor<Stream,Object>::value };
00091     enum { is_insertible = private_detail_about_traits::has_inserter<Stream,Object>::value };
00092 };
00093 
00094 #define XIMOL_TRAITS_STANDARD_OSTREAM(type) template <> struct stream_and_object_traits< ::std::ostream, type> { enum { is_extractible = false }; enum { is_insertible = true }; };
00095 #define XIMOL_TRAITS_STANDARD_WOSTREAM(type) template <> struct stream_and_object_traits< ::std::wostream, type> { enum { is_extractible = false }; enum { is_insertible = true }; };
00096 #define XIMOL_TRAITS_STANDARD_ISTREAM(type) template <> struct stream_and_object_traits< ::std::istream, type> { enum { is_extractible = true }; enum { is_insertible = false }; };
00097 #define XIMOL_TRAITS_STANDARD_WISTREAM(type) template <> struct stream_and_object_traits< ::std::wistream, type> { enum { is_extractible = true }; enum { is_insertible = false }; };
00098 
00099 #define XIMOL_TRAITS_STANDARD(type) XIMOL_TRAITS_STANDARD_OSTREAM(type) XIMOL_TRAITS_STANDARD_WOSTREAM(type) XIMOL_TRAITS_STANDARD_ISTREAM(type) XIMOL_TRAITS_STANDARD_WISTREAM(type)
00100 
00101 XIMOL_TRAITS_STANDARD(bool           )
00102 XIMOL_TRAITS_STANDARD(short          )
00103 XIMOL_TRAITS_STANDARD(long           )
00104 XIMOL_TRAITS_STANDARD(unsigned long  )
00105 XIMOL_TRAITS_STANDARD(int            )
00106 XIMOL_TRAITS_STANDARD(unsigned int   )
00107 XIMOL_TRAITS_STANDARD(float          )
00108 XIMOL_TRAITS_STANDARD(double         )
00109 XIMOL_TRAITS_STANDARD(long double    )
00110 
00111 XIMOL_END_NAMESPACE
00112 
00113 #endif // #ifndef XIMOL_TRAITS_HPP_


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