ketura

stat.tt.v.3

Aug 25th, 2017
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.91 KB | None | 0 0
  1. <#@ template debug="true" hostspecific="false" language="C#" #>
  2. <#@ assembly name="System.Core" #>
  3. <#@ import namespace="System.Linq" #>
  4. <#@ import namespace="System.Text" #>
  5. <#@ import namespace="System.Collections.Generic" #>
  6. <#@ output extension=".cs" #>
  7.  
  8. <#
  9.  
  10. Dictionary<string, TypeInfo> types = new Dictionary<string, TypeInfo>()
  11. {
  12.  
  13. { "Decimal", new TypeInfo() { Type = typeof(decimal), TypeName = "Decimal", Name = "Decimal", Signed = true, Decimal = true, MateName = null, BitCount = 128, ConvertTo = new Dictionary<string, string>(), ConvertFrom = new Dictionary<string, string>() } },
  14. { "Double", new TypeInfo() { Type = typeof(double), TypeName = "Double", Name = "Double", Signed = true, Decimal = true, MateName = null, BitCount = 64, ConvertTo = new Dictionary<string, string>(), ConvertFrom = new Dictionary<string, string>() } },
  15. { "Float", new TypeInfo() { Type = typeof(Single), TypeName = "Single", Name = "Float", Signed = true, Decimal = true, MateName = null, BitCount = 32, ConvertTo = new Dictionary<string, string>(), ConvertFrom = new Dictionary<string, string>() } },
  16. { "Byte", new TypeInfo() { Type = typeof(byte), TypeName = "Byte", Name = "Byte", Signed = false, Decimal = false, MateName = "SByte", BitCount = 8, ConvertTo = new Dictionary<string, string>(), ConvertFrom = new Dictionary<string, string>() } },
  17. { "SByte", new TypeInfo() { Type = typeof(sbyte), TypeName = "SByte", Name = "SByte", Signed = true, Decimal = false, MateName = "Byte", BitCount = 8, ConvertTo = new Dictionary<string, string>(), ConvertFrom = new Dictionary<string, string>() } },
  18. { "Short", new TypeInfo() { Type = typeof(Int16), TypeName = "Int16", Name = "Short", Signed = true, Decimal = false, MateName = "UShort", BitCount = 16, ConvertTo = new Dictionary<string, string>(), ConvertFrom = new Dictionary<string, string>() } },
  19. { "UShort", new TypeInfo() { Type = typeof(UInt16), TypeName = "UInt16", Name = "UShort", Signed = false, Decimal = false, MateName = "Short", BitCount = 16, ConvertTo = new Dictionary<string, string>(), ConvertFrom = new Dictionary<string, string>() } },
  20. { "Int", new TypeInfo() { Type = typeof(Int32), TypeName = "Int32", Name = "Int", Signed = true, Decimal = false, MateName = "UInt", BitCount = 32, ConvertTo = new Dictionary<string, string>(), ConvertFrom = new Dictionary<string, string>() } },
  21. { "UInt", new TypeInfo() { Type = typeof(UInt32), TypeName = "UInt32", Name = "UInt", Signed = false, Decimal = false, MateName = "Int", BitCount = 32, ConvertTo = new Dictionary<string, string>(), ConvertFrom = new Dictionary<string, string>() } },
  22. { "Long", new TypeInfo() { Type = typeof(Int64), TypeName = "Int64", Name = "Long", Signed = true, Decimal = false, MateName = "ULong", BitCount = 64, ConvertTo = new Dictionary<string, string>(), ConvertFrom = new Dictionary<string, string>() } },
  23. { "ULong", new TypeInfo() { Type = typeof(UInt64), TypeName = "UInt64", Name = "ULong", Signed = false, Decimal = false, MateName = "Long", BitCount = 64, ConvertTo = new Dictionary<string, string>(), ConvertFrom = new Dictionary<string, string>() } }
  24.  
  25. };
  26.  
  27.  
  28. #>
  29.  
  30. using System;
  31. //using XGEFCore;
  32. using test;
  33.  
  34. //****************************
  35. //****************************
  36. //
  37. // THE BELOW CODE IS AUTOGENERATED.  Please do not bother editing it, instead please see the template that it was generated from.
  38. //
  39. //****************************
  40. //****************************
  41.  
  42. namespace Stats
  43. {
  44. <#  foreach(var pair in types) {
  45.             string T = pair.Value.Type.Name;
  46.             string Class = pair.Key + "Stat"; #>
  47.  
  48.     public class <#= Class #> : NumericStat<<#= T #>>
  49.     {
  50.         public override <#= T #> MaxValue { get { return Math.Min(<#= T #>.MaxValue, MaxLimit); } }
  51.         public override <#= T #> MinValue { get { return Math.Max(<#= T #>.MinValue, MinLimit); } }
  52.  
  53.         #region IComparable
  54.         public override int CompareTo(object obj)
  55.         {
  56.             if (obj == null)
  57.                 return 1;
  58.  
  59.             if(obj is IData)
  60.             {
  61.                 return Value.CompareTo(((IData)obj).Data);
  62.             }
  63.  
  64.             return Value.CompareTo(obj);
  65.         }
  66.         #endregion
  67.  
  68.         #region IConvertible
  69.  
  70.         public override TypeCode GetTypeCode()
  71.         {
  72.             return TypeCode.<#= pair.Value.TypeName #>;
  73.         }
  74.  
  75.         public override bool ToBoolean(IFormatProvider provider)
  76.         {
  77.             if(Value == 0)
  78.                 return false;
  79.             return true;
  80.         }
  81.  
  82.         public override char ToChar(IFormatProvider provider)
  83.         {
  84.             return Convert.ToChar(Value);
  85.         }
  86.  
  87.         public override sbyte ToSByte(IFormatProvider provider)
  88.         {
  89.             return Convert.ToSByte(Value);
  90.         }
  91.  
  92.         public override byte ToByte(IFormatProvider provider)
  93.         {
  94.             return Convert.ToByte(Value);
  95.         }
  96.  
  97.         public override short ToInt16(IFormatProvider provider)
  98.         {
  99.             return Convert.ToInt16(Value);
  100.         }
  101.  
  102.         public override ushort ToUInt16(IFormatProvider provider)
  103.         {
  104.             return Convert.ToUInt16(Value);
  105.         }
  106.  
  107.         public override int ToInt32(IFormatProvider provider)
  108.         {
  109.             return Convert.ToInt32(Value);
  110.         }
  111.  
  112.         public override uint ToUInt32(IFormatProvider provider)
  113.         {
  114.             return Convert.ToUInt32(Value);
  115.         }
  116.  
  117.         public override long ToInt64(IFormatProvider provider)
  118.         {
  119.             return Convert.ToInt64(Value);
  120.         }
  121.  
  122.         public override ulong ToUInt64(IFormatProvider provider)
  123.         {
  124.             return Convert.ToUInt64(Value);
  125.         }
  126.  
  127.         public override float ToSingle(IFormatProvider provider)
  128.         {
  129.             return Convert.ToSingle(Value);
  130.         }
  131.  
  132.         public override double ToDouble(IFormatProvider provider)
  133.         {
  134.             return Convert.ToDouble(Value);
  135.         }
  136.  
  137.         public override decimal ToDecimal(IFormatProvider provider)
  138.         {
  139.             return Convert.ToDecimal(Value);
  140.         }
  141.  
  142.         public override string ToString(IFormatProvider provider)
  143.         {
  144.             return Convert.ToString(Value);
  145.         }
  146.  
  147.         public override object ToType(Type conversionType, IFormatProvider provider)
  148.         {
  149.             return Convert.ChangeType(Value, conversionType);
  150.         }
  151.         #endregion
  152.  
  153.         #region Type Casting
  154.         <#  foreach(var typePair in types) {
  155.                     string typeName = typePair.Value.Type.Name;
  156.                     string otherStat = typePair.Value.Name + "Stat";
  157.                     string castingTypeTo = "explicit";
  158.                     string castingTypeFrom = "explicit";
  159.  
  160.                     //this class is a container for the casting type
  161.                     if(typeName == T)
  162.                     {
  163.                         castingTypeTo = "implicit";
  164.                         castingTypeFrom = "implicit";
  165.                     }
  166.                     //this class is an integer
  167.                     else if(!pair.Value.Decimal)
  168.                     {
  169.                         //integers only cast to the target if it's the same sign and big enough to fit (or it's a real type)
  170.                         if(typePair.Value.Decimal || typePair.Value.Signed == pair.Value.Signed && pair.Value.BitCount <= typePair.Value.BitCount)
  171.                             castingTypeTo = "implicit";
  172.                         //target can only cast to integer if target is also an integer and the signing matches and this integer is big enough to fit target
  173.                         if(!typePair.Value.Decimal && typePair.Value.Signed == pair.Value.Signed && pair.Value.BitCount >= typePair.Value.BitCount)
  174.                             castingTypeFrom = "implicit";
  175.                     }
  176.                     //this class is a real type
  177.                     else if(typePair.Value.Decimal)
  178.                     {
  179.                         //floats can cast to doubles, but not the other way around, so detect that accordingly
  180.                         if(T == "Single" && typeName == "Double")
  181.                             castingTypeTo = "implicit";
  182.  
  183.                         if(typeName == "Single" && T == "Double")
  184.                             castingTypeFrom = "implicit";
  185.                     }
  186.  
  187.                     pair.Value.ConvertFrom[typeName] = castingTypeFrom;
  188.                     pair.Value.ConvertTo[typeName] = castingTypeTo; #>
  189.  
  190.         public static <#= castingTypeTo #> operator <#= typeName #>(<#= Class #> stat)
  191.         {
  192.             return Convert.To<#= typeName #>(stat.Value);
  193.         }
  194. <# if(otherStat != Class) { #>
  195.         public static <#= castingTypeFrom #> operator <#= Class #>(<#= otherStat #> stat)
  196.         {
  197.             return new <#= Class #>(stat.Name, (<#= T #>)stat.Value, (<#= T #>)stat.MinValue, (<#= T #>)stat.MaxValue, stat.Description, stat.Notes);
  198.         }<# } #>
  199.  
  200.         public static <#= castingTypeFrom #> operator <#= Class #>(<#= typeName #> num)
  201.         {
  202.             <#= Class #> stat = new <#= Class #>();
  203. <# if(castingTypeFrom == "explicit") { #>
  204.             if((<#= T #>)num > stat.MaxLimit)
  205.                 stat.Set(stat.MaxLimit);
  206.             else if((<#= T #>)num < stat.MinLimit)
  207.                 stat.Set(stat.MinLimit);
  208.             else <# } #>
  209.             stat.Set((<#= T #>)num);
  210.             return stat;
  211.         }
  212. <# } #>
  213.  
  214.         #endregion
  215.  
  216.         #region Operator Overloading
  217.         <# foreach(var typePair in types) {
  218.             string otherT = typePair.Value.Type.Name;
  219.             string otherClass = typePair.Value.Name + "Stat";#>
  220.  
  221.         public static <#= Class #> operator +(<#= Class #> lhs, <#= otherT #> rhs)
  222.         {
  223.             <#= Class #> result = lhs.Copy();
  224.             result.Set((<#= T #>)(lhs.Value + (<#= T #>)rhs));
  225.             return result;
  226.         }
  227.         public static <#= Class #> operator -(<#= Class #> lhs, <#= otherT #> rhs)
  228.         {
  229.             <#= Class #> result = lhs.Copy();
  230.             result.Set((<#= T #>)(lhs.Value - (<#= T #>)rhs));
  231.             return result;
  232.         }
  233.         public static <#= Class #> operator *(<#= Class #> lhs, <#= otherT #> rhs)
  234.         {
  235.             <#= Class #> result = lhs.Copy();
  236.             result.Set((<#= T #>)(lhs.Value * (<#= T #>)rhs));
  237.             return result;
  238.         }
  239.         public static <#= Class #> operator /(<#= Class #> lhs, <#= otherT #> rhs)
  240.         {
  241.             <#= Class #> result = lhs.Copy();
  242.             result.Set((<#= T #>)(lhs.Value / (<#= T #>)rhs));
  243.             return result;
  244.         }
  245.         public static <#= Class #> operator %(<#= Class #> lhs, <#= otherT #> rhs)
  246.         {
  247.             <#= Class #> result = lhs.Copy();
  248.             result.Set((<#= T #>)(lhs.Value % (<#= T #>)rhs));
  249.             return result;
  250.         }
  251.         public static <#= Class #> operator ^(<#= Class #> lhs, <#= otherT #> rhs)
  252.         {
  253.             <#= Class #> result = lhs.Copy();
  254.             result.Set((<#= T #>)Math.Pow((double)lhs.Value, (double)rhs));
  255.             return result;
  256.         }
  257.         public static bool operator >(<#= Class #> lhs, <#= otherT #> rhs)
  258.         {
  259.             return lhs.CompareTo(rhs) > 0;
  260.         }
  261.         public static bool operator <(<#= Class #> lhs, <#= otherT #> rhs)
  262.         {
  263.             return lhs.CompareTo(rhs) < 0;
  264.         }
  265. <# } #>
  266.        
  267.         #endregion
  268.  
  269.         public <#= Class #> Copy()
  270.         {
  271.             return new <#= Class #>(Name, Value, MinLimit, MaxLimit, Description, Notes);
  272.         }
  273.  
  274.         public <#= Class #>() : this("UnnamedStat", 0, <#= T #>.MinValue, <#= T #>.MaxValue, null, null) { }
  275.         public <#= Class #>(string name) : this(name, 0, <#= T #>.MinValue, <#= T #>.MaxValue, null, null) { }
  276.         public <#= Class #>(string name, <#= T #> value) : this(name, value, <#= T #>.MinValue, <#= T #>.MaxValue, null, null) { }
  277.         public <#= Class #>(string name, <#= T #> value, <#= T #> min, <#= T #> max) : this(name, value, min, max, null, null) { }
  278.         public <#= Class #>(string name, <#= T #> value, <#= T #> min, <#= T #> max, string desc, string notes) : base(name, value, min, max, desc, notes) { }
  279.  
  280.         public override string ToString() { return Value.ToString(); }
  281.  
  282.     }
  283.  
  284. <# } #>
  285. }
  286.  
  287. <#+
  288. public struct TypeInfo
  289. {
  290.     public Type Type { get; set; }
  291.     public string Name { get; set; }
  292.     public string TypeName { get; set; }
  293.     public bool Signed { get; set; }
  294.     public bool Decimal { get; set; }
  295.     public string MateName { get; set; }
  296.     public int BitCount { get; set; }
  297.     public Dictionary<string, string> ConvertTo { get; set; }
  298.     public Dictionary<string, string> ConvertFrom{ get; set; }
  299. }
  300. #>
Advertisement
Add Comment
Please, Sign In to add comment