From b1d1ea917e35bb5622cd8ec5e9c9dbb612673ae8 Mon Sep 17 00:00:00 2001 From: alemuntoni Date: Tue, 21 Jan 2025 15:28:55 +0100 Subject: [PATCH] [core][render] implement friend swap functions for some classes --- vclib/core/include/vclib/mesh/mesh.h | 12 ++++++- .../core/vector/polymorphic_object_vector.h | 16 ++++++++- .../include/vclib/space/core/vector/vector.h | 14 ++++++++ vclib/core/todo.md | 2 +- .../drawable/drawable_directional_light.h | 5 +++ .../vclib/bgfx/drawable/drawable_mesh.h | 15 ++++++--- .../bgfx/drawable/mesh/mesh_render_buffers.h | 33 ++++++++++--------- vclib/render/include/vclib/bgfx/uniform.h | 9 +++-- .../render/drawable/abstract_drawable_mesh.h | 3 +- .../vclib/render/drawable/drawable_object.h | 5 +-- .../drawable/drawable_directional_light.cpp | 13 ++++---- 11 files changed, 92 insertions(+), 35 deletions(-) diff --git a/vclib/core/include/vclib/mesh/mesh.h b/vclib/core/include/vclib/mesh/mesh.h index 6b2fb4d78..caac9bdac 100644 --- a/vclib/core/include/vclib/mesh/mesh.h +++ b/vclib/core/include/vclib/mesh/mesh.h @@ -485,7 +485,7 @@ class Mesh : public Args... /** * @brief Swaps this mesh with the other input Mesh m2. - * @param m2: the Mesh to swap with this Mesh. + * @param[in] m2: the Mesh to swap with this Mesh. */ void swap(Mesh& m2) { @@ -518,6 +518,16 @@ class Mesh : public Args... (updateReferencesOfContainerType(m2, m1Bases), ...); } + /** + * @brief Specializes the swap function to allow the swapping of two Mesh + * objects. + * + * Swaps the content of the two Mesh objects. Calls `a.swap(b)`. + * @param[in] a: The first Mesh object. + * @param[in] b: The second Mesh object. + */ + friend void swap(Mesh& a, Mesh& b) { a.swap(b); } + /** * @brief Assignment operator of the Mesh. * @param oth: the Mesh from which will create a copy and assign to this diff --git a/vclib/core/include/vclib/space/core/vector/polymorphic_object_vector.h b/vclib/core/include/vclib/space/core/vector/polymorphic_object_vector.h index 76dfe6c41..67d1cd83d 100644 --- a/vclib/core/include/vclib/space/core/vector/polymorphic_object_vector.h +++ b/vclib/core/include/vclib/space/core/vector/polymorphic_object_vector.h @@ -410,12 +410,26 @@ class PolymorphicObjectVector : protected PointerVector, N> } /** - * @brief Exchanges the contents of the container with those of other. + * @brief Swaps the contents of the container with those of other. * @param[in] other: Another PolymorphicObjectVector container of the same * type. */ void swap(PolymorphicObjectVector& other) { Base::swap(other); } + /** + * Specializes the swap function to allow the swapping of two + * PolymorphicObjectVector objects. + * + * Swaps the content of the two PolymorphicObjectVector objects. Calls + * `a.swap(b)`. + * @param[in] a: The first PolymorphicObjectVector object. + * @param[in] b: The second PolymorphicObjectVector object. + */ + friend void swap(PolymorphicObjectVector& a, PolymorphicObjectVector& b) + { + a.swap(b); + } + /* Operators */ /** diff --git a/vclib/core/include/vclib/space/core/vector/vector.h b/vclib/core/include/vclib/space/core/vector/vector.h index 8dc9d50aa..487445dfd 100644 --- a/vclib/core/include/vclib/space/core/vector/vector.h +++ b/vclib/core/include/vclib/space/core/vector/vector.h @@ -495,8 +495,22 @@ class Vector return it - begin(); } + /** + * @brief Swaps the contents of the container with those of other. + * @param[in] other: Another Vector container of the same type. + */ void swap(Vector& other) { mContainer.swap(other.mContainer); } + /** + * Specializes the swap function to allow the swapping of two Vector + * objects. + * + * Swaps the content of the two Vector objects. Calls `a.swap(b)`. + * @param[in] a: The first Vector object. + * @param[in] b: The second Vector object. + */ + friend void swap(Vector& a, Vector& b) { a.swap(b); } + /* Member functions specific for dynamic vector */ /** diff --git a/vclib/core/todo.md b/vclib/core/todo.md index d816a7d89..dbdfffb09 100644 --- a/vclib/core/todo.md +++ b/vclib/core/todo.md @@ -6,7 +6,7 @@ - [ ] Add documentation on how to add a new mesh element using scripts - [x] Remove usage of "vcl::" inside vcl namespace where it can be avoided - [ ] 'friend class Type' should be 'friend Type' - - [ ] implement swap functions in this way (but keep swap member function): https://stackoverflow.com/a/5695855/5851101 + - [x] implement swap functions in this way (but keep swap member function): https://stackoverflow.com/a/5695855/5851101 - Concepts: - [x] add constructor constraints on all concepts - [x] concepts should work for all type of cvref qualified types diff --git a/vclib/render/include/vclib/bgfx/drawable/drawable_directional_light.h b/vclib/render/include/vclib/bgfx/drawable/drawable_directional_light.h index 46e9c9735..3ca332d7a 100644 --- a/vclib/render/include/vclib/bgfx/drawable/drawable_directional_light.h +++ b/vclib/render/include/vclib/bgfx/drawable/drawable_directional_light.h @@ -67,6 +67,11 @@ class DrawableDirectionalLight : public DrawableObject void swap(DrawableDirectionalLight& other); + friend void swap(DrawableDirectionalLight& a, DrawableDirectionalLight& b) + { + a.swap(b); + } + void updateRotation(const vcl::Matrix44f& rot); const vcl::Color& linesColor() const { return mColor; } diff --git a/vclib/render/include/vclib/bgfx/drawable/drawable_mesh.h b/vclib/render/include/vclib/bgfx/drawable/drawable_mesh.h index 3efc26ed7..54dd72e34 100644 --- a/vclib/render/include/vclib/bgfx/drawable/drawable_mesh.h +++ b/vclib/render/include/vclib/bgfx/drawable/drawable_mesh.h @@ -71,13 +71,18 @@ class DrawableMeshBGFX : public AbstractDrawableMesh, public MeshType void swap(DrawableMeshBGFX& other) { + using std::swap; AbstractDrawableMesh::swap(other); MeshType::swap(other); - mMRB.swap(other.mMRB); - std::swap(mProgram, other.mProgram); - std::swap(mMeshUniforms, other.mMeshUniforms); - std::swap( - mMeshRenderSettingsUniforms, other.mMeshRenderSettingsUniforms); + swap(mMRB, other.mMRB); + swap(mProgram, other.mProgram); + swap(mMeshUniforms, other.mMeshUniforms); + swap(mMeshRenderSettingsUniforms, other.mMeshRenderSettingsUniforms); + } + + friend void swap(DrawableMeshBGFX& a, DrawableMeshBGFX& b) + { + a.swap(b); } // DrawableObject implementation diff --git a/vclib/render/include/vclib/bgfx/drawable/mesh/mesh_render_buffers.h b/vclib/render/include/vclib/bgfx/drawable/mesh/mesh_render_buffers.h index 0f1904bdb..164f25453 100644 --- a/vclib/render/include/vclib/bgfx/drawable/mesh/mesh_render_buffers.h +++ b/vclib/render/include/vclib/bgfx/drawable/mesh/mesh_render_buffers.h @@ -83,23 +83,26 @@ class MeshRenderBuffers : public vcl::MeshRenderData void swap(MeshRenderBuffers& other) { - std::swap((Base&) *this, (Base&) other); - std::swap(mVertexCoordBH, other.mVertexCoordBH); - std::swap(mVertexNormalBH, other.mVertexNormalBH); - std::swap(mVertexColorBH, other.mVertexColorBH); - std::swap(mVertexUVBH, other.mVertexUVBH); - std::swap(mVertexWedgeUVBH, other.mVertexWedgeUVBH); - std::swap(mTriangleIndexBH, other.mTriangleIndexBH); - std::swap(mTriangleNormalBH, other.mTriangleNormalBH); - std::swap(mTriangleColorBH, other.mTriangleColorBH); - std::swap(mTriangleTextureIndexBH, other.mTriangleTextureIndexBH); - std::swap(mEdgeIndexBH, other.mEdgeIndexBH); - std::swap(mEdgeNormalBH, other.mEdgeNormalBH); - std::swap(mEdgeColorBH, other.mEdgeColorBH); - std::swap(mWireframeIndexBH, other.mWireframeIndexBH); - std::swap(mTexturesH, other.mTexturesH); + using std::swap; + swap((Base&) *this, (Base&) other); + swap(mVertexCoordBH, other.mVertexCoordBH); + swap(mVertexNormalBH, other.mVertexNormalBH); + swap(mVertexColorBH, other.mVertexColorBH); + swap(mVertexUVBH, other.mVertexUVBH); + swap(mVertexWedgeUVBH, other.mVertexWedgeUVBH); + swap(mTriangleIndexBH, other.mTriangleIndexBH); + swap(mTriangleNormalBH, other.mTriangleNormalBH); + swap(mTriangleColorBH, other.mTriangleColorBH); + swap(mTriangleTextureIndexBH, other.mTriangleTextureIndexBH); + swap(mEdgeIndexBH, other.mEdgeIndexBH); + swap(mEdgeNormalBH, other.mEdgeNormalBH); + swap(mEdgeColorBH, other.mEdgeColorBH); + swap(mWireframeIndexBH, other.mWireframeIndexBH); + swap(mTexturesH, other.mTexturesH); } + friend void swap(MeshRenderBuffers& a, MeshRenderBuffers& b) { a.swap(b); } + void update(const MeshType& mesh) { Base::update(mesh); diff --git a/vclib/render/include/vclib/bgfx/uniform.h b/vclib/render/include/vclib/bgfx/uniform.h index 2c5f31e57..d031f0d28 100644 --- a/vclib/render/include/vclib/bgfx/uniform.h +++ b/vclib/render/include/vclib/bgfx/uniform.h @@ -82,11 +82,14 @@ class Uniform void swap(Uniform& oth) { - std::swap(mUniformHandle, oth.mUniformHandle); - std::swap(mUniformName, oth.mUniformName); - std::swap(mUniformType, oth.mUniformType); + using std::swap; + swap(mUniformHandle, oth.mUniformHandle); + swap(mUniformName, oth.mUniformName); + swap(mUniformType, oth.mUniformType); } + friend void swap(Uniform& a, Uniform& b) { a.swap(b); } + Uniform& operator=(Uniform oth) { swap(oth); diff --git a/vclib/render/include/vclib/render/drawable/abstract_drawable_mesh.h b/vclib/render/include/vclib/render/drawable/abstract_drawable_mesh.h index df10eb922..0fc5a0c45 100644 --- a/vclib/render/include/vclib/render/drawable/abstract_drawable_mesh.h +++ b/vclib/render/include/vclib/render/drawable/abstract_drawable_mesh.h @@ -57,8 +57,9 @@ class AbstractDrawableMesh : public vcl::DrawableObject protected: void swap(AbstractDrawableMesh& other) { + using std::swap; vcl::DrawableObject::swap(other); - std::swap(mMRS, other.mMRS); + swap(mMRS, other.mMRS); } }; diff --git a/vclib/render/include/vclib/render/drawable/drawable_object.h b/vclib/render/include/vclib/render/drawable/drawable_object.h index 61fcb1ed2..e6a729c8a 100644 --- a/vclib/render/include/vclib/render/drawable/drawable_object.h +++ b/vclib/render/include/vclib/render/drawable/drawable_object.h @@ -150,8 +150,9 @@ class DrawableObject */ void swap(DrawableObject& other) { - std::swap(mName, other.mName); - std::swap(mInfo, other.mInfo); + using std::swap; + swap(mName, other.mName); + swap(mInfo, other.mInfo); } }; diff --git a/vclib/render/src/vclib/bgfx/drawable/drawable_directional_light.cpp b/vclib/render/src/vclib/bgfx/drawable/drawable_directional_light.cpp index 298bde8cc..1904b3f81 100644 --- a/vclib/render/src/vclib/bgfx/drawable/drawable_directional_light.cpp +++ b/vclib/render/src/vclib/bgfx/drawable/drawable_directional_light.cpp @@ -90,13 +90,14 @@ DrawableDirectionalLight& DrawableDirectionalLight::operator=( void DrawableDirectionalLight::swap(DrawableDirectionalLight& other) { - std::swap(mVisible, other.mVisible); + using std::swap; + swap(mVisible, other.mVisible); mVertices.swap(other.mVertices); - std::swap(mColor, other.mColor); - std::swap(mUniform, other.mUniform); - std::swap(mProgram, other.mProgram); - std::swap(mTransform, other.mTransform); - std::swap(mVertexCoordBH, other.mVertexCoordBH); + swap(mColor, other.mColor); + swap(mUniform, other.mUniform); + swap(mProgram, other.mProgram); + swap(mTransform, other.mTransform); + swap(mVertexCoordBH, other.mVertexCoordBH); } void DrawableDirectionalLight::updateRotation(const Matrix44f& rot)