Technique for using std::ifstream, std::ofstream in python via SWIG?

后端 未结 5 1751
滥情空心
滥情空心 2021-01-12 09:16

Is there a way to use std::[io]fstream\'s in python via swig?

I have a c-class with functions like:

void readFrom(std::istream& istr         


        
5条回答
  •  無奈伤痛
    2021-01-12 09:29

    I don't know swig but assuming you need to create a copyable object, you might get away using a function like

    std::shared_ptr make_ostream(std::string const& filename) {
        return std::make_shared(filename);
    }
    

    ... and then use a forwarding function to call the function you actually want to call:

    void writeTo(std::shared_ptr stream) {
        if (stream) {
            writeTo(*stream);
        }
    }
    

    (if overloading the names causes issues, you could call the forwarding function differently, of course).

提交回复
热议问题