OPAL (Object Oriented Parallel Accelerator Library) 2024.2
OPAL
parameter.hpp
Go to the documentation of this file.
1//
2// Struct parameter
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 PARAMETER_HPP_
18#define PARAMETER_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 parameter {
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
60 static bool apply() {
61 constexpr std::array<std::pair<attributes, std::string_view>, 3> unsupportedAttributeNames = {{
62 { attributes::SYMBOL, "symbol" },
63 { attributes::FORMAT_STRING, "format_string" },
64 { attributes::FIXED_VALUE, "fixed_value" }
65 }};
66
67 for (const auto& item : unsupportedAttributeNames) {
68 if (item.first == A) {
69 std::cerr << item.second << " not supported yet" << std::endl;
70 return false;
71 }
72 }
73
74 return true;
75 }
76 };
77
78 bool parse(std::string_view input, std::size_t& pos) {
79 if (!type_m) {
80 return false;
81 }
82
83 parser::ValueParser parser(input, pos);
84 switch(*this->type_m) {
86 float f = 0.0f;
87 if (parser.parseFloat(f)) {
88 this->value_m = f;
89 pos = parser.getPosition();
90 return true;
91 }
92 break;
93 }
95 double d = 0.0;
96 if (parser.parseDouble(d)) {
97 this->value_m = d;
98 pos = parser.getPosition();
99 return true;
100 }
101 break;
102 }
104 short s = 0;
105 if (parser.parseShort(s)) {
106 this->value_m = s;
107 pos = parser.getPosition();
108 return true;
109 }
110 break;
111 }
112 case ast::dataType::LONG: {
113 long l = 0;
114 if (parser.parseLong(l)) {
115 this->value_m = l;
116 pos = parser.getPosition();
117 return true;
118 }
119 break;
120 }
122 char c = 0;
123 if (parser.parseChar(c)) {
124 this->value_m = c;
125 pos = parser.getPosition();
126 return true;
127 }
128 break;
129 }
131 std::string s;
132 if (parser.parseStringToEol(s)) {
133 this->value_m = s;
134 pos = parser.getPosition();
135 return true;
136 }
137 break;
138 }
139 }
140 return false;
141 }
142 };
143
144 struct parameterList : std::vector<parameter> {};
145
146 template <typename Iterator>
148 template <typename, typename>
149 struct result {
150 typedef void type;
151 };
152
153 void operator()(parameter& param, Iterator) const {
154 param.order_m = parameter::count_m ++;
155 }
156 };
157
158 inline std::ostream& operator<<(std::ostream& out, const parameter& param) {
159 if (param.name_m) out << "name = " << *param.name_m << ", ";
160 if (param.units_m) out << "units = " << *param.units_m << ", ";
161 if (param.description_m) out << "description = " << *param.description_m << ", ";
162 out << "order = " << param.order_m;
163
164 return out;
165 }
166}
167
168#endif /* PARAMETER_HPP_ */
std::ostream & operator<<(std::ostream &out, const array &)
Definition array.hpp:72
std::variant< float, double, short, long, char, std::string > variant_t
Definition ast.hpp:49
std::optional< std::string > name_m
Definition parameter.hpp:46
std::optional< std::string > description_m
Definition parameter.hpp:48
ast::variant_t value_m
Definition parameter.hpp:50
std::optional< std::string > units_m
Definition parameter.hpp:47
unsigned int order_m
Definition parameter.hpp:45
bool parse(std::string_view input, std::size_t &pos)
Definition parameter.hpp:78
std::optional< ast::dataType > type_m
Definition parameter.hpp:49
static unsigned int count_m
Definition parameter.hpp:51
bool checkMandatories() const
Definition parameter.hpp:53
void operator()(parameter &param, Iterator) const
bool parseLong(long &value)
bool parseChar(char &value)
bool parseFloat(float &value)
std::size_t getPosition() const
bool parseStringToEol(std::string &value)
bool parseDouble(double &value)
bool parseShort(short &value)