Jpp 20.0.0-rc.2
the software that should make you happy
Loading...
Searching...
No Matches
JUnity.cc
Go to the documentation of this file.
1#include <iostream>
2#include <iomanip>
3
4#include "JMath/JUnity.hh"
5
6#include "Jeep/JParser.hh"
7#include "Jeep/JMessage.hh"
8
9
10namespace {
11
12 struct __A__ {
13 /**
14 * Default constructor.
15 */
16 __A__() :
17 a(0)
18 {}
19
20 /**
21 * Constructor.
22 *
23 * \param a value
24 */
25 __A__(double a)
26 {
27 this->a = a;
28 }
29
30 /**
31 * Equals operator.
32 *
33 * \param first first value
34 * \param second second value
35 * \return true if first and second value are equal; else false
36 */
37 friend inline bool operator==(const __A__& first, const __A__& second)
38 {
39 return first.a == second.a;
40 }
41
42 /**
43 * Write object to output.
44 *
45 * \param out output stream
46 * \param object object
47 * \return output stream
48 */
49 friend inline std::ostream& operator<<(std::ostream& out, const __A__& object)
50 {
51 return out << object.a;
52 }
53
54 double a;
55 };
56
57 /**
58 * Test equaliilty.
59 *
60 * \param unity unity value
61 * \return true if JMath::unity equals given unity value; else false
62 */
63 template<class T>
64 bool inline is_equal(const T& unity)
65 {
66 T value = JMATH::unity;
67
68 return value == unity;
69 }
70
71 /**
72 * Print class and unity value.
73 *
74 * \param out output stream
75 * \param name class name
76 */
77 template<class T>
78 inline void print(std::ostream& out, const char* name)
79 {
80 using namespace std;
81
82 T value = JMATH::unity;
83
84 out << setw(12) << left << name << ' ' << value << endl;
85 }
86
87#define PRINT(OUT, CLASS) print<CLASS>(OUT, #CLASS)
88}
89
90
91namespace JMATH {
92
93 /**
94 * Template specialisation of `JMATH::JUnityHelper` for class `::__A__`.
95 */
96 template<>
97 struct JUnityHelper<::__A__, false>
98 {
99 /**
100 * Get unity value.
101 */
102 inline static ::__A__ getUnity()
103 {
104 return ::__A__(1);
105 }
106 };
107}
108
109
110/**
111 * \file
112 *
113 * Example program to test unity values (JMATH::unity).
114 * \author bjung
115 */
116int main(int argc, char**argv)
117{
118 using namespace std;
119
120 int debug;
121
122 try {
123
124 JParser<> zap("Example program to test unity values of non-primitive data types.");
125
126 zap['d'] = make_field(debug) = 3;
127
128 zap(argc, argv);
129 }
130 catch(const exception &error) {
131 FATAL(error.what() << endl);
132 }
133
134 using namespace JPP;
135
136 PRINT(cout, int);
137 PRINT(cout, float);
138 PRINT(cout, double);
139 PRINT(cout, __A__);
140
141 ASSERT(is_equal<int> (1));
142 ASSERT(is_equal<float> (1.0));
143 ASSERT(is_equal<double>(1.0));
144 ASSERT(is_equal<__A__> (__A__(1.0)));
145
146 return 0;
147}
General purpose messaging.
#define ASSERT(A,...)
Assert macro.
Definition JMessage.hh:90
#define FATAL(A)
Definition JMessage.hh:67
int debug
debug level
Definition JSirene.cc:72
Utility class to parse command line options.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition JParser.hh:2142
void print(const TH1 &h1, std::ostream &out)
Print histogram parameters.
int main(int argc, char **argv)
Definition JUnity.cc:116
#define PRINT(OUT, CLASS)
Definition JUnity.cc:87
Definition of unity value for any class.
Utility class to parse command line options.
Definition JParser.hh:1698
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
Auxiliary classes and methods for mathematical operations.
Definition JEigen3D.hh:88
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).
bool operator==(Packet const &p, ID const &id)
::__A__ getUnity()
Get unity value.
Definition JUnity.cc:102
Auxiliary class for obtaining unity values of a given data type.
Definition JUnity.hh:23