Skip to content

Commit

Permalink
[core][render] implement friend swap functions for some classes
Browse files Browse the repository at this point in the history
  • Loading branch information
alemuntoni committed Jan 21, 2025
1 parent d980ed4 commit b1d1ea9
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 35 deletions.
12 changes: 11 additions & 1 deletion vclib/core/include/vclib/mesh/mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -518,6 +518,16 @@ class Mesh : public Args...
(updateReferencesOfContainerType<Args>(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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,12 +410,26 @@ class PolymorphicObjectVector : protected PointerVector<std::shared_ptr<T>, 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 */

/**
Expand Down
14 changes: 14 additions & 0 deletions vclib/core/include/vclib/space/core/vector/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

/**
Expand Down
2 changes: 1 addition & 1 deletion vclib/core/todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
15 changes: 10 additions & 5 deletions vclib/render/include/vclib/bgfx/drawable/drawable_mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 18 additions & 15 deletions vclib/render/include/vclib/bgfx/drawable/mesh/mesh_render_buffers.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +83,26 @@ class MeshRenderBuffers : public vcl::MeshRenderData<MeshType>

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);
Expand Down
9 changes: 6 additions & 3 deletions vclib/render/include/vclib/bgfx/uniform.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};

Expand Down
5 changes: 3 additions & 2 deletions vclib/render/include/vclib/render/drawable/drawable_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b1d1ea9

Please sign in to comment.