OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
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 //
18 #include "Structure/SDDSColumn.h"
20 
21 #include <iomanip>
22 #include <list>
23 
24 SDDSColumn::SDDSColumn(const std::string& name,
25  const std::string& type,
26  const std::string& unit,
27  const std::string& desc,
28  std::ios_base::fmtflags flags,
29  unsigned short prec):
30  name_m(name),
31  description_m(std::make_tuple(type, unit, desc)),
32  writeFlags_m(flags),
33  writePrecision_m(prec),
34  set_m(false)
35 {
36  std::list<std::ios_base::fmtflags> numericalBase({std::ios_base::dec,
37  std::ios_base::hex,
38  std::ios_base::oct});
39  std::list<std::ios_base::fmtflags> floatFormat({std::ios_base::fixed,
40  std::ios_base::scientific});
41  std::list<std::ios_base::fmtflags> adjustmentFlags({std::ios_base::internal,
42  std::ios_base::left,
43  std::ios_base::right});
44 
45  // This code ensures that for each group of flags only one flag is given
46  for (std::ios_base::fmtflags flag: numericalBase) {
47  if (writeFlags_m & flag) {
48  writeFlags_m = (flag | (writeFlags_m & ~std::ios_base::basefield));
49  break;
50  }
51  }
52  for (std::ios_base::fmtflags flag: floatFormat) {
53  if (writeFlags_m & flag) {
54  writeFlags_m = (flag | (writeFlags_m & ~std::ios_base::floatfield));
55  break;
56  }
57  }
58  for (std::ios_base::fmtflags flag: adjustmentFlags) {
59  if (writeFlags_m & flag) {
60  writeFlags_m = (flag | (writeFlags_m & ~std::ios_base::adjustfield));
61  break;
62  }
63  }
64 
65 }
66 
67 
68 void SDDSColumn::writeHeader(std::ostream& os,
69  unsigned int colNr,
70  const std::string& indent) const {
71  os << "&column\n"
72  << indent << "name=" << name_m << ",\n"
73  << indent << "type=" << std::get<0>(description_m) << ",\n";
74 
75  if (std::get<1>(description_m) != "")
76  os << indent << "units=" << std::get<1>(description_m) << ",\n";
77 
78  os << indent << "description=\"" << colNr << " " << std::get<2>(description_m) << "\"\n"
79  << "&end\n";
80 }
81 
82 
83 void SDDSColumn::writeValue(std::ostream& os) const {
84  if (!set_m) {
85  throw OpalException("SDDSColumn::writeValue",
86  "value for column '" + name_m + "' isn't set");
87  }
88 
89  os.flags(writeFlags_m);
90  os.precision(writePrecision_m);
91  os << value_m << std::setw(10) << "\t";
92  set_m = false;
93 }
94 
95 std::ostream& operator<<(std::ostream& os,
96  const SDDSColumn& col) {
97  col.writeValue(os);
98 
99  return os;
100 }
std::ostream & operator<<(std::ostream &os, const SDDSColumn &col)
Definition: SDDSColumn.cpp:95
const std::string name
boost::function< boost::tuple< double, bool >arguments_t)> type
Definition: function.hpp:21
variant_t value_m
Definition: SDDSColumn.h:63
void writeHeader(std::ostream &os, unsigned int colNr, const std::string &indent) const
Definition: SDDSColumn.cpp:68
desc_t description_m
Definition: SDDSColumn.h:62
std::string name_m
Definition: SDDSColumn.h:61
bool set_m
Definition: SDDSColumn.h:68
unsigned short writePrecision_m
Definition: SDDSColumn.h:66
void writeValue(std::ostream &os) const
Definition: SDDSColumn.cpp:83
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)
Definition: SDDSColumn.cpp:24
std::ios_base::fmtflags writeFlags_m
Definition: SDDSColumn.h:65
The base class for all OPAL exceptions.
Definition: OpalException.h:28