Skip to content

Commit

Permalink
#4 use constants in templates
Browse files Browse the repository at this point in the history
  • Loading branch information
kwabenantim committed Mar 25, 2024
1 parent 8325caf commit 2c03dde
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
22 changes: 12 additions & 10 deletions cppwg/templates/pybind11_default.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,45 @@
from cppwg.utils.constants import CPPWG_CLASS_OVERRIDE_SUFFIX, CPPWG_EXT

class_cpp_header = """\
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
{includes}
#include "{class_short_name}.cppwg.hpp"
#include "{class_short_name}.%s.hpp"
namespace py = pybind11;
typedef {class_full_name} {class_short_name};
{smart_ptr_handle};
"""
""" % CPPWG_EXT

class_cpp_header_chaste = """\
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
{includes}
//#include "PythonObjectConverters.hpp"
#include "{class_short_name}.cppwg.hpp"
#include "{class_short_name}.%s.hpp"
namespace py = pybind11;
//PYBIND11_CVECTOR_TYPECASTER2();
//PYBIND11_CVECTOR_TYPECASTER3();
typedef {class_full_name} {class_short_name};
{smart_ptr_handle};
"""
""" % CPPWG_EXT

class_hpp_header = """\
#ifndef {class_short_name}_hpp__cppwg_wrapper
#define {class_short_name}_hpp__cppwg_wrapper
#ifndef {class_short_name}_hpp__%s_wrapper
#define {class_short_name}_hpp__%s_wrapper
#include <pybind11/pybind11.h>
void register_{class_short_name}_class(pybind11::module &m);
#endif // {class_short_name}_hpp__cppwg_wrapper
"""
#endif // {class_short_name}_hpp__%s_wrapper
""" % tuple([CPPWG_EXT]*3)

class_virtual_override_header = """\
class {class_short_name}_Overloads : public {class_short_name}{{
class {class_short_name}%s : public {class_short_name}{{
public:
using {class_short_name}::{class_base_name};
"""
""" % CPPWG_CLASS_OVERRIDE_SUFFIX

class_virtual_override_footer = "}\n"

Expand Down
2 changes: 2 additions & 0 deletions cppwg/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@
CPPWG_FALSE_STRINGS = ["OFF", "NO", "N", "FALSE", "F"]

CPPWG_DEFAULT_WRAPPER_DIR = "cppwg_wrappers"

CPPWG_CLASS_OVERRIDE_SUFFIX = "_Overrides"
15 changes: 9 additions & 6 deletions cppwg/writers/class_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
from pygccxml.declarations.class_declaration import class_t

from cppwg.input.class_info import CppClassInfo
from cppwg.utils.constants import CPPWG_EXT, CPPWG_HEADER_COLLECTION_FILENAME
from cppwg.utils.constants import (
CPPWG_CLASS_OVERRIDE_SUFFIX,
CPPWG_EXT,
CPPWG_HEADER_COLLECTION_FILENAME,
)
from cppwg.writers.base_writer import CppBaseWrapperWriter
from cppwg.writers.constructor_writer import CppConstructorWrapperWriter
from cppwg.writers.method_writer import CppMethodWrapperWriter
Expand Down Expand Up @@ -204,7 +208,7 @@ def add_virtual_overrides(
# Override virtual methods
if methods_needing_override:
# Add virtual override class, e.g.:
# class Foo_Overloads : public Foo{{
# class Foo_Overrides : public Foo{{
# public:
# using Foo::Foo;
override_header_dict = {
Expand Down Expand Up @@ -293,13 +297,12 @@ def write(self, work_dir: str) -> None:
self.add_virtual_overrides(class_decl, short_name)
)

# Add the virtual "trampoline" overrides from "Foo_Overloads" to
# Add the virtual "trampoline" overrides from "Foo_Overrides" to
# the "Foo" wrapper class definition if needed
# e.g. py::class_<Foo, Foo_Overloads >(m, "Foo")
# e.g. py::class_<Foo, Foo_Overrides >(m, "Foo")
overrides_string = ""
if methods_needing_override:
# TODO: Assign the "_Overloads" literal to a constant
overrides_string = f", {short_name}_Overloads"
overrides_string = f", {short_name}{CPPWG_CLASS_OVERRIDE_SUFFIX}"

# Add smart pointer support to the wrapper class definition if needed
# e.g. py::class_<Foo, boost::shared_ptr<Foo > >(m, "Foo")
Expand Down
8 changes: 7 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ classifiers = [
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: Implementation :: CPython",
"Topic :: Software Development",
]
Expand All @@ -45,7 +46,12 @@ Repository = "https://github.com/Chaste/cppwg/"

[tool.black]
target-version = ["py38", "py39", "py310", "py311", "py312"]
extend-exclude = "^/examples/shapes/wrapper/pybind11/"
extend-exclude = """
(
^/examples/shapes/wrapper/pybind11/
| ^/cppwg/templates/
)
"""

[tool.isort]
profile = "black"
Expand Down

0 comments on commit 2c03dde

Please sign in to comment.