diff options
author | Vitor Sousa <vitorsousasilva@gmail.com> | 2017-06-15 23:16:56 -0300 |
---|---|---|
committer | Vitor Sousa <vitorsousasilva@gmail.com> | 2017-06-15 23:16:56 -0300 |
commit | c6660adea2551d6a585bab264dc9d405f52b935c (patch) | |
tree | 33efefaee3f93fee03b8e5e13d5ded85150270e1 | |
parent | 7563ea5d1aaa22caf3af5dbf45751b24df5f4f15 (diff) | |
download | efl-c6660adea2551d6a585bab264dc9d405f52b935c.tar.gz |
efl_mono: add example for function pointer binding
-rw-r--r-- | configure.ac | 1 | ||||
-rw-r--r-- | src/Makefile_Efl_Mono.am | 7 | ||||
-rw-r--r-- | src/examples/efl_mono/.gitignore | 1 | ||||
-rw-r--r-- | src/examples/efl_mono/FunctionPointer01.cs | 45 | ||||
-rw-r--r-- | src/examples/efl_mono/FunctionPointer01.sh.in | 3 | ||||
-rw-r--r-- | src/examples/efl_mono/example_numberwrapper.eo | 17 | ||||
-rw-r--r-- | src/examples/efl_mono/libefl_mono_native_example.c | 29 |
7 files changed, 103 insertions, 0 deletions
diff --git a/configure.ac b/configure.ac index 874c9bc13e..758b8a80bd 100644 --- a/configure.ac +++ b/configure.ac @@ -5806,6 +5806,7 @@ src/examples/efl_mono/EinaBinbuf01.sh src/examples/efl_mono/EinaError01.sh src/examples/efl_mono/EinaHash01.sh src/examples/efl_mono/EoInherit01.sh +src/examples/efl_mono/FunctionPointer01.sh elm_intro.h spec/efl.spec pc/evil.pc diff --git a/src/Makefile_Efl_Mono.am b/src/Makefile_Efl_Mono.am index 9c17087808..95788d700b 100644 --- a/src/Makefile_Efl_Mono.am +++ b/src/Makefile_Efl_Mono.am @@ -248,6 +248,13 @@ examples/efl_mono/EoInherit01.exe$(EXEEXT): $(examples_efl_mono_EoInherit01_exe_ @rm -f examples/efl_mono/EoInherit01_exe$(EXEEXT) $(AM_V_MCS) $(MCS) $(MCS_FLAGS) -r:$(abs_top_builddir)/src/lib/efl_mono/libefl_mono.dll -r:$(abs_top_builddir)/src/examples/efl_mono/libefl_mono_example.dll -out:$@ $(filter %.cs, $(^)) +bin_PROGRAMS += examples/efl_mono/FunctionPointer01.exe +examples_efl_mono_FunctionPointer01_exe_SOURCES = examples/efl_mono/FunctionPointer01.cs +examples/efl_mono/FunctionPointer01.exe$(EXEEXT): $(examples_efl_mono_FunctionPointer01_exe_SOURCES) examples/efl_mono/$(am__dirstamp) lib/efl_mono/libefl_mono.dll examples/efl_mono/libefl_mono_example.dll + @rm -f examples/efl_mono/FunctionPointer01_exe$(EXEEXT) + $(AM_V_MCS) $(MCS) $(MCS_FLAGS) -r:$(abs_top_builddir)/src/lib/efl_mono/libefl_mono.dll -r:$(abs_top_builddir)/src/examples/efl_mono/libefl_mono_example.dll -out:$@ $(filter %.cs, $(^)) + + SUFFIXES += .eo.cs lib/eo/%.eo.cs: lib/eo/%.eo $(_EOLIAN_MONO_DEP) diff --git a/src/examples/efl_mono/.gitignore b/src/examples/efl_mono/.gitignore index d72e4679c7..f418588f42 100644 --- a/src/examples/efl_mono/.gitignore +++ b/src/examples/efl_mono/.gitignore @@ -5,3 +5,4 @@ /EinaError01.sh /EinaHash01.sh /EoInherit01.sh +/FunctionPointer01.sh diff --git a/src/examples/efl_mono/FunctionPointer01.cs b/src/examples/efl_mono/FunctionPointer01.cs new file mode 100644 index 0000000000..7f9fca7720 --- /dev/null +++ b/src/examples/efl_mono/FunctionPointer01.cs @@ -0,0 +1,45 @@ +using static System.Console; + +public class ExampleFunctionPointer01 +{ + private static bool static_called = false; + + private static int twiceCb(int n) + { + static_called = true; + return n * 2; + } + + public static void Main() + { + eina.Config.Init(); + efl.eo.Config.Init(); + + var obj = new example.NumberwrapperConcrete(); + + // Set internal value + obj.number_set(12); + + // With static method + obj.number_callback_set(twiceCb); + + var ret = obj.callback_call(); + + WriteLine($"Callback called? {static_called}."); + WriteLine($"Returned value: {ret}.\n"); + + // With lambda + bool lamda_called = false; + + obj.number_callback_set(n => { + lamda_called = true; + return n * 3; + }); + + ret = obj.callback_call(); + + WriteLine($"Lambda called? {lamda_called}."); + WriteLine($"Returned value: {ret}.\n"); + } +} + diff --git a/src/examples/efl_mono/FunctionPointer01.sh.in b/src/examples/efl_mono/FunctionPointer01.sh.in new file mode 100644 index 0000000000..bf84af3c65 --- /dev/null +++ b/src/examples/efl_mono/FunctionPointer01.sh.in @@ -0,0 +1,3 @@ +#!/bin/sh +export MONO_PATH="@abs_top_builddir@/src/lib/efl_mono:${LD_LIBRARY_PATH_APPEND}" +mono --config "@abs_top_builddir@/src/examples/efl_mono/examples_mono.config" "@abs_top_builddir@/src/examples/efl_mono/FunctionPointer01.exe" diff --git a/src/examples/efl_mono/example_numberwrapper.eo b/src/examples/efl_mono/example_numberwrapper.eo index 077b804154..85f2ce4a4d 100644 --- a/src/examples/efl_mono/example_numberwrapper.eo +++ b/src/examples/efl_mono/example_numberwrapper.eo @@ -1,3 +1,10 @@ +function NumberCb { + params { + n: int; + } + return: int; +}; + class Example.Numberwrapper (Efl.Object) { methods { @property number { @@ -9,5 +16,15 @@ class Example.Numberwrapper (Efl.Object) { n: int; } } + + number_callback_set { + params { + cb: NumberCb; + } + } + + callback_call { + return: int; + } } } diff --git a/src/examples/efl_mono/libefl_mono_native_example.c b/src/examples/efl_mono/libefl_mono_native_example.c index 6f7aab0dfe..e08e995f81 100644 --- a/src/examples/efl_mono/libefl_mono_native_example.c +++ b/src/examples/efl_mono/libefl_mono_native_example.c @@ -37,6 +37,9 @@ typedef struct Example_Numberwrapper_Data { int number; + NumberCb cb; + void *cb_data; + Eina_Free_Cb free_cb; } Example_Numberwrapper_Data; // ##################### // @@ -54,5 +57,31 @@ int _example_numberwrapper_number_get(EINA_UNUSED Eo *obj, Example_Numberwrapper return pd->number; } + +void _example_numberwrapper_number_callback_set(EINA_UNUSED Eo *obj, Example_Numberwrapper_Data *pd, NumberCb cb, void *cb_data, Eina_Free_Cb cb_free_cb) +{ + if (pd->free_cb) + pd->free_cb(pd->cb_data); + + pd->cb = cb; + pd->cb_data = cb_data; + pd->free_cb = cb_free_cb; +} + + +int _example_numberwrapper_callback_call(EINA_UNUSED Eo *obj, Example_Numberwrapper_Data *pd) +{ + if (!pd->cb) + { + static Eina_Error no_cb_err = 0; + if (!no_cb_err) + no_cb_err = eina_error_msg_static_register("Trying to call with no callback set"); + eina_error_set(no_cb_err); + return -1; + } + + return pd->cb(pd->cb_data, pd->number); +} + #include "example_numberwrapper.eo.c" |