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;
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Indeed that can be done, if the library or your code provides the overloads for
operator<<andoperator>>for it to work. Simple example on how one could do it:The problem with using a pure
std::istreamis 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 usingstd::iostreamhere. 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, whileoperator>>is called, you don’t do the transforming yet, but you return expression types that will record the chain of operations within its type: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 🙂 :
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