1 // Copyright 2024 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 #pragma once
15
16 #include "pw_preprocessor/apply.h"
17 #include "pw_tokenizer/internal/enum.h"
18 #include "pw_tokenizer/tokenize.h"
19
20 namespace pw::tokenizer {
21
22 /// @brief Tokenizes a given enumerator value. Used in the vase of a tokenizing
23 /// log backend.
24 /// @param value enumerator value
25 /// @return The 32-bit token (@cpp_type{pw_tokenizer_Token})
26 template <typename T>
EnumToToken(T value)27 constexpr auto EnumToToken(T value) {
28 static_assert(std::is_enum_v<T>, "Must be an enum");
29 static_assert(sizeof(T) <= sizeof(Token), "Must fit in a token");
30 return static_cast<Token>(value);
31 }
32
33 /// Returns a string representation of a given enumerator value name.
34 /// @brief Returns a string representation of a given enumerator value name.
35 /// Used in the case of a non-tokenizing log backend.
36 /// @param value enumerator value
37 /// @return constexpr char array
38 template <typename T>
EnumToString(T value)39 constexpr const char* EnumToString(T value) {
40 return PwTokenizerEnumToString(value);
41 }
42
43 } // namespace pw::tokenizer
44
45 /// Tokenizes the given values within an enumerator. All values of the
46 /// enumerator must be present to compile and have the enumerator be tokenized
47 /// successfully.
48 /// This macro should be in the same namespace as the enum declaration to use
49 /// the `pw::tokenizer::EnumToString` function and avoid compilation errors.
50 #define PW_TOKENIZE_ENUM(fully_qualified_name, ...) \
51 PW_APPLY(_PW_TOKENIZE_ENUMERATOR, \
52 _PW_SEMICOLON, \
53 fully_qualified_name, \
54 __VA_ARGS__); \
55 [[maybe_unused]] constexpr const char* PwTokenizerEnumToString( \
56 fully_qualified_name _pw_enum_value) { \
57 switch (_pw_enum_value) { \
58 PW_APPLY(_PW_TOKENIZE_TO_STRING_CASE, \
59 _PW_SEMICOLON, \
60 fully_qualified_name, \
61 __VA_ARGS__); \
62 } \
63 return "Unknown " #fully_qualified_name " value"; \
64 } \
65 static_assert(true)
66
67 /// Tokenizes a custom string for each given values within an enumerator. All
68 /// values of the enumerator must be followed by a custom string as a tuple
69 /// (value, "string"). All values of the enumerator (and their associated
70 /// custom string) must be present to compile and have the custom strings be
71 /// tokenized successfully.
72 /// This macro should be in the same namespace as the enum declaration to use
73 /// the `pw::tokenizer::EnumToString` function and avoid compilation errors.
74 #define PW_TOKENIZE_ENUM_CUSTOM(fully_qualified_name, ...) \
75 PW_APPLY(_PW_TOKENIZE_ENUMERATOR_CUSTOM, \
76 _PW_SEMICOLON, \
77 fully_qualified_name, \
78 __VA_ARGS__); \
79 [[maybe_unused]] constexpr const char* PwTokenizerEnumToString( \
80 fully_qualified_name _pw_enum_value) { \
81 switch (_pw_enum_value) { \
82 PW_APPLY(_PW_TOKENIZE_TO_STRING_CASE_CUSTOM, \
83 _PW_SEMICOLON, \
84 fully_qualified_name, \
85 __VA_ARGS__); \
86 } \
87 return "Unknown " #fully_qualified_name " value"; \
88 } \
89 static_assert(true)
90