diff --git a/GeoLib/AABB.h b/GeoLib/AABB.h index 2781ae7f1fc4120e879233955c30937da8c6adbe..35e13a548d24c139c0648304fd5899d317d5775b 100644 --- a/GeoLib/AABB.h +++ b/GeoLib/AABB.h @@ -20,6 +20,7 @@ #include <iterator> #include <cassert> #include <vector> +#include <stdexcept> #include "Point.h" @@ -60,18 +61,20 @@ public: /** * Construction of object using input iterators. In contrast to give a vector * this approach is more generic. You can use every (stl) container and - * C arrays as input for constructing the object. The constructor requires - * that std::distance(first, last) > 0. + * C arrays as input for constructing the object. + * @attention{The constructor requires that std::distance(first, last) > 0.} * @param first the input iterator to the initial position in the sequence * @param last the input iterator to the final position in a container, i.e. [first, last). - * The iterator last must be reachable from first. + * @attention{The iterator last must be reachable from first.} */ template <typename InputIterator> AABB(InputIterator first, InputIterator last) : _min_pnt(std::numeric_limits<double>::max(), std::numeric_limits<double>::max(), std::numeric_limits<double>::max()), _max_pnt(std::numeric_limits<double>::min(), std::numeric_limits<double>::min(), std::numeric_limits<double>::min()) { - assert(std::distance(first,last) > 0); + if (! (std::distance(first,last) > 0)) { + throw std::invalid_argument("AABB::AABB(InputIterator first, InputIterator last): first == last"); + } init(*first); InputIterator it(first); while (it != last) { diff --git a/GeoLib/PointVec.cpp b/GeoLib/PointVec.cpp index f60701abbc124c3db259cd506953e6510f2579b2..a9548aac5e44c7f35e1234c1b9704745f06bd236 100644 --- a/GeoLib/PointVec.cpp +++ b/GeoLib/PointVec.cpp @@ -115,7 +115,7 @@ double PointVec::getShortestPointDistance () const return sqrt (_sqr_shortest_dist); } -void PointVec::makePntsUnique (std::vector<GeoLib::Point*> *& pnt_vec, +void PointVec::makePntsUnique (std::vector<GeoLib::Point*>* pnt_vec, std::vector<std::size_t> &pnt_id_map, double eps) { std::size_t n_pnts_in_file(pnt_vec->size()); @@ -184,10 +184,8 @@ void PointVec::makePntsUnique (std::vector<GeoLib::Point*> *& pnt_vec, } } - std::vector<GeoLib::Point*>* tmp_pnt_vec(new std::vector<GeoLib::Point*>(n_pnts_in_file - cnt)); - std::remove_copy(pnt_vec->begin(), pnt_vec->end(), tmp_pnt_vec->begin(), nullptr); - std::swap(tmp_pnt_vec, pnt_vec); - delete tmp_pnt_vec; + auto const pnt_vec_end = std::remove(pnt_vec->begin(), pnt_vec->end(), nullptr); + pnt_vec->erase(pnt_vec_end, pnt_vec->end()); // renumber id-mapping cnt = 0; diff --git a/GeoLib/PointVec.h b/GeoLib/PointVec.h index 61a27d3f18bdcc068210e29ec09c9ad78b5c820c..2ed7a3936db74c2a1b426b4e245c89f8917feeb2 100644 --- a/GeoLib/PointVec.h +++ b/GeoLib/PointVec.h @@ -55,12 +55,14 @@ public: * pointer the vector of names of the points * and sets the type of PointVec. * @param name the name of the point group - * @param points pointer to a vector of GeoLib::Pointers - - * PointVec will take the ownership of the vector, - * i.e. delete the points and the vector itself - * @param name_id_map the names to the points - - * PointVec will take the ownership of the vector, i.e. it - * deletes the names + * @param points Pointer to a vector of pointers to GeoLib::Points. + * @attention{PointVec will take the ownership of (the pointer to) + * the vector, i.e. it deletes the vector in the destructor! The class + * takes also the ownership of the GeoLib::Points the pointers within + * the vector points at, i.e. it delete the points!} + * @param name_id_map A std::map that stores the relation name to point. + * @attention{PointVec will take the ownership of the vector, i.e. it + * deletes the names.} * @param type the type of the point, \sa enum PointType * @param rel_eps This is a relative error tolerance value for the test of identical points. * The size of the axis aligned bounding box multiplied with the value of rel_eps gives the @@ -108,7 +110,7 @@ public: std::vector<GeoLib::Point*>* getSubset(const std::vector<std::size_t> &subset); private: - void makePntsUnique (std::vector<GeoLib::Point*>*& pnt_vec, std::vector<std::size_t> &pnt_id_map, double eps = sqrt(std::numeric_limits<double>::min())); + void makePntsUnique (std::vector<GeoLib::Point*>* pnt_vec, std::vector<std::size_t> &pnt_id_map, double eps = sqrt(std::numeric_limits<double>::min())); /** copy constructor doesn't have an implementation */ // compiler does not create a (possible unwanted) copy constructor diff --git a/GeoLib/TemplateVec.h b/GeoLib/TemplateVec.h index 3d43a94f6b3f20c18e68af7fd541811cbcee4748..e79766dd344a0af07703e4fe8faf4e9e1eb9f06a 100644 --- a/GeoLib/TemplateVec.h +++ b/GeoLib/TemplateVec.h @@ -17,6 +17,7 @@ #define TEMPLATEVEC_H_ #include <algorithm> +#include <stdexcept> namespace GeoLib { @@ -38,7 +39,10 @@ public: * Constructor of class TemlateVec. * @param name unique name of the project the elements belonging to. * In order to access the data elements a unique name is required. - * @param data_vec vector of data elements + * @param data_vec Vector of data elements. + * @attention{TemplateVec will take the ownership of the vector + * and also its elements, + * i.e. delete its elements and delete the vector itself!} * @param elem_name_map Names of data elements can be given by a * std::map<std::string, std::size_t>. Here the std::string is the name * of the element and the value for std::size_t stands for an index in @@ -49,6 +53,9 @@ public: NameIdMap* elem_name_map = nullptr) : _name(name), _data_vec(data_vec), _name_id_map (elem_name_map) { + if (_data_vec == nullptr) { + throw std::invalid_argument("Constructor TemplateVec: vector of data elements is a nullptr."); + } if (!_name_id_map) _name_id_map = new NameIdMap; }