Main Page   Class Hierarchy   Alphabetical List   Compound List   Examples  

code.h

00001 /***************************************************************************
00002     copyright            : (C) 2002-2005 by Stefano Barbato
00003     email                : stefano@codesink.org
00004 
00005     $Id: code.h,v 1.4 2005/02/23 10:26:14 tat Exp $
00006  ***************************************************************************/
00007 
00008 /***************************************************************************
00009  *                                                                         *
00010  *   This program is free software; you can redistribute it and/or modify  *
00011  *   it under the terms of the GNU General Public License as published by  *
00012  *   the Free Software Foundation; either version 2 of the License, or     *
00013  *   (at your option) any later version.                                   *
00014  *                                                                         *
00015  ***************************************************************************/
00016 #ifndef _MIMETIC_CODEC_CODE_H_
00017 #define _MIMETIC_CODEC_CODE_H_
00018 #include <mimetic/codec/codec_base.h>
00019 #include <mimetic/codec/codec_chain.h>
00020 #include <mimetic/codec/other_codecs.h>
00021 #include <mimetic/utils.h>
00022 
00023 namespace mimetic
00024 {
00025 
00026 
00027 
00028 template<typename InIt, typename OutIt, typename Codec>
00029 void code(InIt beg, InIt end, Codec& cc, OutIt out)
00030 {
00031     typedef typename Codec::codec_type codec_type;
00032     code(beg, end, cc, out, codec_type());
00033 }
00034 
00035 // code func for buffered codecs 
00036 template<typename InIt, typename OutIt, typename Codec>
00037 void code(InIt beg, InIt end, Codec& cc, OutIt out,const buffered_codec_type_tag&)
00038 { 
00039     for(; beg != end; ++beg)
00040         cc.process(*beg, out);
00041     cc.flush(out);
00042 }
00043 
00044 // code func for unbuffered codecs 
00045 template<typename InIt, typename OutIt, typename Codec>
00046 void code(InIt beg, InIt end,Codec& codec,OutIt out,const unbuffered_codec_type_tag&)
00047 {
00048     for(; beg != end; ++beg)
00049         codec.process(*beg, out);
00050 }
00051 
00052 // code func for chained codecs
00053 template<typename InIt, typename OutIt, typename Codec, typename Next>
00054 void code(InIt beg, InIt end, const codec_chain<Codec,Next>& cc, OutIt out)
00055 {
00056     typedef codec_chain<Codec,Next> Node1;
00057     typedef codec_chain< oiterator_wrapper<OutIt> > 
00058         TailNode;
00059     typedef typename push_back_node<Node1, TailNode>::node_type 
00060         codec_chain_type;
00061 
00062     oiterator_wrapper<OutIt> oiw(out);
00063     codec_chain_type chain = build_push_back_node<Node1,TailNode>(cc,TailNode(oiw));
00064 
00065     for(; beg != end; ++beg)
00066         chain.process(*beg);
00067     chain.flush();
00068 }
00069 
00070 /// Encodes (beg, end] using \p cc codec 
00071 /*!
00072     Encodes (beg, end] using \p cc codec and write any
00073     output characters to the output iterator \p out.
00074 
00075     \p cc can be a simple codec:
00076     \code
00077         Base64::Encoder b64;
00078         code(beg, end, b64, out);
00079     \endcode
00080     or a chain of codecs:
00081     \code
00082         Base64::Encoder b64;
00083         ToUpperCase tuc;
00084         code(beg, end, tuc | b64, out);
00085     \endcode
00086  */
00087 template<typename InIt, typename OutIt, typename Codec>
00088 void encode(InIt beg, InIt end, Codec& cc, OutIt out)
00089 {
00090     code(beg, end, cc, out);
00091 }
00092 
00093 /// decodes (beg, end] using \e cc codec and write any
00094 /*!
00095     decodes (beg, end] using \e cc codec and write any
00096     output characters to the output iterator \e out
00097  */
00098 template<typename InIt, typename OutIt, typename Codec>
00099 void decode(InIt beg, InIt end, Codec& cc, OutIt out)
00100 {
00101     code(beg, end, cc, out);
00102 }
00103 
00104 
00105 template<typename InIt, typename OutIt, typename Codec, typename Next>
00106 void encode(InIt beg, InIt end, const codec_chain<Codec,Next>& cc, OutIt out)
00107 {
00108     code(beg,end,cc,out);
00109 }
00110 
00111 template<typename InIt, typename OutIt, typename Codec, typename Next>
00112 void decode(InIt beg, InIt end, const codec_chain<Codec,Next>& cc, OutIt out)
00113 {
00114     code(beg,end,cc,out);
00115 }
00116 
00117 } 
00118 
00119 
00120 
00121 #endif 
00122 
00123 
00124 
00125 
00126 
00127