Skip to content

Instantly share code, notes, and snippets.

@atifkarim
Last active February 22, 2021 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atifkarim/bf63fe8f7f89c1ed0c939586f3f8d775 to your computer and use it in GitHub Desktop.
Save atifkarim/bf63fe8f7f89c1ed0c939586f3f8d775 to your computer and use it in GitHub Desktop.
class template consisting virtual function binding with pybind
#include <pybind11/pybind11.h>
#include "sample_code.h"
namespace py = pybind11;
template <typename T = int>
void declare_class_template_abstarct(py::module &m, const std::string& typestr1)
{
// using Base_2 = Base_Abstract<T>;
class PyVirtualClass : public Base_Abstract<T>{
public:
using Base_Abstract<T>::Base_Abstract;
// using Base_1 = Base_Abstract<T>;
/* Trampoline (need one for each virtual function) */
void do_something_abstract(T val) override {
PYBIND11_OVERRIDE_PURE(
void, /* Return type */
Base_Abstract<T>, /* Parent class */
do_something_abstract, /* Name of function in C++ (must match Python name) */
val /* Argument(s) */
);
}
};
py::class_<Base_Abstract <T>, PyVirtualClass /* <--- trampoline*/>(m,"Base_Abstract")
.def(py::init<T>())
.def("do_something_abstract", &Base_Abstract <T>::do_something_abstract);
py::class_<Derived_Abstract<T>, Base_Abstract<T>>(m, "Derived_Abstract")
.def(py::init<T,T>())
.def("show_derived_value", &Derived_Abstract <T>::show_derived_value)
.def("do_something_abstract", &Derived_Abstract <T>::do_something_abstract);
};
PYBIND11_MODULE(somecode, m)
{
declare_class_template_abstarct<int>(m, "int");
}
#ifndef _CLASS_TEMPLATE_ABSTRACT_CLASS_
#define _CLASS_TEMPLATE_ABSTRACT_CLASS_
#include <iostream>
using namespace std;
/**
* class template with virtual function */
template<typename T = int>
class Base_Abstract
{
protected:
T base_Variable;
public:
explicit Base_Abstract(T x):base_Variable{x}{
std::cout<<"Base_Abstract Class Constructor is called and Mem Var: "<<base_Variable<<std::endl;
}
virtual void do_something_abstract(T val) = 0;
virtual ~Base_Abstract() {}
};
template<typename T = int>
class Derived_Abstract : public Base_Abstract<T>
{
public:
Derived_Abstract(T a, T b):Base_Abstract<T>{b}, derived_Variable{a}{
std::cout<<"Derived_Abstract Class Constructor is called"<<std::endl;
}
void show_derived_value(){
cout<<"show_derived_value() of Derived_Abstract class derived_Variable: "<<derived_Variable<<endl;
};
void do_something_abstract(T g) override{
cout<<"virtual function is called from Derived_Abstract class when g: "<<g<<endl;
};
virtual ~Derived_Abstract(){}
private:
T derived_Variable;
};
#endif
cmake_minimum_required(VERSION 3.10)
project(somecode)
set(LIB_GEN_PATH ${PROJECT_SOURCE_DIR}/scripts CACHE STRING "Where generated library will be placed")
add_subdirectory(lib/pybind11)
include_directories(${PROJECT_SOURCE_DIR}/include)
pybind11_add_module(${PROJECT_NAME} ${PROJECT_SOURCE_DIR}/src/binding.cpp)
set_target_properties(${PROJECT_NAME}
PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${LIB_GEN_PATH}
)
import somecode
class_temp_abstract_class_obj = somecode.Derived_Abstract(42, 59)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment