bindings/python: printLn() variadic example
authorJerome St-Louis <jerome@ecere.com>
Sat, 18 Jun 2016 07:21:42 +0000 (03:21 -0400)
committerJerome St-Louis <jerome@ecere.com>
Mon, 21 Nov 2016 14:18:58 +0000 (09:18 -0500)
bindings/python/cffi-ecere.h
bindings/python/pyEcere.py
bindings/python/samples/sample.py

index 9ef461a..c9f19e3 100644 (file)
@@ -22,6 +22,9 @@ void Application_main(Application app);
 
 extern void (*PrintLn)(Class * class_object, const void * object, ...);
 
+extern Class * class_int;
+// extern Class * class_float;
+extern Class * class_double;
 extern Class * class_String;
 extern Class * class_Window;
 extern Class * class_GuiApplication;
index c768f69..e1d04f4 100644 (file)
@@ -1,8 +1,15 @@
 import sys
 from _pyEcere import *
 
-def printLn(args):
-   lib.PrintLn(lib.class_String, args.encode('utf8'), ffi.NULL)
+def convertTypedArgs(args):
+   cargs = ()
+   for a in args:
+      if type(a) == str:   cargs += (lib.class_String, ffi.new("char[]", a.encode('utf8')))
+      if type(a) == int:   cargs += (lib.class_int,    ffi.new("int *", a))
+      if type(a) == float: cargs += (lib.class_double, ffi.new("double *", a))
+   return cargs + (ffi.NULL,)
+
+def printLn(*args): lib.PrintLn(*convertTypedArgs(args))
 
 class Color:
    def __init__(self, r = 0, g = 0, b = 0):
index c1a25d3..d6249a2 100644 (file)
@@ -1,6 +1,18 @@
 # -*- coding: utf-8 -*-
 from pyEcere import *
 app = GuiApplication()
-Window(caption = "Hello, Python!!", hasClose = True, clientSize = Size(640, 480), background = Color(b = 255))
-Window(caption = "Bindings are cool", hasClose = True, clientSize = Size(320, 200), background = Color(255))
+printLn("Testing Variadic Functions!\n", 1, " + ", 2, " = ", 1+2)
+printLn("Pi = ", 3.141592653589)
+
+Window(
+   caption = "Hello, Python!!",
+   hasClose = True,
+   clientSize = Size(640, 480),
+   background = Color(b = 255))
+Window(
+   caption = "Bindings are cool, 詠春 too!",
+   hasClose = True,
+   clientSize = Size(320, 200),
+   background = Color(255))
+
 app.main()