How to use >> and << operators for binary data in C ++?

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

+3
source share
4 answers

Indeed, this can be done if the library or your code provides overloads for operator<<and operator>>for its operation. A simple example of how this can be done:

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 */
}

std::istream , , . , std::iostream . , → call .

, expression template. , , operator>>, , , :

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

. . . , typedef , . . , , , Boost.Iostreams(. ). , , , :):

#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;
}

0 ASCII 48, 1 2, 98, , , . , , - , . , , .

Boost iostreams, . , - . Boost.Iostreams

+3

, . → < " "...

, , toStream (ostream & os) fromStream (istream &),

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

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

. . . , Decrypt MetaDataParser , .

#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;                                                    
}                                                                           
+2

, iostreams? , - . :

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

iostreams , infile filedecrypter, , infile , ..

, - , filedecrypter, .. , .

, ? , .

iostreams ? , .

I suggest you clarify what this means when you say A → B. Perhaps first express it as normal methods, not operator overloads, and this may clarify the issue.

+2
source

All Articles