Advertisement
sglienke

Generics.Operators

Aug 8th, 2015
345
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. unit Generics.Operators;
  2.  
  3. interface
  4.  
  5. uses
  6.   TypInfo;
  7.  
  8. type
  9.   Operators = class
  10.   private
  11.     class procedure RaiseNotSupported(typeInfo: PTypeInfo); static; inline;
  12.  
  13.     class procedure AddInt8(const left, right; var Result); inline;
  14.     class procedure AddUInt8(const left, right; var Result); inline;
  15.     class procedure AddInt16(const left, right; var Result); inline;
  16.     class procedure AddUInt16(const left, right; var Result); inline;
  17.     class procedure AddInt32(const left, right; var Result); inline;
  18.     class procedure AddUInt32(const left, right; var Result); inline;
  19.     class procedure AddInt64(const left, right; var Result); inline;
  20.     class procedure AddUInt64(const left, right; var Result); inline;
  21.  
  22.     class procedure AddSingle(const left, right; var Result); inline;
  23.     class procedure AddDouble(const left, right; var Result); inline;
  24.     class procedure AddCurrency(const left, right; var Result); inline;
  25.     class procedure AddComp(const left, right; var Result); inline;
  26.     class procedure AddExtended(const left, right; var Result); inline;
  27.  
  28.     class procedure AddVariant(const left, right; var Result); inline;
  29.     class procedure AddUnicodeString(const left, right; var Result); inline;
  30.   public
  31.     class function Add<T>(const left, right: T): T; inline;
  32.   end;
  33.  
  34. implementation
  35.  
  36. uses
  37.   SysUtils;
  38.  
  39. class procedure Operators.AddInt8(const left, right; var Result);
  40. begin
  41.   ShortInt(Result) := ShortInt(left) + ShortInt(right);
  42. end;
  43.  
  44. class procedure Operators.AddInt16(const left, right; var Result);
  45. begin
  46.   SmallInt(Result) := SmallInt(left) + SmallInt(right);
  47. end;
  48.  
  49. class procedure Operators.AddInt32(const left, right; var Result);
  50. begin
  51.   Integer(Result) := Integer(left) + Integer(right);
  52. end;
  53.  
  54. class procedure Operators.AddInt64(const left, right; var Result);
  55. begin
  56.   Int64(Result) := Int64(left) + Int64(right);
  57. end;
  58.  
  59. class procedure Operators.AddUInt8(const left, right; var Result);
  60. begin
  61.   Byte(Result) := Byte(left) + Byte(right);
  62. end;
  63.  
  64. class procedure Operators.AddUInt16(const left, right; var Result);
  65. begin
  66.   Word(Result) := Word(left) + Word(right);
  67. end;
  68.  
  69. class procedure Operators.AddUInt32(const left, right; var Result);
  70. begin
  71.   Cardinal(Result) := Cardinal(left) + Cardinal(right);
  72. end;
  73.  
  74. class procedure Operators.AddUInt64(const left, right; var Result);
  75. begin
  76.   UInt64(Result) := UInt64(left) + UInt64(right);
  77. end;
  78.  
  79. class procedure Operators.AddSingle(const left, right; var Result);
  80. begin
  81.   Single(Result) := Single(left) + Single(right);
  82. end;
  83.  
  84. class procedure Operators.AddDouble(const left, right; var Result);
  85. begin
  86.   Double(Result) := Double(left) + Double(right);
  87. end;
  88.  
  89. class procedure Operators.AddCurrency(const left, right; var Result);
  90. begin
  91.   Currency(Result) := Currency(left) + Currency(right);
  92. end;
  93.  
  94. class procedure Operators.AddComp(const left, right; var Result);
  95. begin
  96.   Comp(Result) := Comp(left) + Comp(right);
  97. end;
  98.  
  99. class procedure Operators.AddExtended(const left, right; var Result);
  100. begin
  101.   Extended(Result) := Extended(left) + Extended(right);
  102. end;
  103.  
  104. class procedure Operators.AddVariant(const left, right; var Result);
  105. begin
  106.   Variant(Result) := Variant(left) + Variant(right);
  107. end;
  108.  
  109. class procedure Operators.AddUnicodeString(const left, right; var Result);
  110. begin
  111.   UnicodeString(Result) := UnicodeString(left) + UnicodeString(right);
  112. end;
  113.  
  114. class procedure Operators.RaiseNotSupported(typeInfo: PTypeInfo);
  115. begin
  116.   raise ENotSupportedException.CreateFmt('Operation not supported for type %s', [typeInfo.Name]);
  117. end;
  118.  
  119. class function Operators.Add<T>(const left, right: T): T;
  120. begin
  121.   case GetTypeKind(T) of
  122.     tkInteger:
  123.       case SizeOf(T) of
  124.         1:
  125.           if (TypeInfo(T) = TypeInfo(ShortInt)) then
  126.             AddInt8(left, right, Result)
  127.           else
  128.             AddUInt8(left, right, Result);
  129.         2:
  130.           if (TypeInfo(T) = TypeInfo(SmallInt)) then
  131.             AddInt16(left, right, Result)
  132.           else
  133.             AddUInt16(left, right, Result);
  134.         4:
  135.           if (TypeInfo(T) = TypeInfo(Integer))
  136.             or (TypeInfo(T) = TypeInfo(NativeInt)) then
  137.             AddInt32(left, right, Result);
  138.           else
  139.             AddUInt32(left, right, Result);
  140.       end;
  141.     tkFloat:
  142.       case SizeOf(T) of
  143.         4: AddSingle(left, right, Result);
  144.         8:
  145.           if TypeInfo(T) = TypeInfo(Currency) then
  146.             AddCurrency(left, right, Result)
  147.           else if TypeInfo(T) = TypeInfo(Comp) then
  148.             AddComp(left, right, Result)
  149.           else
  150.             AddDouble(left, right, Result);
  151.         10: AddExtended(left, right, Result);
  152.       end;
  153. //    tkString:
  154. //    tkSet:
  155. //    tkLString:
  156. //    tkWString:
  157.     tkVariant: AddVariant(left, right, Result);
  158. //    tkArray:
  159. //    tkRecord:
  160.     tkInt64:
  161.       if (TypeInfo(T) = TypeInfo(Int64))
  162.         or (TypeInfo(T) = TypeInfo(NativeInt)) then
  163.         AddInt64(left, right, Result)
  164.       else
  165.         AddUInt64(left, right, Result);
  166. //    tkDynArray: System._DynArrayCat3(Result, left, right);
  167.     tkUString: AddUnicodeString(left, right, Result);
  168.   else
  169.     RaiseNotSupported(TypeInfo(T));
  170.   end;
  171. end;
  172.  
  173. end.
Advertisement
RAW Paste Data Copied
Advertisement