mirror of
https://github.com/dlang/phobos.git
synced 2025-04-30 07:00:37 +03:00
Make generated accessor for bitfields @safe pure nothrow.
This commit is contained in:
parent
a47332ca0d
commit
aaaca447cc
1 changed files with 29 additions and 4 deletions
|
@ -80,17 +80,17 @@ private template createAccessors(
|
||||||
static assert(len == 1);
|
static assert(len == 1);
|
||||||
enum result =
|
enum result =
|
||||||
// getter
|
// getter
|
||||||
"@property bool " ~ name ~ "() const { return "
|
"@property @safe bool " ~ name ~ "() pure nothrow const { return "
|
||||||
~"("~store~" & "~myToString(maskAllElse)~") != 0;}\n"
|
~"("~store~" & "~myToString(maskAllElse)~") != 0;}\n"
|
||||||
// setter
|
// setter
|
||||||
~"@property void " ~ name ~ "(bool v){"
|
~"@property @safe void " ~ name ~ "(bool v) pure nothrow {"
|
||||||
~"if (v) "~store~" |= "~myToString(maskAllElse)~";"
|
~"if (v) "~store~" |= "~myToString(maskAllElse)~";"
|
||||||
~"else "~store~" &= ~"~myToString(maskAllElse)~";}\n";
|
~"else "~store~" &= ~"~myToString(maskAllElse)~";}\n";
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// getter
|
// getter
|
||||||
enum result = "@property "~T.stringof~" "~name~"() const { auto result = "
|
enum result = "@property @safe "~T.stringof~" "~name~"() pure nothrow const { auto result = "
|
||||||
"("~store~" & "
|
"("~store~" & "
|
||||||
~ myToString(maskAllElse) ~ ") >>"
|
~ myToString(maskAllElse) ~ ") >>"
|
||||||
~ myToString(offset) ~ ";"
|
~ myToString(offset) ~ ";"
|
||||||
|
@ -100,7 +100,7 @@ private template createAccessors(
|
||||||
: "")
|
: "")
|
||||||
~ " return cast("~T.stringof~") result;}\n"
|
~ " return cast("~T.stringof~") result;}\n"
|
||||||
// setter
|
// setter
|
||||||
~"@property void "~name~"("~T.stringof~" v){ "
|
~"@property @safe void "~name~"("~T.stringof~" v) pure nothrow { "
|
||||||
~"assert(v >= "~name~"_min); "
|
~"assert(v >= "~name~"_min); "
|
||||||
~"assert(v <= "~name~"_max); "
|
~"assert(v <= "~name~"_max); "
|
||||||
~store~" = cast(typeof("~store~"))"
|
~store~" = cast(typeof("~store~"))"
|
||||||
|
@ -203,6 +203,31 @@ template bitfields(T...)
|
||||||
enum { bitfields = createFields!(createStoreName!(T), 0, T).result }
|
enum { bitfields = createFields!(createStoreName!(T), 0, T).result }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
struct Test
|
||||||
|
{
|
||||||
|
mixin(bitfields!(bool, "a", 1,
|
||||||
|
uint, "b", 3,
|
||||||
|
short, "c", 4));
|
||||||
|
}
|
||||||
|
|
||||||
|
@safe void test() pure nothrow
|
||||||
|
{
|
||||||
|
Test t;
|
||||||
|
|
||||||
|
t.a = true;
|
||||||
|
t.b = 5;
|
||||||
|
t.c = 2;
|
||||||
|
|
||||||
|
assert(t.a);
|
||||||
|
assert(t.b == 5);
|
||||||
|
assert(t.c == 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
test();
|
||||||
|
}
|
||||||
|
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
static struct Integrals {
|
static struct Integrals {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue