JayBeams  0.1
Another project to have fun coding.
resize_if_applicable.hpp
Go to the documentation of this file.
1 #ifndef jb_testing_resize_if_application_hpp
2 #define jb_testing_resize_if_application_hpp
3 
4 #include <type_traits>
5 
6 namespace jb {
7 namespace testing {
8 
9 /**
10  * Type trait to evaluate if a collection of numbers (e.g. vector, multi_array)
11  * is resizable.
12  *
13  * has_risize<C>::value is std::true_type if C is resizable.
14  * std::false_type otherwise.
15  *
16  * @tparam C collection of numbers type to be evaluated if it is resizable
17  */
18 template <typename C>
19 class has_resize {
20 private:
21  template <typename T>
22  static constexpr auto check(T*) -> typename std::is_void<
23  decltype(std::declval<T>().resize(std::declval<std::size_t>()))>::type {
24  return std::true_type();
25  }
26 
27  template <typename T>
28  static constexpr std::false_type check(...) {
29  return std::false_type();
30  }
31 
32 public:
33  using type = decltype(check<C>(nullptr));
34  static constexpr bool value = type::value;
35 };
36 
37 /**
38  * Resize a timeseries to a newsize
39  *
40  * @param ts timeseries are any container representation (e.g. vector, deque),
41  * boost::multi_array, c-like array, etc
42  * @param newsize size to resize the timeseries if possible
43  * @tparam timeseries type of the timeseries collection representation
44  */
45 template <typename timeseries>
46 void resize_if_applicable(timeseries& ts, std::size_t newsize, std::true_type) {
47  ts.resize(newsize);
48 }
49 
50 /**
51  * Dummy specialization for non-resizable timeseries collections.
52  *
53  * @param ts timeseries are any container representation (e.g. vector, deque),
54  * boost::multi_array, c-like array, etc
55  * @param newsize size to resize the timeseries if possible
56  * @tparam timeseries type of the timeseries collection representation
57  */
58 template <typename timeseries>
60  timeseries& ts, std::size_t newsize, std::false_type) {
61 }
62 
63 /**
64  * Resize a timeseries to a newsize if it is resizable
65  *
66  * @param ts timeseries are any container representation (e.g. vector, deque),
67  * boost::multi_array, c-like array, etc
68  * @param newsize size to resize the timeseries if possible
69  * @tparam timeseries type of the timeseries collection representation
70  */
71 template <typename timeseries>
72 void resize_if_applicable(timeseries& ts, std::size_t newsize) {
73  using has = typename has_resize<timeseries>::type;
74  resize_if_applicable(ts, newsize, has());
75 }
76 
77 } // namespace testing
78 } // namespace jb
79 
80 #endif // jb_testing_resize_if_application_hpp
static constexpr auto check(T *) -> typename std::is_void< decltype(std::declval< T >().resize(std::declval< std::size_t >()))>::type
static constexpr std::false_type check(...)
void resize_if_applicable(timeseries &ts, std::size_t newsize, std::true_type)
Resize a timeseries to a newsize.
static constexpr bool value
Type trait to evaluate if a collection of numbers (e.g.
decltype(check< C >(nullptr)) type
The top-level namespace for the JayBeams library.
Definition: as_hhmmss.hpp:7