OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
SDDSColumn.cpp
Go to the documentation of this file.
1//
2// Class SDDSColumn
3// This class writes column entries of SDDS files.
4//
5// Copyright (c) 2019, Christof Metzger-Kraus, Open Sourcerer
6// All rights reserved
7//
8// This file is part of OPAL.
9//
10// OPAL is free software: you can redistribute it and/or modify
11// it under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// You should have received a copy of the GNU General Public License
16// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
17//
20
21#include <iomanip>
22#include <list>
23#include <variant>
24
26 const std::string& name, const std::string& type, const std::string& unit,
27 const std::string& desc, std::ios_base::fmtflags flags, unsigned short prec)
28 : name_m(name),
29 description_m(std::make_tuple(type, unit, desc)),
30 writeFlags_m(flags),
31 writePrecision_m(prec),
32 set_m(false) {
33 std::list<std::ios_base::fmtflags> numericalBase(
34 {std::ios_base::dec, std::ios_base::hex, std::ios_base::oct});
35 std::list<std::ios_base::fmtflags> floatFormat(
36 {std::ios_base::fixed, std::ios_base::scientific});
37 std::list<std::ios_base::fmtflags> adjustmentFlags(
38 {std::ios_base::internal, std::ios_base::left, std::ios_base::right});
39
40 // This code ensures that for each group of flags only one flag is given
41 for (std::ios_base::fmtflags flag : numericalBase) {
42 if (writeFlags_m & flag) {
43 writeFlags_m = (flag | (writeFlags_m & ~std::ios_base::basefield));
44 break;
45 }
46 }
47 for (std::ios_base::fmtflags flag : floatFormat) {
48 if (writeFlags_m & flag) {
49 writeFlags_m = (flag | (writeFlags_m & ~std::ios_base::floatfield));
50 break;
51 }
52 }
53 for (std::ios_base::fmtflags flag : adjustmentFlags) {
54 if (writeFlags_m & flag) {
55 writeFlags_m = (flag | (writeFlags_m & ~std::ios_base::adjustfield));
56 break;
57 }
58 }
59}
60
62 std::ostream& os, unsigned int colNr, const std::string& indent) const {
63 os << "&column\n"
64 << indent << "name=" << name_m << ",\n"
65 << indent << "type=" << std::get<0>(description_m) << ",\n";
66
67 if (std::get<1>(description_m) != "")
68 os << indent << "units=" << std::get<1>(description_m) << ",\n";
69
70 os << indent << "description=\"" << colNr << " " << std::get<2>(description_m) << "\"\n"
71 << "&end\n";
72}
73
74void SDDSColumn::writeValue(std::ostream& os) const {
75 if (!set_m) {
76 throw OpalException(
77 "SDDSColumn::writeValue", "value for column '" + name_m + "' isn't set");
78 }
79
80 os.flags(writeFlags_m);
81 os.precision(writePrecision_m);
82 std::visit(
83 [&os](const auto& val) {
84 os << val;
85 },
86 value_m);
87 os << std::setw(10) << "\t";
88 set_m = false;
89}
90
91std::ostream& operator<<(std::ostream& os, const SDDSColumn& col) {
92 col.writeValue(os);
93
94 return os;
95}
std::ostream & operator<<(std::ostream &os, const SDDSColumn &col)
variant_t value_m
Definition SDDSColumn.h:48
void writeHeader(std::ostream &os, unsigned int colNr, const std::string &indent) const
desc_t description_m
Definition SDDSColumn.h:47
std::string name_m
Definition SDDSColumn.h:46
unsigned short writePrecision_m
Definition SDDSColumn.h:51
void writeValue(std::ostream &os) const
SDDSColumn(const std::string &name, const std::string &type, const std::string &unit, const std::string &desc, std::ios_base::fmtflags flags, unsigned short precision)
std::ios_base::fmtflags writeFlags_m
Definition SDDSColumn.h:50
STL namespace.