File: qualifier.d

package info (click to toggle)
mir-core 1.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 560 kB
  • sloc: makefile: 9; sh: 7
file content (180 lines) | stat: -rw-r--r-- 3,547 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/++
Const and Immutable qualifiers helpers for Mir Type System.

License: $(HTTP www.apache.org/licenses/LICENSE-2.0, Apache-2.0)
Authors:   Ilia Ki

Macros:
NDSLICE = $(REF_ALTTEXT $(TT $2), $2, mir, ndslice, $1)$(NBSP)
+/
module mir.qualifier;

import std.traits;

/++
+/
template LightScopeOf(T)
{
    static if (isPointer!T)
    {
        alias LightScopeOf = T;
    }
    else
    {
        static if (__traits(hasMember, T, "lightScope"))
            alias LightScopeOf = typeof(T.init.lightScope());
        else
        static if (is(T == immutable))
            alias LightScopeOf = LightImmutableOf!T;
        else
        static if (is(T == const))
            alias LightScopeOf = LightConstOf!T;
        else
            alias LightScopeOf = T;
    }
}

/++
+/
template LightConstOf(T)
{
    static if (isPointer!T)
    {
        alias LightConstOf = const(PointerTarget!T)*;
    }
    else
    {
        alias LightConstOf = typeof(const(T).init.lightConst());
    }
}

/// ditto
template LightImmutableOf(T)
{
    static if (isPointer!T)
    {
        alias LightImmutableOf = immutable(PointerTarget!T)*;
    }
    else
    {
        alias LightImmutableOf = typeof(immutable(T).init.lightImmutable());
    }
}

@property:

/++
Tries to strip a reference counting handles from the value.
This funciton should be used only when the result never skips the current scope.

This function is used by some algorithms to optimise work with reference counted types.
+/
auto ref lightScope(T)(auto ref return scope T v)
    if (!is(T : P*, P) && __traits(hasMember, T, "lightScope"))
{
    return v.lightScope;
}

/// ditto
auto ref lightScope(T)(auto return ref T v)
    if (!is(T : P*, P) && !__traits(hasMember, T, "lightScope"))
{
    static if (is(T == immutable))
        return lightImmutable(v);
    else
    static if (is(T == const))
        return lightConst(v);
    else
        return v;
}

/// ditto
auto lightScope(T)(return T v)
    if (is(T : P*, P))
{
    return cast(typeof(*v)*) v;
}

///
auto lightImmutable(T)(auto ref immutable T v)
    if (!is(T : P*, P) && __traits(hasMember, immutable T, "lightImmutable"))
{
    return v.lightImmutable;
}

/// ditto
T lightImmutable(T)(auto ref immutable T e)
    if (!isDynamicArray!T && isImplicitlyConvertible!(immutable T, T) && !__traits(hasMember, immutable T, "lightImmutable"))
{
    return e;
}

/// ditto
auto lightImmutable(T)(immutable(T)[] e)
{
    return e;
}

/// ditto
auto lightImmutable(T)(immutable(T)* e)
{
    return e;
}

///
auto lightConst(T)(auto ref const T v)
    if (!is(T : P*, P) && __traits(hasMember, const T, "lightConst"))
{
    return v.lightConst;
}

/// ditto
auto lightConst(T)(auto ref immutable T v)
    if (!is(T : P*, P) && __traits(hasMember, immutable T, "lightConst"))
{
    return v.lightConst;
}

/// ditto
T lightConst(T)(auto ref const T e)
    if (!isDynamicArray!T && isImplicitlyConvertible!(const T, T) && !__traits(hasMember, const T, "lightConst"))
{
    return e;
}

/// ditto
T lightConst(T)(auto ref immutable T e)
    if (!isDynamicArray!T && isImplicitlyConvertible!(immutable T, T) && !__traits(hasMember, immutable T, "lightConst"))
{
    return e;
}

/// ditto
auto lightConst(T)(const(T)[] e)
{
    return e;
}

/// ditto
auto lightConst(T)(immutable(T)[] e)
{
    return e;
}

/// ditto
auto lightConst(T)(const(T)* e)
{
    return e;
}

/// ditto
auto lightConst(T)(immutable(T)* e)
{
    return e;
}

///
auto trustedImmutable(T)(auto ref const T e) @trusted
{
    return lightImmutable(*cast(immutable) &e);
}