Re: I/O class: how to design for special case



On 27/01/2006, jladd@xxxxxxxxxxxxxx wrote:

> If you ask the CFooBuffer instance to get the data itself, then pass
> it to the CFoo::out routine.
> The data must be coming from somewhere, so try this:
>
> CFooBuffer::readFrom(Object source)
>
> If the Object is a socket then handle it, if its a file, then handle
> that.

Yes, I can sort of see where you're coming from, and it does make sense
that CFooBuffer does its own reading. But:

readFrom(Object source)

In the real code, "Object" would have to be replaced by a real class or
data type. One alternative approach would be to use

readFrom(int handle)

and assume that CFooBuffer can use read(handle) for whatever handle
type is passed to it. This will work (sort of) for a file, pipe,
socket, serial connection etc. But let's say one day I want to be able
to read adat from one sound card and outout it onto another. Perhaps
this uses a different API and handle data type: int
GetDataFromSoundCard(sound_card_handle_t handle, ...)

Another approach would be to have CFooBuffer::readFrom() as a virtual
function, which gets overridden by a descendent of CFooBuffer. This
allows us to implement the read in any way we want. But the design is
starting to become more complex and harder to use.

But I think your suggestion of a factory method which creates the
buffer is a useful approach: we can imagine cases where the buffer has
to be a DMA buffer, or be contiguous in real RAM, or have special
alignment characteristics. In these cases we couldn't leave it up to
the user of the class to allocate the bufer "by hand".

--
Simon Elliott http://www.ctsn.co.uk
.



Relevant Pages

  • Re: I/O class: how to design for special case
    ... This design is something that I've given some thought to, ... Can CFoo provide a factory method that provides a CFooBuffer ... > with the CFoo methods able to accept this buffer. ...
    (comp.object)
  • Re: I/O class: how to design for special case
    ... your description of the problem and suggested solutions is ... Can CFoo provide a factory method that provides a CFooBuffer ... To avoid the buffer copy you could also have the CFoo::out methods take ... inverts control and is the OO approach IMHO. ...
    (comp.object)