OPAL (Object Oriented Parallel Accelerator Library) 2024.2
OPAL
ast.hpp
Go to the documentation of this file.
1//
2// Namespace ast
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 AST_HPP_
18#define AST_HPP_
19
20#include <array>
21#include <optional>
22#include <string>
23#include <string_view>
24#include <type_traits>
25#include <utility>
26#include <variant>
27#include <vector>
28
29namespace SDDS {
30 namespace ast {
31 enum class dataType {
32 FLOAT,
33 DOUBLE,
34 SHORT,
35 LONG,
37 STRING
38 };
39 enum class dataMode {
40 ASCII,
41 BINARY
42 };
43
44 using variant_t = std::variant<float,
45 double,
46 short,
47 long,
48 char,
49 std::string>;
50
51 using columnData_t = std::vector<variant_t>;
52
53 constexpr std::array<std::pair<std::string_view, dataType>, 6> dataTypeTable = {{
54 { "float", dataType::FLOAT },
55 { "double", dataType::DOUBLE },
56 { "short", dataType::SHORT },
57 { "long", dataType::LONG },
58 { "character", dataType::CHARACTER },
59 { "string", dataType::STRING }
60 }};
61
62 constexpr char toLowerAscii(char c) {
63 return (c >= 'A' && c <= 'Z') ? static_cast<char>(c + ('a' - 'A')) : c;
64 }
65
66 constexpr bool equalsIgnoreCase(std::string_view lhs, std::string_view rhs) {
67 if (lhs.size() != rhs.size()) {
68 return false;
69 }
70 for (std::size_t i = 0; i < lhs.size(); ++i) {
71 if (toLowerAscii(lhs[i]) != toLowerAscii(rhs[i])) {
72 return false;
73 }
74 }
75 return true;
76 }
77
78 constexpr std::optional<dataType> parseDataType(std::string_view typeName) {
79 for (const auto& [name, type] : dataTypeTable) {
80 if (equalsIgnoreCase(typeName, name)) {
81 return type;
82 }
83 }
84 return std::nullopt;
85 }
86
87 constexpr std::string_view getDataTypeString(dataType type) {
88 for (const auto& [name, value] : dataTypeTable) {
89 if (value == type) {
90 return name;
91 }
92 }
93
94 return "unknown";
95 }
96
97 constexpr std::string_view getDataModeString(dataMode mode) {
98 constexpr std::array<std::string_view, 2> dataModeNames = {
99 "ascii",
100 "binary"
101 };
102
103 const auto index = static_cast<std::underlying_type_t<dataMode>>(mode);
104 if (index < 0 || static_cast<std::size_t>(index) >= dataModeNames.size()) {
105 return "unknown";
106 }
107
108 return dataModeNames[static_cast<std::size_t>(index)];
109 }
110
111 }
112
113 namespace parser {}
114}
115
116#endif // AST_HPP_
const std::string name
constexpr char toLowerAscii(char c)
Definition ast.hpp:62
std::variant< float, double, short, long, char, std::string > variant_t
Definition ast.hpp:49
constexpr std::string_view getDataModeString(dataMode mode)
Definition ast.hpp:97
constexpr std::optional< dataType > parseDataType(std::string_view typeName)
Definition ast.hpp:78
constexpr std::array< std::pair< std::string_view, dataType >, 6 > dataTypeTable
Definition ast.hpp:53
constexpr bool equalsIgnoreCase(std::string_view lhs, std::string_view rhs)
Definition ast.hpp:66
std::vector< variant_t > columnData_t
Definition ast.hpp:51
constexpr std::string_view getDataTypeString(dataType type)
Definition ast.hpp:87