aboutsummaryrefslogtreecommitdiff
path: root/src/bitflags.c
diff options
context:
space:
mode:
authorThijs Schreijer <thijs@thijsschreijer.nl>2024-05-04 09:06:03 +0200
committerThijs Schreijer <thijs@thijsschreijer.nl>2024-05-04 09:06:03 +0200
commitb41df538c72c7e9a26f9ff08f2668769c8d1a1d1 (patch)
tree59820cae6b9edb014520487f5d2b0c9f91bc6b17 /src/bitflags.c
parentfdf62fe6c4125d29b3097e49566b2bbe1f650382 (diff)
downloadluasystem-b41df538c72c7e9a26f9ff08f2668769c8d1a1d1.tar.gz
luasystem-b41df538c72c7e9a26f9ff08f2668769c8d1a1d1.tar.bz2
luasystem-b41df538c72c7e9a26f9ff08f2668769c8d1a1d1.zip
switch "has" to "has_all_of" and "has_any_of"
Diffstat (limited to 'src/bitflags.c')
-rw-r--r--src/bitflags.c58
1 files changed, 41 insertions, 17 deletions
diff --git a/src/bitflags.c b/src/bitflags.c
index d90ad1d..39f8af0 100644
--- a/src/bitflags.c
+++ b/src/bitflags.c
@@ -82,11 +82,12 @@ local flags6 = sys.bitflag(7) -- b0111
82print(flags6 == flags4) -- true, same flags 82print(flags6 == flags4) -- true, same flags
83 83
84-- comparison of subsets 84-- comparison of subsets
85local flags7 = sys.bitflag(0) -- b0000 85local flags7 = sys.bitflag(0) -- b0000
86local flags8 = sys.bitflag(3) -- b0011 86local flags8 = sys.bitflag(3) -- b0011
87local flags9 = sys.bitflag(7) -- b0111 87local flags9 = sys.bitflag(7) -- b0111
88print(flags9:has(flags8)) -- true, flags8 bits are all set in flags9 88print(flags9:has_all_of(flags8)) -- true, flags8 bits are all set in flags9
89print(flags8:has(flags7)) -- false, flags7 (== 0) is not set in flags8 89print(flags8:has_any_of(flags9)) -- true, some of flags9 bits are set in flags8
90print(flags8:has_all_of(flags7)) -- false, flags7 (== 0) is not set in flags8
90*/ 91*/
91static int lsbf_new(lua_State *L) { 92static int lsbf_new(lua_State *L) {
92 LSBF_BITFLAG flags = 0; 93 LSBF_BITFLAG flags = 0;
@@ -134,26 +135,48 @@ static int lsbf_eq(lua_State *L) {
134} 135}
135 136
136/*** 137/***
137Checks if the given flags are set. 138Checks if all the flags in the given subset are set.
138This is different from the `>=` and `<=` operators because if the flag to check 139If the flags to check has a value `0`, it will always return `false`. So if there are flags that are
139has a value `0`, it will always return `false`. So if there are flags that are 140unsupported on a platform, they can be set to 0 and the `has_all_of` function will
140unsupported on a platform, they can be set to 0 and the `has` function will
141return `false` if the flags are checked. 141return `false` if the flags are checked.
142@function bitflag:has 142@function bitflag:has_all_of
143@tparam bitflag subset the flags to check for. 143@tparam bitflag subset the flags to check for.
144@treturn boolean true if all the flags are set, false otherwise. 144@treturn boolean true if all the flags are set, false otherwise.
145@usage 145@usage
146local sys = require 'system' 146local sys = require 'system'
147local flags = sys.bitflag(12) -- b1100 147local flags = sys.bitflag(12) -- b1100
148local myflags = sys.bitflag(15) -- b1111 148local myflags = sys.bitflag(15) -- b1111
149print(flags:has(myflags)) -- false, not all bits in myflags are set in flags 149print(flags:has_all_of(myflags)) -- false, not all bits in myflags are set in flags
150print(myflags:has(flags)) -- true, all bits in flags are set in myflags 150print(myflags:has_all_of(flags)) -- true, all bits in flags are set in myflags
151*/ 151*/
152static int lsbf_has(lua_State *L) { 152static int lsbf_has_all_of(lua_State *L) {
153 LSBF_BITFLAG a = lsbf_checkbitflags(L, 1); 153 LSBF_BITFLAG a = lsbf_checkbitflags(L, 1);
154 LSBF_BITFLAG b = lsbf_checkbitflags(L, 2); 154 LSBF_BITFLAG b = lsbf_checkbitflags(L, 2);
155 // Check if all bits in b are also set in a, and b is not 0 155 // Check if all bits in b are also set in a, and b is not 0
156 lua_pushboolean(L, (a | b) == a && b != 0); 156 lua_pushboolean(L, (a & b) == b && b != 0);
157 return 1;
158}
159
160/***
161Checks if any of the flags in the given subset are set.
162If the flags to check has a value `0`, it will always return `false`. So if there are flags that are
163unsupported on a platform, they can be set to 0 and the `has_any_of` function will
164return `false` if the flags are checked.
165@function bitflag:has_any_of
166@tparam bitflag subset the flags to check for.
167@treturn boolean true if any of the flags are set, false otherwise.
168@usage
169local sys = require 'system'
170local flags = sys.bitflag(12) -- b1100
171local myflags = sys.bitflag(7) -- b0111
172print(flags:has_any_of(myflags)) -- true, some bits in myflags are set in flags
173print(myflags:has_any_of(flags)) -- true, some bits in flags are set in myflags
174*/
175static int lsbf_has_any_of(lua_State *L) {
176 LSBF_BITFLAG a = lsbf_checkbitflags(L, 1);
177 LSBF_BITFLAG b = lsbf_checkbitflags(L, 2);
178 // Check if any bits in b are set in a
179 lua_pushboolean(L, (a & b) != 0);
157 return 1; 180 return 1;
158} 181}
159 182
@@ -201,7 +224,8 @@ static const struct luaL_Reg lsbf_funcs[] = {
201 224
202static const struct luaL_Reg lsbf_methods[] = { 225static const struct luaL_Reg lsbf_methods[] = {
203 {"value", lsbf_value}, 226 {"value", lsbf_value},
204 {"has", lsbf_has}, 227 {"has_all_of", lsbf_has_all_of},
228 {"has_any_of", lsbf_has_any_of},
205 {"__tostring", lsbf_tostring}, 229 {"__tostring", lsbf_tostring},
206 {"__add", lsbf_add}, 230 {"__add", lsbf_add},
207 {"__sub", lsbf_sub}, 231 {"__sub", lsbf_sub},