xref: /aosp_15_r20/external/pigweed/pw_tokenizer/public/pw_tokenizer/internal/enum.h (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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_tokenizer/tokenize.h"
17 
18 namespace pw::tokenizer {
19 
20 template <typename T>
ValidEnumerator(T)21 constexpr bool ValidEnumerator(T) {
22   static_assert(std::is_enum_v<T>, "Must be an enum");
23   static_assert(sizeof(T) <= sizeof(Token), "Must fit in a token");
24   return true;
25 }
26 
27 }  // namespace pw::tokenizer
28 
29 #define _PW_SEMICOLON(...) ;
30 
31 #define _PW_TOKENIZE_TO_STRING_CASE_IMPL(index, name, enumerator, str) \
32   case name::enumerator:                                               \
33     return str;
34 
35 #define _PW_TOKENIZE_ENUMERATOR_IMPL(index, name, enumerator, str)         \
36   static_assert(                                                           \
37       #name[0] == ':' && #name[1] == ':',                                  \
38       "Enum names must be fully qualified and start with ::, but \"" #name \
39       "\" does not");                                                      \
40   static_assert(::pw::tokenizer::ValidEnumerator(name::enumerator));       \
41   PW_TOKENIZER_DEFINE_TOKEN(                                               \
42       static_cast<::pw::tokenizer::Token>(name::enumerator), #name, str)
43 
44 #define _PW_TOKENIZE_TO_STRING_CASE(index, name, enumerator) \
45   _PW_TOKENIZE_TO_STRING_CASE_IMPL(index, name, enumerator, #enumerator)
46 
47 // Declares an individual tokenized enum value.
48 #define _PW_TOKENIZE_ENUMERATOR(index, name, enumerator) \
49   _PW_TOKENIZE_ENUMERATOR_IMPL(index, name, enumerator, #enumerator)
50 
51 // Strip parenthesis around (enumerator, "custom string") tuples
52 #define _PW_CUSTOM_ENUMERATOR(enumerator, str) enumerator, str
53 
54 #define _PW_TOKENIZE_TO_STRING_CASE_CUSTOM(index, name, arg) \
55   _PW_TOKENIZE_TO_STRING_CASE_CUSTOM_EXPAND(                 \
56       index, name, _PW_CUSTOM_ENUMERATOR arg)
57 #define _PW_TOKENIZE_TO_STRING_CASE_CUSTOM_EXPAND(index, name, ...) \
58   _PW_TOKENIZE_TO_STRING_CASE_IMPL(index, name, __VA_ARGS__)
59 
60 // Declares a tokenized custom string for an individual enum value.
61 #define _PW_TOKENIZE_ENUMERATOR_CUSTOM(index, name, arg) \
62   _PW_TOKENIZE_ENUMERATOR_CUSTOM_EXPAND(index, name, _PW_CUSTOM_ENUMERATOR arg)
63 #define _PW_TOKENIZE_ENUMERATOR_CUSTOM_EXPAND(index, name, ...) \
64   _PW_TOKENIZE_ENUMERATOR_IMPL(index, name, __VA_ARGS__)
65