Proposal new type "flags"
Rueschi
rueschi at GIquadrat.de
Tue Jan 9 19:37:30 PST 2007
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi folks,
many functions may be controlled by some flags that are combined
in an uint value. However, this is not type safe. You can unintentionally
provide/combine flags that are not intended for that function.
The solution would be a "flags" data type, similar to enums:
flags BaseFlags
{
FEATURE1,
FEATURE2,
FEATURE3,
FEATURE4
}
flags PrettyFlags : BaseFlags // use all flags defined in BaseFlags, plus:
{
BEHAVE_CORRECT,
DONT_CRASH
}
void service( PrettyFlags whattodo )
{ /* ... */ }
void main()
{
service( 0x02 ); // error: this is the usual but unsafe way
service( PrettyFlags.FEATURE2
| PrettyFlags.DONT_CRASH ); // ok and type safe
service( FEATURE2|DONTCRASH ); // maybe also ok?
}
- the following operations are valid:
==, != equality tests
<>, !<> tests presence of all flags from the right operand:
FEATURE1|FEATURE2|FEATURE3 <> FEATURE1|FEATURE3
FEATURE1|FEATURE2|FEATURE3 !<> FEATURE1|FEATURE4
|, |= setting flags
&, &= erasing flags
^, ^= toggling flags
~ inverting
! conversion to bool
is including is( i : s ) and is( i == s )
- properties:
.init flags value with no bits set
.sizeof 8, 16, 32 or 64, depending on the number of flags
.count number of flags
.all flags value with all flags set (same as ~.init)
- flags can be inherited by another flags type
(by the way: why can enums not be inherited?)
- flags are implicitly converted to theyr base type
- flags are implicitly converted to bool
- flags are cool ;-)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32)
iD8DBQFFpF96xpVjSwvEWI4RAnr/AKDEv2xqRYRTuj+BSxwuAGfdMS/h+wCg4fT3
cpT9/QWMcpfJTNHWlRXpCDE=
=N38j
-----END PGP SIGNATURE-----
More information about the Digitalmars-d
mailing list