HerdSoftware  0.4.0
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 
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 <> constexpr Vec3D defaultValue<Vec3D> = Vec3D();
45 
46 // needed for std::array specialization
47 template <class T, size_t N> constexpr std::array<T, N> make_array(const T &v) {
48  std::array<T, N> ret{};
49  for (auto &val : ret) {
50  val = v;
51  }
52  return ret;
53 }
54 
55 // std::array specialization
56 template <class T, size_t N>
57 constexpr std::array<T, N> defaultValue<std::array<T, N>> = make_array<T, N>(defaultValue<T>);
58 
59 // CooArray specialization
60 template <typename T> constexpr CooArray<T> defaultValue<CooArray<T>> = CooArray<T>(make_array<T, 3>(defaultValue<T>));
61 
62 // std::pair specialization
63 template <typename T, typename U>
64 constexpr std::pair<T, U> defaultValue<std::pair<T, U>> = std::make_pair(defaultValue<T>, defaultValue<U>);
65 
66 namespace _Private { // needed for AxesArray specialization
67 template <typename T> constexpr AxesArray<T> getDefaultAxesArrayValue() {
68  AxesArray<T> result;
69  result[RefFrame::Axis::X] = defaultValue<T>;
70  result[RefFrame::Axis::Y] = defaultValue<T>;
71  result[RefFrame::Axis::Z] = defaultValue<T>;
72  return result;
73 }
74 } // namespace _Private
75 
76 // AxesArray specialization
77 template <typename T> constexpr AxesArray<T> defaultValue<AxesArray<T>> = _Private::getDefaultAxesArrayValue<T>();
78 
79 // bitset specialization
80 template <int N> constexpr std::bitset<N> defaultValue<std::bitset<N>> = std::bitset<N>();
81 
82 // generic getter
83 template <typename T> constexpr T DefaultValue(const T &x) {
84  return defaultValue<std::remove_const_t<std::remove_reference_t<decltype(x)>>>;
85 }
86 
87 } // namespace
88 } // namespace Herd
89 #endif
Herd::AxesArray
An array that accepts axis as subscripts.
Definition: AxesArray.h:27
AxesArray.h
Herd::RefFrame::Axis::X
@ X
Herd::RefFrame::Direction
Direction
Aliases for the six axis directions.
Definition: RefFrame.h:55
Herd::RefFrame::Side
Side
Aliases for the five sides.
Definition: RefFrame.h:111
Vec3D.h
Herd
CssGeoParams.h CssGeoParams class declaration.
Definition: CaloPDCalibrationAlgo.h:22
CooArray.h
Herd::RefFrame::Axis::Y
@ Y
RefFrame.h
Herd::RefFrame::Axis::Z
@ Z
Herd::RefFrame::Direction::NONE
@ NONE
Herd::RefFrame::Side::NONE
@ NONE