tags:

views:

569

answers:

4

Is there a way to use these operators to input and output binary data? The reason I want to do this is that it makes the code readable. Ex: infile >> filedecrypter >> metadataparser >> audiodecoder >> effects >> soundplayer;

+3  A: 

Indeed that can be done, if the library or your code provides the overloads for operator<< and operator>> for it to work. Simple example on how one could do it:

class transformer {
    public:
    virtual std::iostream& transform(std::iostream&) = 0;
};

class noise : public transformer {
    public:
    virtual std::iostream& transform(std::iostream&) {
         /* extract, change and put into again */
    }
};


class echo : public transformer {
    public:
    virtual std::iostream& transform(std::iostream&) {
         /* extract, change and put into again */
    }
};

std::iostream& operator>>(std::iostream& io, transformer& ts) {
    return ts.transform(io);
}

int main() {
    std::stringstream data;
    std::ifstream file("sound.wav");

    noise n; echo e;

    data << file.rdbuf();
    data >> n >> e;
    /* pipelined data now ready to be played back */
}

The problem with using a pure std::istream is that you would read, but then you wouldn't have a way to put the transformed data back for the next step in the pipeline. Thus i'm using std::iostream here. This approach doesn't seem to be efficient, as every operator>> call would extract the whole data, and put into again.

To have a more performant way to stream this would be to create an expression template. This means, while operator>> is called, you don't do the transforming yet, but you return expression types that will record the chain of operations within its type:

typedef transform< echo< noise< istream > > > pipeline;
std::ifstream file("file.wav");
pipeline pipe(file);
int byte = pipe.get();

would be an example of such a type. The pipelines' structure is decoded into the type itself. Therefore, no virtual functions are needed anymore in the pipeline. It's not constructed on-demand, but using typedef here, to show the principle. Programming such a system is not easy. So you probably should look into existing systems, like Boost.Iostreams (see below). To give you an idea how it would look like, here is an example i just coded up for you :) :

#include <iostream>

template<typename T>
struct transformer {
    int get() {
        return static_cast<T*>(this)->read();
    }
};

struct echot {
    template<typename Chain>
    struct chain : transformer< chain<Chain> > {
        Chain c;

        int read() {
            return c.get() + 1;
        }

        chain(Chain const& c):c(c) { }
    };
} echo;

struct noiset {
    template<typename Chain>
    struct chain : transformer< chain<Chain> > {
        Chain c;

        int read() {
            return c.get() * 2;
        }

        chain(Chain c):c(c) { }
    };
} noise;


template<typename T>
typename T::template chain<std::istream&> operator>>(std::istream& is, T) {
    return typename T::template chain<std::istream&>(is);
}

template<typename T, typename U>
typename U::template chain<T> operator>>(T t, U u) {
    return typename U::template chain<T>(t);
}

int main() {
    std::cout << (std::cin >> echo >> noise).get() << std::endl;
}

Entering 0 yields the ASCII code 48 here, which is added 1, and multiplied by 2, yielding a value of 98, which is also finally output. I think you agree this is not some code a starter would want to write. So maybe look into boost.

Boost has an sophisticated iostreams library, which can do many things. I'm sure you would find something fitting to this. Boost.Iostreams

Johannes Schaub - litb
The efficiency concern could probably be mitigated by passing a proxy of some kind rather than a heavy data stream.
John Zwinck
it should read lazily using an expression template.that would be the way to do it i think.the expression template would record the pipeline structure,and could chain the data through different steps.but i think it's better to show him this basic streams,instead of scaring him off with templates.
Johannes Schaub - litb
Templates are fine :) In fact, I have already implemented something quite similar. What attracted me to these operators was that you could use the pipeline as a push or pull model (reversed for output).
carleeto
The only problem with templates is that its too easy for someone else to misuse, plus debugging template error messages from the compiler isn't the most fun :)
carleeto
I've profiled the code using the mechanism described in "tracing local function calls": -O2 code: http://codepad.org/hTR3pIx1 non-optimizing code: http://codepad.org/EGN3R5cZ . All code is inlined! (see http://stackoverflow.com/questions/311840/tool-to-trace-local-function-calls-in-linux#311912)
Johannes Schaub - litb
actually that code has some subtle problems. here i've done a better version for anyone interested: http://codepad.org/pBopzJI4 . But i think i stop this stuff now. who knows no-one will use this stuff anyway :p
Johannes Schaub - litb
+2  A: 

Sure it can be done. Just define your own operator>> and operator<< so they do "the right thing"...

I would make it so I would have methods in the class, like toStream(ostream& os) and fromStream(istream& ), then define

istream& operator>> (istream& is, T& t)
{
     t.fromStream(is);
     return t;
}

ostream& operator<< (ostream& os, const T& t)
{
     t.toStream(os);
     return t;
}
njsf
+2  A: 

There is no need to use streams to move the data. You can create your own classes to do this. This shows an example. Obviously, the Decrypt and MetaDataParser classes can be abstract base classes with virtual functions to allow various functionality be plugged together.

#include <iostream>                                                         
#include <istream>                                                          
using namespace std;                                                        

class Data2                                                                 
{                                                                           
};                                                                          

class Data3                                                                 
{                                                                           
};                                                                          

class Decrypt                                                               
{                                                                         
};                                                                          

class MetaDataParser                                                        
{                                                                           
};                                                                          

Data2& operator>>(istream& in, Decrypt& decrypt)                            
{                                                                           
  return *new Data2;                                                        
}                                                                           

Data3& operator>>(Data2& d2, MetaDataParser& mdp)                           
{                                                                           
  return *new Data3;                                                        
}                                                                           

int main()                                                                  
{                                                                           
  Decrypt decrypt;                                                          
  MetaDataParser mdp;                                                       

  cin >> decrypt >> mdp;                                                    
}
KeithB
+1  A: 

Just to be clear, are you intending to duplicate the semantics of iostreams? Because it looks like you are proposing something different. In the example you give:

infile >> filedecrypter >> metadataparser >> audiodecoder >> effects >> soundplayer;

In iostreams, the meaning here is to read from infile into filedecrypter until you get to whitespace, and then from infile into metadataparser until more whitespace, and so on.

It looks like you are proposing something different, where metadataparser reads from filedecrypter, audiodecoder from metadataparser, etc. In which case I think the answer to your question needs to be qualified a bit.

Can you use operator >> to express this construct? Probably yes.

Can you use iostreams for this? Probably not.

I suggest you clarify what it means when you say A >> B. Perhaps express it as regular methods rather than operator overloads first, and that may clarify the question.

Alastair
OK. I meant something like : Play( ApplyEffects( Decode( ExtractMetaData( Decrypt( BinaryFileRead( <somefile> )))))); I'm leaving out the intermediate types as they are unimportant. Could be as simple as a character buffer, or totally different object types.
carleeto