2 * A library bitfields utility
4 * Copyright: Copyright (C) 1999-2024 by The D Language Foundation, All Rights Reserved
5 * Authors: Dennis Korpel
6 * License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
7 * Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/common/bitfields.d, common/bitfields.d)
8 * Documentation: https://dlang.org/phobos/dmd_common_bitfields.html
9 * Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/common/bitfields.d
11 module dmd
.common
.bitfields
;
14 * Generate code for bit fields inside a struct/class body
16 * S = type of a struct with only boolean fields, which should become bit fields
17 * T = type of bit fields variable, must have enough bits to store all booleans
18 * Returns: D code with a bit fields variable and getter / setter functions
20 extern (D
) string
generateBitFields(S
, T
)()
21 if (__traits(isUnsigned
, T
))
23 string result
= "extern (C++) pure nothrow @nogc @safe final {";
24 enum structName
= __traits(identifier
, S
);
26 string initialValue
= "";
27 foreach (size_t i
, mem
; __traits(allMembers
, S
))
29 static assert(is(typeof(__traits(getMember
, S
, mem
)) == bool));
30 static assert(i
< T
.sizeof
* 8, "too many fields for bit field storage of type `"~T
.stringof
~"`");
31 enum mask
= "(1 << "~i
.stringof
~")";
33 /// set or get the corresponding "~structName
~" member
34 bool "~mem
~"() const scope { return !!(bitFields & "~mask
~"); }
38 v ? (bitFields |= "~mask
~") : (bitFields &= ~"~mask
~");
42 initialValue
= (__traits(getMember
, S
.init
, mem
) ?
"1" : "0") ~ initialValue
;
44 return result
~ "}\n private "~T
.stringof
~" bitFields = 0b" ~ initialValue
~ ";\n";
59 mixin(generateBitFields
!(B
, ubyte));