comparison dstep/internal/Traits.d @ 1:033d260cfc9b

First upload of the bridge
author Jacob Carlborg <doob@me.com>
date Thu, 18 Jun 2009 22:00:13 +0200
parents
children 19885b43130e
comparison
equal deleted inserted replaced
0:c7db221de6e8 1:033d260cfc9b
1 /**
2 * Copyright: Copyright (c) 2009 Jacob Carlborg.
3 * Authors: Jacob Carlborg
4 * Version: Initial created: Apr 28, 2009
5 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
6 */
7 module dstep.internal.Traits;
8
9 /**
10 * Returns the name of the given function
11 *
12 * Params:
13 * func = the function alias to get the name of
14 *
15 * Returns: the name of the function
16 */
17 template functionNameOf (alias func)
18 {
19 version(LDC)
20 const functionNameOf = (&func).stringof[1 .. $];
21
22 else
23 const functionNameOf = (&func).stringof[2 .. $];
24 }
25
26 /**
27 * Returns the parameter names of the given function
28 *
29 * Params:
30 * func = the function alias to get the parameter names of
31 *
32 * Returns: an array of strings containing the parameter names
33 */
34 template parameterNamesOf (alias func)
35 {
36 const parameterNamesOf = parameterNamesOfImpl!(func);
37 }
38
39 private char[][] parameterNamesOfImpl (alias func) ()
40 {
41 char[] funcStr = typeof(&func).stringof;
42
43 auto start = funcStr.indexOf('(');
44 auto end = funcStr.indexOf(')');
45
46 const firstPattern = ' ';
47 const secondPattern = ',';
48
49 funcStr = funcStr[start + 1 .. end];
50
51 if (funcStr == "")
52 return null;
53
54 funcStr ~= secondPattern;
55
56 char[] token;
57 char[][] arr;
58
59 foreach (c ; funcStr)
60 {
61 if (c != firstPattern && c != secondPattern)
62 token ~= c;
63
64 else
65 {
66 if (token)
67 arr ~= token;
68
69 token = null;
70 }
71 }
72
73 char[][] result;
74 bool skip = false;
75
76 foreach (str ; arr)
77 {
78 skip = !skip;
79
80 if (skip)
81 continue;
82
83 result ~= str;
84 }
85
86 return result;
87 }
88
89 /**
90 * Compile-time function to get the index of the give element.
91 *
92 * Performs a linear scan, returning the index of the first occurrence
93 * of the specified element in the array, or U.max if the array does
94 * not contain the element.
95 *
96 * Params:
97 * arr = the array to get the index of the element from
98 * element = the element to find
99 *
100 * Returns: the index of the element or size_t.max if the element was not found.
101 */
102 private size_t indexOf (T) (T[] arr, T element)
103 {
104 foreach (i, e ; arr)
105 if (e == element)
106 return i;
107
108 return size_t.max;
109 }