C/C++ API Reference
Loading...
Searching...
No Matches
enum.h
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
20namespace pw::tokenizer {
21
23
28template <typename T>
29constexpr auto EnumToToken(T value) {
30 static_assert(std::is_enum_v<T>, "Must be an enum");
31 static_assert(sizeof(T) <= sizeof(Token), "Must fit in a token");
32 return static_cast<Token>(value);
33}
34
40template <typename T>
41constexpr const char* EnumToString(T value) {
42 return PwTokenizerEnumToString(value);
43}
44
46
47} // namespace pw::tokenizer
48
50
56#define PW_TOKENIZE_ENUM(fully_qualified_name, ...) \
57 PW_APPLY(_PW_TOKENIZE_ENUMERATOR, \
58 _PW_SEMICOLON, \
59 fully_qualified_name, \
60 __VA_ARGS__); \
61 [[maybe_unused]] constexpr const char* PwTokenizerEnumToString( \
62 fully_qualified_name _pw_enum_value) { \
63 switch (_pw_enum_value) { \
64 PW_APPLY(_PW_TOKENIZE_TO_STRING_CASE, \
65 _PW_SEMICOLON, \
66 fully_qualified_name, \
67 __VA_ARGS__); \
68 } \
69 return "Unknown " #fully_qualified_name " value"; \
70 } \
71 static_assert(true)
72
80#define PW_TOKENIZE_ENUM_CUSTOM(fully_qualified_name, ...) \
81 PW_APPLY(_PW_TOKENIZE_ENUMERATOR_CUSTOM, \
82 _PW_SEMICOLON, \
83 fully_qualified_name, \
84 __VA_ARGS__); \
85 [[maybe_unused]] constexpr const char* PwTokenizerEnumToString( \
86 fully_qualified_name _pw_enum_value) { \
87 switch (_pw_enum_value) { \
88 PW_APPLY(_PW_TOKENIZE_TO_STRING_CASE_CUSTOM, \
89 _PW_SEMICOLON, \
90 fully_qualified_name, \
91 __VA_ARGS__); \
92 } \
93 return "Unknown " #fully_qualified_name " value"; \
94 } \
95 static_assert(true)
96
constexpr const char * EnumToString(T value)
Returns a string representation of a given enumerator value name. Used in the case of a non-tokenizin...
Definition: enum.h:41
constexpr auto EnumToToken(T value)
Tokenizes a given enumerator value. Used in the vase of a tokenizing log backend.
Definition: enum.h:29