flags
This commit is contained in:
61
examples/31-flags.sx
Normal file
61
examples/31-flags.sx
Normal file
@@ -0,0 +1,61 @@
|
||||
#import "modules/std.sx";
|
||||
|
||||
// Auto power-of-2 values: read=1, write=2, execute=4
|
||||
Perms :: enum flags {
|
||||
read;
|
||||
write;
|
||||
execute;
|
||||
}
|
||||
|
||||
// Explicit values (e.g. for C interop)
|
||||
WindowFlags :: enum flags {
|
||||
vsync :: 64;
|
||||
resizable :: 4;
|
||||
hidden :: 128;
|
||||
}
|
||||
|
||||
check_perms :: (p: Perms) {
|
||||
print(" checking: {}\n", p);
|
||||
if p & .read { print(" - can read\n"); }
|
||||
if p & .write { print(" - can write\n"); }
|
||||
if p & .execute { print(" - can execute\n"); }
|
||||
}
|
||||
|
||||
main :: () {
|
||||
// Combine flags with |
|
||||
p :Perms = .read | .write;
|
||||
print("perms: {}\n", p);
|
||||
|
||||
// Test individual flags with &
|
||||
check_perms(p);
|
||||
|
||||
// All flags
|
||||
all :Perms = .read | .write | .execute;
|
||||
print("\nall: {}\n", all);
|
||||
check_perms(all);
|
||||
|
||||
// Single flag
|
||||
r :Perms = .read;
|
||||
print("\nread only: {}\n", r);
|
||||
check_perms(r);
|
||||
|
||||
// Pass flags to functions, match on them
|
||||
print("\nmatch on flags:\n");
|
||||
f :Perms = .execute;
|
||||
if f == {
|
||||
case .read: print(" read\n");
|
||||
case .write: print(" write\n");
|
||||
case .execute: print(" execute\n");
|
||||
}
|
||||
|
||||
// Explicit values
|
||||
w :WindowFlags = .vsync | .resizable;
|
||||
print("\nwindow: {}\n", w);
|
||||
print("raw value: {}\n", cast(s64) w);
|
||||
|
||||
// Bitwise ops work on plain integers too
|
||||
x := 0xFF & 0x0F;
|
||||
y := 1 | 2 | 4;
|
||||
print("\n0xFF & 0x0F = {}\n", x);
|
||||
print("1 | 2 | 4 = {}\n", y);
|
||||
}
|
||||
@@ -13,6 +13,8 @@ type_name :: ($T: Type) -> string #builtin;
|
||||
field_count :: ($T: Type) -> s64 #builtin;
|
||||
field_name :: ($T: Type, idx: s64) -> string #builtin;
|
||||
field_value :: (s: $T, idx: s64) -> Any #builtin;
|
||||
is_flags :: ($T: Type) -> bool #builtin;
|
||||
field_value_int :: ($T: Type, idx: s64) -> s64 #builtin;
|
||||
string :: []u8 #builtin;
|
||||
|
||||
int_to_string :: (n: s64) -> string {
|
||||
@@ -201,7 +203,24 @@ pointer_to_string :: (p: $T) -> string {
|
||||
}
|
||||
}
|
||||
|
||||
flags_to_string :: (val: $T) -> string {
|
||||
v := cast(s64) val;
|
||||
result := "";
|
||||
i := 0;
|
||||
while i < field_count(T) {
|
||||
fv := field_value_int(T, i);
|
||||
if v & fv {
|
||||
if result.len > 0 { result = concat(result, " | "); }
|
||||
result = concat(result, concat(".", field_name(T, i)));
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
if result.len == 0 { result = "0"; }
|
||||
result;
|
||||
}
|
||||
|
||||
enum_to_string :: (u: $T) -> string {
|
||||
if is_flags(T) { return flags_to_string(u); }
|
||||
tag := cast(s64) u;
|
||||
result := concat(".", field_name(T, tag));
|
||||
payload := field_value(u, tag);
|
||||
|
||||
Reference in New Issue
Block a user