Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix empty initializer lists #45

Merged
merged 3 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions cppwg/writers/constructor_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,18 @@ def generate_wrapper(self) -> str:
f"\\b{param}\\b", f"{val}", default_value
)

# Add type if default value is an empty initializer list
# Example:
# `Foo(std::vector<Bar*> laminas = {})` is equivalent to
# `Foo(std::vector<Bar*> laminas = std::vector<Bar*>{})`
# which generates `py::arg("laminas") = std::vector<Bar*>{}`
if default_value.replace(" ", "") == "{}":
default_value = arg.decl_type.decl_string + " {}"

# Remove const keyword
default_value = re.sub(r"\bconst\b", "", default_value)
default_value = default_value.replace(" ", " ")

keyword_args += f" = {default_value}"

wrapper_string += keyword_args + ")\n"
Expand Down
16 changes: 13 additions & 3 deletions examples/shapes/src/primitives/Rectangle.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
#include <cassert>
#include <memory>

#include "Point.hpp"
#include "Rectangle.hpp"

Rectangle::Rectangle(double width, double height) :
Shape<2>()
Rectangle::Rectangle(double width, double height) : Shape<2>()
{
this->mVertices.push_back(std::make_shared<Point<2> >(0.0, 0.0));
this->mVertices.push_back(std::make_shared<Point<2> >(width, 0.0));
this->mVertices.push_back(std::make_shared<Point<2> >(width, height));
this->mVertices.push_back(std::make_shared<Point<2> >(0.0, height));
}

Rectangle::~Rectangle()
Rectangle::Rectangle(const std::vector<std::shared_ptr<Point<2> > > points) : Shape<2>()
{
assert(points.size() == 4);

for (unsigned i = 0; i < points.size(); ++i)
{
this->mVertices.push_back(points[i]);
}
}

Rectangle::~Rectangle()
{
}
10 changes: 7 additions & 3 deletions examples/shapes/src/primitives/Rectangle.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef _RECTANGLE_HPP
#define _RECTANGLE_HPP

#include "Point.hpp"
#include "Shape.hpp"

/**
Expand All @@ -10,17 +11,20 @@ class Rectangle : public Shape<2>
{

public:

/**
* Default Constructor
*/
Rectangle(double width = 2.0, double height = 1.0);

/**
* Construct from points
*/
Rectangle(const std::vector<std::shared_ptr<Point<2> > > points = {});

/**
* Destructor
*/
~Rectangle();

};

#endif // _RECTANGLE_HPP
#endif // _RECTANGLE_HPP
16 changes: 13 additions & 3 deletions examples/shapes/src/python/test/test_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,27 @@ def testGeometry(self):
p0 = pyshapes.geometry.Point2()
self.assertTrue(p0.GetLocation() == [0.0, 0.0])

p1 = pyshapes.geometry.Point2(0.0, 0.0)
p2 = pyshapes.geometry.Point2(1.0, 0.0)
p3 = pyshapes.geometry.Point2(0.0, 1.0)
p1 = pyshapes.geometry.Point2(5.0, 0.0)
self.assertTrue(p1.GetLocation() == [5.0, 0.0])

p2 = pyshapes.geometry.Point2(5.0, 5.0)
self.assertTrue(p2.GetLocation() == [5.0, 5.0])

p3 = pyshapes.geometry.Point2(0.0, 5.0)
self.assertTrue(p3.GetLocation() == [0.0, 5.0])

points = [p1, p2, p3]

triangle = pyshapes.primitives.Shape2()
triangle.SetVertices(points)
self.assertTrue(len(triangle.rGetVertices()) == 3)

rectangle = pyshapes.primitives.Rectangle(5.0, 10.0)
self.assertTrue(len(rectangle.rGetVertices()) == 4)

rectangle = pyshapes.primitives.Rectangle([p0, p1, p2, p3])
self.assertTrue(rectangle.rGetVertices() == [p0, p1, p2, p3])

cuboid = pyshapes.primitives.Cuboid(5.0, 10.0, 20.0)
self.assertTrue(len(cuboid.rGetVertices()) == 8)

Expand Down
1 change: 1 addition & 0 deletions examples/shapes/wrapper/primitives/Rectangle.cppwg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>);
void register_Rectangle_class(py::module &m){
py::class_<Rectangle , std::shared_ptr<Rectangle > , Shape<2> >(m, "Rectangle")
.def(py::init<double, double >(), py::arg("width") = 2., py::arg("height") = 1.)
.def(py::init<::std::vector<std::shared_ptr<Point<2>>> const >(), py::arg("points") = ::std::vector<std::shared_ptr<Point<2>>> {})
;
}