1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://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,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef META_INTERFACE_ENUM_MACROS_H
17 #define META_INTERFACE_ENUM_MACROS_H
18
19 #include <meta/base/meta_types.h>
20 #include <meta/interface/intf_enum.h>
21
META_BEGIN_NAMESPACE()22 META_BEGIN_NAMESPACE()
23 namespace Internal {
24 template<typename Type>
25 struct EnumValue {
26 Type value {};
27 META_NS::EnumValue info;
28 };
29 } // namespace Internal
30
31 template<typename, typename>
32 struct MapAnyType;
33 template<typename EnumType>
34 class Enum;
35 template<typename EnumType>
36 class ArrayEnum;
37
38 META_END_NAMESPACE()
39
40 #define META_BEGIN_ENUM_IMPL(Enum, Name, Desc, DefaultValue, EType) \
41 struct Enum##Metadata { \
42 using Type = Enum; \
43 static constexpr const Type DEFAULT_VALUE = DefaultValue; \
44 static constexpr const META_NS::EnumType ENUM_TYPE = EType; \
45 static constexpr const char* const NAME = Name; \
46 static constexpr const char* const DESC = Desc; \
47 inline static constexpr const META_NS::Internal::EnumValue<Type> VALUES[] = {
48 #define META_BEGIN_ENUM(Enum, Desc, DefaultValue) \
49 META_BEGIN_ENUM_IMPL(Enum, #Enum, Desc, DefaultValue, META_NS::EnumType::SINGLE_VALUE)
50
51 #define META_BEGIN_BITFIELD_ENUM(Enum, Desc, DefaultValue) \
52 META_BEGIN_ENUM_IMPL(Enum, #Enum, Desc, DefaultValue, META_NS::EnumType::BIT_FIELD)
53
54 #define META_END_ENUM() \
55 } \
56 ; \
57 static constexpr const size_t VALUES_SIZE = sizeof(VALUES) / sizeof(*VALUES); \
58 } \
59 ;
60
61 #define META_ENUM_VALUE(Value, Name, Desc) { Value, { Name, Desc } },
62
63 #define META_ENUM_TYPE(MyEnum) \
64 META_TYPE(MyEnum) \
65 template<> \
66 struct ::META_NS::MapAnyType<MyEnum, ::META_NS::EnableSpecialisationType> { \
67 using AnyType = ::META_NS::Enum<MyEnum##Metadata>; \
68 using ArrayAnyType = ::META_NS::ArrayEnum<MyEnum##Metadata>; \
69 };
70
71 #endif