OPAL (Object Oriented Parallel Accelerator Library) 2024.2
OPAL
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 <array>
22#include <iomanip>
23#include <ios>
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 constexpr std::array<std::ios_base::fmtflags, 3> numericalBase{
34 std::ios_base::dec, std::ios_base::hex, std::ios_base::oct};
35
36 constexpr std::array<std::ios_base::fmtflags, 2> floatFormat{
37 std::ios_base::fixed, std::ios_base::scientific};
38
39 constexpr std::array<std::ios_base::fmtflags, 3> adjustmentFlags{
40 std::ios_base::internal, std::ios_base::left, std::ios_base::right};
41
42 // This code ensures that for each group of flags only one flag is given
43 for (std::ios_base::fmtflags flag : numericalBase) {
44 if (writeFlags_m & flag) {
45 writeFlags_m = (flag | (writeFlags_m & ~std::ios_base::basefield));
46 break;
47 }
48 }
49 for (std::ios_base::fmtflags flag : floatFormat) {
50 if (writeFlags_m & flag) {
51 writeFlags_m = (flag | (writeFlags_m & ~std::ios_base::floatfield));
52 break;
53 }
54 }
55 for (std::ios_base::fmtflags flag : adjustmentFlags) {
56 if (writeFlags_m & flag) {
57 writeFlags_m = (flag | (writeFlags_m & ~std::ios_base::adjustfield));
58 break;
59 }
60 }
61}
62
64 std::ostream& os, unsigned int colNr, const std::string& indent) const {
65 os << "&column\n"
66 << indent << "name=" << name_m << ",\n"
67 << indent << "type=" << std::get<0>(description_m) << ",\n";
68
69 if (std::get<1>(description_m) != "")
70 os << indent << "units=" << std::get<1>(description_m) << ",\n";
71
72 os << indent << "description=\"" << colNr << " " << std::get<2>(description_m) << "\"\n"
73 << "&end\n";
74}
75
76void SDDSColumn::writeValue(std::ostream& os) const {
77 if (!set_m) {
78 throw OpalException(
79 "SDDSColumn::writeValue", "value for column '" + name_m + "' isn't set");
80 }
81
82 os.flags(writeFlags_m);
83 os.precision(writePrecision_m);
84
85 std::visit(
86 [&os](auto&& arg) {
87 using T = std::decay_t<decltype(arg)>;
88 if constexpr (std::is_arithmetic_v<T>) {
89 // float, double, int
90 os << static_cast<double>(arg);
91 } else {
92 // string, char
93 os << arg;
94 }
95 os << std::setw(10) << "\t";
96 },
97 value_m);
98
99 set_m = false;
100}
101
102std::ostream& operator<<(std::ostream& os, const SDDSColumn& col) {
103 col.writeValue(os);
104
105 return os;
106}
std::ostream & operator<<(std::ostream &os, const SDDSColumn &col)
arg(a))
const std::string name
variant_t value_m
Definition SDDSColumn.h:61
void writeHeader(std::ostream &os, unsigned int colNr, const std::string &indent) const
desc_t description_m
Definition SDDSColumn.h:60
std::string name_m
Definition SDDSColumn.h:59
unsigned short writePrecision_m
Definition SDDSColumn.h:64
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:63
The base class for all OPAL exceptions.