Spatial C++ Library
Generic Multi-Dimensional Containers and Spatial Operations
spatial_except.hpp
1 // -*- C++ -*-
2 //
3 // Copyright Sylvain Bougerel 2009 - 2013.
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file COPYING or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 
13 #ifndef SPATIAL_EXCEPT_HPP
14 #define SPATIAL_EXCEPT_HPP
15 
16 #include <sstream>
17 #include "../exception.hpp"
18 
19 namespace spatial
20 {
21  namespace except
22  {
27  inline void check_rank(dimension_type rank)
28  { if (0 == rank) throw invalid_rank("0"); }
29 
34  inline void check_even_rank(dimension_type rank)
35  {
36  if (0 == rank) throw invalid_rank("0");
37  if (rank & 1u)
38  {
39  std::stringstream out;
40  out << rank << " is an odd value";
41  throw invalid_odd_rank(out.str());
42  }
43  }
44 
49  inline void check_dimension
50  (dimension_type rank, dimension_type dimension)
51  {
52  if (dimension >= rank)
53  {
54  std::stringstream out;
55  out << dimension << " is out of range";
56  throw invalid_dimension(out.str());
57  }
58  }
59 
66  template<typename Node>
67  inline void check_node(Node* node)
68  {
69  if (node == 0 || node->left == node)
70  throw invalid_node("node points to null or header node");
71  }
72 
78  template<typename Node>
79  inline void check_node_iterator(Node* node)
80  {
81  if (node == 0 || node->left == node)
82  throw invalid_iterator("iterator points to null or header node");
83  }
84 
94  template <typename Ptr1, typename Ptr2>
95  inline void check_iterator(Ptr1 ptr1, Ptr2 ptr2)
96  {
97  if (ptr1 != ptr2)
98  throw invalid_iterator
99  ("iterator is invalid or does not belong to the container used");
100  }
101 
108  template<typename Tp>
109  inline void check_empty_container(const Tp& cont)
110  {
111  if (cont.empty())
112  throw invalid_empty_container("container is empty");
113  }
114 
127  template<typename Tp>
128  inline void check_open_bounds
129  (const Tp& container,
130  const typename container_traits<Tp>::key_type& lower,
131  const typename container_traits<Tp>::key_type& upper)
132  {
133  for (dimension_type dim = 0; dim < container.dimension(); ++dim)
134  if (!container.key_comp()(dim, lower, upper))
135  throw invalid_bounds
136  ("lower is greater or equal to upper over one dimension at least");
137  }
138 
148  template<typename Tp>
149  inline void check_bounds
150  (const Tp& container,
151  const typename container_traits<Tp>::key_type& lower,
152  const typename container_traits<Tp>::key_type& upper)
153  {
154  return check_open_bounds(container, lower, upper);
155  }
156 
166  template<typename Tp>
167  inline void check_closed_bounds
168  (const Tp& container,
169  const typename container_traits<Tp>::key_type& lower,
170  const typename container_traits<Tp>::key_type& upper)
171  {
172  for (dimension_type dim = 0; dim < container.dimension(); ++dim)
173  if (container.key_comp()(dim, upper, lower))
174  throw invalid_bounds
175  ("upper is stricly less than lower over one dimension at least");
176  }
177 
179 
188  template <typename Tp>
189  inline void check_box
190  (const Tp& container,
191  const typename container_traits<Tp>::key_type& box,
193  {
194  dimension_type rank = container.dimension() >> 1;
195  for (dimension_type i = 0; i < rank; ++i)
196  if (container.key_comp()(i + rank, box, i, box))
197  throw invalid_box
198  ("box does not follow specified layout or coordinates are invalid");
199  }
200 
201  template <typename Tp>
202  inline void check_box
203  (const Tp& container,
204  const typename container_traits<Tp>::key_type& box,
206  {
207  dimension_type rank = container.dimension() >> 1;
208  for (dimension_type i = 0; i < rank; ++i)
209  if (container.key_comp()(i, box, i + rank, box))
210  throw invalid_box
211  ("box does not follow specified layout or coordinates are invalid");
212  }
213 
214  template <typename Tp>
215  inline void check_box
216  (const Tp& container,
217  const typename container_traits<Tp>::key_type& box,
219  {
220  for (dimension_type i = 0; i < container.dimension(); i += 2)
221  if (container.key_comp()(i + 1, box, i, box))
222  throw invalid_box
223  ("box does not follow specified layout or coordinates are invalid");
224  }
225 
226  template <typename Tp>
227  inline void check_box
228  (const Tp& container,
229  const typename container_traits<Tp>::key_type& box,
231  {
232  for (dimension_type i = 0; i < container.dimension(); i += 2)
233  if (container.key_comp()(i, box, i + 1, box))
234  throw invalid_box
235  ("box does not follow specified layout or coordinates are invalid");
236  }
238  } // namespace except
239 } // namespace spatial
240 
241 #endif // SPATIAL_EXCEPT_HPP
Thrown to report that an invalid node was passed as an argument.
Definition: exception.hpp:53
void check_node(Node *node)
Checks that the node's pointer given as an argument to a function is not null or does not point to a ...
Thrown to report that an empty container was passed as an argument, while the function does not accep...
Definition: exception.hpp:72
void check_bounds(const Tp &container, const typename container_traits< Tp >::key_type &lower, const typename container_traits< Tp >::key_type &upper)
Checks if all coordinates of lower are strictly less than these of higher along the same dimensions...
void check_open_bounds(const Tp &container, const typename container_traits< Tp >::key_type &lower, const typename container_traits< Tp >::key_type &upper)
Checks if all coordinates of lower are strictly less than these of higher along the same dimensions...
Represents a coordinate layout for the box.
Definition: spatial.hpp:126
Thrown to report that an invalid rank was passed as an argument.
Definition: exception.hpp:25
void check_dimension(dimension_type rank, dimension_type dimension)
Checks that dimension is not greater or equal to rank.
Thrown to report that an invalid range bound has been given as argument.
Definition: exception.hpp:86
Represents a coordinate layout for the box.
Definition: spatial.hpp:97
void check_even_rank(dimension_type rank)
Checks that rank is not null and that it is a multiple of 2.
Tp::key_type key_type
The type representing the key managed by the container.
Definition: traits.hpp:48
void check_box(const Tp &container, const typename container_traits< Tp >::key_type &box, llhh_layout_tag)
Checks that all coordinates of a box are matching with the layout specified or else, raise an invalid_box exception.
Thrown to report that an invalid dimension was passed as an argument.
Definition: exception.hpp:44
void check_rank(dimension_type rank)
Checks that rank is not null.
Thrown to report that a box has incorrect coordinates with regards to its layout. ...
Definition: exception.hpp:97
void check_iterator(Ptr1 ptr1, Ptr2 ptr2)
Checks if two iterators are of equal values, if not raises the invalid_iterator exception.
Thrown to report that an invalid iterator was passed as an argument.
Definition: exception.hpp:62
std::size_t dimension_type
Defines the type for the dimension as being a size.
Definition: spatial.hpp:71
void check_empty_container(const Tp &cont)
Checks that the container given as an argument to a function is not empty.
Represents a coordinate layout for the box.
Definition: spatial.hpp:112
The main namespace used in the library.
Definition: algorithm.hpp:23
Thrown to report that an odd rank value was passed as a argument.
Definition: exception.hpp:34
Represents a coordinate layout for the box.
Definition: spatial.hpp:141
void check_closed_bounds(const Tp &container, const typename container_traits< Tp >::key_type &lower, const typename container_traits< Tp >::key_type &upper)
Checks if all coordinates of lower are less or equal to these of higher along the same dimensions...
void check_node_iterator(Node *node)
Checks that the node pointed to by an iterator and given as an argument to a function is not null or ...