Advertisement
ianharrigan

Untitled

Dec 29th, 2015
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 6.43 KB | None | 0 0
  1. package haxe.ui.util;
  2.  
  3. enum VariantType {
  4.     Int(s:Int);
  5.     Float(s:Float);
  6.     String(s:String);
  7.     Bool(s:Bool);
  8. }
  9.  
  10. abstract Variant(VariantType) from VariantType {
  11.     // ************************************************************************************************************
  12.     // STRINGS
  13.     // ************************************************************************************************************
  14.     @:from static function fromString(s:String):Variant {
  15.         return String(s);
  16.     }
  17.    
  18.     @:to function toString() {
  19.         return switch(this) {
  20.             case String(s): s;
  21.             case Int(s): return Std.string(s);
  22.             case Float(s): return Std.string(s);
  23.             case Bool(s): return Std.string(s);
  24.             default: throw "Variant Type Error";
  25.         }
  26.     }
  27.  
  28.     public var isString(get, never):Bool;
  29.     private function get_isString():Bool {
  30.         return this.match(String(_));
  31.     }
  32.    
  33.     // ************************************************************************************************************
  34.     // INTS
  35.     // ************************************************************************************************************
  36.     @:from static function fromInt(s:Int):Variant {
  37.         return Int(s);
  38.     }
  39.    
  40.     @:to function toInt() {
  41.         return switch(this) {
  42.             case Int(s): return s;
  43.             case Float(s): return Std.int(s);
  44.             default: throw "Variant Type Error";
  45.         }
  46.     }
  47.  
  48.     public var isInt(get, never):Bool;
  49.     private inline function get_isInt():Bool {
  50.         return this.match(Int(_));
  51.     }
  52.    
  53.     // ************************************************************************************************************
  54.     // FLOATS
  55.     // ************************************************************************************************************
  56.     @:from static function fromFloat(s:Float):Variant {
  57.         return Float(s);
  58.     }
  59.    
  60.     @:to function toFloat() {
  61.         return switch(this) {
  62.             case Float(s): return s;
  63.             default: throw "Variant Type Error";
  64.         }
  65.     }
  66.  
  67.     public var isFloat(get, never):Bool;
  68.     private inline function get_isFloat():Bool {
  69.         return this.match(Float(_));
  70.     }
  71.    
  72.     // ************************************************************************************************************
  73.     // NUMBERS
  74.     // ************************************************************************************************************
  75.     public var isNumber(get, never):Bool;
  76.     private inline function get_isNumber():Bool {
  77.         return this.match(Int(_) | Float(_));
  78.     }
  79.    
  80.     function toNumber():Float {
  81.         return switch(this) {
  82.             case Int(s): return s;
  83.             case Float(s): return s;
  84.             default: throw "Variant Type Error";
  85.         }
  86.     }
  87.    
  88.     // ************************************************************************************************************
  89.     // BOOLS
  90.     // ************************************************************************************************************
  91.     @:from static function fromBool(s:Bool):Variant {
  92.         return Bool(s);
  93.     }
  94.    
  95.     @:to function toBool() {
  96.         return switch(this) {
  97.             case Bool(s): return s;
  98.             default: throw "Variant Type Error";
  99.         }
  100.     }
  101.    
  102.     public var isBool(get, never):Bool;
  103.     public function get_isBool():Bool {
  104.         return this.match(Bool(_));
  105.     }
  106.    
  107.     // ************************************************************************************************************
  108.     // OPERATIONS
  109.     // ************************************************************************************************************
  110.     @:op(A + B)
  111.     private function add(rhs:Variant):Variant {
  112.         if (isNumber && rhs.isNumber) {
  113.             return toNumber() + rhs.toNumber();
  114.         } else if (isString && rhs.isString) {
  115.             return toString() + rhs.toString();
  116.         }
  117.  
  118.         throw "Variant operation error";
  119.     }
  120.    
  121.     @:op(A++)
  122.     private inline function postInc():Variant {
  123.         if (isNumber) {
  124.             var v = toNumber();
  125.             v++;
  126.             this = Float(v);
  127.             return v;
  128.         }
  129.         throw "Variant operation error";
  130.     }
  131.    
  132.     @:op(++A)
  133.     private inline function preInc():Variant {
  134.         if (isNumber) {
  135.             var v = toNumber();
  136.             ++v;
  137.             this = Float(v);
  138.             return v;
  139.         }
  140.         throw "Variant operation error";
  141.     }
  142.    
  143.     @:op(A - B)
  144.     private function subtract(rhs:Variant):Variant {
  145.         if (isNumber && rhs.isNumber) {
  146.             return toNumber() - rhs.toNumber();
  147.         } else if (isString && rhs.isString) {
  148.             return StringTools.replace(toString(), rhs.toString(), "");
  149.         }
  150.  
  151.         throw "Variant operation error";
  152.     }
  153.    
  154.     @:op(A--)
  155.     private inline function postDeinc():Variant {
  156.         if (isNumber) {
  157.             var v = toNumber();
  158.             v--;
  159.             this = Float(v);
  160.             return v;
  161.         }
  162.         throw "Variant operation error";
  163.     }
  164.    
  165.     @:op(--A)
  166.     private inline function preDeinc():Variant {
  167.         if (isNumber) {
  168.             var v = toNumber();
  169.             --v;
  170.             this = Float(v);
  171.             return v;
  172.         }
  173.         throw "Variant operation error";
  174.     }
  175.    
  176.     @:op(A * B)
  177.     private function multiply(rhs:Variant):Variant {
  178.         if (isNumber && rhs.isNumber) {
  179.             return toNumber() * rhs.toNumber();
  180.         }
  181.  
  182.         throw "Variant operation error";
  183.     }
  184.    
  185.     @:op(A / B)
  186.     private function divide(rhs:Variant):Variant {
  187.         if (isNumber && rhs.isNumber) {
  188.             return toNumber() / rhs.toNumber();
  189.         }
  190.  
  191.         throw "Variant operation error";
  192.     }
  193.    
  194.     @:op(!A)
  195.     private function invert():Variant {
  196.         if (isBool) {
  197.             var v = toBool();
  198.             v = !v;
  199.             return v;
  200.         }
  201.         throw "Variant operation error";
  202.     }
  203.    
  204.     // ************************************************************************************************************
  205.     // HELPERS
  206.     // ************************************************************************************************************
  207.     public static function fromDynamic(r:Dynamic):Variant {
  208.         var v:Variant = null;
  209.         if (r != null) {
  210.             if (Math.isNaN(Std.parseFloat("" + r)) == false) {
  211.                 if (Std.string(r).indexOf(".") != -1) {
  212.                     v = Std.parseFloat("" + r);
  213.                 } else {
  214.                     v = Std.parseInt("" + r);
  215.                 }
  216.             } else if ((("" + r) == "true" || (r + "") == "false")) {
  217.                 v = (("" + r) == "true");
  218.             } else {
  219.                 v = Std.string(r);
  220.             }
  221.         }
  222.         return v;
  223.     }
  224.    
  225.     public static function toDynamic(v:Variant):Dynamic {
  226.         var d:Dynamic = null;
  227.         if (v != null) {
  228.             switch (v) {
  229.                 case VariantType.Int(y):        d = y;
  230.                 case VariantType.Float(y):      d = y;
  231.                 case VariantType.String(y):     d = y;
  232.                 case VariantType.Bool(y):       d = y;
  233.             }
  234.         }
  235.         return d;
  236.     }
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement