SDDS ToolKit Programs and Libraries for C and Python
Loading...
Searching...
No Matches
SDDSLayout.cc
Go to the documentation of this file.
1/**
2 * @file SDDSLayout.cc
3 * @brief Core type, error-context, and immutable layout lookup operations.
4 *
5 * @details Defines type identification, protocol type names, contextual
6 * exceptions, and name-to-index lookup for immutable layouts.
7 *
8 * @copyright
9 * - (c) 2026 The University of Chicago
10 *
11 * @license
12 * This file is distributed under the terms of the Software License Agreement
13 * found in the file LICENSE included with this distribution.
14 */
15
16#include "SDDS.hpp"
17
18#include <array>
19
20namespace sdds {
21namespace {
22
23[[noreturn]] void unknownField(const char *kind, std::string_view name) {
24 const std::string field(name);
25 throw TypeError(ErrorKind::Type, std::string("unknown ") + kind + ": " + field,
26 {}, 0, field);
27}
28
29} // namespace
30
31Type typeOf(const Scalar &value) noexcept {
32 return static_cast<Type>(value.index() + 1);
33}
34
35Type typeOf(const Values &values) noexcept {
36 return static_cast<Type>(values.index() + 1);
37}
38
39std::string_view typeName(Type type) noexcept {
40 constexpr std::array<std::string_view, 11> names = {
41 "longdouble", "double", "float", "long64", "ulong64", "long", "ulong",
42 "short", "ushort", "string", "character"};
43 const auto index = static_cast<std::int32_t>(type) - 1;
44 return index >= 0 && index < static_cast<std::int32_t>(names.size()) ? names[index] : "unknown";
45}
46
47Error::Error(ErrorKind kind, std::string message, std::filesystem::path path,
48 std::int64_t page, std::optional<std::string> field,
49 std::optional<std::uint64_t> offset, std::optional<std::int64_t> row)
50 : std::runtime_error(std::move(message)), kind_(kind), path_(std::move(path)), page_(page),
51 field_(std::move(field)), offset_(offset), row_(row) {}
52
53std::size_t Layout::parameterIndex(std::string_view name) const {
54 for (std::size_t index = 0; index < parameters.size(); ++index)
55 if (parameters[index].name == name) return index;
56 unknownField("parameter", name);
57}
58
59std::size_t Layout::arrayIndex(std::string_view name) const {
60 for (std::size_t index = 0; index < arrays.size(); ++index)
61 if (arrays[index].name == name) return index;
62 unknownField("array", name);
63}
64
65std::size_t Layout::columnIndex(std::string_view name) const {
66 for (std::size_t index = 0; index < columns.size(); ++index)
67 if (columns[index].name == name) return index;
68 unknownField("column", name);
69}
70
71std::size_t Layout::associateIndex(std::string_view name) const {
72 for (std::size_t index = 0; index < associates.size(); ++index)
73 if (associates[index].name == name) return index;
74 unknownField("associate", name);
75}
76
77} // namespace sdds