OPAL (Object Oriented Parallel Accelerator Library) 2024.2
OPAL
column.hpp
Go to the documentation of this file.
1//
2// Struct column
3//
4// Copyright (c) 2015, Christof Metzger-Kraus, Helmholtz-Zentrum Berlin
5// All rights reserved
6//
7// This file is part of OPAL.
8//
9// OPAL is free software: you can redistribute it and/or modify
10// it under the terms of the GNU General Public License as published by
11// the Free Software Foundation, either version 3 of the License, or
12// (at your option) any later version.
13//
14// You should have received a copy of the GNU General Public License
15// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
16//
17#ifndef COLUMN_HPP_
18#define COLUMN_HPP_
19
22
23#include <array>
24#include <cstddef>
25#include <iostream>
26#include <optional>
27#include <ostream>
28#include <string>
29#include <string_view>
30#include <utility>
31#include <vector>
32
33namespace SDDS {
34 struct column {
35 enum class attributes {
36 NAME,
37 SYMBOL,
38 UNITS,
41 TYPE,
43 };
44
45 unsigned int order_m { 0 };
46 std::optional<std::string> name_m;
47 std::optional<std::string> units_m;
48 std::optional<std::string> description_m;
49 std::optional<ast::dataType> type_m;
51 static unsigned int count_m;
52
53 bool checkMandatories() const {
54 return name_m && type_m;
55 }
56
57 template <attributes A>
59 static bool apply() {
60 constexpr std::array<std::pair<attributes, std::string_view>, 3> unsupportedAttributeNames = {{
61 { attributes::SYMBOL, "symbol" },
62 { attributes::FORMAT_STRING, "format_string" },
63 { attributes::FIELD_LENGTH, "field_length" }
64 }};
65
66 for (const auto& item : unsupportedAttributeNames) {
67 if (item.first == A) {
68 std::cerr << item.second << " not supported yet" << std::endl;
69 return false;
70 }
71 }
72
73 return true;
74 }
75 };
76
77 bool parse(std::string_view input, size_t& pos) {
78 if (!type_m) {
79 return false;
80 }
81 // GCC false positive: variant construction inside vector growth is
82 // misdiagnosed as reading an uninitialized local.
83#if defined(__GNUC__) && !defined(__clang__)
84#pragma GCC diagnostic push
85#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
86#endif
87 parser::ValueParser parser(input, pos);
88 switch(*this->type_m) {
90 float f = 0.0f;
91 if (parser.parseFloat(f)) {
92 this->values_m.push_back(f);
93 pos = parser.getPosition();
94 return true;
95 }
96 break;
97 }
99 double d = 0.0;
100 if (parser.parseDouble(d)) {
101 this->values_m.push_back(d);
102 pos = parser.getPosition();
103 return true;
104 }
105 break;
106 }
108 short s = 0;
109 if (parser.parseShort(s)) {
110 this->values_m.push_back(s);
111 pos = parser.getPosition();
112 return true;
113 }
114 break;
115 }
116 case ast::dataType::LONG: {
117 long l = 0;
118 if (parser.parseLong(l)) {
119 this->values_m.push_back(l);
120 pos = parser.getPosition();
121 return true;
122 }
123 break;
124 }
126 char c = '\0';
127 if (parser.parseChar(c)) {
128 this->values_m.push_back(c);
129 pos = parser.getPosition();
130 return true;
131 }
132 break;
133 }
135 std::string s("");
136 if (parser.parseString(s)) {
137 this->values_m.push_back(s);
138 pos = parser.getPosition();
139 return true;
140 }
141 break;
142 }
143 }
144#if defined(__GNUC__) && !defined(__clang__)
145#pragma GCC diagnostic pop
146#endif
147 return false;
148 }
149 };
150
151 struct columnList : std::vector<column> {};
152
153 template <typename Iterator>
154 struct columnOrder {
155 template <typename, typename>
156 struct result {
157 typedef void type;
158 };
159
160 void operator()(column& col, Iterator) const {
161 col.order_m = column::count_m ++;
162 }
163 };
164
165 inline std::ostream& operator<<(std::ostream& out, const column& col) {
166 if (col.name_m) out << "name = " << *col.name_m << ", ";
167 if (col.units_m) out << "units = " << *col.units_m << ", ";
168 if (col.description_m) out << "description = " << *col.description_m << ", ";
169 out << "order = " << col.order_m;
170
171 return out;
172 }
173}
174
175#endif /* COLUMN_HPP_ */
std::ostream & operator<<(std::ostream &out, const array &)
Definition array.hpp:72
std::vector< variant_t > columnData_t
Definition ast.hpp:51
std::optional< std::string > name_m
Definition column.hpp:46
std::optional< ast::dataType > type_m
Definition column.hpp:49
bool parse(std::string_view input, size_t &pos)
Definition column.hpp:77
unsigned int order_m
Definition column.hpp:45
ast::columnData_t values_m
Definition column.hpp:50
bool checkMandatories() const
Definition column.hpp:53
std::optional< std::string > units_m
Definition column.hpp:47
std::optional< std::string > description_m
Definition column.hpp:48
static unsigned int count_m
Definition column.hpp:51
void operator()(column &col, Iterator) const
Definition column.hpp:160
bool parseLong(long &value)
bool parseString(std::string &value)
bool parseChar(char &value)
bool parseFloat(float &value)
std::size_t getPosition() const
bool parseDouble(double &value)
bool parseShort(short &value)