Ir al contenido


Foto

Aporte Sobrecarga de Operadores de tipo de datos más comunes en Delphi


  • Por favor identifícate para responder
7 respuestas en este tema

#1 angelholberg

angelholberg

    Newbie

  • Miembros
  • Pip
  • 6 mensajes

Escrito 20 octubre 2011 - 09:22


Muy bien saludos a todos los del foro, hoy inicio mis labores haciendo esta pequeña publicación con respecto a la sobrecarga de operadores para los tipos de datos más comunes en Delphi, a continuación expongo mi pequeña librería, espero sus comentarios y si a alguien le es de utilidad este mi aporte a la comunidad espero me lo hagan saber.




delphi
  1. unit TiposDeDatos;
  2. interface
  3. uses
  4. SysUtils, Variants, Classes, StdCtrls, Generics.Collections,JclHashMaps, JclContainerIntf;
  5.  
  6.  
  7. resourcestring
  8. sCantConvertNil = 'No puede contener nil';
  9. sOnlyValidValueIsNil = ' El único valor válido es nil ';
  10. sOnlyValidOperands = ' Los operandos no pueden ser nil ';
  11. sDivideByZero = ' No se puede realizar una división en 0 ';
  12.  
  13.  
  14. type
  15.   WInteger = record
  16.   private
  17.     FValue: Integer;
  18.     FIsNil: Boolean;
  19.   public
  20.     class operator Implicit(const Value: Integer): WInteger;
  21.     class operator Implicit(Value: Pointer): WInteger;
  22.     class operator Implicit(const Value: WInteger): Integer;
  23.     class operator Add( op1, op2 : WInteger ) : WInteger;
  24.     class operator Subtract( op1, op2 : WInteger ) : WInteger;
  25.     class operator Multiply( op1, op2 : WInteger ) : WInteger;
  26.     class operator Divide( op1, op2 : WInteger ) : WInteger;
  27.     class operator LessThan( op1, op2 : WInteger ) : boolean;
  28.     class operator GreaterThan( op1, op2 : WInteger ) : boolean;
  29.     class operator NotEqual( op1, op2 : WInteger ) : boolean;
  30.     class operator Equal( op1, op2 : WInteger ) : boolean;
  31.     class operator GreaterThanOrEqual( op1, op2 : WInteger ) : boolean;
  32.     class operator LessThanOrEqual( op1, op2 : WInteger ) : boolean;
  33.     class operator Negative( op1 : WInteger ) : WInteger;
  34.     function  ToStr( ): String;
  35.     property IsNil: Boolean read FIsNil;
  36.     property Value: Integer read FValue;
  37.   end;
  38.  
  39.  
  40.   WDouble = record
  41.   private
  42.     FValue: Double;
  43.     FIsNil: Boolean;
  44.   public
  45.     class operator Implicit(const Value: Double): WDouble;
  46.     class operator Implicit(Value: Pointer): WDouble;
  47.     class operator Implicit(const Value: WDouble): Double;
  48.     class operator Add( op1, op2 : WDouble ) : WDouble;
  49.     class operator Subtract( op1, op2 : WDouble ) : WDouble;
  50.     class operator Multiply( op1, op2 : WDouble ) : WDouble;
  51.     class operator Divide( op1, op2 : WDouble ) : WDouble;
  52.     class operator LessThan( op1, op2 : WDouble ) : boolean;
  53.     class operator GreaterThan( op1, op2 : WDouble ) : boolean;
  54.     class operator NotEqual( op1, op2 : WDouble ) : boolean;
  55.     class operator Equal( op1, op2 : WDouble ) : boolean;
  56.     class operator GreaterThanOrEqual( op1, op2 : WDouble ) : boolean;
  57.     class operator LessThanOrEqual( op1, op2 : WDouble ) : boolean;
  58.     class operator Negative( op1 : WDouble ) : WDouble;
  59.     function  ToStr( ): String;
  60.     property IsNil: Boolean read FIsNil;
  61.     property Value: Double read FValue;
  62.   end;
  63.  
  64.  
  65.   WChar = record
  66.   private
  67.     FValue: Char;
  68.     FIsNil: Boolean;
  69.   public
  70.     class operator Implicit(const Value: Char): WChar;
  71.     class operator Implicit(Value: Pointer): WChar;
  72.     class operator Implicit(const Value: WChar): Char;
  73.     class operator Add( op1, op2 : WChar ) : String;
  74.     class operator Multiply( op1: WChar; op2 : Integer ) : String;
  75.     class operator LessThan( op1, op2 : WChar ) : boolean;
  76.     class operator GreaterThan( op1, op2 : WChar ) : boolean;
  77.     class operator NotEqual( op1, op2 : WChar ) : boolean;
  78.     class operator Equal( op1, op2 : WChar ) : boolean;
  79.     class operator GreaterThanOrEqual( op1, op2 : WChar ) : boolean;
  80.     class operator LessThanOrEqual( op1, op2 : WChar ) : boolean;
  81.     function quotedString(): String;
  82.     property IsNil: Boolean read FIsNil;
  83.     property Value: Char read FValue;
  84.   end;
  85.  
  86.  
  87.   WString = record
  88.   private
  89.     FValue: String;
  90.     FIsNil: Boolean;
  91.   public
  92.     class operator Implicit(const Value: String): WString;
  93.     class operator Implicit(Value: Pointer): WString;
  94.     class operator Implicit(const Value: WString): String;
  95.     class operator Add( op1, op2 : WString ) : String;
  96.     class operator Multiply( op1: WString; op2 : Integer ) : String;
  97.     class operator LessThan( op1, op2 : WString ) : boolean;
  98.     class operator GreaterThan( op1, op2 : WString ) : boolean;
  99.     class operator NotEqual( op1, op2 : WString ) : boolean;
  100.     class operator Equal( op1, op2 : WString ) : boolean;
  101.     class operator GreaterThanOrEqual( op1, op2 : WString ) : boolean;
  102.     class operator LessThanOrEqual( op1, op2 : WString ) : boolean;
  103.  
  104.  
  105.     function replace( const oldPattern : WString ; const newPattern: WString ): WString;
  106.     function size( ): WInteger;
  107.     function isEmpty() : boolean;
  108.     function quotedString(): WString;
  109.     function lastDelimiterOf(  s : String ):Integer;
  110.     function deleteChars( index : Integer ; n : Integer ): WString;
  111.     property IsNil: Boolean read FIsNil;
  112.     property Value: String read FValue;
  113.   end;
  114.  
  115.  
  116.   WDateTime = record
  117.   private
  118.     FValue: TDateTime;
  119.     FIsNil: Boolean;
  120.   public
  121.     class operator Implicit(const Value: TDateTime): WDateTime;
  122.     class operator Implicit(Value: Pointer): WDateTime;
  123.     class operator Implicit(const Value: WDateTime): TDateTime;
  124.     class operator Add( op1, op2 : WDateTime ) : WDateTime;
  125.     class operator Subtract( op1, op2 : WDateTime ) : WDateTime;
  126.     class operator Multiply( op1, op2 : WDateTime ) : WDateTime;
  127.     class operator Divide( op1, op2 : WDateTime ) : WDateTime;
  128.     class operator LessThan( op1, op2 : WDateTime ) : boolean;
  129.     class operator GreaterThan( op1, op2 : WDateTime ) : boolean;
  130.     class operator NotEqual( op1, op2 : WDateTime ) : boolean;
  131.     class operator Equal( op1, op2 : WDateTime ) : boolean;
  132.     class operator GreaterThanOrEqual( op1, op2 : WDateTime ) : boolean;
  133.     class operator LessThanOrEqual( op1, op2 : WDateTime ) : boolean;
  134.     class operator Negative( op1 : WDateTime ) : WDateTime;
  135.     function  ToStr( ): String; overload;
  136.     function  ToStr( pattern : String ): String; overload;
  137.  
  138.  
  139.     property IsNil: Boolean read FIsNil;
  140.     property Value: TDateTime read FValue;
  141.   end;
  142.  
  143.  
  144. implementation
  145.  
  146.  
  147.   class operator WInteger.Implicit(const Value: Integer): WInteger;
  148.   begin
  149.     Result.FValue := Value;
  150.     Result.FIsNil := False;
  151.   end;
  152.  
  153.  
  154.   class operator WInteger.Implicit(Value: Pointer): WInteger;
  155.   begin
  156.     Assert(Value = nil, sOnlyValidValueIsNil);
  157.     Result.FIsNil := True;
  158.   end;
  159.  
  160.  
  161.   class operator WInteger.Implicit(const Value: WInteger): Integer;
  162.   begin
  163.     if Value.IsNil then
  164.       raise EConvertError.Create(sCantConvertNil);
  165.     Result := Value.FValue;
  166.   end;
  167.  
  168.  
  169.   class operator WInteger.Add( op1, op2 : WInteger ) : WInteger;
  170.   begin
  171.     if op1.IsNil  or op2.IsNil then
  172.         raise EConvertError.Create(sOnlyValidOperands);
  173.     Result := op1.FValue + op2.FValue;
  174.   end;
  175.  
  176.  
  177.   class operator WInteger.Subtract( op1, op2 : WInteger ) : WInteger;
  178.   begin
  179.     if op1.IsNil  or op2.IsNil then
  180.         raise EConvertError.Create(sOnlyValidOperands);
  181.     Result := op1.FValue - op2.FValue;
  182.   end;
  183.  
  184.  
  185.   class operator WInteger.Multiply( op1, op2 : WInteger ) : WInteger;
  186.   begin
  187.     if op1.IsNil  or op2.IsNil then
  188.         raise EConvertError.Create(sOnlyValidOperands);
  189.     Result := op1.FValue * op2.FValue;
  190.   end;
  191.  
  192.  
  193.   class operator WInteger.Divide( op1, op2 : WInteger ) : WInteger;
  194.   begin
  195.     if op1.IsNil  or op2.IsNil then
  196.         raise EConvertError.Create(sOnlyValidOperands);
  197.     Result := op1.FValue div op2.FValue;
  198.   end;
  199.  
  200.  
  201.   class operator WInteger.LessThan( op1, op2 : WInteger ) : boolean;
  202.   begin
  203.     if op1.IsNil  or op2.IsNil then
  204.         raise EConvertError.Create(sOnlyValidOperands);
  205.     Result := op1.FValue < op2.FValue;
  206.   end;
  207.  
  208.  
  209.   class operator WInteger.GreaterThan( op1, op2 : WInteger ) : boolean;
  210.   begin
  211.     if op1.IsNil  or op2.IsNil then
  212.         raise EConvertError.Create(sOnlyValidOperands);
  213.     Result := op1.FValue > op2.FValue;
  214.   end;
  215.  
  216.  
  217.   class operator WInteger.LessThanOrEqual( op1, op2 : WInteger ) : boolean;
  218.   begin
  219.     if op1.IsNil  or op2.IsNil then
  220.         raise EConvertError.Create(sOnlyValidOperands);
  221.     Result := op1.FValue <= op2.FValue;
  222.   end;
  223.  
  224.  
  225.   class operator WInteger.GreaterThanOrEqual( op1, op2 : WInteger ) : boolean;
  226.   begin
  227.     if op1.IsNil  or op2.IsNil then
  228.         raise EConvertError.Create(sOnlyValidOperands);
  229.     Result := op1.FValue >= op2.FValue;
  230.   end;
  231.  
  232.  
  233.   class operator WInteger.Equal( op1, op2 : WInteger ) : boolean;
  234.   begin
  235.     if op1.IsNil and  op2.IsNil then
  236.           Result := True
  237.     else if op1.IsNil or op2.IsNil then
  238.           Result := False
  239.     else
  240.         Result := op1.FValue = op2.FValue;
  241.   end;
  242.  
  243.  
  244.   class operator WInteger.NotEqual( op1, op2 : WInteger ) : boolean;
  245.   begin
  246.     if op1.IsNil and  op2.IsNil then
  247.           Result := False
  248.     else if op1.IsNil or op2.IsNil then
  249.           Result := True
  250.     else
  251.         Result := op1.FValue <> op2.FValue;
  252.   end;
  253.  
  254.  
  255.   class operator WInteger.Negative( op1 : WInteger ) : WInteger;
  256.   begin
  257.     if op1.IsNil then
  258.         raise EConvertError.Create(sOnlyValidOperands);
  259.     Result := - op1.FValue;
  260.   end;
  261.  
  262.  
  263.   function WInteger.toStr() : String;
  264.   begin
  265.     if self.IsNil then
  266.       Result := ''
  267.     else
  268.       Result := InttoStr(self.FValue);
  269.   end;
  270.  
  271.  
  272.   class operator WDouble.Implicit(const Value: Double): WDouble;
  273.   begin
  274.     Result.FValue := Value;
  275.     Result.FIsNil := False;
  276.   end;
  277.  
  278.  
  279.   class operator WDouble.Implicit(Value: Pointer): WDouble;
  280.   begin
  281.     Assert(Value = nil, sOnlyValidValueIsNil);
  282.     Result.FIsNil := True;
  283.   end;
  284.  
  285.  
  286.   class operator WDouble.Implicit(const Value: WDouble): Double;
  287.   begin
  288.     if Value.IsNil then
  289.       raise EConvertError.Create(sCantConvertNil);
  290.     Result := Value.FValue;
  291.   end;
  292.  
  293.  
  294.   class operator WDouble.Add( op1, op2 : WDouble ) : WDouble;
  295.   begin
  296.     if op1.IsNil  or op2.IsNil then
  297.         raise EConvertError.Create(sOnlyValidOperands);
  298.     Result := op1.FValue + op2.FValue;
  299.   end;
  300.  
  301.  
  302.   class operator WDouble.Subtract( op1, op2 : WDouble ) : WDouble;
  303.   begin
  304.     if op1.IsNil  or op2.IsNil then
  305.         raise EConvertError.Create(sOnlyValidOperands);
  306.     Result := op1.FValue - op2.FValue;
  307.   end;
  308.  
  309.  
  310.   class operator WDouble.Multiply( op1, op2 : WDouble ) : WDouble;
  311.   begin
  312.     if op1.IsNil  or op2.IsNil then
  313.         raise EConvertError.Create(sOnlyValidOperands);
  314.     Result := op1.FValue * op2.FValue;
  315.   end;
  316.  
  317.  
  318.   class operator WDouble.Divide( op1, op2 : WDouble ) : WDouble;
  319.   begin
  320.     if op1.IsNil  or op2.IsNil then
  321.         raise EConvertError.Create(sOnlyValidOperands);
  322.     Result := op1.FValue / op2.FValue;
  323.   end;
  324.  
  325.  
  326.   class operator WDouble.LessThan( op1, op2 : WDouble ) : boolean;
  327.   begin
  328.     if op1.IsNil  or op2.IsNil then
  329.         raise EConvertError.Create(sOnlyValidOperands);
  330.     Result := op1.FValue < op2.FValue;
  331.   end;
  332.  
  333.  
  334.   class operator WDouble.GreaterThan( op1, op2 : WDouble ) : boolean;
  335.   begin
  336.     if op1.IsNil  or op2.IsNil then
  337.         raise EConvertError.Create(sOnlyValidOperands);
  338.     Result := op1.FValue > op2.FValue;
  339.   end;
  340.  
  341.  
  342.   class operator WDouble.LessThanOrEqual( op1, op2 : WDouble ) : boolean;
  343.   begin
  344.     if op1.IsNil  or op2.IsNil then
  345.         raise EConvertError.Create(sOnlyValidOperands);
  346.     Result := op1.FValue <= op2.FValue;
  347.   end;
  348.  
  349.  
  350.   class operator WDouble.GreaterThanOrEqual( op1, op2 : WDouble ) : boolean;
  351.   begin
  352.     if op1.IsNil  or op2.IsNil then
  353.         raise EConvertError.Create(sOnlyValidOperands);
  354.     Result := op1.FValue >= op2.FValue;
  355.   end;
  356.  
  357.  
  358.   class operator WDouble.Equal( op1, op2 : WDouble ) : boolean;
  359.   begin
  360.     if op1.IsNil and  op2.IsNil then
  361.           Result := True
  362.     else if op1.IsNil or op2.IsNil then
  363.           Result := False
  364.     else
  365.         Result := op1.FValue = op2.FValue;
  366.   end;
  367.  
  368.  
  369.   class operator WDouble.NotEqual( op1, op2 : WDouble ) : boolean;
  370.   begin
  371.     if op1.IsNil and  op2.IsNil then
  372.           Result := False
  373.     else if op1.IsNil or op2.IsNil then
  374.           Result := True
  375.     else
  376.         Result := op1.FValue <> op2.FValue;
  377.   end;
  378.  
  379.  
  380.   class operator WDouble.Negative( op1 : WDouble ) : WDouble;
  381.   begin
  382.     if op1.IsNil then
  383.         raise EConvertError.Create(sOnlyValidOperands);
  384.     Result := - op1.FValue;
  385.   end;
  386.  
  387.  
  388.   function WDouble.toStr() : String;
  389.   begin
  390.     if self.IsNil then
  391.       Result := ''
  392.     else
  393.       Result := FloattoStr(self.FValue);
  394.   end;
  395.  
  396.  
  397.   class operator WChar.Implicit(const Value: Char): WChar;
  398.   begin
  399.     Result.FValue := Value;
  400.     Result.FIsNil := False;
  401.   end;
  402.  
  403.  
  404.   class operator WChar.Implicit(Value: Pointer): WChar;
  405.   begin
  406.     Assert(Value = nil, sOnlyValidValueIsNil);
  407.     Result.FIsNil := True;
  408.   end;
  409.  
  410.  
  411.   class operator WChar.Implicit(const Value: WChar): Char;
  412.   begin
  413.     if Value.IsNil then
  414.       raise EConvertError.Create(sCantConvertNil);
  415.     Result := Value.FValue;
  416.   end;
  417.  
  418.  
  419.   class operator WChar.Add( op1, op2 : WChar ) : String;
  420.   begin
  421.     if op1.IsNil  or op2.IsNil then
  422.         raise EConvertError.Create(sOnlyValidOperands);
  423.     Result := op1.FValue + op2.FValue;
  424.   end;
  425.  
  426.  
  427.   class operator WChar.Multiply( op1: WChar ; op2 : Integer  ) : String;
  428.   var
  429.     I: Integer;
  430.     temp : String;
  431.   begin
  432.     if op1.IsNil then
  433.         raise EConvertError.Create(sOnlyValidOperands);
  434.     temp := '';
  435.     for I := 0 to op2 - 1 do
  436.         temp  :=  temp + op1.FValue;
  437.     Result := temp;
  438.   end;
  439.  
  440.  
  441.   class operator WChar.LessThan( op1, op2 : WChar ) : boolean;
  442.   begin
  443.     if op1.IsNil  or op2.IsNil then
  444.         raise EConvertError.Create(sOnlyValidOperands);
  445.     Result := op1.FValue < op2.FValue;
  446.   end;
  447.  
  448.  
  449.   class operator WChar.GreaterThan( op1, op2 : WChar ) : boolean;
  450.   begin
  451.     if op1.IsNil  or op2.IsNil then
  452.         raise EConvertError.Create(sOnlyValidOperands);
  453.     Result := op1.FValue > op2.FValue;
  454.   end;
  455.  
  456.  
  457.   class operator WChar.LessThanOrEqual( op1, op2 : WChar ) : boolean;
  458.   begin
  459.     if op1.IsNil  or op2.IsNil then
  460.         raise EConvertError.Create(sOnlyValidOperands);
  461.     Result := op1.FValue <= op2.FValue;
  462.   end;
  463.  
  464.  
  465.   class operator WChar.GreaterThanOrEqual( op1, op2 : WChar ) : boolean;
  466.   begin
  467.     if op1.IsNil  or op2.IsNil then
  468.         raise EConvertError.Create(sOnlyValidOperands);
  469.     Result := op1.FValue >= op2.FValue;
  470.   end;
  471.  
  472.  
  473.   class operator WChar.Equal( op1, op2 : WChar ) : boolean;
  474.   begin
  475.     if op1.IsNil and  op2.IsNil then
  476.           Result := True
  477.     else if op1.IsNil or op2.IsNil then
  478.           Result := False
  479.     else
  480.         Result := op1.FValue = op2.FValue;
  481.   end;
  482.  
  483.  
  484.   class operator WChar.NotEqual( op1, op2 : WChar ) : boolean;
  485.   begin
  486.     if op1.IsNil and  op2.IsNil then
  487.           Result := False
  488.     else if op1.IsNil or op2.IsNil then
  489.           Result := True
  490.     else
  491.         Result := op1.FValue <> op2.FValue;
  492.   end;
  493.  
  494.  
  495.   function WChar.quotedString(): String;
  496.   begin
  497.     Result := QuotedStr( FValue );
  498.   end;
  499.  
  500.  
  501.   class operator WString.Implicit(const Value: String): WString;
  502.   begin
  503.     Result.FValue := Value;
  504.     Result.FIsNil := False;
  505.   end;
  506.  
  507.  
  508.   class operator WString.Implicit(Value: Pointer): WString;
  509.   begin
  510.     Assert(Value = nil, sOnlyValidValueIsNil);
  511.     Result.FIsNil := True;
  512.   end;
  513.  
  514.  
  515.   class operator WString.Implicit(const Value: WString): String;
  516.   begin
  517.     if Value.IsNil then
  518.       raise EConvertError.Create(sCantConvertNil);
  519.     Result := Value.FValue;
  520.   end;
  521.  
  522.  
  523.   class operator WString.Add( op1, op2 : WString ) : String;
  524.   begin
  525.     if op1.IsNil  or op2.IsNil then
  526.         raise EConvertError.Create(sOnlyValidOperands);
  527.     Result := op1.FValue + op2.FValue;
  528.   end;
  529.  
  530.  
  531.   class operator WString.Multiply( op1: WString ; op2 : Integer  ) : String;
  532.   var
  533.     I: Integer;
  534.     temp : String;
  535.   begin
  536.     if op1.IsNil then
  537.         raise EConvertError.Create(sOnlyValidOperands);
  538.     temp := '';
  539.     for I := 0 to op2 - 1 do
  540.         temp  :=  temp + op1.FValue;
  541.     Result := temp;
  542.   end;
  543.  
  544.  
  545.   class operator WString.LessThan( op1, op2 : WString ) : boolean;
  546.   begin
  547.     if op1.IsNil  or op2.IsNil then
  548.         raise EConvertError.Create(sOnlyValidOperands);
  549.     Result := op1.FValue < op2.FValue;
  550.   end;
  551.  
  552.  
  553.   class operator WString.GreaterThan( op1, op2 : WString ) : boolean;
  554.   begin
  555.     if op1.IsNil  or op2.IsNil then
  556.         raise EConvertError.Create(sOnlyValidOperands);
  557.     Result := op1.FValue > op2.FValue;
  558.   end;
  559.  
  560.  
  561.   class operator WString.LessThanOrEqual( op1, op2 : WString ) : boolean;
  562.   begin
  563.     if op1.IsNil  or op2.IsNil then
  564.         raise EConvertError.Create(sOnlyValidOperands);
  565.     Result := op1.FValue <= op2.FValue;
  566.   end;
  567.  
  568.  
  569.   class operator WString.GreaterThanOrEqual( op1, op2 : WString ) : boolean;
  570.   begin
  571.     if op1.IsNil  or op2.IsNil then
  572.         raise EConvertError.Create(sOnlyValidOperands);
  573.     Result := op1.FValue >= op2.FValue;
  574.   end;
  575.  
  576.  
  577.   class operator WString.Equal( op1, op2 : WString ) : boolean;
  578.   begin
  579.     if op1.IsNil and  op2.IsNil then
  580.           Result := True
  581.     else if op1.IsNil or op2.IsNil then
  582.           Result := False
  583.     else
  584.         Result := op1.FValue = op2.FValue;
  585.   end;
  586.  
  587.  
  588.   class operator WString.NotEqual( op1, op2 : WString ) : boolean;
  589.   begin
  590.     if op1.IsNil and  op2.IsNil then
  591.           Result := False
  592.     else if op1.IsNil or op2.IsNil then
  593.           Result := True
  594.     else
  595.         Result := op1.FValue <> op2.FValue;
  596.   end;
  597.  
  598.  
  599. function WString.replace( const oldPattern : WString ; const newPattern: WString ): WString;
  600. begin
  601.       if IsNil then
  602.         raise EConvertError.Create(sOnlyValidOperands);
  603.     Result := StringReplace(FValue, oldPattern , newPattern, [rfReplaceAll, rfIgnoreCase] );
  604. end;
  605.  
  606.  
  607. function WString.size():WInteger;
  608. begin
  609.       if IsNil then
  610.         raise EConvertError.Create(sOnlyValidOperands);
  611.       Result := Length(FValue);
  612. end;
  613.  
  614.  
  615.   function WString.isEmpty():boolean;
  616. begin
  617.       if IsNil then
  618.         raise EConvertError.Create(sOnlyValidOperands);
  619.       if ( size <= 0 ) then
  620.         Result := True
  621.       else
  622.         Result := False;
  623. end;
  624.  
  625.  
  626. function WString.quotedString(): WString;
  627. begin
  628.     Result := QuotedStr( FValue );
  629. end;
  630.  
  631.  
  632. function WString.lastDelimiterOf(  s : String ):Integer;
  633. begin
  634.     if not IsNil then
  635.       Result := LastDelimiter( s, FValue )
  636.     else
  637.       Result := 0;
  638. end;
  639.  
  640.  
  641.   function WString.deleteChars( index : Integer ; n : Integer ): WString;
  642.   var
  643.     temp : String;
  644.   begin
  645.     if not IsNil then
  646.     begin
  647.         temp := FValue;
  648.         Delete( temp , index, n);
  649.         Result := temp;
  650.     end
  651.     else
  652.         Result := '';
  653.   end;
  654.  
  655.  
  656.   class operator WDateTime.Implicit(const Value: TDateTime): WDateTime;
  657.   begin
  658.     Result.FValue := Value;
  659.     Result.FIsNil := False;
  660.   end;
  661.  
  662.  
  663.   class operator WDateTime.Implicit(Value: Pointer): WDateTime;
  664.   begin
  665.     Assert(Value = nil, sOnlyValidValueIsNil);
  666.     Result.FIsNil := True;
  667.   end;
  668.  
  669.  
  670.   class operator WDateTime.Implicit(const Value: WDateTime): TDateTime;
  671.   begin
  672.     if Value.IsNil then
  673.       raise EConvertError.Create(sCantConvertNil);
  674.     Result := Value.FValue;
  675.   end;
  676.  
  677.  
  678.   class operator WDateTime.Add( op1, op2 : WDateTime ) : WDateTime;
  679.   begin
  680.     if op1.IsNil  or op2.IsNil then
  681.         raise EConvertError.Create(sOnlyValidOperands);
  682.     Result := op1.FValue + op2.FValue;
  683.   end;
  684.  
  685.  
  686.   class operator WDateTime.Subtract( op1, op2 : WDateTime ) : WDateTime;
  687.   begin
  688.     if op1.IsNil  or op2.IsNil then
  689.         raise EConvertError.Create(sOnlyValidOperands);
  690.     Result := op1.FValue - op2.FValue;
  691.   end;
  692.  
  693.  
  694.   class operator WDateTime.Multiply( op1, op2 : WDateTime ) : WDateTime;
  695.   begin
  696.     if op1.IsNil  or op2.IsNil then
  697.         raise EConvertError.Create(sOnlyValidOperands);
  698.     Result := op1.FValue * op2.FValue;
  699.   end;
  700.  
  701.  
  702.   class operator WDateTime.Divide( op1, op2 : WDateTime ) : WDateTime;
  703.   begin
  704.     if op1.IsNil  or op2.IsNil then
  705.         raise EConvertError.Create(sOnlyValidOperands);
  706.     Result := op1.FValue / op2.FValue;
  707.   end;
  708.  
  709.  
  710.   class operator WDateTime.LessThan( op1, op2 : WDateTime ) : boolean;
  711.   begin
  712.     if op1.IsNil  or op2.IsNil then
  713.         raise EConvertError.Create(sOnlyValidOperands);
  714.     Result := op1.FValue < op2.FValue;
  715.   end;
  716.  
  717.  
  718.   class operator WDateTime.GreaterThan( op1, op2 : WDateTime ) : boolean;
  719.   begin
  720.     if op1.IsNil  or op2.IsNil then
  721.         raise EConvertError.Create(sOnlyValidOperands);
  722.     Result := op1.FValue > op2.FValue;
  723.   end;
  724.  
  725.  
  726.   class operator WDateTime.LessThanOrEqual( op1, op2 : WDateTime ) : boolean;
  727.   begin
  728.     if op1.IsNil  or op2.IsNil then
  729.         raise EConvertError.Create(sOnlyValidOperands);
  730.     Result := op1.FValue <= op2.FValue;
  731.   end;
  732.  
  733.  
  734.   class operator WDateTime.GreaterThanOrEqual( op1, op2 : WDateTime ) : boolean;
  735.   begin
  736.     if op1.IsNil  or op2.IsNil then
  737.         raise EConvertError.Create(sOnlyValidOperands);
  738.     Result := op1.FValue >= op2.FValue;
  739.   end;
  740.  
  741.  
  742.   class operator WDateTime.Equal( op1, op2 : WDateTime ) : boolean;
  743.   begin
  744.     if op1.IsNil and  op2.IsNil then
  745.           Result := True
  746.     else if op1.IsNil or op2.IsNil then
  747.           Result := False
  748.     else
  749.         Result := op1.FValue = op2.FValue;
  750.   end;
  751.  
  752.  
  753.   class operator WDateTime.NotEqual( op1, op2 : WDateTime ) : boolean;
  754.   begin
  755.     if op1.IsNil and  op2.IsNil then
  756.           Result := False
  757.     else if op1.IsNil or op2.IsNil then
  758.           Result := True
  759.     else
  760.         Result := op1.FValue <> op2.FValue;
  761.   end;
  762.  
  763.  
  764.   class operator WDateTime.Negative( op1 : WDateTime ) : WDateTime;
  765.   begin
  766.     if op1.IsNil then
  767.         raise EConvertError.Create(sOnlyValidOperands);
  768.     Result := - op1.FValue;
  769.   end;
  770.  
  771.  
  772.   function WDateTime.toStr() : String;
  773.   begin
  774.     if self.IsNil then
  775.       Result := ''
  776.     else
  777.       Result := DatetoStr(self.FValue);
  778.   end;
  779.  
  780.  
  781.   function WDateTime.toStr( pattern :String ) : String;
  782.   begin
  783.     if self.IsNil then
  784.       Result := ''
  785.     else
  786.       Result := FormatDateTime( pattern, FValue );
  787.   end;
  788. end.


  • 0

