1 /*
2  * Hunt - A refined core library for D programming language.
3  *
4  * Copyright (C) 2018-2019 HuntLabs
5  *
6  * Website: https://www.huntlabs.net/
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 module hunt.util.Traits;
13 
14 import std.meta;
15 import std.traits;
16 import std.typecons;
17 
18 mixin template GetConstantValues(T) if (is(T == struct) || is(T == class))
19 {
20     static T[] values()
21     {
22         T[] r;
23         enum s = __getValues!(r.stringof, T)();
24         // pragma(msg, s);
25         mixin(s);
26         return r;
27     }
28 
29     private static string __getValues(string name, T)()
30     {
31         string str;
32 
33         foreach (string memberName; __traits(derivedMembers, T))
34         {
35             // enum member = __traits(getMember, T, memberName);
36             alias memberType = typeof(__traits(getMember, T, memberName));
37             static if (is(memberType : T))
38             {
39                 str ~= name ~ " ~= " ~ memberName ~ ";\r\n";
40             }
41         }
42 
43         return str;
44     }
45 
46 }
47 
48 
49 alias Helper(alias T) = T;
50 
51 template Pointer(T) {
52     static if (is(T == class) || is(T == interface)) {
53         alias Pointer = T;
54     } else {
55         alias Pointer = T *;
56     }
57 }
58 
59 template isInheritClass(T, Base) {
60     enum FFilter(U) = is(U == Base);
61     enum isInheritClass = (Filter!(FFilter, BaseTypeTuple!T).length > 0);
62 }
63 
64 template isOnlyCharByte(T) {
65     enum bool isOnlyCharByte = is(T == byte) || is(T == ubyte) || is(T == char);
66 }
67 
68 template isCharByte(T) {
69     enum bool isCharByte = is(Unqual!T == byte) || is(Unqual!T == ubyte) || is(Unqual!T == char);
70 }
71 
72 
73 template isRefType(T)
74 {
75     enum isRefType = /*isPointer!T ||*/ isDelegate!T || isDynamicArray!T ||
76             isAssociativeArray!T || is (T == class) || is(T == interface);
77 }
78 
79 template isPublic(alias T)
80 {
81 	enum protection =  __traits(getProtection,T);
82 	enum isPublic = (protection == "public");
83 }