Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

wvtypedencoder.h

Go to the documentation of this file.
00001 /* -*- Mode: C++ -*-
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
00004  *
00005  * An abstraction for encoders that manipulate typed buffers.
00006  */
00007 #ifndef __WVTYPEDENCODER_H
00008 #define __WVTYPEDENCODER_H
00009 
00010 #include "wvbufbase.h"
00011 #include "wvencoder.h"
00012 
00013 /**
00014  * This template facilitates the creation and use of encoders
00015  * that manipulate typed buffers.
00016  * 
00017  * A typed encoder accepts both typed and untyped buffers, but
00018  * is implementated in terms of typed buffers.  Untyped buffers
00019  * are automatically wrapped into the required form before being
00020  * passed on to the implementation.
00021  * 
00022  * This type is designed to function as a statically bound mixin
00023  * to make it easier to incorporate typed encoders into untyped
00024  * encoder hierarchies.  This is somewhat ugly, but necessary.
00025  * 
00026  *
00027  * "IT" is the input buffer datatype
00028  * "OT" is the output buffer datatype
00029  * "S" is the WvEncoder supertype
00030  * @see WvEncoder
00031  */
00032 template<class IT, class OT, class S = WvEncoder>
00033 class WvTypedEncoder : public S
00034 {
00035 public:
00036     typedef IT IType;
00037     typedef OT OType;
00038     typedef WvBufBase<IType> IBuffer;
00039     typedef WvBufBase<OType> OBuffer;
00040     typedef WvBufViewBase<IType> IBufferView;
00041     typedef WvBufViewBase<OType> OBufferView;
00042 
00043     /**
00044      * Typed variant of encode().
00045      * @see encode(WvBuf&, WvBuf&, bool, bool)
00046      */
00047     bool encode(IBuffer &inbuf, OBuffer &outbuf, bool flush = false,
00048         bool finish = false)
00049     {
00050         WvBufView inview(inbuf);
00051         WvBufView outview(outbuf);
00052         return S::encode(inview, outview, flush, finish);
00053     }
00054 
00055     /**
00056      * Typed variant of flush().
00057      * @see flush(WvBuf, WvBuf, bool)
00058      */
00059     bool flush(IBuffer &inbuf, OBuffer &outbuf, bool finish = false)
00060     {
00061         WvBufView inview(inbuf);
00062         WvBufView outview(outbuf);
00063         return S::flush(inview, outview, finish);
00064     }
00065 
00066     /**
00067      * Typed variant of finish().
00068      * @see finish(WvBuf)
00069      */
00070     bool finish(OBuffer &outbuf)
00071     {
00072         WvBufView outview(outbuf);
00073         return S::finish(outview);
00074     }
00075     
00076     bool encode(WvBuf &inbuf, WvBuf &outbuf,
00077         bool flush = false, bool finish = false)
00078     {
00079         return S::encode(inbuf, outbuf, flush, finish);
00080     }
00081     bool flush(WvBuf &inbuf, WvBuf &outbuf,
00082         bool finish = false)
00083     {
00084         return S::flush(inbuf, outbuf, finish);
00085     }
00086     bool finish(WvBuf &outbuf)
00087     {
00088         return S::finish(outbuf);
00089     }
00090 
00091 protected:
00092     /**
00093      * Typed variant of _encode().
00094      * @see _encode(WvBuf&, WvBuf&, bool)
00095      */
00096     virtual bool _typedencode(IBuffer &inbuf, OBuffer &outbuf,
00097         bool flush) = 0;
00098     
00099     /**
00100      * Typed variant of _finish().
00101      * @see _finish(WvBuf&)
00102      */
00103     virtual bool _typedfinish(OBuffer &outbuf)
00104         { return true; }
00105 
00106     /** Wrapper implementation of _encode(). */
00107     virtual bool _encode(WvBuf &inbuf, WvBuf &outbuf,
00108         bool flush)
00109     {
00110         IBufferView inview(inbuf);
00111         OBufferView outview(outbuf);
00112         return _typedencode(inview, outview, flush);
00113     }
00114     
00115     /** Wrapper implementation of _finish(). */
00116     virtual bool _finish(WvBuf &outbuf)
00117     {
00118         OBufferView outview(outbuf);
00119         return _typedfinish(outview);
00120     }
00121 };
00122 
00123 /**
00124  * Partial template specialization for unsigned char output
00125  * buffer type to avoid compilation errors.
00126  *
00127  * "IType" is the input buffer datatype
00128  */
00129 template<class IT, class S>
00130 class WvTypedEncoder<IT, unsigned char, S> : public S
00131 {
00132 public:
00133     typedef IT IType;
00134     typedef unsigned char OType;
00135     typedef WvBufBase<IType> IBuffer;
00136     typedef WvBufBase<OType> OBuffer;
00137     typedef WvBufViewBase<IType> IBufferView;
00138     typedef WvBufViewBase<OType> OBufferView;
00139 
00140     /**
00141      * Typed variant of encode().
00142      * @see encode(WvBuf&, WvBuf&, bool, bool)
00143      */
00144     bool encode(IBuffer &inbuf, OBuffer &outbuf, bool flush = false,
00145         bool finish = false)
00146     {
00147         WvBufView inview(inbuf);
00148         return S::encode(inview, outbuf, flush, finish);
00149     }
00150 
00151     /**
00152      * Typed variant of flush().
00153      * @see flush(WvBuf, WvBuf, bool)
00154      */
00155     bool flush(IBuffer &inbuf, OBuffer &outbuf, bool finish = false)
00156     {
00157         WvBufView inview(inbuf);
00158         return S::flush(inview, outbuf, finish);
00159     }
00160     
00161     bool encode(WvBuf &inbuf, WvBuf &outbuf,
00162         bool flush = false, bool finish = false)
00163     {
00164         return S::encode(inbuf, outbuf, flush, finish);
00165     }
00166     bool flush(WvBuf &inbuf, WvBuf &outbuf,
00167         bool finish = false)
00168     {
00169         return S::flush(inbuf, outbuf, finish);
00170     }
00171 
00172 protected:
00173     /**
00174      * Typed variant of _encode().
00175      * @see _encode(WvBuf&, WvBuf&, bool)
00176      */
00177     virtual bool _typedencode(IBuffer &inbuf, OBuffer &outbuf,
00178         bool flush) = 0;
00179     
00180     /**
00181      * Typed variant of _finish().
00182      * @see _finish(WvBuf&)
00183      */
00184     virtual bool _typedfinish(OBuffer &outbuf)
00185         { return true; }
00186     
00187     /** Wrapper implementation of _encode(). */
00188     virtual bool _encode(WvBuf &inbuf, WvBuf &outbuf,
00189         bool flush)
00190     {
00191         IBufferView inview(inbuf);
00192         return _typedencode(inview, outbuf, flush);
00193     }
00194     
00195     /** Wrapper implementation of _finish(). */
00196     virtual bool _finish(WvBuf &outbuf)
00197     {
00198         return _typedfinish(outbuf);
00199     }
00200 };
00201 
00202 
00203 /**
00204  * Partial template specialization for unsigned char input
00205  * and output buffer types to avoid compilation errors.
00206  */
00207 template<class S>
00208 class WvTypedEncoder<unsigned char, unsigned char, S> : public S
00209 {
00210 public:
00211     typedef unsigned char IType;
00212     typedef unsigned char OType;
00213     typedef WvBufBase<IType> IBuffer;
00214     typedef WvBufBase<OType> OBuffer;
00215     typedef WvBufViewBase<IType> IBufferView;
00216     typedef WvBufViewBase<OType> OBufferView;
00217 
00218 protected:
00219     /**
00220      * Typed variant of _encode().
00221      * @see _encode(WvBuf&, WvBuf&, bool)
00222      */
00223     virtual bool _typedencode(IBuffer &inbuf, OBuffer &outbuf,
00224         bool flush) = 0;
00225     
00226     /**
00227      * Typed variant of _finish().
00228      * @see _finish(WvBuf&)
00229      */
00230     virtual bool _typedfinish(OBuffer &outbuf)
00231         { return true; }
00232 
00233     /** Wrapper implementation of _encode(). */
00234     virtual bool _encode(WvBuf &inbuf, WvBuf &outbuf,
00235         bool flush)
00236     {
00237         return _typedencode(inbuf, outbuf, flush);
00238     }
00239     
00240     /** Wrapper implementation of _finish(). */
00241     virtual bool _finish(WvBuf &outbuf)
00242     {
00243         return _typedfinish(outbuf);
00244     }
00245 };
00246 
00247 #endif // __WVTYPEDENCODER

Generated on Sat Feb 21 21:05:34 2004 for WvStreams by doxygen 1.3.5