#include #include #include #include #include #include "JLang/JManip.hh" #include "Jeep/JParser.hh" #include "Jeep/JMessage.hh" namespace { struct A { A(const double value) : value(value) {} friend inline std::ostream& operator<<(std::ostream& out, const A& object) { const JFormat format(out, getFormat(JFormat_t(5, 2, std::ios::fixed | std::ios::showpos))); return out << format << object.value; } double value; }; struct B { B(const double value) : value(value) {} friend inline std::ostream& operator<<(std::ostream& out, const B& object) { const JFormat format(out, getFormat(JFormat_t(12, 3, std::ios::scientific | std::ios::showpos))); return out << format << object.value; } double value; }; struct C { friend inline std::ostream& operator<<(std::ostream& out, const C& object) { using namespace JPP; switch (getPrintOption(out)) { case SHORT_PRINT: return out << "C::shortprint"; case MEDIUM_PRINT: return out << "C::mediumprint"; case LONG_PRINT: return out << "C::longprint"; default: return out << "C::undefined"; } } }; inline void print(std::ostream& out, const JFormat_t& format) { out << "(" << format.width << "," << format.precision << ")"; } } /** * \file * Example program to test I/O manipulators. * \author mdejong */ int main(int argc, char* argv[]) { using namespace std; using namespace JPP; int debug; try { JParser<> zap; zap['d'] = make_field(debug) = 1; zap(argc, argv); } catch(const exception &error) { FATAL(error.what() << endl); } for (int value = 1; value < 1000000000; value *= 10) { cout << "CENTER <" << CENTER(12) << value << ">" << endl; } for (int value = 1; value < 1000000000; value *= 10) { cout << "FILL <" << FILL(12,'.') << value << ">" << FILL() << endl; } for (int value = 1; value < 1000000000; value *= 10) { cout << "RIGHT <" << RIGHT(12) << value << ">" << endl; } for (int value = 1; value < 1000000000; value *= 10) { cout << "LEFT <" << LEFT(12) << value << ">" << endl; } for (double value = 0.123456; value < 100000; value *= 10) { cout << "FIXED <" << FIXED(12,6) << value << ">" << endl; } { const double c = 12.34; const A a(c); const B b(c); cout << setprecision(3); cout << "A <" << a << ">" << endl; cout << "c <" << c << ">" << endl; cout << "B <" << b << ">" << endl; cout << "c <" << c << ">" << endl; setFormat(JFormat_t(12, 3, std::ios::scientific)); setFormat(JFormat_t( 5, 2, std::ios::fixed)); cout << "A <" << a << ">" << endl; cout << "c <" << c << ">" << endl; cout << "B <" << b << ">" << endl; cout << "c <" << c << ">" << endl; } { C c; cout << shortprint << c << endl; cout << mediumprint << c << endl; cout << longprint << c << endl; } { ostringstream os[2]; const int i = 123456; os[0] << setw(12) << left << i; os[1] << LEFT(12) << i; ASSERT(os[0].str() == os[1].str(), "<" << os[0].str() << "> == <" << os[1].str() << ">"); } { ostringstream os[2]; const int i = 123456; os[0] << setw(12) << right << i; os[1] << RIGHT(12) << i; ASSERT(os[0].str() == os[1].str(), "<" << os[0].str() << "> == <" << os[1].str() << ">"); } { ostringstream os[2]; const int i = 123456; os[0] << setw(12) << setfill('0') << i; os[1] << FILL(12, '0') << i; ASSERT(os[0].str() == os[1].str(), "<" << os[0].str() << "> == <" << os[1].str() << ">"); } { ostringstream os[2]; const double x = 123.456; os[0] << setw(12) << setprecision(5) << fixed << x; os[1] << FIXED(12,5) << x; ASSERT(os[0].str() == os[1].str(), "<" << os[0].str() << "> == <" << os[1].str() << ">"); } { ostringstream os[2]; const double x = 123.456; os[0] << setw(12) << setprecision(2) << scientific << x; os[1] << SCIENTIFIC(12,2) << x; ASSERT(os[0].str() == os[1].str(), "<" << os[0].str() << "> == <" << os[1].str() << ">"); } { vector V = { 1, 2, 3, 4}; cout << LAMBDA([v = V](ostream& out) { for (const auto& i : v) { out << " " << i; } }) << endl; } return 0; }