farm-ng-core
enum_without_iostream.h
Go to the documentation of this file.
1 // Copyright 2022, farm-ng inc.
2 //
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 // FARM_ENUM is a fork of MY_ENUM.
16 //
17 // https://github.com/facebookincubator/MY_ENUM/
18 //
19 // Copyright (c) Facebook, Inc. and its affiliates.
20 //
21 // This source code is licensed under the MIT license found in the
22 // LICENSE file in the root directory of this source tree.
23 
24 #pragma once
25 
27 
28 /* FARM_ENUM_WITHOUT_IOSTREAM_DEF(NAME, [INT_TYPE], ARGS ...)
29 
30  Enum type definition. Can be in global, and namespace scope only. See
31  FARM_ENUM_WITHOUT_IOSTREAM below for details. */
32 #define FARM_ENUM_WITHOUT_IOSTREAM_DEF(NAME, ...) \
33  FARM_ENUM_DEF_IMPL(NAME, __VA_ARGS__)
34 
35 /* FARM_ENUM_ALIAS(NAME)
36 
37  Enum type alias. Can be in any scope. See FARM_ENUM_WITHOUT_IOSTREAM below for
38  details. */
39 #define FARM_ENUM_ALIAS(NAME) using NAME = enum_wrapper_::NAME##Impl
40 
41 /* FARM_ENUM_WITHOUT_IOSTREAM(NAME, [INT_TYPE], ARGS ...)
42  Enum type. Can be in global, and namespace scope only.
43 
44  NAME: Name of the enum class.
45  INT_TYPE: integral type, optional argument which defaults to ``int``.
46  ARGS: Tuple of enums. Could be just names, such as (foo, bar), or
47  also name/value pairs, such as ((one,1), (two,2)).
48 
49  For example ``FARM_ENUM_WITHOUT_IOSTREAM(FooBar, int, (foo, bar));`` defines
50  an enum class
51 
52  enum class FooBar : int {
53  foo,
54  bar
55  };
56 
57  with the following free functions:
58 
59  // Returns corresponding string of given value
60  //
61  // Preconditions: value must be a valid enum value, i.e. FooBar::foo, or
62  // FooBar::bar.
63  //
64  std::string toString(FooBar value);
65 
66  // Returns corresponding string view of given value
67  //
68  // Preconditions: value must be a valid enum value, i.e. FooBar::foo, or
69  // FooBar::bar.
70  //
71  string_view toStringView(FooBar value);
72 
73  // Returns pretty string representation of given value
74  //
75  // Preconditions: value must be a valid enum value, i.e. FooBar::foo, or
76  // FooBar::bar.
77  //
78  std::string toPretty(FooBar value);
79 
80  // Sets enum given corresponding string, if string is valid (i.e. "foo" or
81  // "bar"). Returns false otherwise.
82  //
83  bool trySetFromString(FooBar& value, std::string str);
84 
85  // Return count of enum type. First argument is needed for ADL only.
86  //
87  constexpr size_t getCount(FooBar) {
88  return 2;
89  }
90 
91  // Return string views of enum type. First argument is needed for ADL only.
92  std::array<string_view, 2> getStrings(FooBar) {
93  return {"foo", "bar"};
94  }
95 
96  // Return string of enum names. First argument is needed for ADL only.
97  std::array<string_view, 2> getStrings(FooBar) {
98  return "foo, bar";
99  }
100 
101  // Return values of enum type. First argument is needed for ADL only.
102  constexpr std::array<int, 2> getValues(FooBar) {
103  return {0, 1};
104  }
105 
106  // Returns the position of enum value in the enum class. This is the inverse
107  // of `getValues(FooBar)[i]`.
108  constexpr size_t getPosition(FooBar value) {
109  switch(value) {
110  case FooBar::foo: { return 0; }
111  case FooBar::bar: { return 1; }
112  }
113  }
114 
115  // Return string representation of type name. First argument is needed for
116  // ADL only.
117  string_view getTypeName(FooBar) {
118  return "FooBar";
119  }
120 */
121 #define FARM_ENUM_WITHOUT_IOSTREAM(NAME, ...) \
122  FARM_ENUM_WITHOUT_IOSTREAM_DEF(NAME, __VA_ARGS__) \
123  FARM_ENUM_ALIAS(NAME)
enum_details.h