Jpp 20.0.0-rc.2
the software that should make you happy
Loading...
Searching...
No Matches
JUnity.hh
Go to the documentation of this file.
1#ifndef __JMATH__JUNITY__
2#define __JMATH__JUNITY__
3
4#include <type_traits>
5
6
7/**
8 * \file
9 *
10 * Definition of unity value for any class.
11 * \author bjung
12 */
13namespace JMATH {}
14namespace JPP { using namespace JMATH; }
15
16namespace JMATH {
17
18 /**
19 * Auxiliary class for obtaining unity values of a given data type.
20 */
21 template<class T, bool = std::is_integral<T>::value>
23 {
24 /**
25 * Get unity value.
26 *
27 * The default implementation of this method returns an object which
28 * is created with the default constructor.
29 * This class should be specialised if this value does not correspond
30 * to the equivalent of a unity result.
31 *
32 * \return unity
33 */
34 inline static T getUnity()
35 {
36 return T();
37 }
38 };
39
40
41 /**
42 * Partial template specialisation for integral types.
43 */
44 template<class T>
45 struct JUnityHelper<T, true>
46 {
47 /**
48 * Get unity value.
49 *
50 * \return unity
51 */
52 inline static T getUnity()
53 {
54 return T(1);
55 }
56 };
57
58
59 /**
60 * Get unity value for a given data type.
61 *
62 * The default implementation of this method returns an object which
63 * is created with the default constructor.
64 * This method should be specialised if this value does not correspond
65 * to the equivalent of a unity result.
66 *
67 * \return unity
68 */
69 template<class T>
70 inline T getUnity() {
72 }
73
74
75 /**
76 * Get unity value for <tt>bool</tt>.
77 *
78 * \return false
79 */
80 template<> inline bool getUnity<bool>()
81 {
82 return true;
83 }
84
85
86 /**
87 * Get unity value for <tt>float</tt>.
88 *
89 * \return unity
90 */
91 template<> inline float getUnity<float>()
92 {
93 return float(1.0);
94 }
95
96
97 /**
98 * Get unity value for <tt>double</tt>.
99 *
100 * \return unity
101 */
102 template<>
103 inline double getUnity<double>()
104 {
105 return double(1.0);
106 }
107
108
109 /**
110 * Get unity value for <tt>long double</tt>.
111 *
112 * \return unity
113 */
114 template<>
115 inline long double getUnity<long double>()
116 {
117 return (long double)(1.0);
118 }
119
120
121 /**
122 * Auxiliary class to assign unity value.
123 */
124 struct JUnity {
125 /**
126 * Default constructor.
127 */
129 {}
130
131
132 /**
133 * Type conversion operator.
134 *
135 * \return unity
136 */
137 template<class T>
138 operator T() const
139 {
140 return getUnity<T>();
141 }
142 };
143
144
145 /**
146 * Function object to assign unity value.
147 */
148 static const JUnity unity;
149}
150
151#endif
Auxiliary classes and methods for mathematical operations.
Definition JEigen3D.hh:88
bool getUnity< bool >()
Get unity value for bool.
Definition JUnity.hh:80
long double getUnity< long double >()
Get unity value for long double.
Definition JUnity.hh:115
T getUnity()
Get unity value for a given data type.
Definition JUnity.hh:70
double getUnity< double >()
Get unity value for double.
Definition JUnity.hh:103
float getUnity< float >()
Get unity value for float.
Definition JUnity.hh:91
static const JUnity unity
Function object to assign unity value.
Definition JUnity.hh:148
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
static T getUnity()
Get unity value.
Definition JUnity.hh:52
Auxiliary class for obtaining unity values of a given data type.
Definition JUnity.hh:23
static T getUnity()
Get unity value.
Definition JUnity.hh:34
Auxiliary class to assign unity value.
Definition JUnity.hh:124
JUnity()
Default constructor.
Definition JUnity.hh:128