3

Cython を使用して C++ ライブラリをラップしようとしています。

ヘッダー自体には、次のようなタイプを定義するための他のライブラリが含まれています (実際には、かなり多く)。

#include <Eigen/Dense>

namespace abc {
namespace xyz {
typedef Eigen::Matrix<double, 5, 1> Column5;
typedef Column5 States;
}}

多くの「外部」型定義があります。Eigen ライブラリの .pxd ファイルも書き込まない方法はありますか? インポートされた (ラップされた) クラス定義の .pxd ファイルで利用可能なタイプ「States」が必要なだけです...

4

1 に答える 1

2

あなたが何を求めているのかはわかりませんが、私が理解している限りでは、必要なものを公開するだけです (ここではStateタイプ)。

あなた.pyxまたはあなたの.pxd

(質問のコードが という名前のファイルの一部であると仮定しますmy_eigen.h

# Expose State 
cdef extern from "some_path/my_eigen.h" namespace "xyz" :
    cdef cppclass States:
        # Expose only what you need here
        pass

上記のラッピングが完了Stateすると、Cython コードの必要な場所で自由に使用できます。

# Use State as is in your custom wrapped class
cdef extern from "my_class_header.h" namespace "my_ns" :
    cdef cppclass MyClassUsingStates:
        double getElement(States s, int row, int col)
        ...

例:

ここで私の必要性は次のとおりでした:Pythonハンドラーを持ち、std::ofstreamそのメソッドを公開する必要はありません(したがって、何も公開しませんでしたが、可能だったでしょう...)

cdef extern from "<fstream>" namespace "std" :
    cdef cppclass ofstream:
        pass

cdef class Py_ofstream:
    cdef ofstream *thisptr

    def __cinit__(self):
       self.thisptr = new ofstream()

    def __dealloc__(self):
       if self.thisptr:
           del self.thisptr

.pyx注:ここでは、 (追加なしで)単一のブロックとして直接使用しました.pyd

質問を誤解していたら教えてください...

于 2013-06-26T13:01:58.463 に答える