HerdSoftware  0.3.2
DefaultValues.h
Go to the documentation of this file.
1 /*
2  * DefaultValues.h
3  *
4  * Created on: 21 Jul 2020
5  * Author: Valerio Formato
6  */
7 
10 #ifndef HERD_DEFAULTVALUES_H_
11 #define HERD_DEFAULTVALUES_H_
12 
13 #include "common/AxesArray.h"
14 #include "common/CooArray.h"
15 #include "common/RefFrame.h"
16 #include "common/Vec3D.h"
17 
18 #include <bitset>
19 #include <limits>
20 #include <string>
21 
22 // clang-format off
23 #define DEFAULT_INIT(x) x{defaultValue<decltype(x)>}
24 #define SET_TO_DEFAULT(x) x = DefaultValue(x)
25 // clang-format on
26 
27 namespace Herd {
28 namespace {
29 
30 // default for numeric types
31 template <typename T>
32 constexpr typename std::enable_if_t<std::is_arithmetic<T>::value, T> defaultValue = std::numeric_limits<T>::max();
33 
34 // std::string specialization
35 template <> const std::string defaultValue<std::string> = "";
36 
37 // RefFrame::Direction specialization
38 template <> constexpr RefFrame::Direction defaultValue<RefFrame::Direction> = RefFrame::Direction::NONE;
39 
40 // RefFrame::Side specialization
41 template <> constexpr RefFrame::Side defaultValue<RefFrame::Side> = RefFrame::Side::NONE;
42 
43 // Vec3D specialization (default constructor is already filled with "invalid" values)
44 template <> const Vec3D defaultValue<Vec3D> = Vec3D();
45 
46 // CooArray specialization
47 template <typename T>
48 const CooArray<T> defaultValue<CooArray<T>> = CooArray<T>(defaultValue<T>, defaultValue<T>, defaultValue<T>);
49 
50 // needed for std::array specialization
51 template <class T, size_t N> std::array<T, N> make_array(const T &v) {
52  std::array<T, N> ret;
53  ret.fill(v);
54  return ret;
55 }
56 
57 // std::pair specialization
58 template <typename T, typename U>
59 constexpr std::pair<T, U> defaultValue<std::pair<T, U>> = std::make_pair(defaultValue<T>, defaultValue<U>);
60 
61 // std::array specialization
62 template <class T, size_t N> const std::array<T, N> defaultValue<std::array<T, N>> = make_array<T, N>(defaultValue<T>);
63 
64 namespace _Private { // needed for AxesArray specialization
65 template <typename T> AxesArray<T> getDefaultAxesArrayValue() {
66  AxesArray<T> result;
67  result[RefFrame::Axis::X] = defaultValue<T>;
68  result[RefFrame::Axis::Y] = defaultValue<T>;
69  result[RefFrame::Axis::Z] = defaultValue<T>;
70  return result;
71 }
72 } // namespace _Private
73 
74 // AxesArray specialization
75 template <typename T> const AxesArray<T> defaultValue<AxesArray<T>> = _Private::getDefaultAxesArrayValue<T>();
76 
77 // bitset specialization
78 template <int N> const std::bitset<N> defaultValue<std::bitset<N>> = std::bitset<N>();
79 
80 // generic getter
81 template <typename T> constexpr T DefaultValue(const T &x) {
82  return defaultValue<std::remove_const_t<std::remove_reference_t<decltype(x)>>>;
83 }
84 
85 } // namespace
86 } // namespace Herd
87 #endif
CssGeoParams.h CssGeoParams class declaration.
Definition: CaloPDCalibrationAlgo.h:24
An array that accepts axis as subscripts.
Definition: AxesArray.h:27
Side
Aliases for the five sides. RefFrame.h common/RefFrame.h.
Definition: RefFrame.h:113
Direction
Aliases for the six axis directions. RefFrame.h common/RefFrame.h.
Definition: RefFrame.h:57