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
37template <typename T>
38[[deprecated("Use pw::EnumToString from pw_enum/to_string.h instead")]]
39constexpr const char* EnumToString(T value) {
40 return PwEnumToString(value);
41}
42
44
45} // namespace pw::tokenizer
46
48
54#define PW_TOKENIZE_ENUM(fully_qualified_name, ...) \
55 PW_APPLY(_PW_TOKENIZE_ENUMERATOR, \
56 _PW_SEMICOLON, \
57 fully_qualified_name, \
58 __VA_ARGS__); \
59 [[maybe_unused]] constexpr const char* PwEnumToString( \
60 fully_qualified_name _pw_enum_value) { \
61 switch (_pw_enum_value) { \
62 PW_APPLY(_PW_TOKENIZE_TO_STRING_CASE, \
63 _PW_SEMICOLON, \
64 fully_qualified_name, \
65 __VA_ARGS__); \
66 } \
67 return "Unknown " #fully_qualified_name " value"; \
68 } \
69 static_assert(true)
70
78#define PW_TOKENIZE_ENUM_CUSTOM(fully_qualified_name, ...) \
79 PW_APPLY(_PW_TOKENIZE_ENUMERATOR_CUSTOM, \
80 _PW_SEMICOLON, \
81 fully_qualified_name, \
82 __VA_ARGS__); \
83 [[maybe_unused]] constexpr const char* PwEnumToString( \
84 fully_qualified_name _pw_enum_value) { \
85 switch (_pw_enum_value) { \
86 PW_APPLY(_PW_TOKENIZE_TO_STRING_CASE_CUSTOM, \
87 _PW_SEMICOLON, \
88 fully_qualified_name, \
89 __VA_ARGS__); \
90 } \
91 return "Unknown " #fully_qualified_name " value"; \
92 } \
93 static_assert(true)
94
constexpr const char * EnumToString(T value)
Returns a string representation of a given enumerator value name.
Definition: enum.h:39
constexpr auto EnumToToken(T value)
Tokenizes a given enumerator value. Used in the vase of a tokenizing log backend.
Definition: enum.h:29