Pigweed
 
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
26template <typename T>
27constexpr 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
38template <typename T>
39constexpr const char* EnumToString(T value) {
40 return PwTokenizerEnumToString(value);
41}
42
43} // namespace pw::tokenizer
44
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
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)