1 /**
2 This module is a container for a set of templates that are used across library and aren't bound to some specific module.
3 License:
4 	Boost Software License - Version 1.0 - August 17th, 2003
5 
6     Permission is hereby granted, free of charge, to any person or organization
7     obtaining a copy of the software and accompanying documentation covered by
8     this license (the "Software") to use, reproduce, display, distribute,
9     execute, and transmit the Software, and to prepare derivative works of the
10     Software, and to permit third-parties to whom the Software is furnished to
11     do so, all subject to the following:
12 
13     The copyright notices in the Software and this entire statement, including
14     the above license grant, this restriction and the following disclaimer,
15     must be included in all copies of the Software, in whole or in part, and
16     all derivative works of the Software, unless such copies or derivative
17     works are solely in the form of machine-executable object code generated by
18     a source language processor.
19 
20     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22     FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
23     SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
24     FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
25     ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26     DEALINGS IN THE SOFTWARE.
27 
28 Authors:
29 	Alexandru Ermicioi
30 **/
31 module aermicioi.aedi.util.traits.traits;
32 import std.traits;
33 import std.typetuple;
34 import std.algorithm;
35 import std.array;
36 
37 public {
38 
39     enum bool isPublic(alias T) = __traits(getProtection, T) == "public";
40     enum bool isPublic(alias T, string member) = getProtection!(T, member) == "public";
41 
42     template identifier(alias T) {
43         alias identifier = Identity!(__traits(identifier, T));
44     }
45 
46     template getMember(alias T, string member) {
47 
48         alias getMember = Identity!(__traits(getMember, T, member));
49     }
50 
51     template getOverloads(alias T, string member) {
52 
53         alias getOverloads = AliasSeq!(__traits(getOverloads, T, member));
54     }
55 
56     template typeOf(alias T) {
57         static if (isValue!T) {
58             alias typeOf = typeof(T);
59         } else {
60             alias typeOf = T;
61         }
62     }
63 
64     template getProtection(alias T, string member) {
65 
66         static if (__traits(compiles, __traits(getProtection, __traits(getMember, T, member)))) {
67             enum auto getProtection = __traits(getProtection, __traits(getMember, T, member));
68         } else {
69             enum auto getProtection = "private";
70         }
71     }
72 
73     template getProtection(alias symbol) {
74         static if (__traits(compiles, __traits(getProtection, symbol))) {
75             enum auto getProtection = __traits(getProtection, symbol);
76         } else {
77             enum auto getProtection = "private";
78         }
79     }
80 
81     template isProtection(alias T, string member, string protection = "public") {
82         enum bool isProtection = getProtection!(T, member) == protection;
83     }
84 
85     template isProtection(T, string member, string protection = "public") {
86         enum bool isProtection = getProtection!(T, member) == protection;
87     }
88 
89     template isProtection(alias symbol, string protection) {
90         enum bool isProtection = getProtection!symbol == protection;
91     }
92 
93     template getMembersWithProtection(T, string member, string protection = "public") {
94         import aermicioi.aedi.util.traits.partial : chain, eq;
95 
96         alias getMembersWithProtection = Filter!(
97             chain!(
98                 eq!"public",
99                 getProtection
100             ),
101             __traits(getOverloads, T, member)
102         );
103     }
104 
105     template isField(T, string field) {
106 
107         auto isField() {
108             import aermicioi.aedi.util.traits.partial : eq;
109             static if (Filter!(eq!field, FieldNameTuple!T).length > 0) {
110                 return true;
111             } else {
112                 return false;
113             }
114         }
115     }
116 
117     template toType(T) {
118         alias toType = T;
119     }
120 
121     template toType(alias T) {
122         static if (is(typeof(T))) {
123 
124             alias toType = typeof(T);
125         } static if (is(T)) {
126 
127             alias toType = T;
128         }
129     }
130 
131     enum bool isDerived(alias T, alias Z) = is(T : Z);
132 
133     template isStaticFunction(alias method)  {
134         enum bool isStaticFunction = __traits(isStaticFunction, method);
135     }
136 }
137 
138 private {
139     alias Identity(alias A) = A;
140 }