HerdSoftware  0.1.1
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> constexpr T defaultValue = std::numeric_limits<T>::max();
32 
33 // std::string specialization
34 template <> const std::string defaultValue<std::string> = "";
35 
36 // RefFrame::Direction specialization
37 template <> constexpr RefFrame::Direction defaultValue<RefFrame::Direction> = RefFrame::Direction::NONE;
38 
39 // RefFrame::Side specialization
40 template <> constexpr RefFrame::Side defaultValue<RefFrame::Side> = RefFrame::Side::NONE;
41 
42 // Vec3D specialization (default constructor is already filled with "invalid" values)
43 template <> const Vec3D defaultValue<Vec3D> = Vec3D();
44 
45 // CooArray specialization
46 template <typename T>
47 const CooArray<T> defaultValue<CooArray<T>> = CooArray<T>(defaultValue<T>, defaultValue<T>, defaultValue<T>);
48 
49 // needed for std::array specialization
50 template <class T, size_t N> std::array<T, N> make_array(const T &v) {
51  std::array<T, N> ret;
52  ret.fill(v);
53  return ret;
54 }
55 
56 // std::array specialization
57 template <class T, size_t N> const std::array<T, N> defaultValue<std::array<T, N>> = make_array<T, N>(defaultValue<T>);
58 
59 namespace _Private { // needed for AxesArray specialization
60 template <typename T> AxesArray<T> getDefaultAxesArrayValue() {
61  AxesArray<T> result;
62  result[RefFrame::Axis::X] = defaultValue<T>;
63  result[RefFrame::Axis::Y] = defaultValue<T>;
64  result[RefFrame::Axis::Z] = defaultValue<T>;
65  return result;
66 }
67 } // namespace _Private
68 
69 // AxesArray specialization
70 template <typename T> const AxesArray<T> defaultValue<AxesArray<T>> = _Private::getDefaultAxesArrayValue<T>();
71 
72 // bitset specialization
73 template <int N> const std::bitset<N> defaultValue<std::bitset<N>> = std::bitset<N>();
74 
75 // generic getter
76 template <typename T> constexpr T DefaultValue(const T &x) {
77  return defaultValue<std::remove_const_t<std::remove_reference_t<decltype(x)>>>;
78 }
79 
80 } // namespace
81 } // namespace Herd
82 #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:104
Direction
Aliases for the six axis directions. RefFrame.h common/RefFrame.h.
Definition: RefFrame.h:52