-
Notifications
You must be signed in to change notification settings - Fork 0
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
Move/copy mechanisms #8
Changes from 11 commits
9da4037
5d4f3cc
08cbe2d
17c845c
e6bdd4c
f04a32c
cafffe6
d9b1a08
f8a9b04
d24294c
40b94e4
0117a54
dfa8b2f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
|
||
|
||
#include "SpacePoint.cuh" | ||
#include "common.cuh" | ||
|
||
|
||
/** | ||
* Returns the normal to faces defined by vertices | ||
|
@@ -19,7 +19,12 @@ class MapNode; | |
|
||
class Polyhedron; | ||
|
||
/// Object describing a polyhedron face | ||
/** | ||
* Object describing a polyhedron face | ||
* | ||
* @warning <b>Before using the class, please, carefully read documentation for both copy and move constructors and | ||
* assignment operators!</b> | ||
*/ | ||
class Face | ||
{ | ||
public: | ||
|
@@ -37,9 +42,38 @@ public: | |
*/ | ||
__device__ Face(const SpacePoint *vertices, int n_of_vertices); | ||
|
||
/// `Face` object copy constructor | ||
/** | ||
* `Face` object copy assignment operator | ||
* | ||
* @warning When copying `Face` object, its `node` field is <b>not</b> copied, but is set to `nullptr`, which means | ||
* the new `Face` won't have a node attached. Please, be careful | ||
*/ | ||
__device__ Face &operator=(const Face &other); | ||
|
||
/** | ||
* `Face` object copy constructor | ||
* | ||
* @warning When copying `Face` object, its `node` field is <b>not</b> copied, but is set to `nullptr`, which means | ||
* the new `Face` won't have a node attached. Please, be careful | ||
*/ | ||
__device__ Face(const Face &other); | ||
|
||
/** | ||
* `Face` object move assignment operator | ||
* | ||
* @warning If the being moved `Face`'s field `node` is not `nullptr`, this might mean there is a node pointing | ||
* to the `Face` being moved, so moving it will <b>invalidate</b> the pointers set at `*node` | ||
*/ | ||
__device__ Face &operator=(Face &&other) noexcept; | ||
|
||
/** | ||
* Face` object move constructor | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be back quote before Face` |
||
* | ||
* @warning If the being moved `Face`'s field `node` is not `nullptr`, this might mean there is a node pointing | ||
* to the `Face` being moved, so moving it will <b>invalidate</b> the pointers set at `*node` | ||
*/ | ||
__device__ Face(Face &&other) noexcept; | ||
|
||
/// Destructs a `Face` object | ||
__device__ ~Face(); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
#include "Polyhedron.cuh" | ||
#include "common.cuh" | ||
|
||
|
||
__device__ Polyhedron::Polyhedron(Face *faces, int n_of_faces) : | ||
|
@@ -7,11 +8,45 @@ __device__ Polyhedron::Polyhedron(Face *faces, int n_of_faces) : | |
|
||
} | ||
|
||
__device__ Polyhedron &Polyhedron::operator=(const Polyhedron &other) | ||
{ | ||
if(this != &other) | ||
{ | ||
faces = malloc_and_copy(other.faces, other.n_of_faces); | ||
n_of_faces = other.n_of_faces; | ||
} | ||
return *this; | ||
} | ||
|
||
__device__ Polyhedron::Polyhedron(const Polyhedron &other) | ||
{ | ||
*this = other; | ||
} | ||
|
||
__device__ Polyhedron &Polyhedron::operator=(Polyhedron &&other) noexcept | ||
{ | ||
if(this != &other) | ||
{ | ||
faces = nullptr; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, you are right (by the way, I think, your explanation could be easier to understand if you'd provide a code snippet with an example). And yes, this does work for all |
||
|
||
swap(faces, other.faces); | ||
swap(n_of_faces, other.n_of_faces); | ||
} | ||
|
||
return *this; | ||
} | ||
|
||
__device__ Polyhedron::Polyhedron(Polyhedron &&other) noexcept | ||
{ | ||
*this = std::move(other); | ||
} | ||
|
||
__device__ Polyhedron::~Polyhedron() | ||
{ | ||
free((void *)faces); | ||
} | ||
|
||
|
||
__device__ Face *Polyhedron::find_face_by_point(SpacePoint point) const | ||
{ | ||
for(int i = 0; i < n_of_faces; ++i) | ||
|
@@ -25,6 +60,16 @@ __device__ Face *Polyhedron::find_face_by_point(SpacePoint point) const | |
return &faces[0]; | ||
} | ||
|
||
__device__ Face *Polyhedron::get_faces() const | ||
{ | ||
return faces; | ||
} | ||
|
||
__device__ int Polyhedron::get_n_of_faces() const | ||
{ | ||
return n_of_faces; | ||
} | ||
|
||
|
||
__device__ bool does_edge_belong_to_face(SpacePoint a, SpacePoint b, const Face *face) | ||
{ | ||
|
@@ -41,12 +86,12 @@ __device__ bool does_edge_belong_to_face(SpacePoint a, SpacePoint b, const Face | |
|
||
__device__ Face *find_face_next_to_edge(int vertex_id, Face *current_face, Polyhedron *polyhedron) | ||
{ | ||
for(int i = 0; i < polyhedron->n_of_faces; ++i) | ||
if(polyhedron->faces[i] != *current_face && | ||
for(int i = 0; i < polyhedron->get_n_of_faces(); ++i) | ||
if(polyhedron->get_faces()[i] != *current_face && | ||
does_edge_belong_to_face(current_face->get_vertices()[vertex_id], | ||
current_face->get_vertices()[vertex_id + 1], | ||
&polyhedron->faces[i])) | ||
return &polyhedron->faces[i]; | ||
&polyhedron->get_faces()[i])) | ||
return &polyhedron->get_faces()[i]; | ||
return current_face; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Field
Face::vertices
has typeconst SpacePoint
. Other variables which were moved to private section of their classes have noconst
in their types orconst
was removed from the type. DoesFace::vertices
really need const in type?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, note, that the actual type is
const SpacePoint *
(*
is an important part). It is different from theconst SpacePoint *const
type (which would mean the fieldvertices
can't be modified), but the actual time means that the content of the array can't be modified without replacing the whole array. If the field was const pointer, it would take away the opportunity to, for example, move a face into an existing one, but the pointer to const only prevents us from accidentally modifying the vertices array (vertices of an existingFace
shouldn't change i. e. have a specific set of vertices, shouldn't they?). Because the field is private, it's already impossible to modify the array from outside of theFace
class, but is still possible to modify it from inside, which shouldn't happen and is worth protecting.The general rule is if you don't modify a variable, make it constant. This way you will get additional protection from your compiler