#2 poliburro

poliburro

    Advanced Member

  • Administrador
  • 4.945 mensajes
  • LocationMéxico

Escrito 20 octubre 2011 - 11:08

Wow¡¡¡¡¡,. excelente trabajo amigo.  ¿Esto nos permite declarar nuestras variables de un determinado tipo y usarlo como si fuera un objeto?

¿ Algo como lo que se usa en .net?
  • 0

#3 angelholberg

angelholberg

    Newbie

  • Miembros
  • Pip
  • 6 mensajes

Escrito 20 octubre 2011 - 11:25

Es correcto poliburro, esta librería surgió debido a la necesidad de hacer más cosas sobre los tipos de datos simples en Delphi; basándome en como lo hace Java por ejemplo sobrecargar la clase String, entonces básicamente esa fue la idea, por ejemplo se puede hacer con esta librería un

delphi
  1. variable.quotedString

en vez de utilizar un

delphi
  1. quotedstr

para el agregado de apostrofos en una cadena, un

delphi
  1. variable.replace('valorAReemplazar','valor')

en vez de utilizar

delphi
  1. StringReplace

, bueno en fin si alguien puede darle más funcionalidad espero lo podamos trabajar, más adelante publicaré sobre el uso de Hash Maps que en Delphi creo si no me equivoco es un concepto poco común el uso de esta utileria (Listas llave-valor), el uso de XML Data Binding para manipular de una manera más rápida XML's a manera de Java con el uso de JAXB, en fin espero poder subir todos estos aportes.
  • 0

