tags:

views:

93

answers:

2
+1  Q: 

C++ and Enumerable

I have a producer/consumers design in my application which implement Produce/Consume functions on user types. But it doesn't work very naturally with the standard library and especially with algorithms.

In C# there is the Enumerable and Observable concepts that can be used to easily implement stuff like this and get alot of nice free functionality.

In C++ there is the ios, istream, ostream, input_iterator, output_iterator concepts which I thought might be useful. But it seems to me that all of these are for primitive character types, e.g. char, int etc... and not for user types.

Sure I could use real functions such as Produce/Consumer and std::mem_fn for algorithms. But I was hoping there was a better way.

Im looking on some best-practice advice on how to go about designing i/o similar solutions on user types in C++.

E.g. from C#

class FrameProducer : IEnumerable<Frame> // pull frames
{...}

// Some engine between

class FrameConsumer : IObserver<Frame> // push frames
{...}

I was hoping for something similar in C++ e.g. which i dont believe is possible.

class FrameProducer : istream<Frame> // pull frames
{...}

// Some engine between

class FrameConsumer : ostream<Frame> // push frames
{...}

Maybe I'm thinking to hard about it and should just go by KISS.

Thoughts?

+3  A: 

Th terms are "insertion operator" and "extraction operator", which insert and extract data from a stream.

Here's an example:

#include <iostream>
#include <sstream>

struct foo
{
    int x;
};

// insertion operator
std::ostream& operator<<(std::ostream& s, const foo& f)
{
    s << f.x; // insert a foo by inserting x
    return s;
}

// extraction operator
std::istream& operator>>(std::istream& s, foo& f)
{
    s >> f.x; // extract a foo by extracting x
    return s;
}

int main(void)
{
    std::stringstream ss;

    foo f1 = {5};
    ss << f1;

    foo f2;
    ss >> f2;
}

Based on your desire to do:

MyFrameProducer producer;
MyFrameConsumer consumer;
Frame frame; // frame should probably be in the while loop, since its 
while(!producer.eof()) // lifetime doesn't need to exist outside the loop
{
    producer >> frame;
    consumer << frame;
} 

You might make:

struct MyFrameProducer {}; // add an eof function
struct MyFrameConsumer {};
struct Frame {};

// producer produces a frame
MyFrameProducer& operator>>(MyFrameProducer& p, Frame& f)
{
    /* whatever it takes to make a frame */

    return p;
}

// consumer consumes a frame
MyFrameConsumer& operator<<(MyFrameConsumer& c, const Frame& f)
{
    /* whatever it takes to use a frame */

    return c;
}

Or something akin to it. (Sorry my understanding of the problem is small.) It is a bit weird to desire this interface, since it has nothing to do with streams, and you might be better off with a different interface (explicit methods).

GMan
Not quite, what you show is how to convert a user type to a string.I was looking for something like this (which doesn't work since the stl istream/ostream seems to be character based)class MyFrameProducer : istream{}class MyFrameConsumer : ostream<Frame>{}int main(){ MyFrameProducer producer; MyFrameConsumer consumer; Frame frame; while(!producer.eof()) { producer >> frame; consumer << frame; }}
ronag
@ronag: I did *not* show how to convert a user type into a string, I showed how to insert it into a stream. (Only coincidentally does `stringstream` provide a way of producing a string.) Let me edit the answer to your comment snippet.
GMan
@GMan: I fixed something, but now it's community wiki, sorry.
Philipp
@Philipp: No, don't worry. I made it CW because I'm basically guessing, it wasn't your edit. And thanks. :)
GMan
+2  A: 

Take a look at this producer/consumer solution. Although it is in pure C it is so elegant so I just can't resist from posting it.

Ihor Kaharlichenko
Interesting, this seems also interesting: http://www.crystalclearsoftware.com/soc/coroutine/
ronag