getVisibility

Returns a string with the value of the field "visibility" if the field is annotated with an UDA of type A. The default visibility is "public".

template getVisibility (
alias field
A
) {
enum getVisibility;
}

Examples

1 @Read("public") int publicInt;
2 @Read("package") int packageInt;
3 @Read("protected") int protectedInt;
4 @Read("private") int privateInt;
5 @Read int defaultVisibleInt;
6 @Read @Write("protected") int publicReadableProtectedWritableInt;
7 
8 static assert(getVisibility!(publicInt, Read) == "public");
9 static assert(getVisibility!(packageInt, Read) == "package");
10 static assert(getVisibility!(protectedInt, Read) == "protected");
11 static assert(getVisibility!(privateInt, Read) == "private");
12 static assert(getVisibility!(defaultVisibleInt, Read) == "public");
13 static assert(getVisibility!(publicReadableProtectedWritableInt, Read) == "public");
14 static assert(getVisibility!(publicReadableProtectedWritableInt, Write) == "protected");

Creates accessors for flags.

1 import std.typecons : Flag, No, Yes;
2 
3 class Test
4 {
5     @Read
6     @Write
7     public Flag!"someFlag" test_ = Yes.someFlag;
8 
9     mixin(GenerateFieldAccessors);
10 }
11 
12 with (new Test)
13 {
14     assert(test == Yes.someFlag);
15 
16     test = No.someFlag;
17 
18     assert(test == No.someFlag);
19 
20     static assert(is(typeof(test) == Flag!"someFlag"));
21 }

Creates accessors for Nullables.

1 import std.typecons : Nullable;
2 
3 class Test
4 {
5     @Read @Write
6     public Nullable!string test_ = Nullable!string("X");
7 
8     mixin(GenerateFieldAccessors);
9 }
10 
11 with (new Test)
12 {
13     assert(!test.isNull);
14     assert(test.get == "X");
15 
16     static assert(is(typeof(test) == Nullable!string));
17 }

Creates non-const reader.

1 class Test
2 {
3     @Read
4     int i_;
5 
6     mixin(GenerateFieldAccessors);
7 }
8 
9 auto mutableObject = new Test;
10 const constObject = mutableObject;
11 
12 mutableObject.i_ = 42;
13 
14 assert(mutableObject.i == 42);
15 
16 static assert(is(typeof(mutableObject.i) == int));
17 static assert(is(typeof(constObject.i) == const(int)));

Creates ref reader.

1 class Test
2 {
3     @RefRead
4     int i_;
5 
6     mixin(GenerateFieldAccessors);
7 }
8 
9 auto mutableTestObject = new Test;
10 
11 mutableTestObject.i = 42;
12 
13 assert(mutableTestObject.i == 42);
14 static assert(is(typeof(mutableTestObject.i) == int));

Creates writer.

1 class Test
2 {
3     @Read @Write
4     private int i_;
5 
6     mixin(GenerateFieldAccessors);
7 }
8 
9 auto mutableTestObject = new Test;
10 mutableTestObject.i = 42;
11 
12 assert(mutableTestObject.i == 42);
13 static assert(!__traits(compiles, mutableTestObject.i += 1));
14 static assert(is(typeof(mutableTestObject.i) == int));

Checks whether hasUDA can be used for each member.

1 class Test
2 {
3     alias Z = int;
4 
5     @Read @Write
6     private int i_;
7 
8     mixin(GenerateFieldAccessors);
9 }
10 
11 auto mutableTestObject = new Test;
12 mutableTestObject.i = 42;
13 
14 assert(mutableTestObject.i == 42);
15 static assert(!__traits(compiles, mutableTestObject.i += 1));

Returns non const for PODs and structs.

1 import std.algorithm : map, sort;
2 import std.array : array;
3 
4 class C
5 {
6     @Read
7     string s_;
8 
9     mixin(GenerateFieldAccessors);
10 }
11 
12 C[] a = null;
13 
14 static assert(__traits(compiles, a.map!(c => c.s).array.sort()));

Regression.

1 class C
2 {
3     @Read @Write
4     string s_;
5 
6     mixin(GenerateFieldAccessors);
7 }
8 
9 with (new C)
10 {
11     s = "foo";
12     assert(s == "foo");
13     static assert(is(typeof(s) == string));
14 }

Supports user-defined accessors.

1 class C
2 {
3     this()
4     {
5         str_ = "foo";
6     }
7 
8     @RefRead
9     private string str_;
10 
11     public @property const(string) str() const
12     {
13         return this.str_.dup;
14     }
15 
16     mixin(GenerateFieldAccessors);
17 }
18 
19 with (new C)
20 {
21     str = "bar";
22 }

Creates accessor for locally defined types.

1 class X
2 {
3 }
4 
5 class Test
6 {
7     @Read
8     public X x_;
9 
10     mixin(GenerateFieldAccessors);
11 }
12 
13 with (new Test)
14 {
15     x_ = new X;
16 
17     assert(x == x_);
18     static assert(is(typeof(x) == X));
19 }

Creates const reader for simple structs.

1 class Test
2 {
3     struct S
4     {
5         int i;
6     }
7 
8     @Read
9     S s_;
10 
11     mixin(GenerateFieldAccessors);
12 }
13 
14 auto mutableObject = new Test;
15 const constObject = mutableObject;
16 
17 mutableObject.s_.i = 42;
18 
19 assert(constObject.s.i == 42);
20 
21 static assert(is(typeof(mutableObject.s) == Test.S));
22 static assert(is(typeof(constObject.s) == const(Test.S)));

Reader for structs return copies.

1 class Test
2 {
3     struct S
4     {
5         int i;
6     }
7 
8     @Read
9     S s_;
10 
11     mixin(GenerateFieldAccessors);
12 }
13 
14 auto mutableObject = new Test;
15 
16 mutableObject.s.i = 42;
17 
18 assert(mutableObject.s.i == int.init);

Creates reader for const arrays.

1 class X
2 {
3 }
4 
5 class C
6 {
7     @Read
8     private const(X)[] foo_;
9 
10     mixin(GenerateFieldAccessors);
11 }
12 
13 auto x = new X;
14 
15 with (new C)
16 {
17     foo_ = [x];
18 
19     auto y = foo;
20 
21     static assert(is(typeof(y) == const(X)[]));
22     static assert(is(typeof(foo) == const(X)[]));
23 }

Property has correct type.

1 class C
2 {
3     @Read
4     private int foo_;
5 
6     mixin(GenerateFieldAccessors);
7 }
8 
9 with (new C)
10 {
11     static assert(is(typeof(foo) == int));
12 }
1 class A
2 {
3     @Read
4     string foo_;
5 
6     mixin(GenerateFieldAccessors);
7 }
8 
9 class B : A
10 {
11     @Read
12     string bar_;
13 
14     mixin(GenerateFieldAccessors);
15 }

Transfers struct attributes.

1 struct S
2 {
3     this(this)
4     {
5     }
6 
7     void opAssign(S s)
8     {
9     }
10 }
11 
12 class A
13 {
14     @Read
15     S[] foo_;
16 
17     @ConstRead
18     S bar_;
19 
20     @Write
21     S baz_;
22 
23     mixin(GenerateFieldAccessors);
24 }

@Read property returns array with mutable elements.

1 struct Field
2 {
3 }
4 
5 struct S
6 {
7     @Read
8     Field[] foo_;
9 
10     mixin(GenerateFieldAccessors);
11 }
12 
13 with (S())
14 {
15     Field[] arr = foo;
16 }

Static properties are generated for static members.

1 class MyStaticTest
2 {
3     @Read
4     static int stuff_ = 8;
5 
6     mixin(GenerateFieldAccessors);
7 }
8 
9 assert(MyStaticTest.stuff == 8);

Meta