Enum.h file
CnxEnum provides macros for declaring algebraic datatypes called algebraic Enums, similar to Rust's Enum, and pattern matching on their variants.
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Defines
- #define Enum(Type, ...)
- Declares and defines an
Enum,Type, with the given variants and additional members. - #define EnumWithTag(Type, TagType, ...)
- Declares and defines an
Enum,Type, using an existing C-styleenum,TagType, to tag/identify its variants. - #define match(x)
- Pattern matches on the given
Enum,xPattern matching works similarly toswitchstatements, but with slightly different semantics. First off, pattern cases are declared withvariant(...), which can, in addition to match the variant, optionally decompose the variant into one or more bound variables. Bound variables are bound by value, as decomposing a variant during pattern matching is intended to be consuming, so mutating theEnumvariant's members in place is not possible without manually doing so. Second, fallthrough is not possible, sobreaking to prevent it is not necessary. - #define variant(...)
- Declares a pattern-matching case in a
matchstatement Pattern-matching cases declare a variant to match against and, in addition, can optionally decompose the variant into one or more bound variables. Bound variables are bound by value, as decomposing a variant during pattern matching is intended to be consuming, so mutating theEnumvariant's members in place is not possible without manually doing so. Unlikeswitchstatements, fallthrough is not possible inmatchstatements, sobreaking betweenvariant(...)s is not necessary. - #define wildcard()
- Declares a catch-all pattern-matching case in a
matchstatement This will match all variants that have not been explicitly matched against prior. Unlikeswitchstatements, fallthrough is not possible inmatchstatements, sobreaking betweenvariant(...)s is not necessary. - #define match_let(self, ...)
- Conditionally pattern matches on the given
Enum,selfmatch_letworks similarly to anifstatement, but with slightly different semantics. First off, in addition to conditionally branching ifselfis the given variant, it can optionally decompose the variant into one or more bound variables. Bound variables are bound by value, as decomposing a variant during pattern matching is intended to be consuming, so mutating theEnumvariant's members in place is not possible without manually doing so. - #define is_variant(self, Variant)
- Determines if
selfis theEnumvariantVariant - #define extract_variant(self, Variant)
- Extracts the internal value of
selfas the variantVariant - #define variant_identifier(Variant)
- Generates the member identifier for an
Enumvariant,Variant, used in the struct definition of anEnum - #define ___ENUM_GET_TYPE_NAMES(...)
- extracts the
Enumvariant names from a list ofEnumparameters - #define ___ENUM_CHECK_IS_TUPLE_AND_EXTRACT_TYPE(x)
- extract the
Enumvariant name from anEnumparameter - #define ___ENUM_DEFINE_STRUCTS(...)
- Defines the structs used to represent
Enumvariants in anEnumdeclaration. - #define ___ENUM_DEFINE_STRUCT(x)
- Defines a struct used to represent an
Enumvariant in anEnumdeclaration. - #define ___ENUM_DEFINE_EMPTY_STRUCT(Name)
- Defines a struct used to represent an
Enumvariant in anEnumdeclaration. - #define ___ENUM_DEFINE_STRUCT_IMPL(Name, x)
- Defines a struct used to represent an
Enumvariant in anEnumdeclaration. - #define ___ENUM_EXPAND_STRUCT_IMPL(...)
- Defines a struct used to represent an
Enumvariant in anEnumdeclaration. - #define ___ENUM_DEFINE_MEMBERS(...)
- Defines any additional members of an
Enumin anEnumdeclaration. - #define ___ENUM_DEFINE_MEMBER(x)
- Defines an additional members of an
Enumin anEnumdeclaration. - #define ___ENUM_BIND_VARIANT(variant_name, ...)
- Binds an
Enumto a decomposition of the variantvariant_name - #define ___ENUM_LET_BIND_VARIANT(self, variant_name, ...)
- Binds an
Enumto a decomposition of the variantvariant_name