Sen API
Sen Libraries
Loading...
Searching...
No Matches
core/src/meta/utils.h
Go to the documentation of this file.
1// === utils.h =========================================================================================================
2// Sen Infrastructure
3// Released under the Apache License v2.0 (SPDX-License-Identifier Apache-2.0).
4// See the LICENSE.txt file for more information.
5// © Airbus SAS, Airbus Helicopters, and Airbus Defence and Space SAU/GmbH/SAS.
6// =====================================================================================================================
7
8#ifndef SEN_LIBS_CORE_SRC_META_UTILS_H
9#define SEN_LIBS_CORE_SRC_META_UTILS_H
10
13
14// spdlog
15#include <spdlog/logger.h>
16#include <spdlog/sinks/stdout_color_sinks.h>
17#include <spdlog/spdlog.h>
18
19// std
20#include <memory>
21#include <string>
22
23namespace sen::impl
24{
25
27inline std::shared_ptr<spdlog::logger> getLogger()
28{
29 static auto logger = []
30 {
31 constexpr auto loggerName = "sen.meta";
32 auto logger = spdlog::get(loggerName);
33 return logger ? logger : spdlog::stdout_color_mt(loggerName);
34 }();
35 return logger;
36}
37
39inline std::string capitalize(const std::string& str) noexcept
40{
41 SEN_EXPECT(!str.empty());
42
43 std::string result = str;
44 result.at(0) = static_cast<char>(std::toupper(str.at(0U)));
45 return result;
46}
47
49inline bool isLower(char ch) noexcept { return std::islower(static_cast<unsigned char>(ch)) != 0; }
50
52inline bool isDigit(char ch) noexcept { return std::isdigit(static_cast<unsigned char>(ch)) != 0; }
53
55inline void checkIdentifier(std::string_view name, bool isQualifiedName = false)
56{
57
58 // names shall not be empty
59 if (name.empty())
60 {
61 throwRuntimeError("identifier is empty");
62 }
63
64 // identifiers must not start with digits
65 if (impl::isDigit(name.at(0U)))
66 {
67 throwRuntimeError("identifiers shall not start with digits");
68 }
69
70 // identifiers must not contain spaces
71 if (name.find_first_of(' ') != std::string::npos)
72 {
73 throwRuntimeError("identifiers shall not contain spaces");
74 }
75
76 // identifiers must not contain invalid characters
77 if (auto pos = name.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_");
78 pos != std::string::npos)
79 {
80 // do not throw error if dot character is found in qualified name identifier
81 if (!isQualifiedName || name[pos] != '.')
82 {
83 std::string err;
84 err.append("invalid character '");
85 err.append(1, name[pos]);
86 err.append("'");
87 err.append(" in identifier: ");
88 err.append(name);
90 }
91 }
92}
93
95inline void checkMemberName(const std::string& name)
96{
97 checkIdentifier(name);
98
99 // member names must start with lower case
100 if (!impl::isLower(name.at(0U)))
101 {
102 throwRuntimeError("member name does not start with lower case");
103 }
104}
105
107inline void checkUserTypeName(const std::string& name)
108{
109 checkIdentifier(name);
110
111 // type names shall start with capital leter
112 if (impl::isLower(name.at(0U)))
113 {
114 throwRuntimeError("user-defined type name does not start with upper case");
115 }
116}
117
118} // namespace sen::impl
119
120#endif // SEN_LIBS_CORE_SRC_META_UTILS_H
The following macros implement a replacement of assert that is connected to the overall fault handlin...
#define SEN_EXPECT(expr)
Checks a pre-condition of a procedure (function parameter for example). NOLINTNEXTLINE.
Definition assert.h:38
void throwRuntimeError(const std::string &err)
Throws an exception that attempts to collect the stack trace.