简体   繁体   中英

C++: bundle together an istream and an ostream + override stream operators

I would like to create a class (IOObj) that manages both an istream and an ostream. The part I am stuck on is how to properly override the stream operators so that given an IOObj io {}; , io << "blah" << std::endl outputs using the ostream, and io >> x inputs to x using the istream. The stream operators I have written fail to work. std::endl, and probably most other manipulators, are simply ignored.

Below is my attempt.

#ifndef IOOBJ_H_
#define IOOBJ_H_

#include <iostream>
#include "SimpleTextUIErrors.h"

namespace SimpleTextUI {

class IOObj: public std::iostream {
public:

    IOObj(std::istream& in=std::cin, std::ostream& out=std::cout):
        i{&in}, o{&out}, iOwner{false}, oOwner{false} {}
    IOObj(std::istream& in, std::ostream* out): i{&in}, o{out}, iOwner{false}, oOwner{true} {}
    IOObj(std::istream* in, std::ostream& out): i{in}, o{&out}, iOwner{true}, oOwner{false} {}
    IOObj(std::istream* in, std::ostream* out): i{in}, o{out}, iOwner{true}, oOwner{true} {}

    IOObj(const IOObj&)=delete;
    IOObj& operator=(const IOObj&)=delete;

    ~IOObj() { releaseIO(); }

    std::istream& in() { return *i; }
    std::ostream& out() { return *o; }

    void setInput(std::istream* in);
    void setInput(std::istream& in);
    void setOutput(std::ostream* out);
    void setOutput(std::ostream& out);

    void outputSeparator() { *o << "-------------------------------" << std::endl; }

protected:
    IOObj(IOObj&&)=default;
    IOObj& operator=(IOObj&&)=default;

private:
    std::istream* i;
    std::ostream* o;
    bool iOwner, oOwner;

    void releaseIO() {
        releaseIn();
        releaseOut();
    }

    void releaseIn() { if (iOwner) delete i; }
    void releaseOut() { if (oOwner) delete o; }

};

template<typename T>
inline IOObj& operator<<(IOObj& io, T output) {
    io.out() << output;
    if (io.out().fail()) throw OutputFailedError{};
    if (io.out().bad()) throw OutputBrokenError{};
    return io;
}

template<typename T>
inline IOObj& operator>>(IOObj& io, T& input) {
    io.in() >> input;
    if (io.in().fail()) throw InputFailedError{};
    if (io.in().bad()) throw InputBrokenError{};
    return io;
}

} /* SimpleTextUI */

#endif /* IOOBJ_H_ */

The trick was to also write a function in the case of manipulators (like std::endl).

I added the following code:

inline IOObj& operator<<(IOObj& io, std::ostream& (*manip) (std::ostream&) ) {
    io.out() << manip;
    if (io.out().fail()) throw OutputFailedError{};
    if (io.out().bad()) throw OutputBrokenError{};
    return io;
}

Unfortunately, this code is the exact same as the << template function in my code, just specially instantiated for stream manipulator functions. I wasn't able to fix this (comment below and I'll update my answer).

Also, the code might need another such function for the >> operator, but the logic would be nearly the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM