vulkan-renderer
vulkan-renderer copied to clipboard
How to make handles to cube class both thread safe and efficient?
We are passing around a lot of pointers, smart pointers or const references to cubes in our code. This is necessary, as it's a major part of our engine. For example I'm working on the new octree collision (see upcoming pull request later) which looks like this:
std::optional<RayCubeCollision<Cube>>
ray_cube_collision_check(const std::shared_ptr<Cube> cube, const glm::vec3 pos,
const glm::vec3 dir, const std::optional<std::uint32_t> grid_level_counter)
So in this code I am passing the cube as std::shared_ptr. This is fine, but clang-tidy suggests to pass it as const reference to a shared pointer. Jason Turner and Scott Meyers however say that this is not a good idea.
We have the following options:
- Pass it as const reference
- Pass it as a raw pointer
- Pass it as a
std::shared_ptr - Pass it as a
std::weak_ptr
Notes:
- We do not want to transfer ownership of the cube, so passing by value and using std::move is not an option in my opinion.
- Copying data could be costly
- We need to copy the data though, because if we pass it as const reference, the data could become invalid while the function body is being executed.
I think this is a common question which will is relevant for many code parts. What do you guys propose?
So... I guess passing it as std::shared_ptr is the best option?