WvStreams
wvfunctorencoder.h
1/* -*- Mode: C++ -*-
2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
4 *
5 * Provides an encoder for applying a functor to data extracted
6 * from a buffer and stored in another.
7 * Assumes binary input is in machine order.
8 */
9#ifndef __WVFUNCTORENCODER_H
10#define __WVFUNCTORENCODER_H
11
12#include "wvtypedencoder.h"
13
30template<class IT, class OT, class FT>
31class WvFunctorEncoder : public WvTypedEncoder<IT, OT>
32{
33protected:
34 FT f;
35
36public:
37 typedef FT FType;
38 typedef IT IType;
39 typedef OT OType;
42 WvFunctorEncoder(const FType &f) : f(f) { }
43 virtual ~WvFunctorEncoder() { }
44
45protected:
46 virtual bool _typedencode(IBuffer &inbuf, OBuffer &outbuf,
47 bool flush)
48 {
49 size_t count;
50 while ( (count = inbuf.optgettable()) )
51 {
52 size_t avail = outbuf.optallocable();
53 if (avail == 0)
54 return ! flush;
55 if (avail < count)
56 count = avail;
57 const IType *indata = inbuf.get(count);
58 OType *outdata = outbuf.alloc(count);
59 while (count-- > 0)
60 *(outdata++) = f(*(indata++));
61 }
62 return true;
63 }
64 virtual bool _reset()
65 {
66 // Assume most functor encoders will be stateless and therefore
67 // support reset() implicitly.
68 // If this is not the case, then override this method for
69 // particular subclasses to return false.
70 return true;
71 }
72};
73
74#endif // __WVFUNCTORENCODER_H
size_t optgettable() const
Returns the optimal maximum number of elements in the buffer currently available for reading without ...
Definition wvbufbase.h:154
const T * get(size_t count)
Reads exactly the specified number of elements and returns a pointer to a storage location owned by t...
Definition wvbufbase.h:114
T * alloc(size_t count)
Allocates exactly the specified number of elements and returns a pointer to an UNINITIALIZED storage ...
Definition wvbufbase.h:379
size_t optallocable() const
Returns the optimal maximum number of elements that the buffer can currently accept for writing witho...
Definition wvbufbase.h:397
The generic buffer base type.
Definition wvbufbase.h:587
Functor specifies the functor type which must have an operator() with a signature compatible with inv...
virtual bool _typedencode(IBuffer &inbuf, OBuffer &outbuf, bool flush)
Typed variant of _encode().
virtual bool _reset()
Template method implementation of reset().
This template facilitates the creation and use of encoders that manipulate typed buffers.
bool flush(IBuffer &inbuf, OBuffer &outbuf, bool finish=false)
Typed variant of flush().