1 /** 2 License: 3 Boost Software License - Version 1.0 - August 17th, 2003 4 5 Permission is hereby granted, free of charge, to any person or organization 6 obtaining a copy of the software and accompanying documentation covered by 7 this license (the "Software") to use, reproduce, display, distribute, 8 execute, and transmit the Software, and to prepare derivative works of the 9 Software, and to permit third-parties to whom the Software is furnished to 10 do so, all subject to the following: 11 12 The copyright notices in the Software and this entire statement, including 13 the above license grant, this restriction and the following disclaimer, 14 must be included in all copies of the Software, in whole or in part, and 15 all derivative works of the Software, unless such copies or derivative 16 works are solely in the form of machine-executable object code generated by 17 a source language processor. 18 19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 22 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 23 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 24 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 DEALINGS IN THE SOFTWARE. 26 27 Authors: 28 Alexandru Ermicioi 29 **/ 30 module aermicioi.aedi_property_reader.convertor.traits; 31 32 import std.traits : functionAttributes, variadicFunctionStyle, Variadic, FunctionAttribute, arity; 33 34 /** 35 Tests if field is property getter. 36 **/ 37 template isPropertyGetter(alias func) { 38 bool isPropertyGetter() { 39 static foreach (overload; __traits(getOverloads, func)) { 40 41 if ( 42 (variadicFunctionStyle!overload == Variadic.no) && 43 (arity!overload == 0) && (functionAttributes!overload & FunctionAttribute.property) 44 ) { 45 return true; 46 } 47 } 48 49 return false; 50 } 51 } 52 53 /** 54 Tests if field is property setter 55 **/ 56 template isPropertyPropertySetter(alias Type, string member) { 57 static foreach (overload; __traits(getOverloads, Type, member)) { 58 static if (!is(found) && isPropertyPropertySetter!overload) { 59 enum found = true; 60 enum isPropertyPropertySetter = found; 61 } 62 } 63 64 static if (!is(found) && isPropertyPropertySetter!overload) { 65 enum isPropertyPropertySetter = false; 66 } 67 68 } 69 70 /** 71 ditto 72 **/ 73 enum isPropertyPropertySetter(alias func) = (variadicFunctionStyle!func == Variadic.no) && 74 (arity!func == 1) && (functionAttributes!func & FunctionAttribute.property); 75 76 template match(alias predicate, Types...) { 77 78 static foreach (alias T; Types) { 79 static if (!is(typeof(found)) && predicate!T) { 80 enum found = true; 81 alias match = T; 82 } 83 } 84 85 static if (!is(typeof(found)) || !found) { 86 87 static assert(false, "No match found for passed template args"); 88 } 89 } 90 91 /** 92 Well, it's a nothrow wrapper usable for logger to allow logging in nothrow functions. 93 **/ 94 T n(T)(lazy T value) nothrow { 95 try { 96 static if (is(T == void)) { 97 98 value(); 99 } else { 100 101 return value(); 102 } 103 } catch (Exception e) { 104 assert(false, "An exception was thrown where it wasn't expected to."); 105 } 106 } 107 108 enum isD(T, X) = is(T : X); 109 110 interface PureSafeNothrowToString { 111 string toString() @safe pure nothrow; 112 }