1 /** 2 * Copyright: © 2012-2014 Anton Gushcha 3 * License: Subject to the terms of the MIT license, as written in the included LICENSE file. 4 * Authors: NCrashed <ncrashed@gmail.com>, 5 * LeMarwin <lemarwin42@gmail.com>, 6 * Nazgull09 <nazgull90@gmail.com> 7 */ 8 module devol.std.typepod; 9 10 import std.random; 11 import std.conv; 12 import std.stream; 13 14 import dyaml.all; 15 16 public 17 { 18 import devol.argument; 19 import devol.type; 20 import devol.std.argpod; 21 } 22 23 alias TypePod!bool TypeBool; 24 alias TypePod!int TypeInt; 25 alias TypePod!float TypeFloat; 26 alias TypePod!long TypeLong; 27 alias TypePod!double TypeDouble; 28 alias TypePod!char TypeChar; 29 alias TypePod!uint TypeUInt; 30 31 class TypePod(T) : Type 32 { 33 this() 34 { 35 super("Type"~T.stringof); 36 } 37 38 override ArgPod!T getNewArg() 39 { 40 auto arg = new ArgPod!T(); 41 arg = T.init; 42 return arg; 43 } 44 45 override ArgPod!T getNewArg(string min, string max, string[] exVal) 46 { 47 auto arg = new ArgPod!T(); 48 49 bool except( T val, string[] ex) 50 { 51 foreach( s; ex) 52 if (to!T(s) == val) 53 return true; 54 return false; 55 } 56 57 static if ( !is(T == bool) ) 58 { 59 do 60 { 61 arg = uniform!("[]")(to!T(min), to!T(max)); 62 } while( except( arg.val, exVal) ); 63 } else 64 { 65 do 66 { 67 arg = (uniform!("[]")(0, 1)) == 1; 68 } while( except( arg.val, exVal) ); 69 } 70 return arg; 71 } 72 73 override Argument loadArgument(InputStream stream) 74 { 75 static if(is(T == bool)) 76 { 77 ubyte val; 78 stream.read(val); 79 80 return new ArgPod!T(cast(bool)val); 81 } else 82 { 83 T val; 84 stream.read(val); 85 return new ArgPod!T(val); 86 } 87 } 88 89 override Argument loadArgument(Node node) 90 { 91 static if(is(T == char)) 92 { 93 return new ArgPod!T(node["value"].as!string[0]); 94 } else 95 { 96 return new ArgPod!T(node["value"].as!T); 97 } 98 } 99 }