vibe.internal.meta.uda 0/20(0%) line coverage

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
210
220
230
240
250
260
270
280
290
300
310
320
330
340
350
360
370
380
390
400
410
420
430
440
450
460
470
480
490
500
510
520
530
540
550
560
570
580
590
600
610
620
630
640
650
660
670
680
690
700
710
720
730
740
750
760
770
780
790
800
810
820
830
840
850
860
870
880
890
900
910
920
930
940
950
960
970
980
990
1000
1010
1020
1030
1040
1050
1060
1070
1080
1090
1100
1110
1120
1130
1140
1150
1160
1170
1180
1190
1200
1210
1220
1230
1240
1250
1260
1270
1280
1290
1300
1310
1320
1330
1340
1350
1360
1370
1380
1390
1400
1410
1420
1430
1440
1450
1460
1470
1480
1490
1500
1510
1520
1530
1540
1550
1560
1570
1580
1590
1600
1610
1620
1630
1640
1650
1660
1670
1680
1690
1700
1710
1720
1730
1740
1750
1760
1770
1780
1790
1800
1810
1820
1830
1840
1850
1860
1870
1880
1890
1900
1910
1920
1930
1940
1950
1960
1970
1980
1990
2000
2010
2020
2030
2040
2050
2060
2070
2080
2090
2100
2110
2120
2130
2140
2150
2160
2170
2180
2190
2200
2210
2220
2230
2240
2250
2260
2270
2280
2290
2300
2310
2320
2330
2340
2350
2360
2370
2380
2390
/** Utility templates that help working with User Defined Attributes Copyright: © 2013 RejectedSoftware e.K. License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. Authors: Sönke Ludwig, Михаил Страшун */ module vibe.internal.meta.uda; //import vibe.internal.meta.traits; /** Small convenience wrapper to find and extract certain UDA from given type. Will stop on first element which is of required type. Params: UDA = type or template to search for in UDA list Symbol = symbol to query for UDA's allow_types = if set to `false` considers attached `UDA` types an error (only accepts instances/values) Returns: aggregated search result struct with 3 field. `value` aliases found UDA. `found` is boolean flag for having a valid find. `index` is integer index in attribute list this UDA was found at. */ template findFirstUDA(alias UDA, alias Symbol, bool allow_types = false) if (!is(UDA)) { enum findFirstUDA = findNextUDA!(UDA, Symbol, 0, allow_types); } /// Ditto template findFirstUDA(UDA, alias Symbol, bool allow_types = false) { enum findFirstUDA = findNextUDA!(UDA, Symbol, 0, allow_types); } private struct UdaSearchResult(alias UDA) { alias value = UDA; bool found = false; long index = -1; } /** Small convenience wrapper to find and extract certain UDA from given type. Will start at the given index and stop on the next element which is of required type. Params: UDA = type or template to search for in UDA list Symbol = symbol to query for UDA's idx = 0-based index to start at. Should be positive, and under the total number of attributes. allow_types = if set to `false` considers attached `UDA` types an error (only accepts instances/values) Returns: aggregated search result struct with 3 field. `value` aliases found UDA. `found` is boolean flag for having a valid find. `index` is integer index in attribute list this UDA was found at. */ template findNextUDA(alias UDA, alias Symbol, long idx, bool allow_types = false) if (!is(UDA)) { import std.traits : isInstanceOf; import std.typetuple : TypeTuple; private alias udaTuple = TypeTuple!(__traits(getAttributes, Symbol)); static assert(idx >= 0, "Index given to findNextUDA can't be negative"); static assert(idx <= udaTuple.length, "Index given to findNextUDA is above the number of attribute"); public template extract(size_t index, list...) { static if (!list.length) enum extract = UdaSearchResult!(null)(false, -1); else { static if (is(list[0])) { static if (is(UDA) && is(list[0] == UDA) || !is(UDA) && isInstanceOf!(UDA, list[0])) { static assert (allow_types, "findNextUDA is designed to look up values, not types"); enum extract = UdaSearchResult!(list[0])(true, index); } else enum extract = extract!(index + 1, list[1..$]); } else { static if (is(UDA) && is(typeof(list[0]) == UDA) || !is(UDA) && isInstanceOf!(UDA, typeof(list[0]))) { import vibe.internal.meta.traits : isPropertyGetter; static if (isPropertyGetter!(list[0])) { enum value = list[0]; enum extract = UdaSearchResult!(value)(true, index); } else enum extract = UdaSearchResult!(list[0])(true, index); } else enum extract = extract!(index + 1, list[1..$]); } } } enum findNextUDA = extract!(idx, udaTuple[idx .. $]); } /// ditto template findNextUDA(UDA, alias Symbol, long idx, bool allow_types = false) { import std.traits : isInstanceOf; import std.typetuple : TypeTuple; private alias udaTuple = TypeTuple!(__traits(getAttributes, Symbol)); static assert(idx >= 0, "Index given to findNextUDA can't be negative"); static assert(idx <= udaTuple.length, "Index given to findNextUDA is above the number of attribute"); public template extract(size_t index, list...) { static if (!list.length) enum extract = UdaSearchResult!(null)(false, -1); else { static if (is(list[0])) { static if (is(list[0] == UDA)) { static assert (allow_types, "findNextUDA is designed to look up values, not types"); enum extract = UdaSearchResult!(list[0])(true, index); } else enum extract = extract!(index + 1, list[1..$]); } else { static if (is(typeof(list[0]) == UDA)) { import vibe.internal.meta.traits : isPropertyGetter; static if (isPropertyGetter!(list[0])) { enum value = list[0]; enum extract = UdaSearchResult!(value)(true, index); } else enum extract = UdaSearchResult!(list[0])(true, index); } else enum extract = extract!(index + 1, list[1..$]); } } } enum findNextUDA = extract!(idx, udaTuple[idx .. $]); } /// unittest { struct Attribute { int x; } @("something", Attribute(42), Attribute(41)) void symbol(); enum result0 = findNextUDA!(string, symbol, 0); static assert (result0.found); static assert (result0.index == 0); static assert (result0.value == "something"); enum result1 = findNextUDA!(Attribute, symbol, 0); static assert (result1.found); static assert (result1.index == 1); static assert (result1.value == Attribute(42)); enum result2 = findNextUDA!(int, symbol, 0); static assert (!result2.found); enum result3 = findNextUDA!(Attribute, symbol, result1.index + 1); static assert (result3.found); static assert (result3.index == 2); static assert (result3.value == Attribute(41)); } unittest { struct Attribute { int x; } @(Attribute) void symbol(); static assert (!is(findNextUDA!(Attribute, symbol, 0))); enum result0 = findNextUDA!(Attribute, symbol, 0, true); static assert (result0.found); static assert (result0.index == 0); static assert (is(result0.value == Attribute)); } unittest { struct Attribute { int x; } enum Dummy; @property static Attribute getter() { return Attribute(42); } @Dummy @getter void symbol(); enum result0 = findNextUDA!(Attribute, symbol, 0); static assert (result0.found); static assert (result0.index == 1); static assert (result0.value == Attribute(42)); } /// Eager version of findNextUDA that represent all instances of UDA in a Tuple. /// If one of the attribute is a type instead of an instance, compilation will fail. template UDATuple(alias UDA, alias Sym) { import std.typetuple : TypeTuple; private template extract(size_t maxSize, Founds...) { private alias LastFound = Founds[$ - 1]; // No more to find static if (!LastFound.found) enum extract = Founds[0 .. $ - 1]; else { // For ease of use, this is a Tuple of UDA, not a tuple of UdaSearchResult!(...) private alias Result = TypeTuple!(Founds[0 .. $ - 1], LastFound.value); // We're at the last parameter static if (LastFound.index == maxSize) enum extract = Result; else enum extract = extract!(maxSize, Result, findNextUDA!(UDA, Sym, LastFound.index + 1)); } } private enum maxIndex = TypeTuple!(__traits(getAttributes, Sym)).length; enum UDATuple = extract!(maxIndex, findNextUDA!(UDA, Sym, 0)); } unittest { import std.typetuple : TypeTuple; struct Attribute { int x; } enum Dummy; @(Dummy, Attribute(21), Dummy, Attribute(42), Attribute(84)) void symbol() {} @(Dummy, Attribute(21), Dummy, Attribute(42), Attribute) void wrong() {} alias Cmp = TypeTuple!(Attribute(21), Attribute(42), Attribute(84)); static assert(Cmp == UDATuple!(Attribute, symbol)); static assert(!is(UDATuple!(Attribute, wrong))); } /// Avoid repeating the same error message again and again. /// ---- /// if (!__ctfe) /// assert(0, onlyAsUda!func); /// ---- template onlyAsUda(string from /*= __FUNCTION__*/) { // With default param, DMD think expression is void, even when writing 'enum string onlyAsUda = ...' enum onlyAsUda = from~" must only be used as an attribute - not called as a runtime function."; }