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// std
15#include <string>
16
17namespace sen::impl
18{
19
21inline std::string capitalize(const std::string& str) noexcept
22{
23 SEN_EXPECT(!str.empty());
24
25 std::string result = str;
26 result.at(0) = static_cast<char>(std::toupper(str.at(0U)));
27 return result;
28}
29
31inline bool isLower(char ch) noexcept { return std::islower(static_cast<unsigned char>(ch)) != 0; }
32
34inline bool isDigit(char ch) noexcept { return std::isdigit(static_cast<unsigned char>(ch)) != 0; }
35
37inline void checkIdentifier(std::string_view name, bool isQualifiedName = false)
38{
39
40 // names shall not be empty
41 if (name.empty())
42 {
43 throwRuntimeError("identifier is empty");
44 }
45
46 // identifiers must not start with digits
47 if (impl::isDigit(name.at(0U)))
48 {
49 throwRuntimeError("identifiers shall not start with digits");
50 }
51
52 // identifiers must not contain spaces
53 if (name.find_first_of(' ') != std::string::npos)
54 {
55 throwRuntimeError("identifiers shall not contain spaces");
56 }
57
58 // identifiers must not contain invalid characters
59 if (auto pos = name.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_");
60 pos != std::string::npos)
61 {
62 // do not throw error if dot character is found in qualified name identifier
63 if (!isQualifiedName || name[pos] != '.')
64 {
65 std::string err;
66 err.append("invalid character '");
67 err.append(1, name[pos]);
68 err.append("'");
69 err.append(" in identifier: ");
70 err.append(name);
72 }
73 }
74}
75
77inline void checkMemberName(const std::string& name)
78{
79 checkIdentifier(name);
80
81 // member names must start with lower case
82 if (!impl::isLower(name.at(0U)))
83 {
84 throwRuntimeError("member name does not start with lower case");
85 }
86}
87
89inline void checkUserTypeName(const std::string& name)
90{
91 checkIdentifier(name);
92
93 // type names shall start with capital leter
94 if (impl::isLower(name.at(0U)))
95 {
96 throwRuntimeError("user-defined type name does not start with upper case");
97 }
98}
99
100} // namespace sen::impl
101
102#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:36
void throwRuntimeError(const std::string &err)
Throws std::exception that attempts to collect the stack trace. We also wrap it to avoid including st...