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 
30 template<class IT, class OT, class FT>
31 class WvFunctorEncoder : public WvTypedEncoder<IT, OT>
32 {
33 protected:
34  FT f;
35 
36 public:
37  typedef FT FType;
38  typedef IT IType;
39  typedef OT OType;
40  typedef WvBufBase<IType> IBuffer;
41  typedef WvBufBase<OType> OBuffer;
42  WvFunctorEncoder(const FType &f) : f(f) { }
43  virtual ~WvFunctorEncoder() { }
44 
45 protected:
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
WvBufBase
The generic buffer base type.
Definition: wvbufbase.h:15
WvFunctorEncoder
Functor specifies the functor type which must have an operator() with a signature compatible with inv...
Definition: wvfunctorencoder.h:31
WvTypedEncoder< IT, OT >::flush
bool flush(IBuffer &inbuf, OBuffer &outbuf, bool finish=false)
Typed variant of flush().
Definition: wvtypedencoder.h:59
WvFunctorEncoder::_reset
virtual bool _reset()
Template method implementation of reset().
Definition: wvfunctorencoder.h:64
WvFunctorEncoder::_typedencode
virtual bool _typedencode(IBuffer &inbuf, OBuffer &outbuf, bool flush)
Typed variant of _encode().
Definition: wvfunctorencoder.h:46
WvTypedEncoder
This template facilitates the creation and use of encoders that manipulate typed buffers.
Definition: wvtypedencoder.h:33