Jpp test-rotations-old
the software that should make you happy
Loading...
Searching...
No Matches
JManip.cc
Go to the documentation of this file.
1#include <string>
2#include <iostream>
3#include <sstream>
4#include <iomanip>
5#include <vector>
6
7#include "JLang/JManip.hh"
8
9#include "Jeep/JParser.hh"
10#include "Jeep/JMessage.hh"
11
12namespace {
13
14 struct A {
15
16 A(const double value) : value(value) {}
17
18 friend inline std::ostream& operator<<(std::ostream& out, const A& object)
19 {
20 const JFormat format(out, getFormat<A>(JFormat_t(5, 2, std::ios::fixed | std::ios::showpos)));
21
22 return out << format << object.value;
23 }
24
25 double value;
26 };
27
28 struct B {
29
30 B(const double value) : value(value) {}
31
32 friend inline std::ostream& operator<<(std::ostream& out, const B& object)
33 {
34 const JFormat format(out, getFormat<B>(JFormat_t(12, 3, std::ios::scientific | std::ios::showpos)));
35
36 return out << format << object.value;
37 }
38
39 double value;
40 };
41
42 struct C {
43
44 friend inline std::ostream& operator<<(std::ostream& out, const C& object)
45 {
46 using namespace JPP;
47
48 switch (getPrintOption(out)) {
49
50 case SHORT_PRINT:
51 return out << "C::shortprint";
52
53 case MEDIUM_PRINT:
54 return out << "C::mediumprint";
55
56 case LONG_PRINT:
57 return out << "C::longprint";
58
59 default:
60 return out << "C::undefined";
61 }
62 }
63 };
64
65 inline void print(std::ostream& out, const JFormat_t& format)
66 {
67 out << "(" << format.width << "," << format.precision << ")";
68 }
69}
70
71
72/**
73 * \file
74 * Example program to test I/O manipulators.
75 * \author mdejong
76 */
77int main(int argc, char* argv[])
78{
79 using namespace std;
80 using namespace JPP;
81
82 int debug;
83
84 try {
85
86 JParser<> zap;
87
88 zap['d'] = make_field(debug) = 1;
89
90 zap(argc, argv);
91 }
92 catch(const exception &error) {
93 FATAL(error.what() << endl);
94 }
95
96 for (int value = 1; value < 1000000000; value *= 10) {
97 cout << "CENTER <" << CENTER(12) << value << ">" << endl;
98 }
99
100 for (int value = 1; value < 1000000000; value *= 10) {
101 cout << "FILL <" << FILL(12,'.') << value << ">" << FILL() << endl;
102 }
103
104 for (int value = 1; value < 1000000000; value *= 10) {
105 cout << "RIGHT <" << RIGHT(12) << value << ">" << endl;
106 }
107
108 for (int value = 1; value < 1000000000; value *= 10) {
109 cout << "LEFT <" << LEFT(12) << value << ">" << endl;
110 }
111
112 for (double value = 0.123456; value < 100000; value *= 10) {
113 cout << "FIXED <" << FIXED(12,6) << value << ">" << endl;
114 }
115
116
117 {
118 const double c = 12.34;
119
120 const A a(c);
121 const B b(c);
122
123 cout << setprecision(3);
124
125 cout << "A <" << a << ">" << endl;
126 cout << "c <" << c << ">" << endl;
127 cout << "B <" << b << ">" << endl;
128 cout << "c <" << c << ">" << endl;
129
130 setFormat<A>(JFormat_t(12, 3, std::ios::scientific));
131 setFormat<B>(JFormat_t( 5, 2, std::ios::fixed));
132
133 cout << "A <" << a << ">" << endl;
134 cout << "c <" << c << ">" << endl;
135 cout << "B <" << b << ">" << endl;
136 cout << "c <" << c << ">" << endl;
137 }
138 {
139 C c;
140
141 cout << shortprint << c << endl;
142 cout << mediumprint << c << endl;
143 cout << longprint << c << endl;
144 }
145 {
146 ostringstream os[2];
147
148 const int i = 123456;
149
150 os[0] << setw(12) << left << i;
151 os[1] << LEFT(12) << i;
152
153 ASSERT(os[0].str() == os[1].str(), "<" << os[0].str() << "> == <" << os[1].str() << ">");
154 }
155 {
156 ostringstream os[2];
157
158 const int i = 123456;
159
160 os[0] << setw(12) << right << i;
161 os[1] << RIGHT(12) << i;
162
163 ASSERT(os[0].str() == os[1].str(), "<" << os[0].str() << "> == <" << os[1].str() << ">");
164 }
165 {
166 ostringstream os[2];
167
168 const int i = 123456;
169
170 os[0] << setw(12) << setfill('0') << i;
171 os[1] << FILL(12, '0') << i;
172
173 ASSERT(os[0].str() == os[1].str(), "<" << os[0].str() << "> == <" << os[1].str() << ">");
174 }
175 {
176 ostringstream os[2];
177
178 const double x = 123.456;
179
180 os[0] << setw(12) << setprecision(5) << fixed << x;
181 os[1] << FIXED(12,5) << x;
182
183 ASSERT(os[0].str() == os[1].str(), "<" << os[0].str() << "> == <" << os[1].str() << ">");
184 }
185 {
186 ostringstream os[2];
187
188 const double x = 123.456;
189
190 os[0] << setw(12) << setprecision(2) << scientific << x;
191 os[1] << SCIENTIFIC(12,2) << x;
192
193 ASSERT(os[0].str() == os[1].str(), "<" << os[0].str() << "> == <" << os[1].str() << ">");
194 }
195
196 {
197 vector<int> V = { 1, 2, 3, 4};
198
199 cout << LAMBDA([v = V](ostream& out) { for (const auto& i : v) { out << " " << i; } }) << endl;
200 }
201
202 return 0;
203}
int main(int argc, char *argv[])
Definition JManip.cc:77
I/O manipulators.
int getPrintOption(std::ostream &out)
Get print option.
Definition JManip.hh:51
std::ostream & mediumprint(std::ostream &out)
Set medium printing.
Definition JManip.hh:158
void setFormat(const JFormat_t &format)
Set format for given type.
Definition JManip.hh:714
std::ostream & longprint(std::ostream &out)
Set long printing.
Definition JManip.hh:172
std::ostream & shortprint(std::ostream &out)
Set short printing.
Definition JManip.hh:144
JFormat_t & getFormat()
Get format for given type.
Definition JManip.hh:682
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.
Utility class to parse command line options.
Definition JParser.hh:1698
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
@ SHORT_PRINT
short print
Definition JManip.hh:38
@ MEDIUM_PRINT
medium print
Definition JManip.hh:39
@ LONG_PRINT
long print
Definition JManip.hh:40
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary data structure for alignment of data.
Definition JManip.hh:368
Auxiliary data structure for sequence of same character.
Definition JManip.hh:330
Auxiliary data structure for floating point format specification.
Definition JManip.hh:448
Data structure for format specifications.
Definition JManip.hh:524
int precision
Definition JManip.hh:623
int width
Definition JManip.hh:622
Auxiliary class to temporarily define format specifications.
Definition JManip.hh:636
Auxiliary data structure to convert (lambda) function to printable object.
Definition JManip.hh:726
Auxiliary data structure for alignment of data.
Definition JManip.hh:266
Auxiliary data structure for alignment of data.
Definition JManip.hh:298
Auxiliary data structure for floating point format specification.
Definition JManip.hh:488