1.10. Binding Classes

C++ classes are bound using the cclass construction.
Start felix section to tut/examples/tut_bind160.flx[1 /1 ]
     1: #line 629 "./lpsrc/flx_tut_bind.pak"
     2: #import <flx.flxh>
     3: open Long;
     4: 
     5: header """#include <stdio.h>""";
     6: 
     7: header """
     8: struct Y {
     9:   int c;
    10:   int x;
    11:   long y;
    12:   Y() : c(-1) {}
    13:   Y(int _x) : c(-50), x(_x) {}
    14:   int h()const { return x + y + c; }
    15:   int f(int z)const { return z + x + y; }
    16:   int g(int z,int a)const { return z + x + y+a; }
    17: 
    18:   void setx(int _x){ x= _x;}
    19:   void setxy(int _x, long _y) {x = _x; y= _y;}
    20:   void yprint() { printf("Y=%d,%ld\\n",x,y); }
    21: };
    22: """;
    23: 
    24: cclass Y {
    25:   val c : int;
    26:   var x : int;
    27:   var y : long;
    28:   fun h: unit -> int;
    29:   fun f: int -> int;
    30:   fun g: int * int -> int;
    31:   proc setx: int;
    32:   proc setxy: int * long;
    33:   proc yprint: unit;
    34:   proc setc: int = "$1->c = $2;";
    35:   ctor : unit;
    36:   ctor : int;
    37: };
    38: 
    39: var b = Y(99);
    40: b.x = 2;
    41: b.y = 3L;
    42: print b.c; endl;
    43: print b.x; endl;
    44: print b.y; endl;
    45: 
    46: print$ b.f 100; endl;
    47: print$ b.g$ 100,1000; endl;
    48: b.yprint ();
    49: 
    50: b.setx 20;
    51: print b.x; endl;
    52: print b.y; endl;
    53: 
    54: b.setxy$ 20,42L;
    55: b.setc 10000;
    56: print b.x; endl;
    57: print b.y; endl;
    58: 
    59: print$ b.h (); endl;
    60: print$ b.f 100; endl;
    61: print$ b.g$ 100,1000; endl;
    62: 
    63: anEff := b.f;
    64: print$ anEff 100; endl;
    65: 
    66: header """
    67: template <class T>
    68: struct V
    69: {
    70:   T x;
    71:   T get()const { return x; }
    72:   void sset(T _x) { x = _x; }
    73: };
    74: """;
    75: 
    76: cclass V[t] {
    77:   fun get: unit -> t;
    78:   proc sset: t;
    79:   ctor : unit;
    80: };
    81: 
    82: v := V[int]();
    83: 
    84: v.sset 1;
    85: print (v.get ()); endl;
    86: 
    87: header """
    88: template<class T> struct X { T x; };
    89: """;
    90: 
    91: cstruct X[t] { x : t; }
    92: xx := X(1);
    93: print$ xx.x; endl;
    94: 
    95: 
    96: header """
    97: struct Z {
    98:   template<class T> T sum(T a, T b)const { return a + b; }
    99: };
   100: """;
   101: 
   102: cclass Z {
   103:   fun sum[t]: t * t -> t;
   104:   ctor : unit;
   105: };
   106: 
   107: var z  = Z();
   108: print$ z.sum[int] (12,13); endl; // 25
   109: 
End felix section to tut/examples/tut_bind160.flx[1]