convertTo.hpp

00001 //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
00002 //
00003 //        This file is part of E-Cell Simulation Environment package
00004 //
00005 //                Copyright (C) 1996-2002 Keio University
00006 //
00007 //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
00008 //
00009 //
00010 // E-Cell is free software; you can redistribute it and/or
00011 // modify it under the terms of the GNU General Public
00012 // License as published by the Free Software Foundation; either
00013 // version 2 of the License, or (at your option) any later version.
00014 // 
00015 // E-Cell is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00018 // See the GNU General Public License for more details.
00019 // 
00020 // You should have received a copy of the GNU General Public
00021 // License along with E-Cell -- see the file COPYING.
00022 // If not, write to the Free Software Foundation, Inc.,
00023 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00024 // 
00025 //END_HEADER
00026 //
00027 // written by Koichi Takahashi <shafi@e-cell.org>,
00028 // E-Cell Project.
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   /** @addtogroup property
00046       
00047   @ingroup libecs
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       // strategy:
00060       // (1) if both of ToType and FromType are arithmetic, and
00061       //     are not the same type, use NumericCaster.
00062       // (2) otherwise, just try StaticCaster.
00063 
00064       typedef typename boost::mpl::if_c<
00065         ( boost::is_arithmetic<FromType>::value &&
00066           boost::is_arithmetic<ToType>::value ) &&  // both are arithmetic, and
00067         ! boost::is_same<FromType,ToType>::value,   // not the same type.
00068         NumericCaster<ToType,FromType>,
00069         StaticCaster<ToType,FromType>
00070         >::type
00071         Converter;
00072       
00073       return Converter()( aValue );
00074     }
00075   };
00076 
00077 
00078   // ConvertTo specializations
00079 
00080 
00081   // for String
00082 
00083   // from String
00084 
00085   template< typename ToType >
00086   class ConvertTo< ToType, String >
00087   {
00088   public:
00089     inline const ToType operator()( StringCref aValue )
00090     {
00091       // strategy:
00092       // (1) if ToType is arithmetic, use LexicalCaster.
00093       // (2) otherwise try StaticCaster
00094 
00095       typedef typename boost::mpl::if_< 
00096         boost::is_arithmetic< ToType >,
00097         LexicalCaster< ToType, String >,     // is arithmetic
00098         StaticCaster< ToType, String >       // is not
00099         >::type
00100         Converter;
00101 
00102       return Converter()( aValue );
00103     }
00104   };
00105   
00106   // to String
00107 
00108   template< typename FromType >
00109   class ConvertTo< String, FromType >
00110   {
00111   public:
00112     inline const String operator()( const FromType& aValue )
00113     {
00114       // strategy:
00115       // (1) if FromType is arithmetic, use LexicalCaster.
00116       // (2) otherwise try StaticCaster.
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   // convertTo template function
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 /* __CONVERTTO_HPP */

Generated on Mon Dec 18 07:24:07 2006 for E-CELL C++ libraries (libecs and libemc) 3.1.105 by  doxygen 1.5.1