diff --git a/Makefile b/Makefile index e2a1064..8efacb6 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CXX= clang++-8 CXXFLAGS= --std=c++17 -O0 -pthread -DGP_TESTS -DFUZZ_STRENGTH=500 -pedantic -Werror \ - -Wno-unknown-attributes \ + -Wno-unknown-attributes -frtti \ -g -fprofile-instr-generate -fcoverage-mapping # -fsanitize=address -fno-omit-frame-pointer all: tests diff --git a/include/gp/function.hpp b/include/gp/function.hpp index 53d9610..5ca6172 100644 --- a/include/gp/function.hpp +++ b/include/gp/function.hpp @@ -30,6 +30,7 @@ namespace gp{ } }; + // tweak a way to store a size in there for trivial copy enum state_t : uint8_t{ INACTIVE = 0, ACTIVE = 1, @@ -51,7 +52,7 @@ namespace gp{ { if(state & SOO) { - ((virtual_callable*)&self)->~virtual_callable(); + ((virtual_callable*)self.inplace)->~virtual_callable(); } else { @@ -60,12 +61,12 @@ namespace gp{ } if constexpr (sizeof(callable) <= sizeof(self)) { - new((void*)&self) callable(t); + new((void*)self.inplace) callable(t); state = (state_t)(ACTIVE | SOO); } else { - self = new callable(t); + self.functor = new callable(t); state = (state_t)(ACTIVE | NO_SOO); } } @@ -80,12 +81,12 @@ namespace gp{ { if constexpr (sizeof(callable) <= sizeof(self)) { - new((void*)&self) callable(t); + new((void*)self.inplace) callable(t); state = (state_t)(ACTIVE | SOO); } else { - self = new callable(t); + self.functor = new callable(t); state = (state_t)(ACTIVE | NO_SOO); } } @@ -119,7 +120,7 @@ namespace gp{ } else { - return (*(self.functor))(arg_list...); + return (*self.functor)(arg_list...); } } diff --git a/tests/math.cpp b/tests/math.cpp index 048592f..e78c13e 100644 --- a/tests/math.cpp +++ b/tests/math.cpp @@ -78,24 +78,24 @@ struct render_test : public test_scaffold { } ); + auto l_sdf = gp::difference_sdf( + gp::sphere_sdf({0.0,0.0,0.0}, 1.0), + gp::sphere_sdf({-0.75,0.0,0.0}, 1.0) + ); auto sphere = a.scene_elements.push( - [=](vec3 pos) -> render_point { + [&](vec3 pos) -> render_point { render_point ret; - const auto l_sdf = gp::difference_sdf( - gp::sphere_sdf({0.0,0.0,0.0}, 1.0), - gp::sphere_sdf({-0.75,0.0,0.0}, 1.0) - ); ret.distance = l_sdf(pos); ret.material = red; return ret; } ); + auto l_sdf_b = gp::sphere_sdf({-0.75,0.0,0.0}, 1.0); auto sphere2 = a.scene_elements.push( - [=](vec3 pos) -> render_point { + [&](vec3 pos) -> render_point { render_point ret; - const auto l_sdf = gp::sphere_sdf({-0.75,0.0,0.0}, 1.0); - ret.distance = l_sdf(pos); + ret.distance = l_sdf_b(pos); ret.material = green; return ret; }