pynid 11でC+構造体をパラメータの関数としてカプセル化してステップを実現します。


pythonはC/C++を呼び出す多くの方法があります。例えば、boots.python、swig、ctypes、pynid 11など、これらの方法は多くの簡略化されています。pynid 11の利点は、C++11に対してよくサポートされています。APIは簡単です。今はPynid 11の入門操作を簡単にメモします。
pynid 11概要
pynind 11はヘッドファイルのみを含む軽量級のライブラリであり、主に既存のC+コードをベースに拡張するために使用されています。その文法と目標はBoost.Pythonに似ていますが、Boost.Pythonは既存の基本的なすべてのC+++コンパイラに対応するために非常に複雑で大きなコンパイラになります。そのために払う代価は多くの難解なテンプレートの技巧と多くの必要でない旧版のコンパイラに対する支持です。Pynid 11はこれらのサポートを破棄しています。python 2.7以上とC++11以上のコンパイラだけをサポートしています。Boost.Pythonよりも簡潔で効率的です。
C言語では、構造体(struct)とは、データ構造のことであり、C言語における集約データタイプの一種である。構造体は、変数、ポインタ、または配列などとして宣言して、より複雑なデータ構造を実現します。構造体もいくつかの要素の集合であり、これらの要素は構造体のメンバーと呼ばれ、これらのメンバーは異なるタイプであり、メンバーは一般的に名前でアクセスすることができる。
構造体、構造体ポインタは、関数としてのパラメータアプリケーションが非常に広く、ここでは、どのようにpynid 11パッケージC+構造体をパラメータの関数として使用するかを紹介します。
一.需要分析
  • はstudentという構造体があります。5つのメンバー変数name、Chinese、Mathematicas、English、totalがあります。構成関数はnameを通じてインスタンスを生成し、メンバー関数setNameはインスタンスのnameに値を与えることができます。
  • calc関数は、一つのstudentのインスタンスをパラメータとして受信し、3つのコースのスコアから合計totalを計算する。
  • は、student、cancを一つのstudentクラスと一つのcanc関数を含むpythonモジュールにパッケージ化する。
  • 二.実現ステップ
  • は、ヘッダファイルにstudent構造体を定義し、calc関数を宣言する。
  • C++ソースファイルでfunc.cpp関数を実現する。
  • pynid 11パッケージ関数を作成します。
  • はpythonでsetupスクリプトを作成します。
  • コンパイルして動的リンクライブラリを生成する。
  • は、関数機能をテストします。
  • 三.コード実現
    先頭ファイルにstudent構造体を定義し、calc関数を宣言します。
    
    //   :whjy.h
    #include <string> 
    using namespace std; 
    struct student{ 
     string name; 
     int Chinese; 
     int Mathematics; 
     int English; 
     int total; 
     student(string n){ 
     this->name = n; 
     } 
     void setName(string stuName){ 
      this->name = stuName; 
     } 
    }; 
    void calc(struct student&);
    C++ソースファイルでfunc.cpp関数を実現します。
    
    //   :func.cpp
    #include "whjy.h" 
    #include <string> 
    void calc(struct student& tyh){ 
     tyh.total = tyh.Chinese + tyh.Mathematics + tyh.English; 
    }
    pynid 11パッケージ機能の作成
    
    //   :func_wrapper.cpp
    #include <pybind11/pybind11.h> 
    #include "whjy.h" 
    namespace py = pybind11; 
    PYBIND11_MODULE(abctest, m){ 
     m.doc() = "simple example"; 
     
     py::class_<student>(m, "student") 
      .def(py::init<string>()) 
      .def("setName", &student::setName) 
      .def_readonly("name", &student::name) 
      .def_readwrite("Chinese", &student::Chinese) 
      .def_readwrite("Mathematics", &student::Mathematics) 
      .def_readwrite("English", &student::English) 
      .def_readwrite("total", &student::total); 
     m.def("calc", &calc); 
    }
    pythonでsetupスクリプトを作成します。
    
    #   :setup.py
    from setuptools import setup, Extension 
     
    functions_module = Extension( 
     name = 'abctest', 
     sources = ['func.cpp', 'func_wrapper.cpp'], 
     include_dirs = [r'D:\software\pybind11-master\include', 
         r'D:\software\Anaconda\include'] 
    ) 
     
    setup(ext_modules = [functions_module])
    コンパイルによるダイナミックリンクライブラリの生成
    コマンドライン実行python setup.py build_ext --inplace では、現在の経路でpydダイナミックライブラリが生成される。
    テスト関数機能
    
    #   :test.py
    import abctest 
    s = abctest.student("  ") 
    s.Chinese = 100 
    s.Mathematics = 110 
    s.English =120 
    abctest.calc(s) 
    print(s.name + ":" + str(s.total) + " ") 
    print("----------------------") 
    s.setName("  ") 
    print(s.name + ":" + str(s.total) + " ")
    out put:
    ミン:330分です
    ------------
    紅ちゃん:330分です
    締め括りをつける
    ここでは、pynid 11パッケージC+構造体をパラメータの関数として使用した実装手順についての記事を紹介します。より多くの関連するpynid 11パッケージC+構造体パラメータの内容については、以前の記事を検索してください。または、以下の関連記事を参照してください。よろしくお願いします。