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.argpod; 9 10 import std.conv; 11 import std.random; 12 import std.traits; 13 import std.math; 14 import devol.serializable; 15 import devol.typemng; 16 17 import dyaml.all; 18 19 class ArgPod(T) : Argument, ISerializable 20 { 21 this() 22 { 23 super( TypeMng.getSingleton().getType("Type"~T.stringof) ); 24 } 25 26 this(T val) 27 { 28 this(); 29 opAssign(val); 30 } 31 32 ref ArgPod!T opAssign(Argument val) 33 { 34 auto apod = cast(ArgPod!T)(val); 35 if (apod is null) return this; 36 37 mVal = apod.mVal; 38 return this; 39 } 40 41 ref ArgPod!T opAssign(T val) 42 { 43 mVal = val; 44 return this; 45 } 46 47 override @property string tostring(uint depth=0) 48 { 49 return to!string(mVal); 50 } 51 52 @property T val() 53 { 54 return mVal; 55 } 56 57 @property T value() 58 { 59 return mVal; 60 } 61 62 override void randomChange() 63 { 64 static if (!is(T == bool)) 65 { 66 mVal = uniform!"[]"(-mVal.max, mVal.max); 67 } else 68 { 69 mVal = uniform!"[]"(0, 1) != 0; 70 } 71 } 72 73 override void randomChange(string maxChange) 74 { 75 static if (!is(T == bool)) 76 { 77 T ch; 78 try 79 { 80 ch = to!T(maxChange); 81 } catch( Exception e ) 82 { 83 return; 84 } 85 86 static if(isFloatingPoint!T) 87 { 88 if(mVal.isNaN) 89 { 90 mVal = uniform!"[]"(0.0, 1.0); 91 return; 92 } 93 94 if(ch.isNaN) 95 { 96 return; 97 } 98 } 99 100 mVal = uniform!"[]"(cast(T)(mVal-ch), cast(T)(mVal+ch)); 101 } else 102 { 103 mVal = uniform!"[]"(0, 1) != 0; 104 } 105 } 106 107 override @property Argument dup() 108 { 109 auto darg = new ArgPod!T(); 110 darg.mVal = mVal; 111 return darg; 112 } 113 114 void saveBinary(OutputStream stream) 115 { 116 static if(is(T == bool)) 117 { 118 stream.write(cast(ubyte)mVal); 119 } else 120 { 121 stream.write(mVal); 122 } 123 } 124 125 override Node saveYaml() 126 { 127 static if(is(T == char)) 128 { 129 return Node([ 130 "class": Node("plain"), 131 "value": Node([mVal].idup) 132 ]); 133 } else 134 { 135 return Node([ 136 "class": Node("plain"), 137 "value": Node(mVal) 138 ]); 139 } 140 } 141 142 protected T mVal; 143 }