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

后端 未结 5 1753
滥情空心
滥情空心 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:52

    Working .i file based on Dietmar's suggestion to use shared pointers:

    %module ptrtest
    
    %include "boost_shared_ptr.i"
    %include "std_string.i"
    
    %shared_ptr( std::ostream )
    
    %{
    #include 
    #include 
    #include 
    
    typedef boost::shared_ptr< std::ostream > ostream_ptr;
    
    ostream_ptr mk_out(const std::string& fname ){
        return ostream_ptr( new std::ofstream( fname.c_str() ) );
    }
    
    void writeTo(std::ostream& ostr){
        ostr<<"OK"< ostream_ptr;
    ostream_ptr mk_out(const std::string& fname );
    void writeTo(std::ostream& ostr);
    
    // Usage:
    //>>>ostr=mk_out('/path/to/file.txt')
    //>>>writeTo(ostr) # no need to cast/call-function!
    

提交回复
热议问题