#4 poliburro

poliburro

    Advanced Member

  • Administrador
  • 4.945 mensajes
  • LocationMéxico

Escrito 20 octubre 2011 - 11:35

más adelante publicaré sobre el uso de Hash Maps que en Delphi creo si no me equivoco es un concepto poco común el uso de esta utileria (Listas llave-valor), el uso de XML Data Binding para manipular de una manera más rápida XML's a manera de Java con el uso de JAXB, en fin espero poder subir todos estos aportes.


Pues estaremos esperando con imapciencia tus aportes amigo mio.

Por cierto, yo estoy acostumbrado a procesar más del lado del servidor los bloques XML.  Así que me gustará conocer más sobre Hash Maps
  • 0

#5 Delphius

Delphius

    Advanced Member

  • Administrador
  • 6.295 mensajes
  • LocationArgentina

Escrito 20 octubre 2011 - 05:22

Hola,
Interesante propuesta. No he visto algo así nunca. Yo tengo una pregunta ¿Ese código funciona en cualquier versión de Delphi? Creería que no  :(


Como consejo, o tip: cuando se trata de números flotantes no se debe hacer uso de comparaciones tan directas. No es aplicable cosas como Variable1 = Variable2 debido a la aritmética de punto flotante que no almacena representaciones exactas sino aproximadas.
Lo más adecuado es emplear una tolerancia o margen de error, comunmente llamado epsilon para el error relativo y absoluto. Delphi ya cuenta con funciones de éste tipo, como las SameValue() de la unidad Math.


Saludos,
  • 0

#6 Rolphy Reyes

Rolphy Reyes

    Advanced Member

  • Moderadores
  • PipPipPip
  • 2.092 mensajes
  • LocationRepública Dominicana

Escrito 20 octubre 2011 - 05:56

Hola,
Interesante propuesta. No he visto algo así nunca. Yo tengo una pregunta ¿Ese código funciona en cualquier versión de Delphi? Creería que no  :(
Saludos,


Saludos.

Amigo Delphius, esa funcionalidad fue implementada (si no me equivoco) fue a partir de Delphi 2005, sé que en Delphi 2007 la tengo.

Lo que no tengo es mucha referencia de donde echar mano para entender y utilizar este super recurso. 

¿angelholberg, puedes indicarme algunas referencias?


  • 0

#7 javsolis3

javsolis3

    Advanced Member

  • Moderadores
  • PipPipPip
  • 1.380 mensajes
  • LocationPanama

Escrito 20 octubre 2011 - 07:36

muy excelente trabajo.  :o
  • 0

#8 angelholberg

angelholberg

    Newbie

  • Miembros
  • Pip
  • 6 mensajes

Escrito 21 octubre 2011 - 08:16

Que tal Sr. Rolphy Reyes saludos desde Oaxaca, México, pues bien primero tu comentario "Amigo Delphius, esa funcionalidad fue implementada (si no me equivoco) fue a partir de Delphi 2005, sé que en Delphi 2007 la tengo." según fuentes es correcto a partir de esas versiones fue implementada la parte de sobrecarga de operadores, yo donde realice esta utilería fue en la versión 2009 de Delphi y en cuanto a las referencias solo fueron si no mal recuerdo únicamente dos fuentes donde me base, a continuación paso las ligas:

http://cgarcia.blogs...radores-en.html

y (Para las validaciones dentro de la implementación de cada operación)

http://www.infonegoc.../exception2.htm


Todo lo demás echandole un poco de relación a la poca experiencia que tengo ayudo a realizar todo lo expuesto.
  • 0




IP.Board spam blocked by CleanTalk.