conversion.hh

00001 // Copyright (C) 2001, 2002, 2003, 2004  EPITA Research and Development Laboratory
00002 //
00003 // This file is part of the Olena Library.  This library is free
00004 // software; you can redistribute it and/or modify it under the terms
00005 // of the GNU General Public License version 2 as published by the
00006 // Free Software Foundation.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this library; see the file COPYING.  If not, write to
00015 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
00016 // MA 02111-1307, USA.
00017 //
00018 // As a special exception, you may use this file as part of a free
00019 // software library without restriction.  Specifically, if other files
00020 // instantiate templates or use macros or inline functions from this
00021 // file, or you compile this file and link it with other files to
00022 // produce an executable, this file does not by itself cause the
00023 // resulting executable to be covered by the GNU General Public
00024 // License.  This exception does not however invalidate any other
00025 // reasons why the executable file might be covered by the GNU General
00026 // Public License.
00027 
00028 #ifndef OLENA_CONVERT_CONVERSION_HH
00029 # define OLENA_CONVERT_CONVERSION_HH
00030 
00031 # include <mlc/type.hh>
00032 # include <oln/core/image.hh>
00033 # include <oln/core/compose.hh>
00034 # include <ntg/utils/debug.hh>
00035 # include <oln/convert/abstract/conversion.hh>
00036 # include <functional>
00037 
00038 namespace oln {
00043   namespace convert {
00044 
00054     template<class ConvType, class Base, class InputType>
00055     struct convoutput
00056     {
00057       typedef typename abstract::conversion<ConvType, Base>::template output<InputType>::ret ret;
00058     };
00059 
00061     namespace internal {
00065       template <class C, class UF>
00066       struct compconv1_ :
00067         public std::unary_function <typename UF::argument_type,
00068           typename C::template output<typename UF::argument_type>::ret>
00069       {
00070         typedef compconv1_ self_type;
00071 
00072         typename self_type::result_type
00073         operator()(typename self_type::argument_type arg) const
00074         {
00075           return conv_(func_(arg));
00076         }
00077 
00078         compconv1_(const C& conv, const UF& func)
00079           : conv_(conv), func_(func)
00080         {}
00081       private:
00082         const C conv_;
00083         const UF func_;
00084       };
00085 
00089       template <class C, class BF>
00090       struct compconv2_ :
00091         public std::binary_function <typename BF::first_argument_type,
00092           typename BF::second_argument_type,
00093           typename C::template output<typename BF::result_type>::ret>
00094       {
00095         typedef compconv2_ self_type;
00096 
00097         typename self_type::result_type
00098         operator()(typename self_type::first_argument_type arg1,
00099                    typename self_type::second_argument_type arg2) const
00100         {
00101           return conv_(func_(arg1, arg2));
00102         }
00103 
00104         compconv2_(const C &conv, const BF &func)
00105           : conv_(conv), func_(func)
00106         {}
00107       private:
00108         const C conv_;
00109         const BF func_;
00110       };
00111 
00112     } // end of internal
00113 
00117     template <class C, class B, class UF>
00118     internal::compconv1_<C, UF>
00119     compconv1(const abstract::conversion<C, B>& conv, const UF &func)
00120     {
00121       return internal::compconv1_<C, UF>(conv.exact(), func);
00122     }
00123 
00125     template <class C, class B, class BF>
00126     internal::compconv2_<C, BF>
00127     compconv2(const abstract::conversion<C, B>& conv, const BF &func)
00128     {
00129       return internal::compconv2_<C, BF>(conv.exact(), func);
00130     }
00131 
00132 
00139     template<class C, class B, class I> inline
00140     typename mute<I, typename convoutput<C, B, oln_value_type(I)>::ret>::ret
00141     apply(const abstract::conversion<C, B>& conv, const oln::abstract::image<I>& input)
00142     {
00143       /* CONV can now be wrapped as an Adaptable Unary Function
00144          because we know the input type.  Composing CONV with the
00145          identity for the input type will cause such wrapping to
00146          happen.  */
00147       return apply(compconv1(conv, f_identity<oln_value_type(I)>()), input);
00148     }
00149 
00150   } // convert
00151 
00152   /* Export conversion and convouput into oln:: to simplify the
00153      writing of processings.  */
00154   using convert::convoutput;
00155 
00156 } // oln
00157 
00158 
00159 #endif // OLENA_CONVERT_CONVERSION_HH

Generated on Thu Apr 15 20:13:07 2004 for Olena by doxygen 1.3.6-20040222