From 3ad3455551be0d7859ecb02290376206d5e66498 Mon Sep 17 00:00:00 2001 From: David Walter Seikel Date: Sun, 22 Apr 2012 09:20:32 +1000 Subject: And actually include new files, plus elementary libraries. --- libraries/eina/m4/common/efl_attribute.m4 | 56 + libraries/eina/m4/common/efl_benchmark.m4 | 33 + libraries/eina/m4/common/efl_compiler_flag.m4 | 57 + libraries/eina/m4/common/efl_coverage.m4 | 62 + libraries/eina/m4/common/efl_cpu.m4 | 341 ++ libraries/eina/m4/common/efl_doxygen.m4 | 94 + libraries/eina/m4/common/efl_examples.m4 | 63 + libraries/eina/m4/common/efl_fnmatch.m4 | 31 + libraries/eina/m4/common/efl_path_max.m4 | 36 + libraries/eina/m4/common/efl_tests.m4 | 43 + libraries/eina/m4/common/efl_threads.m4 | 219 + libraries/eina/m4/common/efl_voltron.m4 | 93 + libraries/eina/m4/eina/eina_bench.m4 | 31 + libraries/eina/m4/eina/eina_check.m4 | 48 + libraries/eina/sha1-for-source.txt | 1 + libraries/eina/src/examples/eina_inarray_01.c | 52 + libraries/eina/src/examples/eina_model_01.c | 235 + libraries/eina/src/examples/eina_model_02.c | 61 + libraries/eina/src/examples/eina_model_03.c | 236 + libraries/eina/src/examples/eina_model_04_animal.c | 76 + libraries/eina/src/examples/eina_model_04_child.c | 81 + libraries/eina/src/examples/eina_model_04_human.c | 157 + libraries/eina/src/examples/eina_model_04_main.c | 110 + libraries/eina/src/examples/eina_model_04_parrot.c | 95 + .../eina/src/examples/eina_model_04_whistler.c | 59 + libraries/eina/src/examples/eina_value_01.c | 53 + libraries/eina/src/examples/eina_value_02.c | 100 + libraries/eina/src/examples/eina_value_03.c | 178 + libraries/eina/src/include/eina_model.h | 3105 +++++++++++ libraries/eina/src/lib/eina_model.c | 5548 ++++++++++++++++++++ libraries/eina/src/tests/eina_test_model.c | 1288 +++++ 31 files changed, 12642 insertions(+) create mode 100644 libraries/eina/m4/common/efl_attribute.m4 create mode 100644 libraries/eina/m4/common/efl_benchmark.m4 create mode 100644 libraries/eina/m4/common/efl_compiler_flag.m4 create mode 100644 libraries/eina/m4/common/efl_coverage.m4 create mode 100644 libraries/eina/m4/common/efl_cpu.m4 create mode 100644 libraries/eina/m4/common/efl_doxygen.m4 create mode 100644 libraries/eina/m4/common/efl_examples.m4 create mode 100644 libraries/eina/m4/common/efl_fnmatch.m4 create mode 100644 libraries/eina/m4/common/efl_path_max.m4 create mode 100644 libraries/eina/m4/common/efl_tests.m4 create mode 100644 libraries/eina/m4/common/efl_threads.m4 create mode 100644 libraries/eina/m4/common/efl_voltron.m4 create mode 100644 libraries/eina/m4/eina/eina_bench.m4 create mode 100644 libraries/eina/m4/eina/eina_check.m4 create mode 100644 libraries/eina/sha1-for-source.txt create mode 100644 libraries/eina/src/examples/eina_inarray_01.c create mode 100644 libraries/eina/src/examples/eina_model_01.c create mode 100644 libraries/eina/src/examples/eina_model_02.c create mode 100644 libraries/eina/src/examples/eina_model_03.c create mode 100644 libraries/eina/src/examples/eina_model_04_animal.c create mode 100644 libraries/eina/src/examples/eina_model_04_child.c create mode 100644 libraries/eina/src/examples/eina_model_04_human.c create mode 100644 libraries/eina/src/examples/eina_model_04_main.c create mode 100644 libraries/eina/src/examples/eina_model_04_parrot.c create mode 100644 libraries/eina/src/examples/eina_model_04_whistler.c create mode 100644 libraries/eina/src/examples/eina_value_01.c create mode 100644 libraries/eina/src/examples/eina_value_02.c create mode 100644 libraries/eina/src/examples/eina_value_03.c create mode 100644 libraries/eina/src/include/eina_model.h create mode 100644 libraries/eina/src/lib/eina_model.c create mode 100644 libraries/eina/src/tests/eina_test_model.c (limited to 'libraries/eina') diff --git a/libraries/eina/m4/common/efl_attribute.m4 b/libraries/eina/m4/common/efl_attribute.m4 new file mode 100644 index 0000000..78bff15 --- /dev/null +++ b/libraries/eina/m4/common/efl_attribute.m4 @@ -0,0 +1,56 @@ +dnl Copyright (C) 2011 Vincent Torri +dnl That code is public domain and can be freely used or copied. + +dnl Macros for checking if the compiler supports some __attribute__ uses + +dnl Usage: EFL_ATTRIBUTE_UNUSED +dnl call AC_DEFINE for __UNUSED__ if __attribute__((unused)) is available + +AC_DEFUN([EFL_ATTRIBUTE_UNUSED], +[ +AC_MSG_CHECKING([for __attribute__ ((unused))]) +AC_COMPILE_IFELSE( + [AC_LANG_PROGRAM( + [[ +void foo(int x __attribute__ ((unused))) {} + ]], + [[ + ]])], + [have_attribute_unused="yes"], + [have_attribute_unused="no"]) +AC_MSG_RESULT([${have_attribute_unused}]) + +if test "x${have_attribute_unused}" = "xyes" ; then + AC_DEFINE([__UNUSED__], [__attribute__ ((unused))], [Macro declaring a function argument to be unused.]) +else + AC_DEFINE([__UNUSED__], [], [__attribute__ ((unused)) is not supported.]) +fi +]) + +dnl Usage: EFL_ATTRIBUTE_VECTOR +dnl call AC_DEFINE for HAVE_GCC_ATTRIBUTE_VECTOR if __attribute__((vector)) is available + +AC_DEFUN([EFL_ATTRIBUTE_VECTOR], +[ +AC_MSG_CHECKING([for __attribute__ ((vector))]) +AC_COMPILE_IFELSE( + [AC_LANG_PROGRAM( + [[ +typedef int v4si __attribute__ ((vector_size (16))); + ]], + [[ +if (sizeof(v4si) == 16) + return 0; +else + return -1; + ]])], + [have_attribute_vector="yes"], + [have_attribute_vector="no"]) +AC_MSG_RESULT([${have_attribute_vector}]) + +if test "x${have_attribute_vector}" = "xyes" ; then + AC_DEFINE([HAVE_GCC_ATTRIBUTE_VECTOR], [1], [Define to 1 if your compiler supports __attribute__ ((vector)).]) +fi +]) + +dnl End of efl_attribute.m4 diff --git a/libraries/eina/m4/common/efl_benchmark.m4 b/libraries/eina/m4/common/efl_benchmark.m4 new file mode 100644 index 0000000..1d1e22e --- /dev/null +++ b/libraries/eina/m4/common/efl_benchmark.m4 @@ -0,0 +1,33 @@ +dnl Copyright (C) 2008 Vincent Torri +dnl That code is public domain and can be freely used or copied. + +dnl Macro that check if benchmark support is wanted. + +dnl Usage: EFL_CHECK_BENCHMARK([ACTION-IF-FOUND [, ACTION-IF-NOT-FOUND]]) +dnl Defines the automake conditionnal EFL_ENABLE_BENCHMARK + +AC_DEFUN([EFL_CHECK_BENCHMARK], +[ + +dnl configure option + +AC_ARG_ENABLE([benchmark], + [AC_HELP_STRING([--enable-benchmark], [enable benchmarking @<:@default=disabled@:>@])], + [ + if test "x${enableval}" = "xyes" ; then + _efl_enable_benchmark="yes" + else + _efl_enable_benchmark="no" + fi + ], + [_efl_enable_benchmark="no"]) + +AC_MSG_CHECKING([whether benchmark are built]) +AC_MSG_RESULT([${_efl_enable_benchmark}]) + +AM_CONDITIONAL(EFL_ENABLE_BENCHMARK, test "x${_efl_enable_benchmark}" = "xyes") + +AS_IF([test "x$_efl_enable_benchmark" = "xyes"], [$1], [$2]) +]) + +dnl End of efl_benchmark.m4 diff --git a/libraries/eina/m4/common/efl_compiler_flag.m4 b/libraries/eina/m4/common/efl_compiler_flag.m4 new file mode 100644 index 0000000..25c285d --- /dev/null +++ b/libraries/eina/m4/common/efl_compiler_flag.m4 @@ -0,0 +1,57 @@ +dnl Copyright (C) 2010 Vincent Torri +dnl and Albin Tonnerre +dnl That code is public domain and can be freely used or copied. + +dnl Macro that checks if a compiler flag is supported by the compiler. + +dnl Usage: EFL_COMPILER_FLAG(flag) +dnl flag is added to CFLAGS if supported. + +AC_DEFUN([EFL_COMPILER_FLAG], +[ + +CFLAGS_save="${CFLAGS}" +CFLAGS="${CFLAGS} $1" + +AC_LANG_PUSH([C]) +AC_MSG_CHECKING([whether the compiler supports $1]) + +AC_COMPILE_IFELSE( + [AC_LANG_PROGRAM([[]])], + [have_flag="yes"], + [have_flag="no"]) +AC_MSG_RESULT([${have_flag}]) + +if test "x${have_flag}" = "xno" ; then + CFLAGS="${CFLAGS_save}" +fi +AC_LANG_POP([C]) + +]) + +dnl Macro that checks if a linker flag is supported by the compiler. + +dnl Usage: EFL_LINKER_FLAG(flag) +dnl flag is added to LDFLAGS if supported (will be passed to ld anyway). + +AC_DEFUN([EFL_LINKER_FLAG], +[ + +LDFLAGS_save="${LDFLAGS}" +LDFLAGS="${LDFLAGS} $1" + +AC_LANG_PUSH([C]) +AC_MSG_CHECKING([whether the compiler supports $1]) + +AC_LINK_IFELSE( + [AC_LANG_PROGRAM([[]])], + [have_flag="yes"], + [have_flag="no"]) +AC_MSG_RESULT([${have_flag}]) + +if test "x${have_flag}" = "xno" ; then + LDFLAGS="${LDFLAGS_save}" +fi +AC_LANG_POP([C]) + +]) diff --git a/libraries/eina/m4/common/efl_coverage.m4 b/libraries/eina/m4/common/efl_coverage.m4 new file mode 100644 index 0000000..85d0321 --- /dev/null +++ b/libraries/eina/m4/common/efl_coverage.m4 @@ -0,0 +1,62 @@ +dnl Copyright (C) 2008 Vincent Torri +dnl That code is public domain and can be freely used or copied. + +dnl Macro that check if coverage support is wanted and, if yes, if +dnl lcov is available. + +dnl Usage: EFL_CHECK_COVERAGE(tests [, ACTION-IF-FOUND [, ACTION-IF-NOT-FOUND]]) +dnl The parameter 'tests' is used if a dependency is needed. If set to "yes", +dnl the dependency is available. +dnl Defines EFL_COVERAGE_CFLAGS and EFL_COVERAGE_LIBS variables +dnl Defines the automake conditionnal EFL_ENABLE_COVERAGE + +AC_DEFUN([EFL_CHECK_COVERAGE], +[ + +dnl configure option + +AC_ARG_ENABLE([coverage], + [AC_HELP_STRING([--enable-coverage], [enable coverage profiling instrumentation @<:@default=disabled@:>@])], + [ + if test "x${enableval}" = "xyes" ; then + _efl_enable_coverage="yes" + else + _efl_enable_coverage="no" + fi + ], + [_efl_enable_coverage="no"]) + +AC_MSG_CHECKING([whether to use profiling instrumentation]) +AC_MSG_RESULT([$_efl_enable_coverage]) + +dnl lcov check + +if test "x$_efl_enable_coverage" = "xyes" && test ! "x$1" = "xyes" ; then + AC_MSG_WARN([Coverage report requested but tests not being built, disable profiling instrumentation.]) + AC_MSG_WARN([Run configure with --enable-tests]) + _efl_enable_coverage="no" +fi + +if test "x$_efl_enable_coverage" = "xyes" ; then + AC_CHECK_PROG(have_lcov, [lcov], [yes], [no]) + if test "x$have_lcov" = "xyes" ; then + EFL_COVERAGE_CFLAGS="-fprofile-arcs -ftest-coverage" + EFL_COVERAGE_LIBS="-lgcov" +# remove any optimisation flag and force debug symbols + EFL_DEBUG_CFLAGS="-g -O0 -DDEBUG" + else + AC_MSG_WARN([lcov is not found, disable profiling instrumentation]) + _efl_enable_coverage="no" + fi +fi + +dnl Substitution +AC_SUBST(EFL_COVERAGE_CFLAGS) +AC_SUBST(EFL_COVERAGE_LIBS) + +AM_CONDITIONAL(EFL_ENABLE_COVERAGE, test "x${_efl_enable_coverage}" = "xyes") + +AS_IF([test "x$_efl_enable_coverage" = "xyes"], [$2], [$3]) +]) + +dnl End of efl_coverage.m4 diff --git a/libraries/eina/m4/common/efl_cpu.m4 b/libraries/eina/m4/common/efl_cpu.m4 new file mode 100644 index 0000000..8fa6cd7 --- /dev/null +++ b/libraries/eina/m4/common/efl_cpu.m4 @@ -0,0 +1,341 @@ +dnl Copyright (C) 2008 Vincent Torri +dnl That code is public domain and can be freely used or copied. + +dnl Macro that check if several ASM instruction sets are available or not. + +dnl Usage: EFL_CHECK_CPU_MMX([ACTION-IF-FOUND [, ACTION-IF-NOT-FOUND]]) +dnl Add -mmmx to EFL_SIMD_FLAGS if the compiler supports it and call AC_SUBST(EFL_SIMD_FLAGS) +dnl Define EFL_HAVE_MMX + +AC_DEFUN([EFL_CHECK_CPU_MMX], +[ +dnl configure option + +AC_ARG_ENABLE([cpu-mmx], + [AC_HELP_STRING([--disable-cpu-mmx], [disable mmx code @<:@default=enabled@:>@])], + [ + if test "x${enableval}" = "xyes" ; then + _efl_enable_cpu_mmx="yes" + else + _efl_enable_cpu_mmx="no" + fi + ], + [_efl_enable_cpu_mmx="yes"]) + +AC_MSG_CHECKING([whether to build mmx code]) +AC_MSG_RESULT([${_efl_enable_cpu_mmx}]) + +dnl check if the CPU is supporting MMX instruction sets + +_efl_build_cpu_mmx="no" +if test "x${_efl_enable_cpu_mmx}" = "xyes" ; then + case $host_cpu in + i*86 | x86_64 | amd64) + _efl_build_cpu_mmx="yes" + ;; + esac +fi + +AC_MSG_CHECKING([whether mmx instructions set is available]) +AC_MSG_RESULT([${_efl_build_cpu_mmx}]) + +dnl check if the compiler supports -mmmx + +if test "x${_efl_build_cpu_mmx}" = "xyes" ; then + SAVE_CFLAGS=${CFLAGS} + CFLAGS="-mmmx" + AC_LANG_PUSH([C]) + + AC_COMPILE_IFELSE( + [AC_LANG_PROGRAM([[]], + [[]])], + [ + have_linker_option="yes" + EFL_SIMD_FLAGS="${EFL_SIMD_FLAGS} -mmmx"], + [have_linker_option="no"]) + + AC_LANG_POP([C]) + CFLAGS=${SAVE_CFLAGS} + + AC_MSG_CHECKING([whether mmx linker option is supported]) + AC_MSG_RESULT([${have_linker_option}]) +fi + +AC_SUBST(EFL_SIMD_FLAGS) + +if test "x${_efl_build_cpu_mmx}" = "xyes" ; then + AC_DEFINE([EFL_HAVE_MMX], [1], [Define to mention that MMX is supported]) +fi + +AS_IF([test "x$_efl_build_cpu_mmx" = "xyes"], [$1], [$2]) +]) + +dnl Usage: EFL_CHECK_CPU_SSE([ACTION-IF-FOUND [, ACTION-IF-NOT-FOUND]]) +dnl Add -msse to EFL_SIMD_FLAGS if the compiler supports it and call AC_SUBST(EFL_SIMD_FLAGS) +dnl Define EFL_HAVE_SSE + +AC_DEFUN([EFL_CHECK_CPU_SSE], +[ +dnl configure option + +AC_ARG_ENABLE([cpu-sse], + [AC_HELP_STRING([--disable-cpu-sse], [disable sse code @<:@default=enabled@:>@])], + [ + if test "x${enableval}" = "xyes" ; then + _efl_enable_cpu_sse="yes" + else + _efl_enable_cpu_sse="no" + fi + ], + [_efl_enable_cpu_sse="yes"]) + +AC_MSG_CHECKING([whether to build sse code]) +AC_MSG_RESULT([${_efl_enable_cpu_sse}]) + +dnl check if the CPU is supporting SSE instruction sets + +_efl_build_cpu_sse="no" +if test "x${_efl_enable_cpu_sse}" = "xyes" ; then + case $host_cpu in + i*86 | x86_64 | amd64) + _efl_build_cpu_sse="yes" + ;; + esac +fi + +AC_MSG_CHECKING([whether sse instructions set is available]) +AC_MSG_RESULT([${_efl_build_cpu_sse}]) + +dnl check if the compiler supports -msse + +if test "x${_efl_build_cpu_sse}" = "xyes" ; then + SAVE_CFLAGS=${CFLAGS} + CFLAGS="-msse" + AC_LANG_PUSH([C]) + + AC_COMPILE_IFELSE( + [AC_LANG_PROGRAM([[]], + [[]]) + ], + [ + have_linker_option="yes" + EFL_SIMD_FLAGS="${EFL_SIMD_FLAGS} -msse" + ], + [have_linker_option="no"]) + + AC_LANG_POP([C]) + CFLAGS=${SAVE_CFLAGS} + + AC_MSG_CHECKING([whether sse linker option is supported]) + AC_MSG_RESULT([${have_linker_option}]) +fi + +AC_SUBST(EFL_SIMD_FLAGS) + +if test "x${_efl_build_cpu_sse}" = "xyes" ; then + AC_DEFINE([EFL_HAVE_SSE], [1], [Define to mention that SSE is supported]) +fi + +AS_IF([test "x$_efl_build_cpu_sse" = "xyes"], [$1], [$2]) +]) + +dnl Usage: EFL_CHECK_CPU_SSE2([ACTION-IF-FOUND [, ACTION-IF-NOT-FOUND]]) +dnl Add -msse2 to EFL_SIMD_FLAGS if the compiler supports it and call AC_SUBST(EFL_SIMD_FLAGS) +dnl Define EFL_HAVE_SSE2 + +AC_DEFUN([EFL_CHECK_CPU_SSE2], +[ + +dnl configure option + +AC_ARG_ENABLE([cpu-sse2], + [AC_HELP_STRING([--disable-cpu-sse2], [disable sse2 code @<:@default=enabled@:>@])], + [ + if test "x${enableval}" = "xyes" ; then + _efl_enable_cpu_sse2="yes" + else + _efl_enable_cpu_sse2="no" + fi + ], + [_efl_enable_cpu_sse2="yes"]) + +AC_MSG_CHECKING([whether to build sse2 code]) +AC_MSG_RESULT([${_efl_enable_cpu_sse2}]) + +dnl check if the CPU is supporting SSE2 instruction sets + +_efl_build_cpu_sse2="no" +if test "x${_efl_enable_cpu_sse2}" = "xyes" ; then + case $host_cpu in + i*86 | x86_64 | amd64) + _efl_build_cpu_sse2="yes" + ;; + esac +fi + +AC_MSG_CHECKING([whether sse2 instructions set is available]) +AC_MSG_RESULT([${_efl_build_cpu_sse2}]) + +dnl check if the compiler supports -msse2 + +if test "x${_efl_build_cpu_sse2}" = "xyes" ; then + SAVE_CFLAGS=${CFLAGS} + CFLAGS="-msse2" + AC_LANG_PUSH([C]) + + AC_COMPILE_IFELSE( + [AC_LANG_PROGRAM([[]], + [[]]) + ], + [ + have_linker_option="yes" + EFL_SIMD_FLAGS="${EFL_SIMD_FLAGS} -msse2" + ], + [have_linker_option="no"]) + + AC_LANG_POP([C]) + CFLAGS=${SAVE_CFLAGS} + + AC_MSG_CHECKING([whether sse2 linker option is supported]) + AC_MSG_RESULT([${have_linker_option}]) +fi + +AC_SUBST(EFL_SIMD_FLAGS) + +if test "x${_efl_build_cpu_sse2}" = "xyes" ; then + AC_DEFINE([EFL_HAVE_SSE2], [1], [Define to mention that SSE2 is supported]) +fi + +AS_IF([test "x$_efl_build_cpu_sse2" = "xyes"], [$1], [$2]) +]) + +dnl Usage: EFL_CHECK_CPU_ALTIVEC([ACTION-IF-FOUND [, ACTION-IF-NOT-FOUND]]) +dnl Add -faltivec or -maltivec to EFL_SIMD_FLAGS if the compiler supports it and +dnl call AC_SUBST(EFL_SIMD_FLAGS) +dnl Define EFL_HAVE_ALTIVEC + +AC_DEFUN([EFL_CHECK_CPU_ALTIVEC], +[ + +dnl configure option + +AC_ARG_ENABLE([cpu-altivec], + [AC_HELP_STRING([--disable-cpu-altivec], [disable altivec code @<:@default=enabled@:>@])], + [ + if test "x${enableval}" = "xyes" ; then + _efl_enable_cpu_altivec="yes" + else + _efl_enable_cpu_altivec="no" + fi + ], + [_efl_enable_cpu_altivec="yes"] +) +AC_MSG_CHECKING([whether to build altivec code]) +AC_MSG_RESULT([${_efl_enable_cpu_altivec}]) + +dnl check if the CPU is supporting ALTIVEC instruction sets + +_efl_build_cpu_altivec="no" +if test "x${_efl_enable_cpu_altivec}" = "xyes" ; then + case $host_cpu in + *power* | *ppc*) + _efl_build_cpu_altivec="yes" + ;; + esac +fi + +AC_MSG_CHECKING([whether altivec instructions set is available]) +AC_MSG_RESULT([${_efl_build_cpu_altivec}]) + +dnl check if the compiler supports -faltivec or -maltivec and +dnl if altivec.h is available. + +_efl_have_faltivec="no" +if test "x${_efl_build_cpu_altivec}" = "xyes" ; then + SAVE_CFLAGS=${CFLAGS} + CFLAGS="-faltivec" + AC_LANG_PUSH([C]) + + AC_COMPILE_IFELSE( + [AC_LANG_PROGRAM([[ +#include + ]], + [[]])], + [_efl_have_faltivec="yes" + _efl_altivec_flag="-faltivec"], + [_efl_have_faltivec="no"]) + + if test "x${_efl_have_faltivec}" = "xno" ; then + CFLAGS="-maltivec" + + AC_COMPILE_IFELSE( + [AC_LANG_PROGRAM([[ +#include + ]], + [[]])], + [_efl_have_faltivec="yes" + _efl_altivec_flag="-maltivec"], + [_efl_have_faltivec="no"]) + fi + + AC_LANG_POP([C]) + CFLAGS=${SAVE_CFLAGS} + + AC_MSG_CHECKING([whether altivec linker option is supported]) + AC_MSG_RESULT([${_efl_have_faltivec}]) +fi + +EFL_SIMD_FLAGS="${EFL_SIMD_FLAGS} ${_efl_altivec_flag}" +AC_SUBST(EFL_SIMD_FLAGS) + +if test "x${_efl_have_faltivec}" = "xyes" ; then + AC_DEFINE([EFL_HAVE_ALTIVEC], [1], [Define to mention that ALTIVEC is supported]) +fi + +AS_IF([test "x$_efl_have_faltivec" = "xyes"], [$1], [$2]) +]) + +dnl Usage: EFL_CHECK_CPU_NEON([ACTION-IF-FOUND [, ACTION-IF-NOT-FOUND]]) +dnl Add -mneon to EFL_SIMD_FLAGS if the compiler supports it and call AC_SUBST(EFL_SIMD_FLAGS) +dnl Define EFL_HAVE_NEON + +AC_DEFUN([EFL_CHECK_CPU_NEON], +[ +dnl configure option + +AC_ARG_ENABLE([cpu-neon], + [AC_HELP_STRING([--disable-cpu-neon], [disable neon code @<:@default=enabled@:>@])], + [ + if test "x${enableval}" = "xyes" ; then + _efl_enable_cpu_neon="yes" + else + _efl_enable_cpu_neon="no" + fi + ], + [_efl_enable_cpu_neon="yes"]) + +AC_MSG_CHECKING([whether to build neon code]) +AC_MSG_RESULT([${_efl_enable_cpu_neon}]) + +dnl check if the CPU is supporting NEON instruction sets + +_efl_build_cpu_neon="no" +if test "x${_efl_enable_cpu_neon}" = "xyes" ; then + case $host_cpu in + armv7*) + _efl_build_cpu_neon="yes" + ;; + esac +fi + +AC_MSG_CHECKING([whether neon instructions set is available]) +AC_MSG_RESULT([${_efl_build_cpu_neon}]) + +if test "x${_efl_build_cpu_neon}" = "xyes" ; then + AC_DEFINE([EFL_HAVE_NEON], [1], [Define to mention that NEON is supported]) +fi + +AS_IF([test "x$_efl_build_cpu_neon" = "xyes"], [$1], [$2]) +]) + +dnl End of efl_cpu.m4 diff --git a/libraries/eina/m4/common/efl_doxygen.m4 b/libraries/eina/m4/common/efl_doxygen.m4 new file mode 100644 index 0000000..7324af3 --- /dev/null +++ b/libraries/eina/m4/common/efl_doxygen.m4 @@ -0,0 +1,94 @@ +dnl Copyright (C) 2008 Vincent Torri +dnl That code is public domain and can be freely used or copied. + +dnl Macro that check if doxygen is available or not. + +dnl EFL_CHECK_DOXYGEN([ACTION-IF-FOUND [, ACTION-IF-NOT-FOUND]]) +dnl Test for the doxygen program +dnl Defines efl_doxygen +dnl Defines the automake conditionnal EFL_BUILD_DOC +dnl +AC_DEFUN([EFL_CHECK_DOXYGEN], +[ + +dnl +dnl Disable the build of the documentation +dnl +AC_ARG_ENABLE([doc], + [AC_HELP_STRING( + [--disable-doc], + [Disable documentation build @<:@default=enabled@:>@])], + [ + if test "x${enableval}" = "xyes" ; then + efl_enable_doc="yes" + else + efl_enable_doc="no" + fi + ], + [efl_enable_doc="yes"]) + +AC_MSG_CHECKING([whether to build documentation]) +AC_MSG_RESULT([${efl_enable_doc}]) + +if test "x${efl_enable_doc}" = "xyes" ; then + +dnl Specify the file name, without path + + efl_doxygen="doxygen" + + AC_ARG_WITH([doxygen], + [AC_HELP_STRING( + [--with-doxygen=FILE], + [doxygen program to use @<:@default=doxygen@:>@])], + +dnl Check the given doxygen program. + + [efl_doxygen=${withval} + AC_CHECK_PROG([efl_have_doxygen], + [${efl_doxygen}], + [yes], + [no]) + if test "x${efl_have_doxygen}" = "xno" ; then + echo "WARNING:" + echo "The doxygen program you specified:" + echo "${efl_doxygen}" + echo "was not found. Please check the path and make sure " + echo "the program exists and is executable." + AC_MSG_WARN([no doxygen detected. Documentation will not be built]) + fi + ], + [AC_CHECK_PROG([efl_have_doxygen], + [${efl_doxygen}], + [yes], + [no]) + if test "x${efl_have_doxygen}" = "xno" ; then + echo "WARNING:" + echo "The doxygen program was not found in your execute path." + echo "You may have doxygen installed somewhere not covered by your path." + echo "" + echo "If this is the case make sure you have the packages installed, AND" + echo "that the doxygen program is in your execute path (see your" + echo "shell manual page on setting the \$PATH environment variable), OR" + echo "alternatively, specify the program to use with --with-doxygen." + AC_MSG_WARN([no doxygen detected. Documentation will not be built]) + fi + ]) +else + efl_have_doxygen="no" +fi + +dnl +dnl Substitution +dnl +AC_SUBST([efl_doxygen]) + +if ! test "x${efl_have_doxygen}" = "xyes" ; then + efl_enable_doc="no" +fi + +AM_CONDITIONAL(EFL_BUILD_DOC, test "x${efl_have_doxygen}" = "xyes") + +AS_IF([test "x$efl_have_doxygen" = "xyes"], [$1], [$2]) +]) + +dnl End of efl_doxygen.m4 diff --git a/libraries/eina/m4/common/efl_examples.m4 b/libraries/eina/m4/common/efl_examples.m4 new file mode 100644 index 0000000..2a809ad --- /dev/null +++ b/libraries/eina/m4/common/efl_examples.m4 @@ -0,0 +1,63 @@ +dnl Copyright (C) 2008 Vincent Torri +dnl That code is public domain and can be freely used or copied. + +dnl Macro that check if building examples is wanted. + +dnl Usage: EFL_CHECK_BUILD_EXAMPLES([ACTION-IF-FOUND [, ACTION-IF-NOT-FOUND]]) +dnl Defines the automake conditionnal EFL_ENABLE_BUILD_EXAMPLES + +AC_DEFUN([EFL_CHECK_BUILD_EXAMPLES], +[ + +dnl configure option + +AC_ARG_ENABLE([build-examples], + [AC_HELP_STRING([--enable-build-examples], [enable building examples @<:@default=disabled@:>@])], + [ + if test "x${enableval}" = "xyes" ; then + _efl_enable_build_examples="yes" + else + _efl_enable_build_examples="no" + fi + ], + [_efl_enable_build_examples="no"]) + +AC_MSG_CHECKING([whether examples are built]) +AC_MSG_RESULT([${_efl_enable_build_examples}]) + +AM_CONDITIONAL(EFL_BUILD_EXAMPLES, test "x${_efl_enable_build_examples}" = "xyes") + +AS_IF([test "x$_efl_enable_build_examples" = "xyes"], [$1], [$2]) +]) + + +dnl Macro that check if installing examples is wanted. + +dnl Usage: EFL_CHECK_INSTALL_EXAMPLES([ACTION-IF-FOUND [, ACTION-IF-NOT-FOUND]]) +dnl Defines the automake conditionnal EFL_ENABLE_INSTALL_EXAMPLES + +AC_DEFUN([EFL_CHECK_INSTALL_EXAMPLES], +[ + +dnl configure option + +AC_ARG_ENABLE([install-examples], + [AC_HELP_STRING([--enable-install-examples], [enable installing example source files @<:@default=disabled@:>@])], + [ + if test "x${enableval}" = "xyes" ; then + _efl_enable_install_examples="yes" + else + _efl_enable_install_examples="no" + fi + ], + [_efl_enable_install_examples="no"]) + +AC_MSG_CHECKING([whether examples are installed]) +AC_MSG_RESULT([${_efl_enable_install_examples}]) + +AM_CONDITIONAL(EFL_INSTALL_EXAMPLES, test "x${_efl_enable_install_examples}" = "xyes") + +AS_IF([test "x$_efl_enable_install_examples" = "xyes"], [$1], [$2]) +]) + +dnl End of efl_examples.m4 diff --git a/libraries/eina/m4/common/efl_fnmatch.m4 b/libraries/eina/m4/common/efl_fnmatch.m4 new file mode 100644 index 0000000..a92ac6b --- /dev/null +++ b/libraries/eina/m4/common/efl_fnmatch.m4 @@ -0,0 +1,31 @@ +dnl Copyright (C) 2010 Vincent Torri +dnl That code is public domain and can be freely used or copied. + +dnl Macro that check if fnmatch functions are available or not. + +dnl Usage: EFL_CHECK_FNMATCH([, ACTION-IF-FOUND [, ACTION-IF-NOT-FOUND]]) +dnl Call AC_SUBST(EFL_FNMATCH_LIBS) + +AC_DEFUN([EFL_CHECK_FNMATCH], +[ + +AC_CHECK_HEADER([fnmatch.h], [_efl_have_fnmatch="yes"], [_efl_have_fnmatch="no"]) + +if test "x${_efl_have_fnmatch}" = "xyes" ; then + AC_SEARCH_LIBS([fnmatch], + [fnmatch evil iberty], + [_efl_have_fnmatch="yes"], + [_efl_have_fnmatch="no"]) +fi + +EFL_FNMATCH_LIBS="" + +if (! test "x${ac_cv_search_fnmatch}" = "xnone required") && (! test "x${ac_cv_search_fnmatch}" = "xno") && (! test "x${ac_cv_search_fnmatch}" = "x-levil") ; then + EFL_FNMATCH_LIBS=${ac_cv_search_fnmatch} +fi + +AC_SUBST(EFL_FNMATCH_LIBS) + +AS_IF([test "x$_efl_have_fnmatch" = "xyes"], [$1], [$2]) + +]) diff --git a/libraries/eina/m4/common/efl_path_max.m4 b/libraries/eina/m4/common/efl_path_max.m4 new file mode 100644 index 0000000..f57bfd2 --- /dev/null +++ b/libraries/eina/m4/common/efl_path_max.m4 @@ -0,0 +1,36 @@ +dnl Check for PATH_MAX in limits.h, and define a default value if not found +dnl This is a workaround for systems not providing PATH_MAX, like GNU/Hurd + +dnl EFL_CHECK_PATH_MAX([DEFAULT_VALUE_IF_NOT_FOUND]) +dnl +dnl If PATH_MAX is not defined in , defines it +dnl to DEFAULT_VALUE_IF_NOT_FOUND if it exists, or fallback +dnl to using 4096 + +AC_DEFUN([EFL_CHECK_PATH_MAX], +[ + +default_max=m4_default([$1], "4096") +AC_LANG_PUSH([C]) + +AC_MSG_CHECKING([for PATH_MAX in limits.h]) +AC_COMPILE_IFELSE( + [AC_LANG_PROGRAM( + [[ +#include + ]], + [[ +int i = PATH_MAX; + ]])], + [AC_MSG_RESULT([yes])], + [ + AC_DEFINE_UNQUOTED([PATH_MAX], + [${default_max}], + [default value since PATH_MAX is not defined]) + AC_MSG_RESULT([no: using ${default_max}]) + ]) + +AC_LANG_POP([C]) + +]) +dnl end of efl_path_max.m4 diff --git a/libraries/eina/m4/common/efl_tests.m4 b/libraries/eina/m4/common/efl_tests.m4 new file mode 100644 index 0000000..3a4dfe2 --- /dev/null +++ b/libraries/eina/m4/common/efl_tests.m4 @@ -0,0 +1,43 @@ +dnl Copyright (C) 2008 Vincent Torri +dnl That code is public domain and can be freely used or copied. + +dnl Macro that check if tests programs are wanted and if yes, if +dnl the Check library is available. + +dnl Usage: EFL_CHECK_TESTS([ACTION-IF-FOUND [, ACTION-IF-NOT-FOUND]]) +dnl Define the automake conditionnal EFL_ENABLE_TESTS + +AC_DEFUN([EFL_CHECK_TESTS], +[ + +dnl configure option + +AC_ARG_ENABLE([tests], + [AC_HELP_STRING([--enable-tests], [enable tests @<:@default=disabled@:>@])], + [ + if test "x${enableval}" = "xyes" ; then + _efl_enable_tests="yes" + else + _efl_enable_tests="no" + fi + ], + [_efl_enable_tests="no"]) + +AC_MSG_CHECKING([whether tests are built]) +AC_MSG_RESULT([${_efl_enable_tests}]) + +AC_REQUIRE([PKG_PROG_PKG_CONFIG]) + +if test "x${_efl_enable_tests}" = "xyes" ; then + PKG_CHECK_MODULES([CHECK], + [check >= 0.9.5], + [dummy="yes"], + [_efl_enable_tests="no"]) +fi + +AM_CONDITIONAL(EFL_ENABLE_TESTS, test "x${_efl_enable_tests}" = "xyes") + +AS_IF([test "x$_efl_enable_tests" = "xyes"], [$1], [$2]) +]) + +dnl End of efl_tests.m4 diff --git a/libraries/eina/m4/common/efl_threads.m4 b/libraries/eina/m4/common/efl_threads.m4 new file mode 100644 index 0000000..a761d51 --- /dev/null +++ b/libraries/eina/m4/common/efl_threads.m4 @@ -0,0 +1,219 @@ +dnl Copyright (C) 2010 Vincent Torri +dnl rwlock code added by Mike Blumenkrantz +dnl This code is public domain and can be freely used or copied. + +dnl Macro that check if POSIX or Win32 threads library is available or not. + +dnl Usage: EFL_CHECK_THREADS(ACTION-IF-FOUND [, ACTION-IF-NOT-FOUND]) +dnl Call AC_SUBST(EFL_PTHREAD_CFLAGS) +dnl Call AC_SUBST(EFL_PTHREAD_LIBS) +dnl Defines EFL_HAVE_POSIX_THREADS or EFL_HAVE_WIN32_THREADS, and EFL_HAVE_THREADS + +AC_DEFUN([EFL_CHECK_THREADS], +[ + +dnl configure option + +AC_ARG_ENABLE([posix-threads], + [AC_HELP_STRING([--disable-posix-threads], [enable POSIX threads code @<:@default=auto@:>@])], + [ + if test "x${enableval}" = "xyes" ; then + _efl_enable_posix_threads="yes" + else + _efl_enable_posix_threads="no" + fi + ], + [_efl_enable_posix_threads="auto"]) + +AC_MSG_CHECKING([whether to build POSIX threads code]) +AC_MSG_RESULT([${_efl_enable_posix_threads}]) + +AC_ARG_ENABLE([win32-threads], + [AC_HELP_STRING([--disable-win32-threads], [enable Win32 threads code @<:@default=no@:>@])], + [ + if test "x${enableval}" = "xyes" ; then + _efl_enable_win32_threads="yes" + else + _efl_enable_win32_threads="no" + fi + ], + [_efl_enable_win32_threads="no"]) + +AC_MSG_CHECKING([whether to build Windows threads code]) +AC_MSG_RESULT([${_efl_enable_win32_threads}]) + +dnl +dnl * no + no +dnl * yes + no : win32: error, other : pthread +dnl * yes + yes : win32 : wthread, other : pthread +dnl * no + yes : win32 : wthread, other : error + +if test "x${_efl_enable_posix_threads}" = "xyes" && test "x${_efl_enable_win32_threads}" = "xyes" ; then + case "$host_os" in + mingw*) + _efl_enable_posix_threads=no + ;; + *) + _efl_enable_win32_threads=no + ;; + esac +fi + +if test "x${_efl_enable_win32_threads}" = "xyes" ; then + case "$host_os" in + mingw*) + ;; + *) + AC_MSG_ERROR([Win32 threads support requested but non Windows system found.]) + ;; + esac +fi + +if test "x${_efl_enable_posix_threads}" = "xyes" ; then + case "$host_os" in + mingw*) + AC_MSG_ERROR([POSIX threads support requested but Windows system found.]) + ;; + *) + ;; + esac +fi + +dnl check if the compiler supports POSIX threads + +case "$host_os" in + mingw*) + ;; + solaris*) + _efl_threads_cflags="-mt" + _efl_threads_libs="-mt" + ;; + *) + _efl_threads_cflags="-pthread" + _efl_threads_libs="-pthread" + ;; +esac + +_efl_have_posix_threads="no" +_efl_have_win32_threads="no" + +if test "x${_efl_enable_posix_threads}" = "xyes" || test "x${_efl_enable_posix_threads}" = "xauto" ; then + + SAVE_CFLAGS=${CFLAGS} + CFLAGS="${CFLAGS} ${_efl_threads_cflags}" + SAVE_LIBS=${LIBS} + LIBS="${LIBS} ${_efl_threads_libs}" + AC_LINK_IFELSE( + [AC_LANG_PROGRAM([[ +#include + ]], + [[ +pthread_t id; +id = pthread_self(); + ]])], + [_efl_have_posix_threads="yes"], + [_efl_have_posix_threads="no"]) + CFLAGS=${SAVE_CFLAGS} + LIBS=${SAVE_LIBS} + +fi + +AC_MSG_CHECKING([whether system support POSIX threads]) +AC_MSG_RESULT([${_efl_have_posix_threads}]) +if test "$x{_efl_enable_posix_threads}" = "xyes" && test "x${_efl_have_posix_threads}" = "xno"; then + AC_MSG_ERROR([POSIX threads support requested but not found.]) +fi + +EFL_PTHREAD_CFLAGS="" +EFL_PTHREAD_LIBS="" +if test "x${_efl_have_posix_threads}" = "xyes" ; then + EFL_PTHREAD_CFLAGS=${_efl_threads_cflags} + EFL_PTHREAD_LIBS=${_efl_threads_libs} +fi + +AC_SUBST(EFL_PTHREAD_CFLAGS) +AC_SUBST(EFL_PTHREAD_LIBS) + +_efl_enable_on_off_threads="no" +AC_ARG_ENABLE([on-off-threads], + [AC_HELP_STRING([--enable-on-off-threads], [only turn this on if you know what you are doing, and don't complain if the world freeze])], + [_efl_enable_on_off_threads="${enableval}"]) + +have_on_off_threads="no" +if test "x${_efl_enable_on_off_threads}" = "xyes"; then + have_on_off_threads="yes" + AC_DEFINE([EFL_ON_OFF_THREADS], [1], [make it possible to disable all locks]) +fi +AC_MSG_CHECKING([whether to turn on/off threads lock on demand]) +AC_MSG_RESULT([${_efl_enable_on_off_threads}]) + +_efl_enable_debug_threads="no" +AC_ARG_ENABLE([debug-threads], + [AC_HELP_STRING([--enable-debug-threads], [disable assert when you forgot to call eina_threads_init])], + [_efl_enable_debug_threads="${enableval}"]) + +have_debug_threads="no" +if test "x${_efl_have_posix_threads}" = "xyes" -a "x${_efl_enable_debug_threads}" = "xyes"; then + have_debug_threads="yes" + AC_DEFINE([EFL_DEBUG_THREADS], [1], [Assert when forgot to call eina_threads_init]) +fi + +if test "x${_efl_have_posix_threads}" = "xyes" ; then + AC_DEFINE([EFL_HAVE_POSIX_THREADS], [1], [Define to mention that POSIX threads are supported]) +fi + +if test "x${_efl_enable_win32_threads}" = "xyes" ; then + _efl_have_win32_threads="yes" + AC_DEFINE([EFL_HAVE_WIN32_THREADS], [1], [Define to mention that Win32 threads are supported]) +fi + +if test "x${_efl_have_posix_threads}" = "xyes" || test "x${_efl_have_win32_threads}" = "xyes" ; then + AC_DEFINE([EFL_HAVE_THREADS], [1], [Define to mention that POSIX or Win32 threads are supported]) +fi + +AS_IF([test "x$_efl_have_posix_threads" = "xyes" || test "x$_efl_have_win32_threads" = "xyes"], [$1], [$2]) +]) + +dnl Usage: EFL_CHECK_SPINLOCK(ACTION-IF-FOUND [, ACTION-IF-NOT-FOUND]) +dnl Defines EFL_HAVE_POSIX_THREADS_SPINLOCK +AC_DEFUN([EFL_CHECK_SPINLOCK], +[ + +dnl check if the compiler supports pthreads spinlock + +_efl_have_posix_threads_spinlock="no" + +if test "x${_efl_have_posix_threads}" = "xyes" ; then + + SAVE_CFLAGS=${CFLAGS} + CFLAGS="${CFLAGS} ${EFL_PTHREAD_CFLAGS}" + SAVE_LIBS=${LIBS} + LIBS="${LIBS} ${EFL_PTHREAD_LIBS}" + AC_LINK_IFELSE( + [AC_LANG_PROGRAM([[ +#include + ]], + [[ +pthread_spinlock_t lock; +int res; +res = pthread_spin_init(&lock, PTHREAD_PROCESS_PRIVATE); + ]])], + [_efl_have_posix_threads_spinlock="yes"], + [_efl_have_posix_threads_spinlock="no"]) + CFLAGS=${SAVE_CFLAGS} + LIBS=${SAVE_LIBS} + +fi + +AC_MSG_CHECKING([whether to build POSIX threads spinlock code]) +AC_MSG_RESULT([${_efl_have_posix_threads_spinlock}]) +if test "x${_efl_enable_posix_threads}" = "xyes" && test "x${_efl_have_posix_threads_spinlock}" = "xno" ; then + AC_MSG_WARN([POSIX threads support requested but spinlocks are not supported]) +fi + +if test "x${_efl_have_posix_threads_spinlock}" = "xyes" ; then + AC_DEFINE([EFL_HAVE_POSIX_THREADS_SPINLOCK], [1], [Define to mention that POSIX threads spinlocks are supported]) +fi +AS_IF([test "x$_efl_have_posix_threads_spinlock" = "xyes"], [$1], [$2]) +]) + diff --git a/libraries/eina/m4/common/efl_voltron.m4 b/libraries/eina/m4/common/efl_voltron.m4 new file mode 100644 index 0000000..43b334e --- /dev/null +++ b/libraries/eina/m4/common/efl_voltron.m4 @@ -0,0 +1,93 @@ +dnl EFL_FORM_VOLTRON +dnl Outputs ascii art of Voltron if terminal has enough columns +dnl +dnl ascii art was found at http://www.codeismylife.com/ascii_voltron/5239.html +dnl and is the work of its original author. + +AC_DEFUN([EFL_FORM_VOLTRON], +[ +AC_ARG_ENABLE([voltron], + [AC_HELP_STRING([--enable-voltron], [enable forming of voltron when all files combine @<:@default=no@:>@])], + [ + if test "x${enableval}" = "xyes" ; then + have_voltron="yes" + else + have_voltron="no" + fi + ], + [have_voltron="no"] +) + + if test "x$have_voltron" = "xyes" -a "x$do_amalgamation" = "xyes" -o "x${have_on_off_threads}" = "xyes"; then + echo "//////////////////////////////////////////////////////////////////////////////////////////////////////////////////" + echo "/////////////////////////////////////////////////////////////////////////////////////{#///////////////////////////" + echo "/////////////////////////////////////////////////////////////////////////////////// EN3 xx&HWx////////////////////" + echo "////////////////////////////////////////////////#{//xd3:& \/{:x////////////////////#WJNd_ HHNp#///////////////////" + echo "///////////////////////////////////////////////\x WDMMM EDDzEQp&^x ^^_x #///////////&37z^xHHH7_///////////////////" + echo "////////////////////////////////////////////////xPMMMMM LMMMLQQzzQDzH\{xx\{////////{x&H9@^&d^ {//////////////////" + echo "///////////////////////////////////////////^JLE:PMMMQ9N^EDMMMMMMMLzJJ^ @&dx////////#dHdN^x{/{x {//////////////////" + echo "//////////////////////////////////////////#LMMM EEE7p@H@NE777QMMMMMMM3H3_x/////////^HH3W^pH@x x///////////////////" + echo "//////////////////////////////////////////#MMLN_^: {/:L&\{:@7EWWWzMMM3H3_x////{:x ^3dd3 HHNENd^{//////////////////" + echo "//////////////////////////////////////////#WW7N^//////7Q:////{xHE93H3^^_ #////{H3H3JDdxx&H3Wz3:///////////////////" + echo "//////////////////////////////////////////_E93 ///////{^#/#{{{{{{#x37dx77x////{Hp3pd _ xx_7H////////////////////" + echo "#/////////////////////////////////////////#&MHd{////////#x^ ^&__ xxzMJH9P ////{xx\#\:x\:d@_://///////////////////" + echo "3H #///////////////////////////////////////{pp^Jpx{///{ &&__^:{{:xxxx7MQNWpx///@x//{# xxx#///////////////////////#" + echo "3HN7Hx{//////////////////////////////////////\xd NQd#x \# &x{/x3pHHH&#&PP&@zNxx##:{////////////////////////////{ ^" + echo "HH{@37N_x{////////////////////////////////////x_//#zDxx x //# ^x#//#E9_P&p://////////////////////////////{\&:\\" + echo "H@/JMJ799H_:{/////////////////////////////////{\/// L7_xx^#/\LMMMMM3MLN@Mp7 dW{\////////////////////////////{ #^H" + echo "H^/PMMMD9773d^\{////////////////////////////////////dzWLp/{x7MMMMMEJMM7DMHD#^z&#//////////////////////////{x&\ dHH" + echo "H^{MMMMMMLEN33Hd_://////////////////////////////////#WNMLzzPPLMMMLHMML^NNNQW93JdE_ //////////////////////#_ :@H3HH" + echo "HdxWMMMMMMMMzWdHHpNH {/////////////////////////////{z99QEJDQJ7@EM3LMM37M9LHLMNL:^\{////////////////////{x_\_HHH&&x" + echo "HH_^zMMMMMMMML7dHH39zN_\//////////////////////////{WQLHEENpDPDLHQMMPHMDEzNMLWJ///////////////////////#_x d3Hd^dHx" + echo "/\@HH HLMMMMMMMM9 3HHHp9DJ&#////////////////////////xN_\LzPMMMLpPMMPdLMWLdMMNQ3/////////////////////{ ^x&dddd HHH " + echo "//{ HH@ 9MMMMMMLxdHHHHH3HpEQN /////////////////////:PL\/QMMMLE3LMMp#JMJJ9JMM&M //////////////////{#\ _ ^^&:x##HHHx" + echo "////:d3H_&PMMMMN&3HHHHHHHHHHWEJH\/////////////////{zMd//^EE {/xHEH&HMLHLHMMJpL#////////{#x _^^&WpHHHHH3HHNxHHxHHHx" + echo "/////#_HHH^WMMP3L9HHHHHHHHHHHHHWE7_{//////////////7Mz{///:7EWNEzPL3LMWQWDMM_zN/{\x ^& zLMDpHp973HHHHHH3HH7x3H dHHx" + echo "///////xHHH@_E&3D9N3H3HHHHHHHHHH3HW7dx///////////dMM ////#QJEEJJ9:Hp9dz^WdH_&d3p3HHHHdLQE3pNNHHHHHHHHH3HH9xHH &HHx" + echo "////////#@3H3^ HHN3WpHHHHHHHHHHHHHHH33d #/////// MM7//{x@pppH__ME_MME@xH3Hzz3H33HHHH@7pHH33HHHHHHHHHHH3HH9xHH__HHx" + echo "/////////{ HHHHHHHH333HHHHHHHHHHHHHHHHH33&x{///:Pz9HpWp@ :::{/pM^EMMML H3HELQ3H3HHHH_pHHHHHHHHHHHHHHHHHHp9\@d& HHx" + echo "///////////:dHHHHHHHH33HHHHHHHHHHHHHHHHHHHHd^\\H33d_x::##{/{x_PDxMMMME&HHHHH99H3HHHd&HHHHHHHHHHHHHHHHHH9Wx&^ x HHx" + echo "////////////{^HHHHHHHHHHHHHHHHHHHHHHHHHHH@&HN33@H77ppppppWW3&JMdHMMMMHHHHHHHHWHHHHH&3pWHHHHHHHHHH3HHHpz_ HHH3&xHHx" + echo "//////////////xHHHHHHHHHHHHHHHHHHHH3H3@d3p3@x:\{/# HN73& xx DL\DMMML_HHHHHHHHpHHHpH7JHHHHHHH3p33d&_@d\ 3HHH3&:3Hx" + echo "///////////////#&HHHHHHHHHHHHHHHHHHHH_H7^x\//#^ddH3^Nz@_&&&&_MN MMMMJ@H3HHHHH3p3H3pQQ3Hd@_^ xx ^@dH&\{x@HH3_\HHx" + echo "////////////////{ HHHHHHHHHHHHHHH@ x{{z\/#^33d {Nz3:LPx&&&&^WMxNMMMM3HHH3N3Hp79zWH _ xx _@dHHHHHHHHHHH^#{xd_{#@Hx" + echo "//////////////////\@HH3HHHHHH@^x#{: &&7_@ddH:7d@__&@Mp^&&&&xPz#LMMML&3HJMMzNH@^ ^&:@HHHHHHHHHHHHHHHHHHd&^{/////:Hx" + echo "/////////////{{{/{/{^dHHHd^x#{\ &HHH W ^@&Q9 _^ _d3QL ^ xWMW#dJMMz@HJMEx\x^HHHHHdx&HHHHHHHd&^ xx\#{//////////#Hx" + echo "/////////{x ^&:xEDW^ x#:## d^dH3HHHHxd_HHdMzHx# NMMM7x&dH_JLp7 {x\&@HHD@\:{#x ^HHHd&#x x\#{////////////////////{Hx" + echo "//////{#x^^&&\ _D9^x\{{:^NQP3HHHHHH pxHHd9MMdxx EMMM _^@@PD3PMMLEQH@3W& &^ x##\##{/////////////////////////////{@x" + echo "/////#:x^ @d:^^7N#{\ &HHWHH99HHHHHH:7 d&^LMM3^_dNpH_#^_3LEWMMMMMPddHpd:HHHH@ ^ x{#:////////////////////{x&////{x:" + echo "////:x ^x W:^^HWx3dHHHH333H39HHHH3@&&xxx@J7@#:_@&&d : ELp9MMMMMz&H333#dHHH3H\///{:3x##{/{##{/{#\\\##### W_{////^x/" + echo "//{ :x^ #E:^^&N\ELWHHH3dHHH3333HHH E\:xx__^_ d^EPML^dDPHDMMMMM7&H33N#&HHHHH /////{3//////////////////\3@{//////{//" + echo "/{ xx{x{7_ ^^N^xJNzN3Hp@HHHH39HH3H J: ^ NEQ__^zMMM9dLJ3LMMMML3@HHH9\ H3HHH&{/////^ ////////////////\3d{///////////" + echo "/x x///:9:^^p&x@3W3p3HHHpddd@H9HHH_Lx^ EMM7x&^MMMEEMWNMMMMMD@d3HH9 \HHHHHH //////7{//////////////#3H#/////////////" + echo "\_:#///@ ^d3^xHH33N3@x ^&dH&@HHH M_^^MML&x_3QJWQLdzMMMMMJ_HH3HNd#@HHHHHHx/////:W/////////////{x #///////////////" + echo "^&:///{p\^_W^x&HWH^x &_HHHHHHd HHH PNx9LQ7W_3N779d\WMMMMMW&HHH3WN{_HHHHHH3://///p ////////////////////////////////" + echo "@dx////Ep3Np^\@_:^dHHHd^Hd_ :\xHH3&EP^3d@d3Hp7zLMQ_{ JMLd@HHHHpE:&HHHHHHHH\/////@{////////////////////////////////" + echo "H@ :#//DLMM9_: :HHHHH@^x#/////#HHHd_NNNJPMMMMMMMMMMQ&{d_HHHHH3zx@HHHHHHHHH#&@@@@7@@dpdx{//////////////////////////" + echo "dJx #//_E_d&&^::_d_x#//\///////&HHH_HPMMMMMMMMMMLJ7Wp@_HHHHHHz_ HHHHHHHHHHx7QzJE97^x{/////////////////////////////" + echo "^z_ {/\{_9 ^{\#//////:///////xHHH3H&7MMMPJN3d__@HHHHHHH3HH73:HHHHHHHHHH ^ x#{@{///////////////////////////////" + echo "^dD \{ x#^7^x\/////////x///////#HH33HHHd3d37pHH3HHHHHHHHHHHp7#dHHHHHHHH&_EMMMMMMMPz7d #///////////////////////////" + echo "^^_73x ^ :xd^{/////////x////////_HHHHHH33NNWpp3HHHHHHHHHHHHE:^HHHHHH3H WMMMMMMMMDH7:\ JQ7d {//////////////////////" + echo "^^@JzE^\x^ :\x{////////_///\_WJHxHHHHHHHHHHH3HHHHHHHHHHHHH9&xHHHHHHd 9MMMMMMMMLJ J//xQ\7LMMQpx///////////////////" + echo " _3zLLLpx x{///////{_ zH9PP9H {{dHHHHHHHHHHHHHHHHHHHHHHHp9#dHHH3H@x^zMMMMMMMMMPJ d//93DMMMMMMMQH\////////////////" + echo " x^NE9dxx x://///////#dNP_\/{^H79@pHHHHHHHHHHHHHHHHHHH3H3zx_HHHHHH_&DMMMMMMMMMMDE :/ 7QMMMMMMMMMMMJ&#/////////////" + echo " @3pQMp^7N^x//////////// __7LMMMM JHHHHHHHHHHHHH3HHHHH3H9@ HHHHHHH&LMMMMMMMMMMMP7^#{EWMMMMMMMMMMMMMML7 {//////////" + echo "^^^&HLWd7d {///////////#3LMMMMMMMHpJpHHHHHHHHHHH3JQJ7NDWE\HHHH3H@ QMMMMMMMMMMMMLp&##LMMMMMMMMMMMMMMMMMMPd{////////" + echo "3WNNN7pH99Wd #/////////pHMMMMMMMMD N993HH3HHHHHHHH9LMMMLx@3HHHH_ JMMMMMMMMMMMMMMd@##MMMMMMMMMMMMMMMMMMMMMQ{///////" + echo "MMMMM3ELMMMMLE@&@_x////EWMMMMMMMMM3JJpEN3HHHHHHHH7DDMMMH HHHHd ^NMMMMMMMMMMMMMMM_d{#MMMMMMMMMMMMMMMMMMMMM@////////" + echo "MMMM7{DMMMMD3JMMMMDJ /#DzMMMMMMMMML^Qz7W9p3pWN7799zJE7d\x_HH@:_HMMMMMMMMMMMMMMMWx3{\MMMMMMMMMMMMMMMMMMMMD{////////" + echo "MMML:HMMMM9@MMMLdHJEH{ PLMMMMMMMMMME&pNp^^_^ x ^^__ \#x{&&LMMMMMMMMMMMMMMMJ:p{\MMMMMMMMMMMMMMMMMMMM_/////////" + echo "MMMW/7MMMM&LM3LdQz@JM33MMMMMMMMMMMMM&x _@dHHHHHHH@&_^ ^_ ^x:^LMMMMMMPQJ7Np333pW 3 xEDMMMMMMMMMMMMMMMMMJ//////////" + echo "MML\/EMMMP3_xNpJMM3^&WzMPMMMMMMMMMMML3x^^^^_&dH3W7EzPPpx\x\\xd333H&_&@pEQPLMMQPM7E9LHd@@3ELMMMMMMMMMMMM //////////" + echo "MM3//zMMMNW//QdMMM3 \#LQWMMMMMMMMDp@__&dd@@&__&&@dHpHd33 &x@33Hd@_^^^xMMMMMMM3pMx{HM x9/{\:@pNQMMMMMMMQ///////////" + echo "MMH::PMMMW7{/P_MMM7^{xM7^MMMMMMD@@W&&dH3Hx_x:7DzJ97Np &H^&x_dHd@&&@d3WMMMMMMME&M WMMN/7{/WHzMLEWWNELMMW///////////" + echo "_H_^^dNJzWH@/7HPMM@_/dD&@MMMLzp W@^7zE7pxHHN99EzQQ7_p9^H^_:&d&^ ^_&&MMMMMMMD M xMML#^x/{JMMMMMMMDW3Wx///////////" + echo "Hz^x::xx{/{x^p3dMQx /7N H3@^_@#dW#:_d@@HHHHHH3NPN&NLMQ H & NN799997WWzMMMMMMMLxM^&LMM^{&/#LMMMMMMMMMMLN^#/////////" + echo " x WpH@x\//////{@ { _^_@d^ ^__\dH MEH_d3HHHHHHN&3MMMMD H & H&&______ HMMMMMMMM P_3MMMN/&/#LMMMMMMMMMMMMML@////////" + echo "\{xH7x::\////////\7MM9@_^_@HHd d_ MMM9@7HHd@NWHp^WMMMDx3 & 3dHW7EzDLMLLDQzJE9NxHxxp33_/x{/NMMMMMMMMMMMMMMP#///////" + echo "# ^_ ^ ^x///////{9&MMJ^^^ ^_3xH_ MMM^73@_WQz93&@&@LLN_H __9WH&^ x:\#{\^&& {///{p//////////{x&WzLMMMMMMMMMH///////" + echo "^^ 3^^^#///////H_LMME&@dHH@ _x3@xPMz&&_7MMMMLDJHx\ 3pd #{/////////{x@HH&\//////7{////{#////////{x&WzMMMMML#//////" + echo ": :d^^^x/////// &PMMM&_dd@&^^ {^W7^p xxH@@&__37NNW7NH^:xxxx ^_&&@dHH&:////////p ////{^/////////////\&ELMM3//////" + echo "{\:xxxx#//////{Hp77p@ x#////////#&& xx::\\:xxxx ^_&&&&&@@@&&&&_____x//////////##/////&////////////////{^NQ#/////" + fi +]) diff --git a/libraries/eina/m4/eina/eina_bench.m4 b/libraries/eina/m4/eina/eina_bench.m4 new file mode 100644 index 0000000..e586eff --- /dev/null +++ b/libraries/eina/m4/eina/eina_bench.m4 @@ -0,0 +1,31 @@ +dnl use: EINA_BENCH_MODULE(foo-bar, have_bench, pkg_file [, ACTION-IF-FOUND [, ACTION-IF-NOT-FOUND]]) +AC_DEFUN([EINA_BENCH_MODULE], +[ +m4_pushdef([UP], m4_translit([$1], [-a-z], [_A-Z]))dnl +m4_pushdef([DOWN], m4_translit([$1], [-A-Z], [_a-z]))dnl + +want_module="$2" +have_module="no" + +AC_ARG_ENABLE([$1], + [AC_HELP_STRING([--disable-benchmark-$1], [disable benchmarking against ]DOWN[ @<:@default=yes@:>@])], + [ + if test "x${enableval}" = "xyes" ; then + enable_module="yes" + else + enable_module="no" + fi + ], + [enable_module="yes"]) + +if test "x${enable_module}" = "xyes" && test "x${want_module}" = "xyes"; then + PKG_CHECK_MODULES([]UP[], [$3], [have_module="yes"], [have_module="no"]) +fi + +AM_CONDITIONAL([EINA_HAVE_]UP[], [test "x${have_module}" = "xyes"]) + +AS_IF([test "x$have_module" = "xyes"], [$4], [$5]) + +m4_popdef([UP]) +m4_popdef([DOWN]) +]) diff --git a/libraries/eina/m4/eina/eina_check.m4 b/libraries/eina/m4/eina/eina_check.m4 new file mode 100644 index 0000000..f8a4f8c --- /dev/null +++ b/libraries/eina/m4/eina/eina_check.m4 @@ -0,0 +1,48 @@ +dnl use: EINA_CHECK_MODULE(foo-bar, have_dependency, description) +AC_DEFUN([EINA_CHECK_MODULE], +[ +m4_pushdef([UP], m4_translit([$1], [-a-z], [_A-Z]))dnl +m4_pushdef([DOWN], m4_translit([$1], [-A-Z], [_a-z]))dnl + +AC_ARG_ENABLE([mempool-$1], + [AC_HELP_STRING([--enable-mempool-$1], [enable build of $3 @<:@default=$2@:>@])], + [ + if test "x${enableval}" = "xyes" ; then + enable_module="yes" + else + if test "x${enableval}" = "xstatic" ; then + enable_module="static" + else + enable_module="no" + fi + fi + ], + [enable_module=$2]) + +have_module="no" +if test "x${enable_module}" = "xyes" || test "x${enable_module}" = "xstatic" ; then + have_module="yes" +fi + +AC_MSG_CHECKING([whether to enable $3 built]) +AC_MSG_RESULT([${have_module}]) + +static_module="no" +if test "x${enable_module}" = "xstatic" ; then + static_module="yes" + have_static_module="yes" + AC_DEFINE(EINA_STATIC_BUILD_[]UP, 1, [Set to 1 if $2 is statically built]) +fi + +if ! test "x${enable_module}" = "xno" ; then + AC_DEFINE(EINA_BUILD_[]UP, 1, [Set to 1 if $2 is built]) +fi + +AM_CONDITIONAL(EINA_BUILD_[]UP, [test "x${have_module}" = "xyes"]) +AM_CONDITIONAL(EINA_STATIC_BUILD_[]UP, [test "x${static_module}" = "xyes"]) + +enable_[]DOWN=${enable_module} + +m4_popdef([UP]) +m4_popdef([DOWN]) +]) diff --git a/libraries/eina/sha1-for-source.txt b/libraries/eina/sha1-for-source.txt new file mode 100644 index 0000000..6ad7a70 --- /dev/null +++ b/libraries/eina/sha1-for-source.txt @@ -0,0 +1 @@ +8b0b9aa8a4d4780406c294288ce3f30c7569ca9c diff --git a/libraries/eina/src/examples/eina_inarray_01.c b/libraries/eina/src/examples/eina_inarray_01.c new file mode 100644 index 0000000..b9ab4e7 --- /dev/null +++ b/libraries/eina/src/examples/eina_inarray_01.c @@ -0,0 +1,52 @@ +//Compile with: +//gcc -g eina_inarray_01.c -o eina_inarray_01 `pkg-config --cflags --libs eina` + +#include + +int +cmp(const void *a, const void *b) +{ + return *(int*)a > *(int*)b; +} + +int main(int argc, char **argv) +{ + Eina_Inarray *iarr; + char ch, *ch2; + int a, *b; + + eina_init(); + iarr = eina_inarray_new(sizeof(char), 0); + + ch = 'a'; + eina_inarray_append(iarr, &ch); + ch = 'b'; + eina_inarray_append(iarr, &ch); + ch = 'c'; + eina_inarray_append(iarr, &ch); + ch = 'd'; + eina_inarray_append(iarr, &ch); + + printf("Inline array of chars:\n"); + EINA_INARRAY_FOREACH(iarr, ch2) + printf("char: %c(pointer: %p)\n", *ch2, ch2); + + eina_inarray_flush(iarr); + eina_inarray_setup(iarr, sizeof(int), 4); + + a = 97; + eina_inarray_append(iarr, &a); + a = 98; + eina_inarray_append(iarr, &a); + a = 100; + eina_inarray_append(iarr, &a); + a = 99; + eina_inarray_insert_sorted(iarr, &a, cmp); + + printf("Inline array of integers with %d elements:\n", eina_inarray_count(iarr)); + EINA_INARRAY_FOREACH(iarr, b) + printf("int: %d(pointer: %p)\n", *b, b); + + eina_inarray_free(iarr); + eina_shutdown(); +} diff --git a/libraries/eina/src/examples/eina_model_01.c b/libraries/eina/src/examples/eina_model_01.c new file mode 100644 index 0000000..28895aa --- /dev/null +++ b/libraries/eina/src/examples/eina_model_01.c @@ -0,0 +1,235 @@ +/* + * Compile with: + * gcc -o eina_model_01 eina_model_01.c `pkg-config --cflags --libs eina` + */ + +/* + * This example demonstrates the usage of Eina Model by implementing + * Bank Account Class, which is inherited from Base Class; + * and Credit Card Class, which is inherited from Bank Account Class. + * + * Base Class(Eina_Model_Type) --> Bank Account Class --> Credit Card Class + * + * Bank Account Class implements "bank_account_data_set()" and "print()" methods; + * Credit Card Class inherits these two and implements "credit_card_data_set()" + * + * + * Bank Account Class::print() calls for "_bank_account_data_print" + * Credit Card Class ::print() is reloaded with "_credit_card_data_print()" + * which calls for parent function "_bank_account_data_print" + * + */ + + +#include +#include + +/* + * Defining type for new model type + * Model will have two methods + */ +typedef struct _Bank_Account_Type +{ + Eina_Model_Type parent_class; + void (*bank_account_data_set)(Eina_Model *, const char *name, const char *number); + void (*print)(Eina_Model *); +} Bank_Account_Type; + +/* + * Defining type for Bank Account private data + */ +typedef struct _Bank_Account_Data +{ + char name[30]; + char number[30]; +} Bank_Account_Data; + +/* + * Defining type for Credit Card model type, which will be inherited from Bank Account model type + * Model will have two parent's methods and additional one + */ +typedef struct _Credit_Card_Type +{ + Bank_Account_Type parent_class; + void (*credit_card_data_set)(Eina_Model *, const char *, const char *, int) ; +} Credit_Card_Type; + +/* + * Defining type for Credit Card private data + */ +typedef struct _Credit_Card_Data +{ + char number[30]; + char expiry_date[30]; + int pin; +} Credit_Card_Data; + +static Bank_Account_Type _BANK_ACCOUNT_TYPE; +static Credit_Card_Type _CREDIT_CARD_TYPE; +static Eina_Model_Type *BANK_ACCOUNT_TYPE = (Eina_Model_Type *) &_BANK_ACCOUNT_TYPE; +static Eina_Model_Type *CREDIT_CARD_TYPE = (Eina_Model_Type *) &_CREDIT_CARD_TYPE; + + +/* + * Defining method for for Bank Account data + */ +static void +_bank_account_data_set(Eina_Model *mdl, const char *name, const char *number) +{ + Bank_Account_Data *bdata = eina_model_type_private_data_get(mdl, BANK_ACCOUNT_TYPE); + + if (!bdata) + printf("ERROR\n"); + + if (name != NULL) + { + strncpy(bdata->name, name, sizeof(bdata->name)); + bdata->name[sizeof(bdata->number) - 1] = '\0'; + } + + if (number != NULL) + { + strncpy(bdata->number, number, sizeof(bdata->number)); + bdata->number[sizeof(bdata->number) - 1] = '\0'; + } + + printf("%s :: %s %p\n", eina_model_type_name_get(eina_model_type_get(mdl)) ,__func__, mdl); +} + + +static void +_credit_card_data_set(Eina_Model *mdl, const char *number, const char *expiry_date, int pin) +{ + Credit_Card_Data *cdata = eina_model_type_private_data_get(mdl, CREDIT_CARD_TYPE); + + if (!cdata) + printf("ERROR\n"); + + if (number != NULL) + { + strncpy(cdata->number, number, sizeof(cdata->number)); + cdata->number[sizeof(cdata->number) - 1] = '\0'; + } + + if (expiry_date != NULL) + { + strncpy(cdata->expiry_date, expiry_date, sizeof(cdata->expiry_date)); + cdata->expiry_date[sizeof(cdata->expiry_date) - 1] = '\0'; + } + + cdata->pin = pin; + printf("%s :: %s %p\n", eina_model_type_name_get(eina_model_type_get(mdl)) ,__func__, mdl); +} + +static void +_bank_account_data_print(Eina_Model *mdl) +{ + const Bank_Account_Data *bdata = eina_model_type_private_data_get(mdl, BANK_ACCOUNT_TYPE); + + printf("\n%s :: %s %p \n\tName: %s(%p)\n\tAccount: %s(%p)\n", eina_model_type_name_get(eina_model_type_get(mdl)) ,__func__, mdl + , bdata->name, bdata->name, bdata->number, bdata->number); +} + +static void +_credit_card_data_print(Eina_Model *mdl) +{ + void (*pf)(Eina_Model *); + const Eina_Model_Type *ptype = eina_model_type_parent_get(eina_model_type_get(mdl)); + //const Eina_Model_Type *ptype = eina_model_type_get(mdl); + + pf = eina_model_type_method_resolve(ptype, mdl, Bank_Account_Type, print); + if (pf) + pf(mdl); + else + printf("ERROR: %d", __LINE__); + const Credit_Card_Data *cdata = eina_model_type_private_data_get(mdl, CREDIT_CARD_TYPE); + printf("%s :: %s %p \n\tNumber: %s(%p)\n\tCC Expiry Date: %s(%p)\n\tCC PIN: %d(%p)\n", eina_model_type_name_get(eina_model_type_get(mdl)) ,__func__, mdl + , cdata->number, cdata->number, cdata->expiry_date, cdata->expiry_date, cdata->pin, &cdata->pin); +} + +#define BANK_ACCOUNT(x) ((Bank_Account_Type *) x) +#define CREDIT_CARD(x) ((Credit_Card_Type *) x) + +void +bank_account_data_set(Eina_Model *mdl, const char *name, char *number) +{ + + EINA_SAFETY_ON_FALSE_RETURN(eina_model_instance_check(mdl, BANK_ACCOUNT_TYPE)); + + void (*pf)(Eina_Model *, const char *, const char *); + pf = eina_model_method_resolve(mdl, Bank_Account_Type, bank_account_data_set); + if (pf) + pf(mdl, name, number); + else + printf("ERROR %d\n", __LINE__); +} + +void +data_print(Eina_Model *mdl) +{ + EINA_SAFETY_ON_FALSE_RETURN(eina_model_instance_check(mdl, BANK_ACCOUNT_TYPE)); + + void (*pf)(Eina_Model *); + pf = eina_model_method_resolve(mdl, Bank_Account_Type, print); + if (pf) + pf(mdl); + else + printf("ERROR %d\n", __LINE__); +} + +void +credit_card_data_set(Eina_Model *mdl, const char *number, const char *expiry_date, int pin) +{ + EINA_SAFETY_ON_FALSE_RETURN(eina_model_instance_check(mdl, CREDIT_CARD_TYPE)); + + void (*pf)(Eina_Model *, const char *, const char *, int); + pf = eina_model_method_resolve(mdl, Credit_Card_Type, credit_card_data_set); + if (pf) + pf(mdl, number, expiry_date, pin); + else + printf("ERROR %d\n", __LINE__); +} + +int main(void) +{ + Eina_Model *b, *cc; + + eina_init(); + + memset(&_BANK_ACCOUNT_TYPE, 0, sizeof(_BANK_ACCOUNT_TYPE)); + memset(&_CREDIT_CARD_TYPE, 0, sizeof(_CREDIT_CARD_TYPE)); + + BANK_ACCOUNT_TYPE->version = EINA_MODEL_TYPE_VERSION; + BANK_ACCOUNT_TYPE->type_size = sizeof(Bank_Account_Type); + BANK_ACCOUNT_TYPE->private_size = sizeof(Bank_Account_Data); + BANK_ACCOUNT_TYPE->name = "Bank_Account_Model"; + BANK_ACCOUNT_TYPE->parent = EINA_MODEL_TYPE_GENERIC; + BANK_ACCOUNT(BANK_ACCOUNT_TYPE)->bank_account_data_set = _bank_account_data_set; + BANK_ACCOUNT(BANK_ACCOUNT_TYPE)->print = _bank_account_data_print; + + CREDIT_CARD_TYPE->version = EINA_MODEL_TYPE_VERSION; + CREDIT_CARD_TYPE->type_size = sizeof(Credit_Card_Type); + CREDIT_CARD_TYPE->private_size = sizeof(Credit_Card_Data); + CREDIT_CARD_TYPE->name = "Credit_Card_Model"; + CREDIT_CARD_TYPE->parent = BANK_ACCOUNT_TYPE; + CREDIT_CARD(CREDIT_CARD_TYPE)->credit_card_data_set = _credit_card_data_set; + BANK_ACCOUNT(CREDIT_CARD_TYPE)->print = _credit_card_data_print; + + b = eina_model_new(BANK_ACCOUNT_TYPE); //creating object of bank class + cc = eina_model_new(CREDIT_CARD_TYPE); //creating object of credit card class + + bank_account_data_set(b, "Bill Clark", "8569214756"); + bank_account_data_set(cc, "John Smith", "3154789"); + + credit_card_data_set(cc, "5803 6589 4786 3279 9173", "01/01/2015", 1234); + + data_print(b); + data_print(cc); + + eina_model_unref(b); + eina_model_unref(cc); + + eina_shutdown(); + + return 0; +} diff --git a/libraries/eina/src/examples/eina_model_02.c b/libraries/eina/src/examples/eina_model_02.c new file mode 100644 index 0000000..a2cb693 --- /dev/null +++ b/libraries/eina/src/examples/eina_model_02.c @@ -0,0 +1,61 @@ +//Compile with: +//gcc -g eina_model_02.c -o eina_model_02 `pkg-config --cflags --libs eina` + +#include + +static void _cb_on_deleted(void *data, Eina_Model *model, const Eina_Model_Event_Description *desc, void *event_info); + +int main(void) +{ + Eina_Model *m; + char *s; + int i; + + eina_init(); + + m = eina_model_new(EINA_MODEL_TYPE_GENERIC); + + eina_model_event_callback_add(m, "deleted", _cb_on_deleted, NULL); + + //Adding properties to model + for (i = 0; i < 5; i++) + { + Eina_Value val; + char name[2] = {'a'+ i, 0}; + eina_value_setup(&val, EINA_VALUE_TYPE_INT); + eina_value_set(&val, i); + eina_model_property_set(m, name, &val); + eina_value_flush(&val); + } + + //Adding children to model + for (i = 0; i < 5; i++) + { + Eina_Value val; + Eina_Model *c = eina_model_new(EINA_MODEL_TYPE_GENERIC); + eina_value_setup(&val, EINA_VALUE_TYPE_INT); + eina_value_set(&val, i); + eina_model_property_set(c, "x", &val); + + eina_model_event_callback_add(c, "deleted", _cb_on_deleted, NULL); + + eina_model_child_append(m, c); + //Now that the child has been appended to a model, it's parent will manage it's lifecycle + eina_model_unref(c); + eina_value_flush(&val); + } + + s = eina_model_to_string(m); + printf("model as string:\n%s\n", s); + + free(s); + eina_model_unref(m); + eina_shutdown(); + + return 0; +} + +static void _cb_on_deleted(void *data, Eina_Model *model, const Eina_Model_Event_Description *desc, void *event_info) +{ + printf("deleted %p\n", model); +} diff --git a/libraries/eina/src/examples/eina_model_03.c b/libraries/eina/src/examples/eina_model_03.c new file mode 100644 index 0000000..a5e2562 --- /dev/null +++ b/libraries/eina/src/examples/eina_model_03.c @@ -0,0 +1,236 @@ +//Compile with: +//gcc -g eina_model_03.c -o eina_model_03 `pkg-config --cflags --libs eina` + +#include +#include +#include +#include + +static Eina_Model_Type *ADDRESS_BOOK_TYPE; +static Eina_Model_Type *PERSON_TYPE; + +static void address_book_init(void); + +int main(void) +{ + Eina_Model *address_book; + Eina_Value val; + int i, count; + char *s; + + eina_init(); + + address_book_init(); + + address_book = eina_model_new(ADDRESS_BOOK_TYPE); + + eina_value_setup(&val, EINA_VALUE_TYPE_STRING); + eina_value_set(&val, "addr_book.txt"); + eina_model_property_set(address_book, "filename", &val); + eina_value_flush(&val); + + eina_model_load(address_book); + s = eina_model_to_string(address_book); + printf("model as string:\n%s\n\n", s); + free(s); + + count = eina_model_child_count(address_book); + printf("Address Book with %d entries:\n", count); + for (i = 0; i < count; i++) + { + Eina_Model *person = eina_model_child_get(address_book, i); + Eina_Value nameval, emailval; + const char *name, *email; + + eina_model_property_get(person, "name", &nameval); + eina_model_property_get(person, "email", &emailval); + + eina_value_get(&nameval, &name); + eina_value_get(&emailval, &email); + + printf("%02d \"%s\" <%s>\n", i, name, email); + + // We don't need property values anymore + eina_value_flush(&nameval); + eina_value_flush(&emailval); + + // We don't need our reference to person anymore + eina_model_unref(person); + } + + eina_model_unref(address_book); + eina_shutdown(); + + return 0; +} + +// Structure Descriptions are just used internally in the type constructors: +static Eina_Value_Struct_Desc *ADDRESS_BOOK_DESC; +static Eina_Value_Struct_Desc *PERSON_DESC; + +static Eina_Bool +_person_constructor(Eina_Model *model) +{ + // call parent type constructor, like "super" in other languages: + if (!eina_model_type_constructor(EINA_MODEL_TYPE_STRUCT, model)) + return EINA_FALSE; + + // Do specific setup of our internal structure, letting it know about + // our description + return eina_model_struct_set(model, PERSON_DESC, NULL); +} + +static Eina_Bool +_address_book_constructor(Eina_Model *model) +{ + // call parent type constructor, like "super" in other languages: + if (!eina_model_type_constructor(EINA_MODEL_TYPE_STRUCT, model)) + return EINA_FALSE; + + // Do specific setup of our internal structure, letting it know about + // our description + return eina_model_struct_set(model, ADDRESS_BOOK_DESC, NULL); +} + +static Eina_Bool +_address_book_load(Eina_Model *model) +{ + const char *filename; + Eina_Value val; + char buf[256]; + FILE *f; + + // We retrieve filename from property of same name: + eina_model_property_get(model, "filename", &val); + eina_value_get(&val, &filename); + + EINA_SAFETY_ON_NULL_RETURN_VAL(filename, EINA_FALSE); + + f = fopen(filename, "r"); + + // Now that we have used filename, we must free its memory holder: + eina_value_flush(&val); + + EINA_SAFETY_ON_NULL_RETURN_VAL(f, EINA_FALSE); + + while (fgets(buf, sizeof(buf), f)) + { + Eina_Model *person; + char *name, *email; + + if (strlen(buf) <= 1) + continue; + + name = strtok(buf, "\t"); + email = strtok(NULL, "\n"); + + if ((!name) || (!email)) continue; + + // Create person + person = eina_model_new(PERSON_TYPE); + + // Setup value type as string, as our properties are strings: + eina_value_setup(&val, EINA_VALUE_TYPE_STRING); + + // Set string properties: + eina_value_set(&val, name); + eina_model_property_set(person, "name", &val); + + eina_value_set(&val, email); + eina_model_property_set(person, "email", &val); + + // Flush value, free string + eina_value_flush(&val); + + // Add person to the end of model children + eina_model_child_append(model, person); + + // Model already holds its reference to person, we release ours + eina_model_unref(person); + } + + fclose(f); + return EINA_TRUE; +} + +static void +address_book_init(void) +{ + // Declare type for internal struct, this is just used to easily + // create Eina_Value_Struct_Member array for Eina_Value_Struct_Desc. + // + // We don't need this structure outside address_book_init() + // as it is managed automatically by Eina_Value_Struct, used by + // Eina_Model_Struct! Handy! :-) + typedef struct _Person Person; + struct _Person + { + const char *name; + const char *email; + }; + static Eina_Value_Struct_Member person_members[] = { + // no eina_value_type as they are not constant initializers, see below. + EINA_VALUE_STRUCT_MEMBER(NULL, Person, name), + EINA_VALUE_STRUCT_MEMBER(NULL, Person, email) + }; + // Values that cannot be set on static declarations since they are not + // constant initializers. It is a nitpick from C that we need to deal with + // here and on all our other declarations. + person_members[0].type = EINA_VALUE_TYPE_STRING; + person_members[1].type = EINA_VALUE_TYPE_STRING; + + static Eina_Value_Struct_Desc person_desc = { + EINA_VALUE_STRUCT_DESC_VERSION, + NULL, // no special operations + person_members, + EINA_C_ARRAY_LENGTH(person_members), + sizeof(Person) + }; + static Eina_Model_Type person_type = EINA_MODEL_TYPE_INIT_NOPRIVATE + ("Person_Type", + Eina_Model_Type, + NULL, // no type as EINA_MODEL_TYPE_STRUCT is not constant initializer! + NULL, // no extra interfaces + NULL // no extra events); + ); + person_type.parent = EINA_MODEL_TYPE_STRUCT; + // Set our overloaded methods: + person_type.constructor = _person_constructor; + + typedef struct _Address_Book Address_Book; + struct _Address_Book + { + const char *filename; + }; + static Eina_Value_Struct_Member address_book_members[] = { + // no eina_value_type as they are not constant initializers, see below. + EINA_VALUE_STRUCT_MEMBER(NULL, Address_Book, filename) + }; + address_book_members[0].type = EINA_VALUE_TYPE_STRING; + static Eina_Value_Struct_Desc address_book_desc = { + EINA_VALUE_STRUCT_DESC_VERSION, + NULL, // no special operations + address_book_members, + EINA_C_ARRAY_LENGTH(address_book_members), + sizeof(Address_Book) + }; + static Eina_Model_Type address_book_type = EINA_MODEL_TYPE_INIT_NOPRIVATE + ("Address_Book_Type", + Eina_Model_Type, + NULL, // no type as EINA_MODEL_TYPE_STRUCT is not constant initializer! + NULL, // no extra interfaces + NULL // no extra events); + ); + address_book_type.parent = EINA_MODEL_TYPE_STRUCT; + // Set our overloaded methods: + address_book_type.constructor = _address_book_constructor; + address_book_type.load = _address_book_load; + + // Expose the configured pointers to public usage: + // NOTE: they are static, so they live after this function returns! + PERSON_TYPE = &person_type; + PERSON_DESC = &person_desc; + + ADDRESS_BOOK_TYPE = &address_book_type; + ADDRESS_BOOK_DESC = &address_book_desc; +} diff --git a/libraries/eina/src/examples/eina_model_04_animal.c b/libraries/eina/src/examples/eina_model_04_animal.c new file mode 100644 index 0000000..bc9f06b --- /dev/null +++ b/libraries/eina/src/examples/eina_model_04_animal.c @@ -0,0 +1,76 @@ +/* + * animal.c + */ + +#include "eina_model_04_animal.h" + +static Eina_Bool initialized = EINA_FALSE; + +static void +_animal_eat(Eina_Model *m) +{ + printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)), + __func__); + printf("\t\t Eat Animal\n"); +} + +static void +_animal_breathe(Eina_Model *m) +{ + printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)), + __func__); + printf("\t\t Breathe Animal\n"); +} + +const char *ANIMAL_MODEL_TYPE_NAME = NULL; +static Animal_Type _ANIMAL_TYPE; + +const Eina_Model_Type * const ANIMAL_TYPE = (Eina_Model_Type *) &_ANIMAL_TYPE; + +void +animal_init(void) +{ + Eina_Model_Type *type; + + if (initialized) return; + initialized = EINA_TRUE; + + ANIMAL_MODEL_TYPE_NAME = "Animal_Model_Type"; + + type = (Eina_Model_Type *)&_ANIMAL_TYPE; + type->version = EINA_MODEL_TYPE_VERSION; + type->name = ANIMAL_MODEL_TYPE_NAME; + type->private_size = 0; + + eina_model_type_subclass_setup(type, EINA_MODEL_TYPE_GENERIC); + + /* define extra methods */ + + type->type_size = sizeof(Animal_Type); + ANIMAL_TYPE(type)->breathe = _animal_breathe; + ANIMAL_TYPE(type)->eat = _animal_eat; +} + +void +animal_breathe(Eina_Model *m) +{ + EINA_SAFETY_ON_FALSE_RETURN(eina_model_instance_check(m, ANIMAL_TYPE)); + + void (*pf)(Eina_Model *m); + pf = eina_model_method_resolve(m, Animal_Type, breathe); + EINA_SAFETY_ON_NULL_RETURN(pf); + printf("%s() \t", __func__); + pf(m); +} + +void +animal_eat(Eina_Model *m) +{ + EINA_SAFETY_ON_FALSE_RETURN(eina_model_instance_check(m, ANIMAL_TYPE)); + + void (*pf)(Eina_Model *m); + pf = eina_model_method_resolve(m, Animal_Type, eat); + EINA_SAFETY_ON_NULL_RETURN(pf); + printf("%s() \t", __func__); + pf(m); +} diff --git a/libraries/eina/src/examples/eina_model_04_child.c b/libraries/eina/src/examples/eina_model_04_child.c new file mode 100644 index 0000000..59b8aa5 --- /dev/null +++ b/libraries/eina/src/examples/eina_model_04_child.c @@ -0,0 +1,81 @@ +/* + * child.c + */ + +#include "eina_model_04_child.h" +#include "eina_model_04_whistler.h" + +static Eina_Bool initialized = EINA_FALSE; + +static void +_child_cry(Eina_Model *m) +{ + printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)), + __func__); + printf("\t\t Cry Child\n"); +} + +static void +_child_dive(Eina_Model *m) +{ + printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)), + __func__); + printf("\t\t Dive Child\n"); +} + +const char *CHILD_MODEL_TYPE_NAME = NULL; + +static Child_Type _CHILD_TYPE; +const Eina_Model_Type * const CHILD_TYPE = (Eina_Model_Type *) &_CHILD_TYPE; + +static const Diver_Interface _DIVER_INTERFACE; +static const Eina_Model_Interface * const DIVER_INTERFACE = + (Eina_Model_Interface *) &_DIVER_INTERFACE; + +static const Eina_Model_Interface * CLASS_INTERFACE_ARRAY[] = + { &_DIVER_INTERFACE.base_interface, NULL }; //this array is for model + +void +child_init() +{ + Eina_Model_Type *type; + + if (initialized) return; + initialized = EINA_TRUE; + + human_init(); + + //overriding Diver Interface + Eina_Model_Interface * iface = (Eina_Model_Interface *) &_DIVER_INTERFACE; + iface->version = EINA_MODEL_INTERFACE_VERSION; + iface->interface_size = sizeof(Diver_Interface); + iface->name = DIVER_INTERFACE_NAME; + DIVER_INTERFACE(iface)->dive = _child_dive; + + //creating instance of Child type + CHILD_MODEL_TYPE_NAME = "Child_Model_Type"; + + type = (Eina_Model_Type *) &_CHILD_TYPE; + type->version = EINA_MODEL_TYPE_VERSION; + type->name = CHILD_MODEL_TYPE_NAME; + + eina_model_type_subclass_setup(type, HUMAN_TYPE); + + type->type_size = sizeof(Child_Type); + type->interfaces = CLASS_INTERFACE_ARRAY; + + CHILD_TYPE(type)->cry = _child_cry; +} + +//call for implemented Child Class function +void +child_cry(Eina_Model *m) +{ + EINA_SAFETY_ON_FALSE_RETURN(eina_model_instance_check(m, CHILD_TYPE)); + + void (*pf)(Eina_Model *m); + pf = eina_model_method_resolve(m, Child_Type, cry); + EINA_SAFETY_ON_NULL_RETURN(pf); + printf("%s() \t\t", __func__); + pf(m); +} diff --git a/libraries/eina/src/examples/eina_model_04_human.c b/libraries/eina/src/examples/eina_model_04_human.c new file mode 100644 index 0000000..d9a10ab --- /dev/null +++ b/libraries/eina/src/examples/eina_model_04_human.c @@ -0,0 +1,157 @@ +/* + * human.c + * + */ + +#include "eina_model_04_human.h" +#include "eina_model_04_whistler.h" + +static Eina_Bool initialized = EINA_FALSE; + +static void +_human_eat(Eina_Model *m) +{ + printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)), + __func__); + printf("\t\t Salad\n"); +} + +static void +_human_walk(Eina_Model *m) +{ + printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)), + __func__); + printf("\t\t Walk\n"); +} + +static void +_human_whistle(Eina_Model *m) +{ + printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)), + __func__); + printf("\t\t Whistle Human\n"); +} + +static void +_human_swim(Eina_Model *m) +{ + printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)), + __func__); + printf("\t\t Swim Human\n"); +} + +static void +_human_dive(Eina_Model *m) +{ + printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)), + __func__); + printf("\t\t Dive Human\n"); +} +/* + * defining Human Model Instance + * defining Whistler Interface instance + * defining Swimmer Interface instance + * defining Diver Interface instance + */ + +const char *HUMAN_MODEL_TYPE_NAME = NULL; + +static Human_Type _HUMAN_TYPE; +const Eina_Model_Type * const HUMAN_TYPE = (Eina_Model_Type *) &_HUMAN_TYPE; + +static const Whistler_Interface _WHISTLER_INTERFACE; +static const Eina_Model_Interface * const WHISTLER_INTERFACE = + (Eina_Model_Interface *) &_WHISTLER_INTERFACE; + +static const Swimmer_Interface _SWIMMER_INTERFACE; +static const Eina_Model_Interface * const SWIMMER_INTERFACE = + (Eina_Model_Interface *) &_SWIMMER_INTERFACE; + +static const Diver_Interface _DIVER_INTERFACE; +static const Eina_Model_Interface * const DIVER_INTERFACE = + (Eina_Model_Interface *) &_DIVER_INTERFACE; + +/* + * defining parent interfaces for Diver Interface instance + * defining Interfaces for Human Model instance + */ +static const Eina_Model_Interface * PARENT_INTERFACES_ARRAY[] = + { &_SWIMMER_INTERFACE.base_interface, NULL }; //this array is for model +static const Eina_Model_Interface * MODEL_INTERFACES_ARRAY[] = + { &_WHISTLER_INTERFACE.base_interface, &_DIVER_INTERFACE.base_interface, + NULL }; //this array is for model + +void +human_init() +{ + Eina_Model_Type *type; + + if (initialized) return; + initialized = EINA_TRUE; + + animal_init(); + + /* + * Initializing Whistler Interface Instance + */ + Eina_Model_Interface *iface = (Eina_Model_Interface *) &_WHISTLER_INTERFACE; + iface->version = EINA_MODEL_INTERFACE_VERSION; + iface->interface_size = sizeof(Whistler_Interface); + iface->name = WHISTLER_INTERFACE_NAME; + WHISTLER_INTERFACE(iface)->whistle = _human_whistle; + + /* + * Initializing Swimmer Interface Instance + */ + iface = (Eina_Model_Interface *) &_SWIMMER_INTERFACE; + iface->version = EINA_MODEL_INTERFACE_VERSION; + iface->interface_size = sizeof(Swimmer_Interface); + iface->name = SWIMMER_INTERFACE_NAME; + SWIMMER_INTERFACE(iface)->swim = _human_swim; + + /* + * Initializing Diver Interface Instance + * Diver_Interface is inherited from Swimmer + */ + iface = (Eina_Model_Interface *) &_DIVER_INTERFACE; + iface->version = EINA_MODEL_INTERFACE_VERSION; + iface->interface_size = sizeof(Diver_Interface); + iface->name = DIVER_INTERFACE_NAME; + iface->interfaces = PARENT_INTERFACES_ARRAY; + DIVER_INTERFACE(iface)->dive = _human_dive; + + /* + * Initializing instance of Human Model + */ + + HUMAN_MODEL_TYPE_NAME = "Human_Model_Type"; + + type = (Eina_Model_Type *) &_HUMAN_TYPE; + type->version = EINA_MODEL_TYPE_VERSION; + type->name = HUMAN_MODEL_TYPE_NAME; + type->private_size = 0; + + eina_model_type_subclass_setup(type, ANIMAL_TYPE); + + type->type_size = sizeof(Human_Type); + type->interfaces = MODEL_INTERFACES_ARRAY; + + ANIMAL_TYPE(type)->eat = _human_eat; + HUMAN_TYPE(type)->walk =_human_walk; +} + + +/* + * call for implemented Human Class function + */ +void +human_walk(Eina_Model *m) +{ + EINA_SAFETY_ON_FALSE_RETURN(eina_model_instance_check(m, HUMAN_TYPE)); + + void (*pf)(Eina_Model *m); + pf = eina_model_method_resolve(m, Human_Type, walk); + EINA_SAFETY_ON_NULL_RETURN(pf); + printf("%s() \t", __func__); + pf(m); +} diff --git a/libraries/eina/src/examples/eina_model_04_main.c b/libraries/eina/src/examples/eina_model_04_main.c new file mode 100644 index 0000000..0e419ec --- /dev/null +++ b/libraries/eina/src/examples/eina_model_04_main.c @@ -0,0 +1,110 @@ +/* + * main_animal.c + * compile with: gcc eina_model_04_*.c -o eina_model_04 `pkg-config --cflags --libs eina` + */ + +/* + * This example demonstrates the extended usage of Eina Model. + * Class inheritance and interface implementation + * + * Animal Class is inherited from BaseClass and implements + * "_breathe_animal()" and "_eat_animal()" methods. + * + * Human Class is inherited from Animal class. + * Parrot Class is inherited from Animal class. + * + * Child Class is inherited from Human class. + * + * Human Class and Parrot Class implement Whistler Interface. + * Human Class implements Diver Interface. Diver Interface inherited from Swimmer Interface + * + * + * Animal Class (inherited from Base Class) + * + _breathe_animal() + * + _eat_animal() + * / -------/ \-------------\ + * / \ + * Human Class Parrot Class + * inherits inherits + * + animal_breathe() + animal_breathe() + * overrides overrides + * + animal_eat(); + animal_eat(); + * implements implements + * + human_walk(); + parrot_fly(); + * + * implements Whistler, Swimmer, implements Whistler, + * Diver Interfaces: + whistler_whistle() + * + whistler_whistle() + * + swimmer_swim() + * + diver_dive() + * + * ---------------------------------------------------------- + * | Swim_Interface | + * | + swim() | + * | | | + * | | | + * | Dive Intarface (inherited from Swim Interface) | + * | + dive() | + * --------------------------------------------------------- + * | + * | + * Child Class + * + inherits all parent's methods + * + implements cry_child() + * + overrides dive() interface method + */ + +#include +#include "eina_model_04_human.h" +#include "eina_model_04_parrot.h" +#include "eina_model_04_child.h" +#include "eina_model_04_whistler.h" + +int +main() +{ + Eina_Model *h, *p, *c; + + eina_init(); + + human_init(); + parrot_init(); + child_init(); + + h = eina_model_new(HUMAN_TYPE); + p = eina_model_new(PARROT_TYPE); + c = eina_model_new(CHILD_TYPE); + + animal_breathe(p); + animal_eat(p); + parrot_fly(p); + whistler_whistle(p); + + printf("\n"); + animal_breathe(h); + animal_eat(h); + human_walk(h); + whistler_whistle(h); + swimmer_swim(h); + diver_dive(h); + + printf("\n"); + animal_breathe(c); + animal_eat(c); + human_walk(c); + whistler_whistle(c); + swimmer_swim(c); + diver_dive(c); + child_cry(c); + + eina_model_unref(c); + eina_model_unref(h); + eina_model_unref(p); + + eina_shutdown(); + + return 0; +} + + + diff --git a/libraries/eina/src/examples/eina_model_04_parrot.c b/libraries/eina/src/examples/eina_model_04_parrot.c new file mode 100644 index 0000000..ac619ee --- /dev/null +++ b/libraries/eina/src/examples/eina_model_04_parrot.c @@ -0,0 +1,95 @@ +/* + * parrot.c + */ + +#include "eina_model_04_parrot.h" +#include "eina_model_04_whistler.h" + +static Eina_Bool initialized = EINA_FALSE; + +static void +_parrot_fly(Eina_Model *m) +{ + printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)), + __func__); + printf("\t\t Fly Parrot\n"); +} + +static void +_parrot_eat(Eina_Model *m) +{ + printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)), + __func__); + printf("\t\t Grain \n"); +} + +static void +_parrot_whistle(Eina_Model *m) +{ + printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)), + __func__); + printf("\t\t Whistle Parrot\n"); +} + +/* + * defining Parrot Model Instance + * defining Whistler Interface instance + */ +const char *PARROT_MODEL_TYPE_NAME = NULL; + +static Parrot_Type _PARROT_TYPE; +const Eina_Model_Type * const PARROT_TYPE = (Eina_Model_Type *) &_PARROT_TYPE; + +static const Whistler_Interface _WHISTLER_INTERFACE; +static const Eina_Model_Interface * const WHISTLER_INTERFACE = + (Eina_Model_Interface *) &_WHISTLER_INTERFACE; + +static const Eina_Model_Interface * MODEL_INTERFACES_ARRAY[] = + { &_WHISTLER_INTERFACE.base_interface, NULL }; //this array is for model + +void +parrot_init() +{ + Eina_Model_Type *type; + if (initialized) return; + initialized = EINA_TRUE; + + animal_init(); + /* + *overriding Whistler Interface (creating instance of Whistler Interface) + */ + Eina_Model_Interface *iface = (Eina_Model_Interface *) &_WHISTLER_INTERFACE; + iface->version = EINA_MODEL_INTERFACE_VERSION; + iface->interface_size = sizeof(Whistler_Interface); + iface->name = WHISTLER_INTERFACE_NAME; + WHISTLER_INTERFACE(iface)->whistle = _parrot_whistle; + + PARROT_MODEL_TYPE_NAME = "Parrot_Model_Type"; + + type = (Eina_Model_Type *)&_PARROT_TYPE; + type->version = EINA_MODEL_TYPE_VERSION; + type->name = PARROT_MODEL_TYPE_NAME; + type->private_size = 0; + + eina_model_type_subclass_setup(type, ANIMAL_TYPE); + + type->type_size = sizeof(Parrot_Type); + type->interfaces = MODEL_INTERFACES_ARRAY; + + ANIMAL_TYPE(type)->eat = _parrot_eat; + PARROT_TYPE(type)->fly = _parrot_fly; +} + + +void +parrot_fly(Eina_Model *m) +{ + EINA_SAFETY_ON_FALSE_RETURN(eina_model_instance_check(m, PARROT_TYPE)); + + void (*pf)(Eina_Model *m); + pf = eina_model_method_resolve(m, Parrot_Type, fly); + EINA_SAFETY_ON_NULL_RETURN(pf); + printf("%s() \t", __func__); + pf(m); +} + diff --git a/libraries/eina/src/examples/eina_model_04_whistler.c b/libraries/eina/src/examples/eina_model_04_whistler.c new file mode 100644 index 0000000..ed9832f --- /dev/null +++ b/libraries/eina/src/examples/eina_model_04_whistler.c @@ -0,0 +1,59 @@ +/* + * whistler.c + * + */ + +#include "eina_model_04_whistler.h" + +void +whistler_whistle(Eina_Model *m) +{ + const Eina_Model_Interface *iface = NULL; + iface = eina_model_interface_get(m, WHISTLER_INTERFACE_NAME); + + EINA_SAFETY_ON_NULL_RETURN(iface); + + void (*pf)(Eina_Model *); + + pf = eina_model_interface_method_resolve(iface, m, Whistler_Interface, whistle); + EINA_SAFETY_ON_NULL_RETURN(pf); + printf("%s() \t", __func__); + pf(m); +} +/* + * call for overridden Swimmer Interface function + */ +void +swimmer_swim(Eina_Model *m) +{ + const Eina_Model_Interface *iface = NULL; + iface = eina_model_interface_get(m, SWIMMER_INTERFACE_NAME); + + EINA_SAFETY_ON_NULL_RETURN(iface); + + void (*pf)(Eina_Model *); + + pf = eina_model_interface_method_resolve(iface, m, Swimmer_Interface, swim); + EINA_SAFETY_ON_NULL_RETURN(pf); + printf("%s() \t", __func__); + pf(m); +} + +/* + * call for overridden Diver Interface function + */ +void +diver_dive(Eina_Model *m) +{ + const Eina_Model_Interface *iface = NULL; + iface = eina_model_interface_get(m, DIVER_INTERFACE_NAME); + + EINA_SAFETY_ON_NULL_RETURN(iface); + + void (*pf)(Eina_Model *); + + pf = eina_model_interface_method_resolve(iface, m, Diver_Interface, dive); + EINA_SAFETY_ON_NULL_RETURN(pf); + printf("%s() \t", __func__); + pf(m); +} diff --git a/libraries/eina/src/examples/eina_value_01.c b/libraries/eina/src/examples/eina_value_01.c new file mode 100644 index 0000000..8a20828 --- /dev/null +++ b/libraries/eina/src/examples/eina_value_01.c @@ -0,0 +1,53 @@ +//Compile with: +//gcc eina_value_01.c -o eina_value_01 `pkg-config --cflags --libs eina` + +#include + +int main(int argc, char **argv) +{ + Eina_Value v; + int i; + char *newstr; + + eina_init(); + + eina_value_setup(&v, EINA_VALUE_TYPE_INT); + eina_value_set(&v, 123); + eina_value_get(&v, &i); + printf("v=%d\n", i); + + newstr = eina_value_to_string(&v); + printf("v as string: %s\n", newstr); + free(newstr); // it was allocated by eina_value_to_string() + eina_value_flush(&v); // destroy v contents, will not use anymore + + const char *s; + eina_value_setup(&v, EINA_VALUE_TYPE_STRING); + eina_value_set(&v, "My string"); + eina_value_get(&v, &s); + printf("v=%s (pointer: %p)\n", s, s); + + newstr = eina_value_to_string(&v); + printf("v as string: %s (pointer: %p)\n", newstr, newstr); + free(newstr); // it was allocated by eina_value_to_string() + eina_value_flush(&v); // destroy v contents, string 's' is not valid anymore! + + Eina_Value otherv; + eina_value_setup(&otherv, EINA_VALUE_TYPE_STRING); + eina_value_setup(&v, EINA_VALUE_TYPE_INT); + + // convert from int to string: + eina_value_set(&v, 123); + eina_value_convert(&v, &otherv); + eina_value_get(&otherv, &s); + printf("otherv=%s\n", s); + + // and the other way around! + eina_value_set(&otherv, "33"); + eina_value_convert(&otherv, &v); + eina_value_get(&v, &i); + printf("v=%d\n", i); + + eina_value_flush(&otherv); + eina_value_flush(&v); +} diff --git a/libraries/eina/src/examples/eina_value_02.c b/libraries/eina/src/examples/eina_value_02.c new file mode 100644 index 0000000..9c659ec --- /dev/null +++ b/libraries/eina/src/examples/eina_value_02.c @@ -0,0 +1,100 @@ +//Compile with: +//gcc eina_value_02.c -o eina_value_02 `pkg-config --cflags --libs eina` + +#include + +static Eina_Value_Struct_Desc *V1_DESC = NULL; +static Eina_Value_Struct_Desc *V2_DESC = NULL; + +void value_init(void) +{ + typedef struct _My_Struct_V1 { + int param1; + char param2; + } My_Struct_V1; + + + static Eina_Value_Struct_Member v1_members[] = { + // no eina_value_type as they are not constant initializers, see below. + EINA_VALUE_STRUCT_MEMBER(NULL, My_Struct_V1, param1), + EINA_VALUE_STRUCT_MEMBER(NULL, My_Struct_V1, param2) + }; + v1_members[0].type = EINA_VALUE_TYPE_INT; + v1_members[1].type = EINA_VALUE_TYPE_CHAR; + static Eina_Value_Struct_Desc v1_desc = { + EINA_VALUE_STRUCT_DESC_VERSION, + NULL, // no special operations + v1_members, + EINA_C_ARRAY_LENGTH(v1_members), + sizeof(My_Struct_V1) + }; + V1_DESC = &v1_desc; + + typedef struct _My_Struct_V2 { + int param1; + char param2; + int param3; + } My_Struct_V2; + static Eina_Value_Struct_Member v2_members[] = { + // no eina_value_type as they are not constant initializers, see below. + EINA_VALUE_STRUCT_MEMBER(NULL, My_Struct_V2, param1), + EINA_VALUE_STRUCT_MEMBER(NULL, My_Struct_V2, param2), + EINA_VALUE_STRUCT_MEMBER(NULL, My_Struct_V2, param3) + }; + v2_members[0].type = EINA_VALUE_TYPE_INT; + v2_members[1].type = EINA_VALUE_TYPE_CHAR; + v2_members[2].type = EINA_VALUE_TYPE_INT; + static Eina_Value_Struct_Desc v2_desc = { + EINA_VALUE_STRUCT_DESC_VERSION, + NULL, // no special operations + v2_members, + EINA_C_ARRAY_LENGTH(v2_members), + sizeof(My_Struct_V2) + }; + V2_DESC = &v2_desc; +} + +void rand_init(Eina_Value *v) +{ + if (v->type != EINA_VALUE_TYPE_STRUCT) + return; + + eina_value_struct_set(v, "param1", rand()); + eina_value_struct_set(v, "param2", rand() % 256); + eina_value_struct_set(v, "param3", rand()); +} + +void my_struct_use(Eina_Value *params) +{ + int p1, p3; + char p2; + + eina_value_struct_get(params, "param1", &p1); + eina_value_struct_get(params, "param2", &p2); + printf("param1: %d\nparam2: %c\n", p1, p2); + + if (eina_value_struct_get(params, "param3", &p3)) + printf("param3: %d\n", p3); +} + +int main(int argc, char **argv) +{ + Eina_Value *v1, *v2; + + eina_init(); + value_init(); + srand(time(NULL)); + + v1 = eina_value_struct_new(V1_DESC); + v2 = eina_value_struct_new(V2_DESC); + + rand_init(v1); + my_struct_use(v1); + + rand_init(v2); + my_struct_use(v2); + + eina_value_free(v1); + eina_value_free(v2); + eina_shutdown(); +} diff --git a/libraries/eina/src/examples/eina_value_03.c b/libraries/eina/src/examples/eina_value_03.c new file mode 100644 index 0000000..85f42b3 --- /dev/null +++ b/libraries/eina/src/examples/eina_value_03.c @@ -0,0 +1,178 @@ +//Compile with: +//gcc eina_value_03.c -o eina_value_03 `pkg-config --cflags --libs eina` + +#include +#include + +static Eina_Bool +_tz_setup(const Eina_Value_Type *type, void *mem) +{ + memset(mem, 0, type->value_size); + return EINA_TRUE; +} + +static Eina_Bool +_tz_flush(const Eina_Value_Type *type, void *mem) +{ + return EINA_TRUE; +} + +static Eina_Bool +_tz_copy(const Eina_Value_Type *type, const void *src, void * dst) +{ + struct timezone *tzsrc = src; + struct timezone *tzdst = dst; + *tzdst = *tzsrc; + return EINA_TRUE; +} + +static Eina_Bool +_tz_compare(const Eina_Value_Type *type, const void *a, const void *b) +{ + struct timezone tza = *(struct timezone*)a; + struct timezone tzb = *(struct timezone*)b; + + if (tza.tz_minuteswest < tzb.tz_minuteswest) + return -1; + else if (tza.tz_minuteswest > tzb.tz_minuteswest) + return 1; + return 0; +} + +static Eina_Bool +_tz_pset(const Eina_Value_Type *type, void *mem, const void *ptr) +{ + *(struct timezone*)mem = *(struct timezone*)ptr; + return EINA_TRUE; +} + +static Eina_Bool +_tz_vset(const Eina_Value_Type *type, void *mem, va_list args) +{ + const struct timezone tz = va_arg(args, struct timezone); + return _tz_pset(type, mem, &tz); +} + +static Eina_Bool +_tz_pget(const Eina_Value_Type *type, const void *mem, void *ptr) +{ + memcpy(ptr, mem, type->value_size); + return EINA_TRUE; +} + +static Eina_Bool +_tz_convert_to(const Eina_Value_Type *type, const Eina_Value_Type *convert, const void *type_mem, void *convert_mem) +{ + struct timezone v = *(struct timezone*)type_mem; + + eina_error_set(0); + + if (convert == EINA_VALUE_TYPE_UCHAR) + { + unsigned char other_mem = v.tz_minuteswest; + return eina_value_type_pset(convert, convert_mem, &other_mem); + } + else if (convert == EINA_VALUE_TYPE_USHORT) + { + unsigned short other_mem = v.tz_minuteswest; + return eina_value_type_pset(convert, convert_mem, &other_mem); + } + else if (convert == EINA_VALUE_TYPE_UINT) + { + unsigned int other_mem = v.tz_minuteswest; + return eina_value_type_pset(convert, convert_mem, &other_mem); + } + else if ((convert == EINA_VALUE_TYPE_ULONG) || (convert == EINA_VALUE_TYPE_TIMESTAMP)) + { + unsigned long other_mem = v.tz_minuteswest; + return eina_value_type_pset(convert, convert_mem, &other_mem); + } + else if (convert == EINA_VALUE_TYPE_UINT64) + { + uint64_t other_mem = v.tz_minuteswest; + return eina_value_type_pset(convert, convert_mem, &other_mem); + } + else if (convert == EINA_VALUE_TYPE_CHAR) + { + char other_mem = v.tz_minuteswest; + return eina_value_type_pset(convert, convert_mem, &other_mem); + } + else if (convert == EINA_VALUE_TYPE_SHORT) + { + short other_mem = v.tz_minuteswest; + return eina_value_type_pset(convert, convert_mem, &other_mem); + } + else if (convert == EINA_VALUE_TYPE_INT) + { + int other_mem = v.tz_minuteswest; + return eina_value_type_pset(convert, convert_mem, &other_mem); + } + else if (convert == EINA_VALUE_TYPE_LONG) + { + long other_mem = v.tz_minuteswest; + return eina_value_type_pset(convert, convert_mem, &other_mem); + } + else if (convert == EINA_VALUE_TYPE_INT64) + { + int64_t other_mem = v.tz_minuteswest; + return eina_value_type_pset(convert, convert_mem, &other_mem); + } + else if (convert == EINA_VALUE_TYPE_FLOAT) + return eina_value_type_pset(convert, convert_mem, &v.tz_minuteswest); + else if (convert == EINA_VALUE_TYPE_DOUBLE) + return eina_value_type_pset(convert, convert_mem, &v.tz_minuteswest); + else if (convert == EINA_VALUE_TYPE_STRINGSHARE || + convert == EINA_VALUE_TYPE_STRING) + { + const char *other_mem; + char buf[64]; + snprintf(buf, sizeof(buf), "%d", v.tz_minuteswest); + other_mem = buf; /* required due &buf == buf */ + return eina_value_type_pset(convert, convert_mem, &other_mem); + } + + eina_error_set(EINA_ERROR_VALUE_FAILED); + return EINA_FALSE; +} + +static Eina_Value_Type TZ_TYPE = { + EINA_VALUE_TYPE_VERSION, + sizeof(struct timezone), + "struct timezone", + _tz_setup, + _tz_flush, + _tz_copy, + _tz_compare, + _tz_convert_to, + NULL, //No convert from + _tz_vset, + _tz_pset, + _tz_pget +}; + +int main(int argc, char **argv) +{ + Eina_Value vtv, vtz; + struct timeval tv; + struct timezone tz; + char *s; + + eina_init(); + + eina_value_setup(&vtv, EINA_VALUE_TYPE_TIMEVAL); + eina_value_setup(&vtz, &TZ_TYPE); + + gettimeofday(&tv, &tz); + eina_value_set(&vtv, tv); + eina_value_set(&vtz, tz); + + s = eina_value_to_string(&vtv); + printf("time: %s\n", s); + free(s); + s = eina_value_to_string(&vtz); + printf("timezone: %s\n", s); + free(s); + + eina_value_flush(&vtz); + eina_value_flush(&vtv); +} diff --git a/libraries/eina/src/include/eina_model.h b/libraries/eina/src/include/eina_model.h new file mode 100644 index 0000000..0a1566e --- /dev/null +++ b/libraries/eina/src/include/eina_model.h @@ -0,0 +1,3105 @@ +/* EINA - EFL data type library + * Copyright (C) 2012 ProFUSION embedded systems + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; + * if not, see . + */ + +#ifndef EINA_MODEL_H_ +#define EINA_MODEL_H_ + +#include "eina_types.h" +#include "eina_value.h" +#include "eina_inlist.h" +#include + +/** + * @page eina_model_01_c Eina_Model inheritance and function overriding + * @include eina_model_01.c + */ + +/** + * @page eina_model_04_c Eina_Model inheritance, interfaces, and function overriding + * @include eina_model_04_main.c + * @include eina_model_04_animal.c + * @include eina_model_04_human.c + * @include eina_model_04_parrot.c + * @include eina_model_04_child.c + * @include eina_model_04_main.c + * @include eina_model_04_whistler.c + * @include eina_model_04_animal.h + * @include eina_model_04_human.h + * @include eina_model_04_whistler.h + * @include eina_model_04_child.h + * @include eina_model_04_parrot.h + */ + +/** + * @page eina_model_02_example_page Creating a simple model + * @dontinclude eina_model_02.c + * + * This example shows the creation of a model with five properties, named: + * 'a', 'b', 'c', 'd' and 'e' with values 0, 1, 2, 3 and 4 + * respectively. In addition to the 5 properties our model also add 5 children, + * and to each child we give a property named 'x' with a value of 1, 2, 3, 4 and + * 5. + * + * In other words this piece of code shows how to use eina_model to store a list + * of elements, given that the list itself has some properties. + * + * Now let's walk through the code and examine the interesting bits. + * + * This is some pretty standard initialization code. + * @until eina_init + * + * We now create our eina_model, the important detail here is the type of the + * model being created, for this example we use the generic type provided by + * eina: + * @until model_new + * + * Once our model has been created we can add callbacks to be notified of events + * that happen to our model, for this example we are just going to add a + * callback for the "delete" event. To get a list of events a given eina model + * can emit see @ref eina_model_event_names_list_get(). + * @until callback_add + * + * Once we have a model, we need to populate it with information. There are two + * types of information we can store on an eina model: properties and eina + * models. We are going to start by looking at properties. + * + * Properties are, simply put, named values. They have a char* identifier and an + * Eina_Value value. This means you can store in a property almost any type of + * data. For this example we are going to add some very simple numeric + * properties which will have single letter identifiers. + * @until } + * @until } + * + * Despite being able to store almost any value properties the least flexible + * information unit we can put in an eina model. We can add eina models to our + * eina model, this allows us to represt complex information hierarchies. This + * example adds 5 models(with no children of their own) to our parent model @c + * m. + * @until } + * The code here should be pretty easy to understand, we create a model, much + * like we did before, and we then add a property to our model, again a task we + * have already done. + * + * The important issue to note here is that we could have given each of our @c c + * child models as complex an structure as we needed, they could each be a list + * or a tree on their own right. + * + * Now that we have a populated model we print a string representation of + * it(without forgetting to free the string): + * @until free + * + * And since we are done using our model we release our reference to it(and + * since no else holds references to it, it will be freed): + * @until } + * + * Note that we don't need to iterate over the children of @c m unrefing it, + * this is because we don't hold references to it, we freed our references right + * after we added them to their parent model, so when the parent model dies(and + * releases the references to it's children) they will be freed. + * + * The only thing we are going to look at is the callback we registered for + * whenever a model is deleted, since our models don't do anything fancy we are + * just going to print the memory address of the model being freed. + * @until } + * + * Note that this means the memory address is still valid, our callback is + * called just before the memory is freed so we could still access its + * information here. + * + * The full code can be seen in @ref eina_model_02_c + */ + +/** + * @page eina_model_02_c eina_model_02.c + * @include eina_model_02.c + * @example eina_model_02.c + */ + +/** + * @page eina_model_03_example_page Using Eina_Model and inheritance + * @dontinclude eina_model_03.c + * + * This example will use two custom defined eina model types: @c PERSON_TYPE to + * represent a person and @c ADDRESS_BOOK_TYPE to represent the an address book. + * Both our types inherit from EINA_MODEL_TYPE_STRUCT, and, therefore, + * store it's data on a struct. Our address book will be very simple it will + * only contain one property, the name of the file where we store our address + * book. The person type will contain two fields a name and en email. Let's look + * at the code. + * + * We'll start with declaring the variables and functions we'll need to define + * our custom type. This will all be explained when the variables get used. + * @until address_book_init + * + * We then jump into our @c main function, declare a couple of variables and + * initialize eina: + * @until eina_init + * + * After eina is initialized we'll @c address_book_init() which will initialize + * both our @c PERSON_TYPE and our @c ADDRESS_BOOK_TYPE. Details of this will be + * shown latter on: + * @until address_book_init + * + * Now that everything is correctly initialized we can create the model that + * will represent our address book's + * @until eina_model_new + * + * Before we can load data into our model we need to tell it where to load from, + * we do this by setting it's filename property: + * @until value_flush + * + * We then load data into our model and display it as a string: + * @until free + * + * While @c eina_model_to_string allows you to see the contents of the model, + * it's display format is not user friendly, it's best used for debugging. So + * let's now print our model in a user friendly way. + * + * First we see how many people are in our address book and print that: + * @until printf + * + * And now we iterate over every child of our address book model, which + * represents a person: + * @until person + * + * But again simply calling @c eina_model_to_string would result in not very + * user friendly output, so we'll need to get the properties of the person(name + * and email) and print them with some formatting: + * @until printf + * + * We then free the resources we allocated to print this person: + * @until } + * + * And that's it for our main function, now just freeing our resources: + * @until } + * + * This however obviously doesn't conclude our example we need to examine how + * the the loading of data works to really understand what is happening in the + * @c main function. + * + * Let's start with the constructors(and the variables they use). Both our + * constructors do two very important tasks: + * @li Calls our parent's constructor, and + * @li Sets the description of the struct on our model + * + * For these constructors that's all we need to do since most of our + * functionality is provided by @c EINA_MODEL_TYPE_STRUCT. + * @until } + * @until } + * + * And now we have our load function, it opens the file from which we'll + * read the address book: + * @until EINA_SAFETY_ON_NULL_RETURN_VAL + * + * Once the file has been opened we read from it line by line and for each + * non-blank line we get a name and an email: + * @until email + * @until email + * + * Once we have the name and email we create our person model, set it's + * properties and make our person a child of the address book: + * @until } + * + * And now that we're done reading the file we close it: + * @until } + * + * This next function is perphaps the most interesting one of our example, it's + * the one that creates the definition of our derived types. + * + * First thing we'll do is the description of the members of our person type. + * @until person_members[1].type + * Now the description of the struct itself(which uses the members): + * @until } + * And finally we define the person type itself: + * @until person_type.constructor + * + * With the person now described we'll do the same process for our address book + * type: + * @until address_book_type.load + * + * So far everything we created has been in the scope of our function to make + * this available outside(such as in the @c main function where we use @c + * ADDRESS_BOOK_TYPE and on @c _address_book_load function where we use @c + * PERSON_TYPE) we need to assign our descriptions and type to global variables: + * @until } + * + * This concludes this example. A good exercise for the reader is to extend this + * example to have the model save the addres book, for example once it's + * unloaded, this can be done by overriding the .unload property of @c + * ADDRESS_BOOK_TYPE. + * + * For the full code see: @ref eina_model_03_c + */ + +/** + * @page eina_model_03_c eina_model_03.c + * @include eina_model_03.c + * @example eina_model_03.c + */ + +/** + * @addtogroup Eina_Data_Types_Group Data Types + * + * @since 1.2 + * + * @{ + */ + +/** + * @addtogroup Eina_Containers_Group Containers + * + * @{ + */ + +/** + * @defgroup Eina_Model_Group Data Model API + * + * Abstracts data access to hierarchical data in an efficient way, + * extensible to different backing stores such as database or remote + * access. + * + * It is heavily based on #Eina_Value, as properties are exchanged + * using this data type as interface, although internally models may + * store them as they want. See @ref Eina_Value_Group. + * + * Although extensible and easy to optimize, a simple generic type is + * provided as #EINA_MODEL_TYPE_GENERIC. It is recommended that people + * use it during development, get the logic right and just then + * optimize what is needed (properties or children management). + * + * Not as generic as #EINA_MODEL_TYPE_GENERIC, but way more efficient + * is #EINA_MODEL_TYPE_STRUCT that instead of a hash of properties of + * any type, it uses a struct to map properties. Its properties are + * fixed set of names and they have fixed type, as defined by the + * #Eina_Value_Struct_Desc description used internally. + * + * Examples: + * @li @ref eina_model_01_c inheritance example, uses #EINA_MODEL_TYPE_GENERIC + * @li @ref eina_model_02_example_page contains an easy to follow + * example that demonstrates several of the important features of + * eina_model, uses #EINA_MODEL_TYPE_GENERIC. + * @li @ref eina_model_03_example_page walk-through example on how to + * inherit types, a suggestion of eina_model_load() usage and uses + * #EINA_MODEL_TYPE_STRUCT. + * @li @ref eina_model_04_c Advanced inheritance, interfaces and interface + * function overloading example. + * + * @{ + */ + +/** + * @var EINA_ERROR_MODEL_FAILED + * Defined when model-specific errors happens. + */ +EAPI extern Eina_Error EINA_ERROR_MODEL_FAILED; + +/** + * @var EINA_ERROR_MODEL_METHOD_MISSING + * Defined when model-specific errors happens. + */ +EAPI extern Eina_Error EINA_ERROR_MODEL_METHOD_MISSING; + +/** + * @typedef Eina_Model + * Data Model Object. + * + * This is an opaque handle that is created with eina_model_new() and + * released with eina_model_unref(). + * + * It contains properties, children and may emit events. See + * respectively: + * @li eina_model_property_get() and eina_model_property_set() + * @li eina_model_child_get() and eina_model_child_set() + * @li eina_model_event_names_list_get(), eina_model_event_callback_add() and eina_model_event_callback_del() + * + * @see eina_model_new() + * @see eina_model_ref() and eina_model_xref() + * @see eina_model_unref(), eina_model_xunref() and eina_model_del() + * @see eina_model_type_get() and eina_model_interface_get() + * @since 1.2 + */ +typedef struct _Eina_Model Eina_Model; + +/** + * @typedef Eina_Model_Type + * Data Model Type. + * + * @see #_Eina_Model_Type explains fields. + * @since 1.2 + */ +typedef struct _Eina_Model_Type Eina_Model_Type; + +/** + * @typedef Eina_Model_Interface + * Data Model Interface. + * + * @see #_Eina_Model_Interface explains fields. + * @since 1.2 + */ +typedef struct _Eina_Model_Interface Eina_Model_Interface; + +/** + * @typedef Eina_Model_Event_Description + * Data Model Event Description. + * + * This is used to declare events supported by types and interfaces + * and also to provide introspection to receivers of signals so they + * can know which data they are receiving as @c event_info. + * + * @see EINA_MODEL_EVENT_DESCRIPTION() + * @see #EINA_MODEL_EVENT_DESCRIPTION_SENTINEL + * @see #_Eina_Model_Event_Description explains fields. + * @since 1.2 + */ +typedef struct _Eina_Model_Event_Description Eina_Model_Event_Description; + +/** + * @brief Creates a new model of type @a Type. + * @param type The type of the model to create. + * @return If successfull pointer to model, NULL otherwise. + * + * @see _Eina_Model_Type + * @see eina_model_del() + * @since 1.2 + */ +EAPI Eina_Model *eina_model_new(const Eina_Model_Type *type); +/** + * @brief Frees the memory associated with @a model + * @param model The model instance. + * + * @see eina_model_new() + * @since 1.2 + */ +EAPI void eina_model_del(Eina_Model *model) EINA_ARG_NONNULL(1); + +/** + * @brief Returns the type of @a model. + * @param model The model instance. + * @return The type of @a model. + * + * @see eina_model_new() + * @see _Eina_Model_Type + * @since 1.2 + */ +EAPI const Eina_Model_Type *eina_model_type_get(const Eina_Model *model) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT EINA_PURE; + +/** + * @brief Returns the interface named @a name of @a model. + * @param model The model instance. + * @param name Name of interface to get. + * @return If successfull requested interface, NULL otherwise. + * + * The name of every interface of @a model will be compared to @a name, the + * first one to match will be returned. + * + * @see eina_model_new() + * @see _Eina_Model_Interface + * @since 1.2 + */ +EAPI const Eina_Model_Interface *eina_model_interface_get(const Eina_Model *model, + const char *name) EINA_ARG_NONNULL(1, 2) EINA_WARN_UNUSED_RESULT EINA_PURE; + +/** + * @brief Increases the refcount of @a model. + * @param model The model to increase reference. + * @return The @a model with reference increased. + * @return If successfull pointer to model, NULL otherwise. + * + * @see eina_model_new() + * @see eina_model_unref() + * @since 1.2 + */ +EAPI Eina_Model *eina_model_ref(Eina_Model *model) EINA_ARG_NONNULL(1); + +/** + * @brief Increases the refcount of @a model, informs reference identifier. + * @param model The model to increase reference. + * @param id An identifier to mark this reference. + * @param label An optional label to help debug, may be @c NULL. + * @return The @a model with reference increased. + * + * This extended version of reference explicitly marks the origin of + * the reference and eina_model_xunref() should be used to check and + * remove it. + * + * Usually the @a id is another object, like a parent object, or some + * class/structure/file/function that is holding the reference for + * some reason. + * + * Its purpose is to help debuging if Eina was compiled with model + * usage debug enabled and environment variable @c EINA_MODEL_DEBUG=1 + * is set. + * + * It is recommended to use eina_model_xref() and eina_model_xunref() + * pair whenever you want to be sure you released your + * references. Both at your own type, or using applications. As an + * example #EINA_MODEL_INTERFACE_CHILDREN_INARRAY will use this to + * make sure it deleted every managed children. + * + * In order to debug leaks, consider using eina_model_xrefs_get() or + * eina_models_usage_dump() for a global picture. However, some + * references are not tracked, namely: + * + * @li eina_model_new() + * @li eina_model_child_get() + * @li eina_model_child_iterator_get() + * @li eina_model_child_reversed_iterator_get() + * @li eina_model_child_sorted_iterator_get() + * @li eina_model_child_filtered_iterator_get() + * @li eina_model_child_slice_iterator_get() + * @li eina_model_child_slice_reversed_iterator_get() + * @li eina_model_child_slice_sorted_iterator_get() + * @li eina_model_child_slice_filtered_iterator_get() + * + * @note this function is slower than eina_model_ref() if + * @c EINA_MODEL_DEBUG is set to "1" or "backtrace". Otherwise it + * should have the same performance cost. + * + * @see eina_model_ref() + * @see eina_model_xunref() + * @since 1.2 + */ +EAPI Eina_Model *eina_model_xref(Eina_Model *model, + const void *id, + const char *label) EINA_ARG_NONNULL(1, 2); + +/** + * @brief Decreases the refcount of @a model. + * @param model The model to decrease reference. + * @return If successfull pointer to model, NULL otherwise. + * + * After this function returns, consider @a model pointer invalid. + * + * @see eina_model_ref() + * @see eina_model_del() + * @since 1.2 + */ +EAPI void eina_model_unref(Eina_Model *model) EINA_ARG_NONNULL(1); + +/** + * @brief Decreases the refcount of @a model, informs reference identifier. + * @param model The model to decrease reference. + * @param id An identifier to mark this reference. + * @return If successfull pointer to model, NULL otherwise. + * + * This function will match eina_model_xref() and the @a id must match + * a previously call, otherwise it will produce an error if @c + * EINA_MODEL_DEBUG is set to "1" or "backtrace", and the reference is + * not decreased! + * + * After this function returns, consider @a model pointer invalid. + * + * @note this function is slower than eina_model_unref() if + * @c EINA_MODEL_DEBUG is set to "1" or "backtrace". Otherwise it + * should have the same performance cost. + * + * @see eina_model_xref() + * @since 1.2 + */ +EAPI void eina_model_xunref(Eina_Model *model, + const void *id) EINA_ARG_NONNULL(1, 2); + + + +/** + * @defgroup Eina_Model_Event_Group Data Model Events + * Events and their usage with models. + * + * Events are specified by each type and interface level using + * #Eina_Model_Event_Description. One can know all events supported by + * a model with eina_model_event_names_list_get() and then + * eina_model_event_description_get() to retrieve details. + * + * By default the following events are supported in every object: + * @li deleted: last reference was released or eina_model_del() was called. + * @li freed: memory was destroyed, destructors were called. + * @li property,set: eina_model_property_set() was done. + * @li property,deleted: eina_model_property_del() was done. + * @li children,changed: children was changed somehow (added, modified, deleted) + * @li child,inserted: new child was added (eina_model_child_append() or eina_model_child_insert_at()) + * @li child,set: child was replaced (eina_model_child_set()) + * @li child,deleted: eina_model_child_del() was done. + * @li loaded: eina_model_load() was done. + * @li unloaded: eina_model_unload() was done. + * + * Mix-in interfaces may emit these: + * @li properties,loaded + * @li properties,unloaded + * @li children,loaded + * @li children,unloaded + * + * One can be notified of events with eina_model_event_callback_add(). + * + * Types emit these events with eina_model_event_callback_call(), + * these are handled asynchronously unless event is frozen with + * eina_model_event_callback_freeze() is blocking it. In this case the + * events are ignored. Usually this is used in some cases that want to + * avoid storm of events in batch operations. + * + * @{ + */ + +/** + * @typedef Eina_Model_Event_Cb + * Notifies of events in this model. + * + * @since 1.2 + */ +typedef void (*Eina_Model_Event_Cb)(void *data, Eina_Model *model, const Eina_Model_Event_Description *desc, void *event_info); + +/** + * @brief Add a callback to be called when @a event_name is emited. + * @param model The model instance. + * @param event_name The name of event for which @a cb will be called. + * @param cb The function to be called. + * @param data Data @a cb will be called with. May be NULL. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @see eina_model_event_callback_del() + * @since 1.2 + */ +EAPI Eina_Bool eina_model_event_callback_add(Eina_Model *model, + const char *event_name, + Eina_Model_Event_Cb cb, + const void *data) EINA_ARG_NONNULL(1, 2, 3); +/** + * @brief Remove a callback that was to be called when @a event_name was emited. + * @param model The model instance. + * @param event_name The name of event for which to delete callback. + * @param cb The function given to eina_model_event_callback_add(). + * @param data Data given to eina_model_event_callback_add(). A NULL value means + * every @a data will not be compared. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @see eina_model_event_callback_add() + * @since 1.2 + */ +EAPI Eina_Bool eina_model_event_callback_del(Eina_Model *model, + const char *event_name, + Eina_Model_Event_Cb cb, + const void *data) EINA_ARG_NONNULL(1, 2, 3); + +/** + * @brief Returns a description of the event named @c event_name + * @param model The model instance. + * @param event_name Name of event whose description is wanted. + * @return Description of event. + * + * @see Eina_Model_Event_Description + * @since 1.2 + */ +EAPI const Eina_Model_Event_Description *eina_model_event_description_get(const Eina_Model *model, + const char *event_name) EINA_ARG_NONNULL(1, 2) EINA_WARN_UNUSED_RESULT EINA_PURE; + +/** + * @brief Returns list of events this model may emit. + * @param model The model whose events are to be listed. + * @return An Eina_List of stringshares with the name of every event. Free the + * list with eina_model_event_names_list_free(). + * + * @since 1.2 + */ +EAPI Eina_List *eina_model_event_names_list_get(const Eina_Model *model) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT; +/** + * @brief Frees the list of event's names gotten from + * eina_model_event_names_list_get(). + * @param list The list to free. + * + * @see eina_model_event_names_list_get() + * @since 1.2 + */ +EAPI void eina_model_event_names_list_free(Eina_List *list); + +/** + * @brief Calls every callback associated to @a name on model @a model with @a + * event_info. + * @param model The model instance. + * @param name The event whose callbacks will be called. + * @param event_info The data given to the callback as event_info. May be NULL. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @see eina_model_event_callback_add() + * @see eina_model_event_callback_del() + * @since 1.2 + */ +EAPI Eina_Bool eina_model_event_callback_call(Eina_Model *model, + const char *name, + const void *event_info) EINA_ARG_NONNULL(1, 2); + +/** + * @brief Makes @a model not call the callbacks associated with @a name. + * @param model The model instance. + * @param name The event whose callbacks are to be frozen. + * @return Count of freezes called on this event. + * + * @see eina_model_event_callback_call() + * @see eina_model_event_callback_thaw() + * @since 1.2 + */ +EAPI int eina_model_event_callback_freeze(Eina_Model *model, + const char *name) EINA_ARG_NONNULL(1, 2); +/** + * @brief Makes @a model able to call the callbacks associated with @a name. + * @param model The model instance. + * @param name The event whose callbacks are to be frozen. + * @return Count of freezes still valid in this event. + * + * @warning Behavior is undefined if called on a @a model, @a name not frozen. + * + * @see eina_model_event_callback_call() + * @see eina_model_event_callback_freeze() + * @since 1.2 + */ +EAPI int eina_model_event_callback_thaw(Eina_Model *model, + const char *name) EINA_ARG_NONNULL(1, 2); + +/** + * @} + */ + + +/** + * @brief Makes a shallow copy of @a model. + * @param model The model instance. + * @return Copied model. + * + * The returned model will have a copy of the properties of @a model and a + * reference to the children of @a model. + * + * @see eina_model_deep_copy() + * @since 1.2 + */ +EAPI Eina_Model *eina_model_copy(const Eina_Model *model) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT EINA_MALLOC; +/** + * @brief Makes a deep(complete) copy of @a model. + * @param model The model instance. + * @return Copied model. + * + * The returned model will have a copy of the properties of @a model, its + * children will be created by making a deep copy of the children of @a model. + * + * @see eina_model_copy() + * @since 1.2 + */ +EAPI Eina_Model *eina_model_deep_copy(const Eina_Model *model) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT EINA_MALLOC; + +/** + * @brief Compares two models. + * @param a The first model to compare. + * @param b The second model to compare. + * @return Greater than zero if @a a > @a b, zero if @a a == @a b and less than + * zero if @a a < @a b + * + * The default comparison checks that the properties of @a a and @a b all have + * the same name and value, and then recursively compares all children. + * + * A model with less properties or children is considered smaller than one with + * more properties. + * + * @since 1.2 + */ +EAPI int eina_model_compare(const Eina_Model *a, const Eina_Model *b) EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(1, 2); + +/** + * @brief Loads the @a model's data. + * @param model The model instance. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * By convention this means loading data from an external source and populating + * the models properties and children with it. For example in the case of file + * system backed model, this means opening the relevant files and reading the + * data from them(creating the properties and children from it). + * @warning This convention should be followed, but no guarantees of behaviour + * by user defined types can be given. + * + * @note The types provided by Eina_Model don't implement this method. + * @note Calling this function on a model that doesn't implement it returns @c + * EINA_TRUE without any effect on @a model. + * + * @see eina_model_unload() + * @since 1.2 + */ +EAPI Eina_Bool eina_model_load(Eina_Model *model) EINA_ARG_NONNULL(1); +/** + * @brief Unloads the @a model's data. + * @param model The model instance. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * By convention this means releasing data gotten from an external source. For + * example of a database backed model this might mean releasing the iterator for + * the currently loaded data or deleting a temporary table. + * @warning This convention should be followed, but no guarantees of behaviour + * by user defined types can be given. + * + * @note The types provided by Eina_Model don't implement this method. + * @note Calling this function on a model that doesn't implement it returns @c + * EINA_TRUE without any effect on @a model. + * + * @see eina_model_load() + * @since 1.2 + */ +EAPI Eina_Bool eina_model_unload(Eina_Model *model) EINA_ARG_NONNULL(1); + + +/** + * @defgroup Eina_Model_Properties_Group Data Model Properties + * Properties and their usage with models. + * + * Properties are attributes of model. They have a name and contain a + * data value (@ref Eina_Value_Group). + * + * The actual values and their types, if it is possible to read and + * write them and if new properties can be created or deleted it is up + * to the type. + * + * @{ + */ +/** + * @brief Gets the value of @a model's property named @a name. + * @param[in] model The model from which to get the property. + * @param[in] name The name of the property whose value is wanted. + * @param[out] value A pointer to an Eina_Value to receive the property's value. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @return EINA_TRUE if @a model has a property named @a name, EINA_FALSE + * otherwise. + * + * @see eina_model_property_set() + * @see eina_model_property_del() + * @since 1.2 + */ +EAPI Eina_Bool eina_model_property_get(const Eina_Model *model, + const char *name, + Eina_Value *value) EINA_ARG_NONNULL(1, 2, 3); +/** + * @brief Sets the value of @a model's property named @a name to @a value. + * @param model The model in which to set the property. + * @param name The name of the property whose value is to set. + * @param value A pointer to a const Eina_Value to containing the property's + * value. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @see eina_model_property_get() + * @see eina_model_property_del() + * @since 1.2 + */ +EAPI Eina_Bool eina_model_property_set(Eina_Model *model, + const char *name, + const Eina_Value *value) EINA_ARG_NONNULL(1, 2, 3); +/** + * @brief Deletes @a model's property named @a name. + * @param model The model from which to delete the property. + * @param name The name of the property to delete. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @see eina_model_property_set() + * @see eina_model_property_get() + * @since 1.2 + */ +EAPI Eina_Bool eina_model_property_del(Eina_Model *model, + const char *name) EINA_ARG_NONNULL(1, 2); + +/** + * @brief Gets a list of the names of every property of @a model. + * @param model The model instance. + * @return #Eina_List of names. + * + * @note The returned list should be freed with @c + * eina_model_properties_names_list_free(). + * + * @see eina_model_properties_names_list_free() + * @see eina_model_property_get() + * @since 1.2 + */ +EAPI Eina_List *eina_model_properties_names_list_get(const Eina_Model *model) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT; +/** + * @brief Frees a list of names of properties gotten with @c + * eina_model_properties_names_list_get(). + * @param list The list to free. + * + * @warning Behavior is undefined if called on a list not gotten from @c + * eina_model_properties_names_list_get(). + * + * @see eina_model_properties_names_list_get() + * @see eina_model_property_get() + * @since 1.2 + */ +EAPI void eina_model_properties_names_list_free(Eina_List *list); + +/** + * @} + */ + +/** + * @defgroup Eina_Model_Children_Group Data Model Children + * Children and their usage with models. + * + * Children are other model instances that are kept sequentially in + * the model. They are accessed by their integer index within the + * model. Their index may change if child are inserted or deleted + * before them, as in an array. + * + * @{ + */ + +/** + * @brief Returns the number of child models in @a model. + * @param model The model instance. + * @return Number of children in @a model. + * + * @see eina_model_child_append() + * @see eina_model_child_get() + * @see eina_model_child_del() + * @since 1.2 + */ +EAPI int eina_model_child_count(const Eina_Model *model) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT; + +/** + * @brief Get the child at a given position from a model. + * @param model the model instance. + * @param position index of child to get. + * @return child instance with reference @b increased, or @c NULL on error. + * + * The given @a position must be valid, otherwise it may fail and + * return @c NULL, one should check for a valid position with + * eina_model_child_count(). + * + * @warning The returned model has its reference increased, you must release it + * with eina_model_unref(). This convention is imposed to avoid the + * object being removed before the caller function has time to use it. + */ +EAPI Eina_Model *eina_model_child_get(const Eina_Model *model, + unsigned int position) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT; + +/** + * @brief Set the child at a given position from a model. + * @param model the model instance. + * @param position index of child to set. + * @param child the child to use at given position. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * The given @a position must be valid, otherwise it may fail and + * return #EINA_FALSE, one should check for a valid position with + * eina_model_child_count(). + * + * The existing child is replaced. Its reference will be decreased + * automatically. To insert a new item instead of replacing, use + * eina_model_child_insert_at() or eina_model_child_append(). + * + * The given model will be adopted by @a model, that is, the @a child + * will have its reference increased if this call succeeds. + * + * @see eina_model_child_append() + * @see eina_model_child_insert_at() + * @since 1.2 + */ +EAPI Eina_Bool eina_model_child_set(Eina_Model *model, + unsigned int position, + Eina_Model *child) EINA_ARG_NONNULL(1, 3); + +/** + * @brief Deletes the child model in @a position-th of @a model. + * @param model The model instance. + * @param position The position of the child to be deleted. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @warning This decrements the reference count of the child being deleted, + * which may, or not, cause it to be deconstructed and freed. + * + * @see eina_model_child_append() + * @see eina_model_child_get() + * @since 1.2 + */ +EAPI Eina_Bool eina_model_child_del(Eina_Model *model, + unsigned int position) EINA_ARG_NONNULL(1); + +/** + * @brief Insert @a child in the @a position-th of the list of children of @a + * model. + * @param model The model instance. + * @param position Position in which to insert child. + * @param child The child to be inserted. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @warning This increments the reference count of the child being inserted, if + * it will no longer be used by the inserting code it should call + * eina_model_unref() on it. + * + * @see eina_model_child_append() + * @see eina_model_child_set() + * @since 1.2 + */ +EAPI Eina_Bool eina_model_child_insert_at(Eina_Model *model, + unsigned int position, + Eina_Model *child) EINA_ARG_NONNULL(1, 3); + +/** + * @brief Appends @a child in @a model. + * @param model The model instance. + * @param child The child to be appended. + * @return The position of the added child, or -1 on failure. + * + * @warning This increments the reference count of the child being inserted, if + * it will no longer be used by the inserting code it should call + * eina_model_unref() on it. + * + * @see eina_model_child_insert_at() + * @see eina_model_child_set() + * @since 1.2 + */ +EAPI int eina_model_child_append(Eina_Model *model, + Eina_Model *child) EINA_ARG_NONNULL(1, 2); + +/** + * @brief Returns the position of @a other amongst the children of @a model. + * @param model The parent model whose children will be searched. + * @param start_position The first children to be compared with @a other. + * @param other The model whose position is desired. + * @return The position of the searched for child, or -1 if not found. + * + * @since 1.2 + */ +EAPI int eina_model_child_find(const Eina_Model *model, + unsigned int start_position, + const Eina_Model *other) EINA_ARG_NONNULL(1, 3) EINA_WARN_UNUSED_RESULT; + +/** + * @brief Returns the position of a child of @a model that mathes the criteria. + * @param model The model whose children will be searched. + * @param start_position The position of the first child to be checked. + * @param match The function used to check if a child matches the criteria. + * @param data Data given the to the @a match function. + * @return The position of the first child to match the criteria or -1 if no + * child matches it. + * + * Returns the position of the first(from @a start_position) child of @a model + * to which @a match returns EINA_TRUE. + * + * @since 1.2 + */ +EAPI int eina_model_child_criteria_match(const Eina_Model *model, + unsigned int start_position, + Eina_Each_Cb match, + const void *data) EINA_ARG_NONNULL(1, 3) EINA_WARN_UNUSED_RESULT; + +/** + * @brief Sorts the children of @a model according to @a compare. + * @param model The model instance. + * @param compare The function to be used in the comparison. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * The @a compare function receives to const pointer to eina models(const + * *Eina_Model). + * + * @since 1.2 + */ +EAPI Eina_Bool eina_model_child_sort(Eina_Model *model, + Eina_Compare_Cb compare) EINA_ARG_NONNULL(1, 2); + +/** + * @} + */ + +/** + * @defgroup Eina_Model_Iterators_Group Data Model Iterators + * Iterators and their usage with models. + * + * One of the most common tasks of models is to iterate over their + * children, either forwards or backwards, filtering by some criteria + * or a different ordering function. + * + * @{ + */ + +/** + * @brief create an iterator that outputs a child model on each iteration. + * @param model the model instance. + * @return Newly created iterator instance on success or @c NULL on failure. + * + * @code + * Eina_Model *child; + * Eina_Iterator *it = eina_model_child_iterator_get(model); + * EINA_ITERATOR_FOREACH(it, child) + * { + * use_child(child); + * eina_model_unref(child); + * } + * eina_iterator_free(it); + * @endcode + * This code shows how to use iterators to do something (in this example call + * use_child()) on every child element. + * + * @warning Each iteration(call to eina_iterator_next()) gives a child model + * with reference @b increased! You must call eina_model_unref() after you're + * done with it. + * + * @see eina_model_child_slice_iterator_get() + * @see eina_model_child_reversed_iterator_get() + * @see eina_model_child_sorted_iterator_get() + * @see eina_model_child_filtered_iterator_get() + * @since 1.2 + */ +EAPI Eina_Iterator *eina_model_child_iterator_get(Eina_Model *model) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT; +/** + * @brief Gets an iterator to a slice of @a model's children. + * @param model The model whose children to iterate over. + * @param start The first child included in the iterator. + * @param count The number of children included in the iterator. + * @return Newly created iterator instance on success or @c NULL on failure. + * + * @warning Each iteration(call to eina_iterator_next()) gives a child model + * with reference @b increased! You must call eina_model_unref() after you're + * done with it. + * + * @see eina_model_child_iterator_get() + * @see eina_model_child_slice_reversed_iterator_get() + * @since 1.2 + */ +EAPI Eina_Iterator *eina_model_child_slice_iterator_get(Eina_Model *model, + unsigned int start, + unsigned int count) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT; + +/** + * @brief create an iterator that outputs a child model in reversed order. + * @param model the model instance. + * @return Newly created iterator instance on success or @c NULL on failure. + * + * Each iteration output a child model with reference @b increased! + * You must call eina_model_unref() after you're done with it. + * + * The order is reversed, that is, the last element is outputted first. + * + * @code + * Eina_Model *child; + * Eina_Iterator *it = eina_model_child_reversed_iterator_get(model); + * EINA_ITERATOR_FOREACH(it, child) + * { + * use_child(child); + * eina_model_unref(child); + * } + * eina_iterator_free(it); + * @endcode + * This code shows how to use iterators to do something (in this example call + * use_child()) on every child element starting from last to first. + * + * @warning Each iteration(call to eina_iterator_next()) gives a child model + * with reference @b increased! You must call eina_model_unref() after you're + * done with it. + * + * @see eina_model_child_slice_iterator_get() + * @see eina_model_child_reversed_iterator_get() + * @see eina_model_child_sorted_iterator_get() + * @see eina_model_child_filtered_iterator_get() + * @since 1.2 + */ +EAPI Eina_Iterator *eina_model_child_reversed_iterator_get(Eina_Model *model) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT; + +/** + * @brief Gets a reversed iterator to a slice of @a model's children. + * @param model The model whose children to iterate over. + * @param start The first child included in the iterator. + * @param count The number of children included in the iterator. + * @return Newly created iterator instance on success or @c NULL on failure. + * + * @warning Each iteration(call to eina_iterator_next()) gives a child model + * with reference @b increased! You must call eina_model_unref() after you're + * done with it. + * + * @see eina_model_child_reversed_iterator_get() + * @see eina_model_child_slice_iterator_get() + * @since 1.2 + */ +EAPI Eina_Iterator *eina_model_child_slice_reversed_iterator_get(Eina_Model *model, + unsigned int start, + unsigned int count) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT; + +/** + * @brief create an iterator that outputs a child model using sort criteria. + * @param model the model instance. + * @param compare compare function to use as sort criteria. + * @return Newly created iterator instance on success or @c NULL on failure. + * + * Each iteration output a child model with reference @b increased! + * You must call eina_model_unref() after you're done with it. + * + * The sort will not affect the main object @a model, it's just a view + * of it. + * + * @code + * Eina_Model *child; + * Eina_Iterator *it = eina_model_child_sorted_iterator_get(model, EINA_COMPARE_CB(eina_model_compare)); + * EINA_ITERATOR_FOREACH(it, child) + * { + * use_child(child); + * eina_model_unref(child); + * } + * eina_iterator_free(it); + * @endcode + * This bit of code shows how to use iterators to do something (in this example + * call use_child()) on every child element in the order given by the @a compare + * function. + * + * @warning Each iteration(call to eina_iterator_next()) gives a child model + * with reference @b increased! You must call eina_model_unref() after you're + * done with it. + * + * @see eina_model_child_slice_iterator_get() + * @see eina_model_child_reversed_iterator_get() + * @see eina_model_child_sorted_iterator_get() + * @see eina_model_child_filtered_iterator_get() + * @since 1.2 + */ +EAPI Eina_Iterator *eina_model_child_sorted_iterator_get(Eina_Model *model, + Eina_Compare_Cb compare) EINA_ARG_NONNULL(1, 2) EINA_WARN_UNUSED_RESULT; + +/** + * @brief Returns a sorted iterator to a slice of @a model's children. + * @param model The model whose children to iterate over. + * @param start The position(before sorting) of the first child included in + * the iterator. + * @param count The number of children included in the iterator. + * @param compare The function used to sort the children. + * @return Newly created iterator instance on success or @c NULL on failure. + * + * @warning Each iteration(call to eina_iterator_next()) gives a child model + * with reference @b increased! You must call eina_model_unref() after you're + * done with it. + * + * @see eina_model_child_slice_iterator_get() + * @see eina_model_child_reversed_iterator_get() + * @see eina_model_child_sorted_iterator_get() + * @see eina_model_child_filtered_iterator_get() + * @since 1.2 + */ +EAPI Eina_Iterator *eina_model_child_slice_sorted_iterator_get(Eina_Model *model, + unsigned int start, + unsigned int count, + Eina_Compare_Cb compare) EINA_ARG_NONNULL(1, 4) EINA_WARN_UNUSED_RESULT; + +/** + * @brief create an iterator that indexes of children that matches. + * @param model the model instance. + * @param match function to select children. + * @param data extra context given to @a match function. + * @return Newly created iterator instance on success or @c NULL on failure. + * + * Unlike other iterators, each iteration output an integer index! + * This is useful if you want to highlight the matching model + * somewhere else. + * + * If no child element matches a valid, and empty, iterator will be returned. + * Indexes returned by this iterator are guaranteed to exists. + * + * @code + * unsigned int idx; + * Eina_Iterator *it = eina_model_child_filtered_iterator_get(model, filter, ctx); + * EINA_ITERATOR_FOREACH(it, idx) + * { + * Eina_Model *child = eina_model_child_get(model, idx); + * printf("matches at %u %p\n", idx, child); + * eina_model_unref(child); + * } + * eina_iterator_free(it); + * @endcode + * This bit of code shows how to use iterators to do something (in this example + * print the address) on child elements that match the criteria given of @a match. + * + * @see eina_model_child_slice_iterator_get() + * @see eina_model_child_reversed_iterator_get() + * @see eina_model_child_sorted_iterator_get() + * @see eina_model_child_filtered_iterator_get() + * @since 1.2 + */ +EAPI Eina_Iterator *eina_model_child_filtered_iterator_get(Eina_Model *model, + Eina_Each_Cb match, + const void *data) EINA_ARG_NONNULL(1, 2) EINA_WARN_UNUSED_RESULT; + +/** + * @brief Returns a filtered slice of the @a model's children. + * @param model The model whose children to iterate over. + * @param start The position of the first child to be tested for inclusion in + * the iterator. + * @param count The number of children to be tested for inclusion in the + * iterator. + * @param match The function used to decide which children will be included in + * the iterator. + * @param data Data passed to the @a match function. + * @return Newly created iterator instance on success or @c NULL on failure. + * + * @note Only children for whom @a match returns EINA_TRUE will be included in + * the iterator. + * + * @note Each iteration(call to eina_iterator_next()) gives an integer index! + * + * @warning The iterator may have less than @a count children, but not more. + * + * @see eina_model_child_slice_iterator_get() + * @see eina_model_child_reversed_iterator_get() + * @see eina_model_child_sorted_iterator_get() + * @see eina_model_child_filtered_iterator_get() + * @since 1.2 + */ +EAPI Eina_Iterator *eina_model_child_slice_filtered_iterator_get(Eina_Model *model, + unsigned int start, + unsigned int count, + Eina_Each_Cb match, + const void *data) EINA_ARG_NONNULL(1, 4) EINA_WARN_UNUSED_RESULT; + + +/** + * @} + */ + +/** + * @brief Convert model to string. + * @param model the model instance. + * @return Newly allocated memory or @c NULL on failure. + * + * The default format of the ouput is: + * Type_Name({Property_Name: Property_Value, ...}, [Child0, Child1, ...]) + * + * Where: + * @li Type_Name: eina_model_type_name_get(eina_model_type_get(model)) + * @li Properties are sorted alphabetically. + * @li Property_Value is created using eina_value_to_string(). + * @li Children are converted using eina_model_to_string() + * + * @since 1.2 + */ +EAPI char *eina_model_to_string(const Eina_Model *model) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT EINA_MALLOC; + +/** + * @defgroup Eina_Model_Type_Group Data Model Type management + * + * Functions and structures related to implementing new types or + * extending existing ones. + * + * All eina_model_type functions takes an Eina_Model_Type or + * Eina_Model_Interface as parameter and may be used to validate or + * query information about them. + * + * The functions with prefix eina_model_type that matches eina_model + * counterparts, such as eina_model_type_compare() and + * eina_model_compare() are used as "super", that is, calls the @c + * compare() method of the given type (or its parent) instead of the + * most specific type of provided Eina_Model. + * + * Examples: + * @li @ref eina_model_02_example_page contains an easy to follow + * example that demonstrates several of the important features of + * eina_model, uses #EINA_MODEL_TYPE_GENERIC. + * @li @ref eina_model_03_example_page walk-through example on how to + * inherit types, a suggestion of eina_model_load() usage and uses + * #EINA_MODEL_TYPE_STRUCT. + * + * @{ + */ + +/** + * @def EINA_MODEL_TYPE_VERSION + * Current API version, used to validate #_Eina_Model_Type. + */ +#define EINA_MODEL_TYPE_VERSION (1) + +/** + * @struct _Eina_Model_Type + * API to access models. + * + * Each type of the hierarchy and each interface will get its own + * private data of size @c private_size (defined at each subtype or + * interface), this can be retrieved with + * eina_model_type_private_data_get() and + * eina_model_interface_private_data_get(). + * + * Private are created @b automatically and should be setup with @c + * setup and flushed with @c flush. All types (or interfaces) + * functions that exist are called! Don't call your parent's @c setup or + * @c flush! The setup is done from parent to child. Flush is done from + * child to parent. + * + * After memory setup was done, @c constructor of the toplevel type + * defining it is called. If desired it may call parent's constructor + * in whatever order is desired. This may be used to create + * properties, children and may use parent's data if needed. The only + * constructor caled is that of the most specialized type, if interface + * constructors should be called, do them in the desired order from the type + * constructor. + * + * When the model is deleted, explicitly with eina_model_del() or + * implicitly with eina_model_unref() on the last reference, the @c + * destructor is called. It must release references to other + * models. When the last reference is dropped, every @c flush is + * called from child to parent, then memory is freed. The only + * destructor caled is that of the most specialized type, if interface + * destructors should be called, do them in the desired order from the type + * destructor. + * + * + * @note The methods @c setup and @c flush should exist if there is + * private data, otherwise memory may be uninitialized or leaks. + * @note It is recommended that @c constructor and @c destructor exist + * to correctly do their roles and call parents in the correct + * order. Whenever they do not exist, their parent pointer is + * called. + * @note a runtime check will enforce just types with ABI version + * #EINA_MODEL_TYPE_VERSION are used by comparing with the @c version + * member. + * + * @since 1.2 + */ +struct _Eina_Model_Type +{ + unsigned int version; /**< must be #EINA_MODEL_TYPE_VERSION */ + unsigned int private_size; /**< used to allocate type private data */ + unsigned int type_size; /**< used to know sizeof(Eina_Model_Type) or subtypes (which may be bigger, by including Eina_Model_Type as a header */ + const char *name; /**< name for debug and introspection */ + const Eina_Model_Type *parent; /**< parent type, must be EINA_MODEL_TYPE_BASE or a child of */ + const Eina_Model_Interface **interfaces; /**< null terminated array of interfaces */ + const Eina_Model_Event_Description *events; /**< null terminated array of events */ + Eina_Bool (*setup)(Eina_Model *model); /**< setup type private data, do @b not call parent type setup! */ + Eina_Bool (*flush)(Eina_Model *model); /**< flush type private data, do @b not call parent type flush! */ + Eina_Bool (*constructor)(Eina_Model *model); /**< construct type instance, setup was already called. Should call parent's or interfaces' constructor if needed */ + Eina_Bool (*destructor)(Eina_Model *model); /**< destruct type instance, flush will be called after it. Should call parent's or interfaces' destructor if needed. Release reference to other models here. */ + Eina_Bool (*copy)(const Eina_Model *src, Eina_Model *dst); /**< copy type private data, do @b not call parent type copy! */ + Eina_Bool (*deep_copy)(const Eina_Model *src, Eina_Model *dst); /**< deep copy type private data, do @b not call parent type deep copy! */ + Eina_Bool (*compare)(const Eina_Model *a, const Eina_Model *b, int *cmp); + Eina_Bool (*load)(Eina_Model *model); + Eina_Bool (*unload)(Eina_Model *model); + Eina_Bool (*property_get)(const Eina_Model *model, const char *name, Eina_Value *value); + Eina_Bool (*property_set)(Eina_Model *model, const char *name, const Eina_Value *value); + Eina_Bool (*property_del)(Eina_Model *model, const char *name); + Eina_List *(*properties_names_list_get)(const Eina_Model *model); /**< list of stringshare */ + int (*child_count)(const Eina_Model *model); + Eina_Model *(*child_get)(const Eina_Model *model, unsigned int position); + Eina_Bool (*child_set)(Eina_Model *model, unsigned int position, Eina_Model *child); + Eina_Bool (*child_del)(Eina_Model *model, unsigned int position); + Eina_Bool (*child_insert_at)(Eina_Model *model, unsigned int position, Eina_Model *child); + int (*child_find)(const Eina_Model *model, unsigned int start_position, const Eina_Model *other); + int (*child_criteria_match)(const Eina_Model *model, unsigned int start_position, Eina_Each_Cb match, const void *data); + void (*child_sort)(Eina_Model *model, Eina_Compare_Cb compare); + Eina_Iterator *(*child_iterator_get)(Eina_Model *model, unsigned int start, unsigned int count); + Eina_Iterator *(*child_reversed_iterator_get)(Eina_Model *model, unsigned int start, unsigned int count); + Eina_Iterator *(*child_sorted_iterator_get)(Eina_Model *model, unsigned int start, unsigned int count, Eina_Compare_Cb compare); + Eina_Iterator *(*child_filtered_iterator_get)(Eina_Model *model, unsigned int start, unsigned int count, Eina_Each_Cb match, const void *data); + char *(*to_string)(const Eina_Model *model); /**< used to represent model as string, usually for debug purposes or user convenience */ + void *__extension_ptr0; /**< not to be used */ + void *__extension_ptr1; /**< not to be used */ + void *__extension_ptr2; /**< not to be used */ + void *__extension_ptr3; /**< not to be used */ +}; + +#define EINA_MODEL_TYPE_INIT(name, type, private_type, parent, interfaces, events) \ + {EINA_MODEL_TYPE_VERSION, \ + sizeof(private_type), \ + sizeof(type), \ + name, \ + parent, \ + interfaces, \ + events, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL \ + } + +#define EINA_MODEL_TYPE_INIT_NOPRIVATE(name, type, parent, interfaces, events) \ + {EINA_MODEL_TYPE_VERSION, \ + 0, \ + sizeof(type), \ + name, \ + parent, \ + interfaces, \ + events, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL \ + } + +#define EINA_MODEL_TYPE_INIT_NULL \ + {0, \ + 0, \ + 0, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL \ + } + +/** + * @brief Calls the constructor of @a type for @a model. + * @param type The type whose constructor will be called. + * @param model The model instance. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * This should be used to call the parent's type constructor, something like: + * @code + * static Eina_Bool my_type_constructor(Eina_Model *m) + * { + * // call parents constructor: + * if (!eina_model_type_constructor(MY_TYPE->parent, m)) + * return EINA_FALSE; + * // do my stuff + * return EINA_TRUE; + * } + * @endcode + * @note You should only do your type's initialization after the parent type has + * done his own(this is as to ensure you can call on your parent's methods). + * + * @warning If model doesn't inherit from(or is of) @a type does nothing and + * returns EINA_FALSE. + * + * @see eina_model_new() + * @see _Eina_Model_Type + * @since 1.2 + */ +EAPI Eina_Bool eina_model_type_constructor(const Eina_Model_Type *type, + Eina_Model *model) EINA_ARG_NONNULL(1, 2) EINA_WARN_UNUSED_RESULT; +/** + * @brief Calls the destructor of @a type for @a model. + * @param type The type whose destructor will be called. + * @param model The model instance. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * This should be used to call the parent's type destructor, something like: + * @code + * static Eina_Bool my_type_destructor(Eina_Model *m) + * { + * // do my stuff + * // call parents destructor: + * if (!eina_model_type_destructor(MY_TYPE->parent, m)) + * return EINA_FALSE; + * return EINA_TRUE; + * } + * @endcode + * @note It's considered good practice to free your type's resources before + * calling the parent's destructor. + * + * @warning If model doesn't inherit from(or is of) @a type does nothing and + * returns EINA_FALSE. + * + * @see eina_model_del() + * @see _Eina_Model_Type + * @since 1.2 + */ +EAPI Eina_Bool eina_model_type_destructor(const Eina_Model_Type *type, + Eina_Model *model) EINA_ARG_NONNULL(1, 2); +/** + * @brief Calls the copy method of @a type for @a model. + * @param type The type whose copy method will be called. + * @param src Pointer to the model to be copied. + * @param dst Pointer to where copy will be put. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @warning If model doesn't inherit from(or is of) @a type does nothing and + * returns EINA_FALSE. + * + * @see eina_model_copy() + * @see _Eina_Model_Type + * @since 1.2 + */ +EAPI Eina_Bool eina_model_type_copy(const Eina_Model_Type *type, + const Eina_Model *src, + Eina_Model *dst) EINA_ARG_NONNULL(1, 2, 3); +/** + * @brief Calls the deep copy method of @a type for @a model. + * @param type The type whose copy method will be called. + * @param src Pointer to the model to be copied. + * @param dst Pointer to where copy will be put. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @warning If model doesn't inherit from(or is of) @a type does nothing and + * returns EINA_FALSE. + * + * @see eina_model_deep_copy() + * @see _Eina_Model_Type + * @since 1.2 + */ +EAPI Eina_Bool eina_model_type_deep_copy(const Eina_Model_Type *type, + const Eina_Model *src, + Eina_Model *dst) EINA_ARG_NONNULL(1, 2, 3); +/** + * @brief Calls the compare method of @a type for @a model. + * @param[in] type The type whose compare method will be called. + * @param[in] a Pointer to the first model to be compared. + * @param[in] b Pointer to the second model to be compared. + * @param[out] cmp The value of the comparison, 1 if @a b is greater than @a a, + * -1 if @a b is smaller than @a a, 0 if @a a and @a b are equal. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @warning If model doesn't inherit from(or is of) @a type does nothing and + * returns EINA_FALSE. + * + * @see eina_model_compare() + * @see _Eina_Model_Type + * @since 1.2 + */ +EAPI Eina_Bool eina_model_type_compare(const Eina_Model_Type *type, + const Eina_Model *a, + const Eina_Model *b, + int *cmp) EINA_ARG_NONNULL(1, 2, 3, 4); +/** + * @brief Calls the load method of @a type for @a model. + * @param type The type whose load method will be called. + * @param model The model instance. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @warning If model doesn't inherit from(or is of) @a type does nothing and + * returns EINA_FALSE. + * + * @see eina_model_load() + * @see _Eina_Model_Type + * @since 1.2 + */ +EAPI Eina_Bool eina_model_type_load(const Eina_Model_Type *type, + Eina_Model *model) EINA_ARG_NONNULL(1, 2); +/** + * @brief Calls the unload method of @a type for @a model. + * @param type The type whose unload method will be called. + * @param model The model instance. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @warning If model doesn't inherit from(or is of) @a type does nothing and + * returns EINA_FALSE. + * + * @see eina_model_unload() + * @see _Eina_Model_Type + * @since 1.2 + */ +EAPI Eina_Bool eina_model_type_unload(const Eina_Model_Type *type, + Eina_Model *model) EINA_ARG_NONNULL(1, 2); +/** + * @brief Calls the property get method of @a type for @a model. + * @param[in] type The type whose property get method will be called. + * @param[in] model The model instance. + * @param[in] name Name of property to get. + * @param[out] value Pointer to where value of property will be placed. + * @return EINA_TRUE if able to get property, EINA_FALSE otherwise. + * + * @warning If model doesn't inherit from(or is of) @a type does nothing and + * returns EINA_FALSE. + * + * @see eina_model_property_get() + * @see _Eina_Model_Type + * @since 1.2 + */ +EAPI Eina_Bool eina_model_type_property_get(const Eina_Model_Type *type, + const Eina_Model *model, + const char *name, + Eina_Value *value) EINA_ARG_NONNULL(1, 2, 3, 4); +/** + * @brief Calls the property set method of @a type for @a model. + * @param type The type whose property set method will be called. + * @param model The model instance. + * @param name Name of property whose value will be set. + * @param value The value to be set. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @warning If model doesn't inherit from(or is of) @a type does nothing and + * returns EINA_FALSE. + * + * @see eina_model_property_set() + * @see _Eina_Model_Type + * @since 1.2 + */ +EAPI Eina_Bool eina_model_type_property_set(const Eina_Model_Type *type, + Eina_Model *model, + const char *name, + const Eina_Value *value) EINA_ARG_NONNULL(1, 2, 3, 4); +/** + * @brief Calls the property del method of @a type for @a model. + * @param type The type whose property delete method will be called. + * @param model The model instance. + * @param name The name of the property to be deleted. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @warning If model doesn't inherit from(or is of) @a type does nothing and + * returns EINA_FALSE. + * + * @see eina_model_property_del() + * @see _Eina_Model_Type + * @since 1.2 + */ +EAPI Eina_Bool eina_model_type_property_del(const Eina_Model_Type *type, + Eina_Model *model, + const char *name) EINA_ARG_NONNULL(1, 2, 3); +/** + * @brief Calls the properties name list method of @a type for @a model. + * @param type The type whose property name list get method will be called. + * @param model The model instance. + * @return #Eina_List of properties' names. + * + * @warning If model doesn't inherit from(or is of) @a type does nothing and + * returns EINA_FALSE. + * + * @see eina_model_properties_names_list_get() + * @see _Eina_Model_Type + * @since 1.2 + */ +EAPI Eina_List *eina_model_type_properties_names_list_get(const Eina_Model_Type *type, + const Eina_Model *model) EINA_ARG_NONNULL(1, 2); +/** + * @brief Calls the child count method of @a type for @a model. + * @param type The type whose child count method will be called. + * @param model The model instance. + * @return Number of children in @a model. + * + * @warning If model doesn't inherit from(or is of) @a type does nothing and + * returns EINA_FALSE. + * + * @see eina_model_child_count() + * @see _Eina_Model_Type + * @since 1.2 + */ +EAPI int eina_model_type_child_count(const Eina_Model_Type *type, + const Eina_Model *model) EINA_ARG_NONNULL(1, 2); +/** + * @brief Calls the child get method of @a type for @a model. + * @param type The type whose child get method will be called. + * @param model The model instance. + * @param position The position of the child to get. + * @return The child model, or NULL on failure. + * + * @warning If model doesn't inherit from(or is of) @a type does nothing and + * returns EINA_FALSE. + * + * @see eina_model_child_get() + * @see _Eina_Model_Type + * @since 1.2 + */ +EAPI Eina_Model *eina_model_type_child_get(const Eina_Model_Type *type, + const Eina_Model *model, + unsigned int position) EINA_ARG_NONNULL(1, 2); +/** + * @brief Calls the child set method of @a type for @a model. + * @param type The type whose child set method will be called. + * @param model The model instance. + * @param position The position of the child to be set. + * @param child Pointer to value(child) to be set. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @warning If model doesn't inherit from(or is of) @a type does nothing and + * returns EINA_FALSE. + * + * @see eina_model_child_set() + * @see _Eina_Model_Type + * @since 1.2 + */ +EAPI Eina_Bool eina_model_type_child_set(const Eina_Model_Type *type, + Eina_Model *model, + unsigned int position, + Eina_Model *child) EINA_ARG_NONNULL(1, 2, 4); +/** + * @brief Calls the child del method of @a type for @a model. + * @param type The type whose child delete method will be called. + * @param model The model instance. + * @param position Position of child to be deleted. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @warning If model doesn't inherit from(or is of) @a type does nothing and + * returns EINA_FALSE. + * + * @see eina_model_child_del() + * @see _Eina_Model_Type + * @since 1.2 + */ +EAPI Eina_Bool eina_model_type_child_del(const Eina_Model_Type *type, + Eina_Model *model, + unsigned int position) EINA_ARG_NONNULL(1, 2); +/** + * @brief Calls the child insert at method of @a type for @a model. + * @param type The type whose child insert method will be called. + * @param model The model instance. + * @param position Position in which @a child will be inserted. + * @param child The child to be inserted. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @warning If model doesn't inherit from(or is of) @a type does nothing and + * returns EINA_FALSE. + * + * @see eina_model_child_insert_at() + * @see _Eina_Model_Type + * @since 1.2 + */ +EAPI Eina_Bool eina_model_type_child_insert_at(const Eina_Model_Type *type, + Eina_Model *model, + unsigned int position, + Eina_Model *child) EINA_ARG_NONNULL(1, 2, 4); +/** + * @brief Calls the child find method of @a type for @a model. + * @param type The type whose find method will be called. + * @param model The model instance. + * @param start_position The first position to search for. + * @param other The child being searched for. + * @return The index of the searched child, or -1 if not found. + * + * @warning If model doesn't inherit from(or is of) @a type does nothing and + * returns EINA_FALSE. + * + * @see eina_model_child_find() + * @see _Eina_Model_Type + * @since 1.2 + */ +EAPI int eina_model_type_child_find(const Eina_Model_Type *type, + const Eina_Model *model, + unsigned int start_position, + const Eina_Model *other) EINA_ARG_NONNULL(1, 2, 4); +/** + * @brief Calls the child criteria match method of @a type for @a model. + * @param type The type whose child criteria match method will be called. + * @param model The model instance. + * @param start_position The first position to be checked. + * @param match The function used to determine if a child matches the criteria. + * @param data Data given to the @a match function. May be NULL. + * @return The position of the first child to match the criteria or -1 if no + * child matches it. + * + * @warning If model doesn't inherit from(or is of) @a type does nothing and + * returns EINA_FALSE. + * + * @see eina_model_child_criteria_match() + * @see _Eina_Model_Type + * @since 1.2 + */ +EAPI int eina_model_type_child_criteria_match(const Eina_Model_Type *type, + const Eina_Model *model, + unsigned int start_position, + Eina_Each_Cb match, + const void *data) EINA_ARG_NONNULL(1, 2, 4); +/** + * @brief Calls the child sort method of @a type for @a model. + * @param type The type whose child sort method will be called. + * @param model The model instance. + * @param compare Function used to compare children. + * + * @warning If model doesn't inherit from(or is of) @a type does nothing and + * returns EINA_FALSE. + * + * @see eina_model_child_sort() + * @see _Eina_Model_Type + * @since 1.2 + */ +EAPI void eina_model_type_child_sort(const Eina_Model_Type *type, + Eina_Model *model, + Eina_Compare_Cb compare) EINA_ARG_NONNULL(1, 2, 3); +/** + * @brief Calls the child iterator get method of @a type for @a model. + * @param type The type whose child iterator get method will be called. + * @param model The model instance. + * @param start The first child to be a part of the iterator. + * @param count The number of children included in the iterator. + * @return Newly created iterator instance on success or @c NULL on failure. + * + * @warning If model doesn't inherit from(or is of) @a type does nothing and + * returns EINA_FALSE. + * + * @see eina_model_child_iterator_get() + * @see _Eina_Model_Type + * @since 1.2 + */ +EAPI Eina_Iterator *eina_model_type_child_iterator_get(const Eina_Model_Type *type, + Eina_Model *model, + unsigned int start, + unsigned int count) EINA_ARG_NONNULL(1, 2); +/** + * @brief Calls the child reversed iterator get method of @a type for @a model. + * @param type The type whose child reversed iterator get method will be called. + * @param model The model instance. + * @param start The first child to be a part of the iterator. + * @param count The number of children included in the iterator. + * @return Newly created iterator instance on success or @c NULL on failure. + * + * @warning If model doesn't inherit from(or is of) @a type does nothing and + * returns EINA_FALSE. + * + * @see eina_model_child_reversed_iterator_get() + * @see _Eina_Model_Type + * @since 1.2 + */ +EAPI Eina_Iterator *eina_model_type_child_reversed_iterator_get(const Eina_Model_Type *type, + Eina_Model *model, + unsigned int start, + unsigned int count) EINA_ARG_NONNULL(1, 2); +/** + * @brief Calls the child sorted iterator get method of @a type for @a model. + * @param type The type whose child sorted iterator get method will be called. + * @param model The model instance. + * @param start The first child to be a part of the iterator. + * @param count The number of children included in the iterator. + * @param compare Function used to compare children. + * @return Newly created iterator instance on success or @c NULL on failure. + * + * @warning If model doesn't inherit from(or is of) @a type does nothing and + * returns EINA_FALSE. + * + * @see eina_model_child_sorted_iterator_get() + * @see _Eina_Model_Type + * @since 1.2 + */ +EAPI Eina_Iterator *eina_model_type_child_sorted_iterator_get(const Eina_Model_Type *type, + Eina_Model *model, + unsigned int start, + unsigned int count, + Eina_Compare_Cb compare) EINA_ARG_NONNULL(1, 2, 5); +/** + * @brief Calls the child filtered get method of @a type for @a model. + * @param type The type whose child filtered iterator get method will be called. + * @param model The model instance. + * @param start The first child to be a part of the iterator. + * @param count Number of children to be checked for inclusion in the iterator. + * @param match Function used to check if child will be included in the iterator. + * @param data Data given to the @a match function. May be NULL. + * @return Newly created iterator instance on success or @c NULL on failure. + * + * @warning If model doesn't inherit from(or is of) @a type does nothing and + * returns EINA_FALSE. + * + * @see eina_model_child_filtered_iterator_get() + * @see _Eina_Model_Type + * @since 1.2 + */ +EAPI Eina_Iterator *eina_model_type_child_filtered_iterator_get(const Eina_Model_Type *type, + Eina_Model *model, + unsigned int start, + unsigned int count, + Eina_Each_Cb match, + const void *data) EINA_ARG_NONNULL(1, 2, 5); +/** + * @brief Calls the to string method of @a type for @a model. + * @param type The type whose to string method will be called. + * @param model The model instance. + * @return String representationof @a model. + * + * @warning If model doesn't inherit from(or is of) @a type does nothing and + * returns EINA_FALSE. + * + * @see eina_model_to_string() + * @see _Eina_Model_Type + * @since 1.2 + */ +EAPI char *eina_model_type_to_string(const Eina_Model_Type *type, + const Eina_Model *model) EINA_ARG_NONNULL(1, 2) EINA_WARN_UNUSED_RESULT EINA_MALLOC; + + +/** + * @brief Get resolved method from types that extend Eina_Model_Type given @a offset. + * @param model the model to query the method + * @param offset the byte offset in the structure given as type, it + * must be bigger than Eina_Model_Type itself. + * @return Address to resolved method, or @c NULL if method is not implemented. + * + * The use of this function is discouraged, you should use + * eina_model_method_resolve() instead. + * + * When implementing new types that augments the basic methods from + * Eina_Model_Type, the recommended structure layout is as follow: + * @code + * typedef struct _My_Type My_Type; + * struct _My_Type { + * Eina_Model_Type base; + * int (*my_method)(Eina_Model *model); + * }; + * + * int my_type_my_method(Eina_Model *model); + * @endcode + * + * Then the implementation of @c my_type_my_method() needs to get the + * most specific @c my_method that is not @c NULL from type hierarchy, + * also called "resolve the method". + * + * To do this in an efficient way, Eina_Model infrastructure + * pre-resolves all methods and provides this function for efficient + * query. The recommended implementation of my_type_my_method() would + * be: + * @code + * int my_type_my_method(Eina_Model *model) + * { + * int (*meth)(Eina_Model *); + * + * EINA_SAFETY_ON_FALSE_RETURN(eina_model_instance_check(model, MY_TYPE), -1); + * + * meth = eina_model_method_offset_resolve(model, offsetof(My_Type, my_method)); + * EINA_SAFETY_ON_NULL_RETURN(meth, -1); + * return meth(model); + * } + * @endcode + * + * @note offset must be bigger than Eina_Model_Type, otherwise use + * specific functions such as eina_model_property_get(). + * + * @see eina_model_method_resolve + * @see eina_model_type_method_resolve + * @since 1.2 + */ +EAPI const void *eina_model_method_offset_resolve(const Eina_Model *model, unsigned int offset) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT EINA_PURE; + +/** + * @brief Gets resolved method from @a type of @a model given @a offset. + * @param type The type whose method offset resolve method will be called. + * @param model The model instance. + * @param offset The offset of the wanted method. + * @return Address to resolved method, or @c NULL if method is not implemented. + * + * @see eina_model_method_offset_resolve() + * @since 1.2 + */ +EAPI const void *eina_model_type_method_offset_resolve(const Eina_Model_Type *type, const Eina_Model *model, unsigned int offset) EINA_ARG_NONNULL(1, 2) EINA_WARN_UNUSED_RESULT EINA_PURE; + +#define eina_model_method_resolve(model, struct_type, method) eina_model_method_offset_resolve((model), offsetof(struct_type, method)) + +#define eina_model_type_method_resolve(type, model, struct_type, method) eina_model_type_method_offset_resolve((type), (model), offsetof(struct_type, method)) + +/** + * @def EINA_MODEL_INTERFACE_VERSION + * Current API version, used to validate #_Eina_Model_Interface. + */ +#define EINA_MODEL_INTERFACE_VERSION (1) + +/** + * @struct _Eina_Model_Interface + * API to access models. + * + * Interfaces are managed by name, then multiple Eina_Model_Interface + * may have the same name meaning it implements that name. + * + * Each interface will get its own private data of size @c + * private_size (defined at each sub interface), this can be retrieved + * with eina_model_interface_private_data_get(). + * + * Private are created @b automatically and should be setup with @c + * setup and flushed with @c flush. All interfaces functions that + * exist are called! Don't call your parent's @c setup or @c flush! + * The setup is done from parent to child. Flush is done from child to + * parent (topological sort is applied to interface graph). + * + * @note The methods @c setup and @c flush should exist if there is + * private data, otherwise memory may be uninitialized or leaks. + * @note It is recommended that @c constructor and @c destructor exist + * to correctly do their roles and call parents in the correct + * order. Whenever they do not exist, their parent pointer is + * called. + * @note Interface's constructor and destructor are only called by + * type counterparts. Unlike setup and flush, they are not + * guaranteed to be called. + * @note use the same name pointer on queries to speed up the lookups! + * @note a runtime check will enforce just types with ABI version + * #EINA_MODEL_INTERFACE_VERSION are used by comparing with the + * @c version member. + * + * @since 1.2 + */ +struct _Eina_Model_Interface +{ + unsigned int version; /**< must be #EINA_MODEL_INTERFACE_VERSION */ + unsigned int private_size; /**< used to allocate interface private data */ + unsigned int interface_size; /**< used to know sizeof(Eina_Model_Interface) or subtypes (which may be bigger, by including Eina_Model_Interface as header */ + const char *name; /**< name for debug and introspection */ + const Eina_Model_Interface **interfaces; /**< null terminated array of parent interfaces */ + const Eina_Model_Event_Description *events; /**< null terminated array of events */ + Eina_Bool (*setup)(Eina_Model *model); /**< setup interface private data, do @b not call parent interface setup! */ + Eina_Bool (*flush)(Eina_Model *model); /**< flush interface private data, do @b not call parent interface flush! */ + Eina_Bool (*constructor)(Eina_Model *model); /**< construct interface instance, setup was already called. Should call parent's constructor if needed */ + Eina_Bool (*destructor)(Eina_Model *model); /**< destruct interface instance, flush will be called after it. Should call parent's destructor if needed. Release reference to other models here. */ + Eina_Bool (*copy)(const Eina_Model *src, Eina_Model *dst); /**< copy interface private data, do @b not call parent interface copy! */ + Eina_Bool (*deep_copy)(const Eina_Model *src, Eina_Model *dst); /**< deep copy interface private data, do @b not call parent interface deep copy! */ + void *__extension_ptr0; /**< not to be used @internal */ + void *__extension_ptr1; /**< not to be used @internal */ + void *__extension_ptr2; /**< not to be used @internal */ + void *__extension_ptr3; /**< not to be used @internal */ +}; + +#define EINA_MODEL_INTERFACE_INIT(name, iface, private_type, parent, events) \ + {EINA_MODEL_INTERFACE_VERSION, \ + sizeof(private_type), \ + sizeof(iface), \ + name, \ + parent, \ + events, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL \ + } + +#define EINA_MODEL_INTERFACE_INIT_NOPRIVATE(name, iface, parent, events) \ + {EINA_MODEL_INTERFACE_VERSION, \ + 0, \ + sizeof(iface), \ + name, \ + parent, \ + events, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL \ + } + +#define EINA_MODEL_INTERFACE_INIT_NULL \ + {0, \ + 0, \ + 0, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + NULL \ + } + +/** + * @brief Calls the constructor of @a iface on @a model. + * @param iface The interface whose constructor will be called. + * @param model The model instance. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @warning If @a model doesn't implement @a iface does nothing and returns + * EINA_FALSE. + * + * @see eina_model_new() + * @see _Eina_Model_Interface + * @since 1.2 + */ +EAPI Eina_Bool eina_model_interface_constructor(const Eina_Model_Interface *iface, + Eina_Model *model) EINA_ARG_NONNULL(1, 2) EINA_WARN_UNUSED_RESULT; +/** + * @brief Calls the destructor of @a iface on @a model. + * @param iface The interface whose destructor will be called. + * @param model The model instance. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @warning If @a model doesn't implement @a iface does nothing and returns + * EINA_FALSE. + * + * @see eina_model_del() + * @see _Eina_Model_Interface + * @since 1.2 + */ +EAPI Eina_Bool eina_model_interface_destructor(const Eina_Model_Interface *iface, + Eina_Model *model) EINA_ARG_NONNULL(1, 2); +/** + * @brief Calls the copy method of @a iface on @a model. + * @param iface The interface whose copy method will be called. + * @param src Pointer to the model to be copied. + * @param dst Pointer to where copy will be put. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @warning If @a model doesn't implement @a iface does nothing and returns + * EINA_FALSE. + * + * @see eina_model_copy() + * @see _Eina_Model_Interface + * @since 1.2 + */ +EAPI Eina_Bool eina_model_interface_copy(const Eina_Model_Interface *iface, + const Eina_Model *src, + Eina_Model *dst) EINA_ARG_NONNULL(1, 2, 3); +/** + * @brief Calls the deep copy method of @a iface on @a model. + * @param iface The interface whose deep copy method will be called. + * @param src Pointer to the model to be copied. + * @param dst Pointer to where copy will be put. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @warning If @a model doesn't implement @a iface does nothing and returns + * EINA_FALSE. + * + * @see eina_model_deep_copy() + * @see _Eina_Model_Interface + * @since 1.2 + */ +EAPI Eina_Bool eina_model_interface_deep_copy(const Eina_Model_Interface *iface, + const Eina_Model *src, + Eina_Model *dst) EINA_ARG_NONNULL(1, 2, 3); + +#define eina_model_interface_method_resolve(iface, model, struct_type, method) eina_model_interface_method_offset_resolve((iface), (model), offsetof(struct_type, method)) + +/** + * @brief Gets the @a iface's method for @a model at @a offset. + * @param iface The interface whose method offset resolve method will be called. + * @param model The model instance. + * @param offset The offset of the wanted method. + * @return Address to resolved method, or @c NULL if method is not implemented. + * + * @see eina_model_method_offset_resolve() + * @since 1.2 + */ +EAPI const void *eina_model_interface_method_offset_resolve(const Eina_Model_Interface *iface, const Eina_Model *model, unsigned int offset) EINA_ARG_NONNULL(1, 2) EINA_WARN_UNUSED_RESULT EINA_PURE; + + +/** + * @struct _Eina_Model_Event_Description + * Data Model Event Description. + * + * @see EINA_MODEL_EVENT_DESCRIPTION() + * @see #EINA_MODEL_EVENT_DESCRIPTION_SENTINEL + * @since 1.2 + */ +struct _Eina_Model_Event_Description +{ + const char *name; /**< name used for lookups */ + const char *type; /**< used for introspection purposes, documents what goes as callback event information (@c event_info) */ + const char *doc; /**< documentation for introspection purposes */ +}; + +/** + * @def EINA_MODEL_EVENT_DESCRIPTION + * + * Helper to define Eina_Model_Event_Description fields. + * + * @since 1.2 + */ +#define EINA_MODEL_EVENT_DESCRIPTION(name, type, doc) {name, type, doc} + +/** + * @def EINA_MODEL_EVENT_DESCRIPTION_SENTINEL + * + * Helper to define Eina_Model_Event_Description fields for sentinel (last + * item). + * + * @since 1.2 + */ +#define EINA_MODEL_EVENT_DESCRIPTION_SENTINEL {NULL, NULL, NULL} + +/** + * @brief Check @a type is valid. + * @param type The type to be checked. + * @return #EINA_TRUE on success, #EINA_FALSE otherwise. + * + * @since 1.2 + */ +EAPI Eina_Bool eina_model_type_check(const Eina_Model_Type *type) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT EINA_PURE; +/** + * @brief Gets the name @a type. + * @param type The type whose name is wanted. + * @return Name of @a type. + * + * @since 1.2 + */ +EAPI const char *eina_model_type_name_get(const Eina_Model_Type *type) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT EINA_PURE; +/** + * @brief Gets the parent type of @a type. + * @param type The type whose parent is wanted. + * @return Type of parent. + * + * @since 1.2 + */ +EAPI const Eina_Model_Type *eina_model_type_parent_get(const Eina_Model_Type *type) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT EINA_PURE; + +/** + * @brief Setup the type to be a subclass of another parent type. + * @param type type to be modified + * @param parent type to be used as parent + * @return #EINA_TRUE on success, #EINA_FALSE otherwise. + * + * Although @a type is modified, the following properties are not + * touched or they are actually used for validation: + * + * @li @c type->version must be #EINA_MODEL_TYPE_VERSION; + * @li @c type->private_size unmodified, should be set to type's size; + * @li @c type->name unmodified, should be set to type's name. + * + * + * All other fields are modified as follow: + * + * @li @c type->type_size initiated to parent->type_size + * @li @c type->interfaces = NULL; + * @li @c type->events = NULL; + * @li @c type->setup = NULL; + * @li @c type->flush = NULL; + * @li @c type->constructor = NULL; + * @li @c type->destructor = NULL; + * @li @c type->copy = NULL; + * @li @c type->deep_copy = NULL; + * @li @c type->compare = NULL; + * @li @c type->load = NULL; + * @li @c type->unload = NULL; + * @li @c type->property_get = NULL; + * @li @c type->property_set = NULL; + * @li @c type->property_del = NULL; + * @li @c type->properties_names_list_get = NULL; + * @li @c type->child_count = NULL; + * @li @c type->child_get = NULL; + * @li @c type->child_set = NULL; + * @li @c type->child_del = NULL; + * @li @c type->child_insert_at = NULL; + * @li @c type->child_find = NULL; + * @li @c type->child_criteria_match = NULL; + * @li @c type->child_sort = NULL; + * @li @c type->child_iterator_get = NULL; + * @li @c type->child_reversed_iterator_get = NULL; + * @li @c type->child_sorted_iterator_get = NULL; + * @li @c type->child_filtered_iterator_get = NULL; + * @li @c type->to_string = NULL; + * + * If you have custom methods, overload them afterwards + * eina_model_type_subclass_setup() returns with #EINA_TRUE. + * + * @since 1.2 + */ +EAPI Eina_Bool eina_model_type_subclass_setup(Eina_Model_Type *type, + const Eina_Model_Type *parent) EINA_ARG_NONNULL(1, 2); + +/** + * @brief Checks if @a type is a subclass of(or the same as) @a self_or_parent. + * @param type The type to be checked. + * @param self_or_parent The type being checked for. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @since 1.2 + */ +EAPI Eina_Bool eina_model_type_subclass_check(const Eina_Model_Type *type, + const Eina_Model_Type *self_or_parent) EINA_ARG_NONNULL(1, 2) EINA_WARN_UNUSED_RESULT EINA_PURE; + + +/** + * @brief Gets a interface with name @a name from @a type. + * @param type The type instance. + * @param name The name of the desired interface. + * @return The interface implemented by @a type with name @a name, or null if + * this type doesn't implement any interface with name @a name. + * + * @since 1.2 + */ +EAPI const Eina_Model_Interface *eina_model_type_interface_get(const Eina_Model_Type *type, + const char *name) EINA_ARG_NONNULL(1, 2) EINA_WARN_UNUSED_RESULT EINA_PURE; + +/** + * @brief Gets the private date of @a model for type @a type. + * @param model The model instance. + * @param type The type whose private data will be gotten. + * @return Pointer to type's private data. + * + * @since 1.2 + */ +EAPI void *eina_model_type_private_data_get(const Eina_Model *model, + const Eina_Model_Type *type) EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(1, 2) EINA_PURE; + +/** + * @brief Checks if @a iface is a valid interface. + * @param iface The interface instance. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @since 1.2 + */ +EAPI Eina_Bool eina_model_interface_check(const Eina_Model_Interface *iface) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT EINA_PURE; + +/** + * @brief Gets the private date of @a model for interface @a iface. + * @param model The model instance. + * @param iface The interface whose private data will be gotten. + * @return Pointer to interface's private data. + * + * @since 1.2 + */ +EAPI void *eina_model_interface_private_data_get(const Eina_Model *model, + const Eina_Model_Interface *iface) EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(1, 2) EINA_PURE; + +/** + * @def EINA_MODEL_INTERFACE_PROPERTIES_VERSION + * Current API version, used to validate #_Eina_Model_Interface_Properties. + */ +#define EINA_MODEL_INTERFACE_PROPERTIES_VERSION (1) + +/** + * @typedef Eina_Model_Interface_Properties + * Interface to manage model's properties. + * + * This extends #Eina_Model_Interface as expected by interface name + * #EINA_MODEL_INTERFACE_NAME_PROPERTIES. + * + * This interface is meant to help managing properties of a model, it + * is used by #EINA_MODEL_TYPE_MIXIN in order to configure methods for + * children independently from properties. + * + * @see #_Eina_Model_Interface_Properties explains fields. + * @since 1.2 + */ +typedef struct _Eina_Model_Interface_Properties Eina_Model_Interface_Properties; + +/** + * @struct _Eina_Model_Interface_Properties + * Interface to manage model's properties. + * + * This extends #Eina_Model_Interface as expected by interface name + * #EINA_MODEL_INTERFACE_NAME_PROPERTIES. + * + * This interface is meant to help managing properties of a model, it + * is used by #EINA_MODEL_TYPE_MIXIN in order to configure methods for + * children independently from properties. + * + * @since 1.2 + */ +struct _Eina_Model_Interface_Properties +{ + Eina_Model_Interface base; /**< common interface methods */ + unsigned int version; /**< must be #EINA_MODEL_INTERFACE_PROPERTIES_VERSION */ + Eina_Bool (*compare)(const Eina_Model *a, const Eina_Model *b, int *cmp); /**< How to compare properties of this model */ + Eina_Bool (*load)(Eina_Model *model); /**< How to load properties of this model */ + Eina_Bool (*unload)(Eina_Model *model); /**< How to unload properties of this model */ + Eina_Bool (*get)(const Eina_Model *model, const char *name, Eina_Value *value); /**< Retrieve a property of this model given its name. The value will be returned as a copy and must be flushed with eina_value_flush(). The previous contents of value is ignored. */ + Eina_Bool (*set)(Eina_Model *model, const char *name, const Eina_Value *value); /**< Set a property of this model given its name. The value is assumed to be valied and is copied internally, thus it can be safely cleared with eina_value_flush() after this function returns. */ + Eina_Bool (*del)(Eina_Model *model, const char *name); /**< Delete a property given its name */ + Eina_List *(*names_list_get)(const Eina_Model *model); /**< List of stringshare with known property names */ +}; + +/** + * @brief Compares properties using @a iface's comparing function. + * + * @param[in] iface The interface used to compare the properties. + * @param[in] a The first model whose properties will be compared. + * @param[in] b The second model whose properties will be compared. + * @param[out] cmp A pointer to an integer which will contain the result of the + * comparison. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @warning If either model doesn't implement @a iface will do nothing and + * return EINA_FALSE. + * + * @see eina_model_compare() + * @since 1.2 + */ +EAPI Eina_Bool eina_model_interface_properties_compare(const Eina_Model_Interface *iface, + const Eina_Model *a, + const Eina_Model *b, + int *cmp) EINA_ARG_NONNULL(1, 2, 3, 4) EINA_WARN_UNUSED_RESULT; + +/** + * @brief Loads properties using @a iface's loading function. + * @param iface The properties interface whose load method will be called. + * @param model The model instance. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @warning If either model doesn't implement @a iface will do nothing and + * return EINA_FALSE. + * + * @see eina_model_load() + * @since 1.2 + */ +EAPI Eina_Bool eina_model_interface_properties_load(const Eina_Model_Interface *iface, + Eina_Model *model) EINA_ARG_NONNULL(1, 2); +/** + * @brief Unloads properties using @a iface's unloading function. + * @param iface The properties interface whose unload method will be called. + * @param model The model instance. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @warning If either model doesn't implement @a iface will do nothing and + * return EINA_FALSE. + * + * @see eina_model_unload() + * @since 1.2 + */ +EAPI Eina_Bool eina_model_interface_properties_unload(const Eina_Model_Interface *iface, + Eina_Model *model) EINA_ARG_NONNULL(1, 2); +/** + * @brief Gets property named @a name using @a iface's function to get properties. + * @param iface The properties interface whose property get method will be called. + * @param model The model instance. + * @param name The name of the property to get. + * @param value Pointer to where value will be stored. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @warning If either model doesn't implement @a iface will do nothing and + * return EINA_FALSE. + * + * @see eina_model_property_get() + * @since 1.2 + */ +EAPI Eina_Bool eina_model_interface_properties_get(const Eina_Model_Interface *iface, + const Eina_Model *model, + const char *name, + Eina_Value *value) EINA_ARG_NONNULL(1, 2, 3, 4); +/** + * @brief Sets property named @a name using @a iface's function to set properties. + * @param iface The properties interface whose property set method will be called. + * @param model The model instance. + * @param name The name of the property to set. + * @param value The value to be set. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @warning If either model doesn't implement @a iface will do nothing and + * return EINA_FALSE. + * + * @see eina_model_property_set() + * @since 1.2 + */ +EAPI Eina_Bool eina_model_interface_properties_set(const Eina_Model_Interface *iface, + Eina_Model *model, + const char *name, + const Eina_Value *value) EINA_ARG_NONNULL(1, 2, 3, 4); +/** + * @brief Deletes property named @a name using @a iface's function to delete properties. + * @param iface The properties interface whose property delete method will be called. + * @param model The model instance. + * @param name The name of the property to delete. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @warning If either model doesn't implement @a iface will do nothing and + * return EINA_FALSE. + * + * @see eina_model_property_del() + * @since 1.2 + */ +EAPI Eina_Bool eina_model_interface_properties_del(const Eina_Model_Interface *iface, + Eina_Model *model, + const char *name) EINA_ARG_NONNULL(1, 2, 3); +/** + * @brief Gets properties name list using @a iface's function to get properties + * name list. + * @param iface The properties interface whose property name list get method + * will be called. + * @param model The model instance. + * @return #Eina_List of properties' names. + * + * @warning If either model doesn't implement @a iface will do nothing and + * return EINA_FALSE. + * + * @see eina_model_properties_names_list_get() + * @since 1.2 + */ +EAPI Eina_List *eina_model_interface_properties_names_list_get(const Eina_Model_Interface *iface, + const Eina_Model *model) EINA_ARG_NONNULL(1, 2); /**< list of stringshare */ + +/** + * @typedef Eina_Model_Interface_Children + * Interface to manage model's children. + * + * This extends #Eina_Model_Interface as expected by interface name + * #EINA_MODEL_INTERFACE_NAME_CHILDREN. + * + * This interface is meant to help managing properties of a model, it + * is used by #EINA_MODEL_TYPE_MIXIN in order to configure methods for + * children independently from properties. + * + * @see #_Eina_Model_Interface_Children explains fields. + * @since 1.2 + */ +typedef struct _Eina_Model_Interface_Children Eina_Model_Interface_Children; + +/** + * @def EINA_MODEL_INTERFACE_CHILDREN_VERSION + * Current API version, used to validate #_Eina_Model_Interface_Children. + */ +#define EINA_MODEL_INTERFACE_CHILDREN_VERSION (1) + +/** + * @struct _Eina_Model_Interface_Children + * Interface to manage model's children. + * + * This extends #Eina_Model_Interface as expected by interface name + * #EINA_MODEL_INTERFACE_NAME_CHILDREN. + * + * This interface is meant to help managing properties of a model, it + * is used by #EINA_MODEL_TYPE_MIXIN in order to configure methods for + * children independently from properties. + * + * @since 1.2 + */ +struct _Eina_Model_Interface_Children +{ + Eina_Model_Interface base; /**< common interface methods */ + unsigned int version; /**< must be #EINA_MODEL_INTERFACE_CHILDREN_VERSION */ + Eina_Bool (*compare)(const Eina_Model *a, const Eina_Model *b, int *cmp); /**< How to compare children of this model */ + Eina_Bool (*load)(Eina_Model *model); /**< How to load children of this model */ + Eina_Bool (*unload)(Eina_Model *model); /**< How to unload children of this model */ + int (*count)(const Eina_Model *model); /**< How many children of this model */ + Eina_Model *(*get)(const Eina_Model *model, unsigned int position); /**< Retrieve a child of this model, returned child must have reference increased! */ + Eina_Bool (*set)(Eina_Model *model, unsigned int position, Eina_Model *child); /**< Set (replace) a child of this model, given child will have reference increased! */ + Eina_Bool (*del)(Eina_Model *model, unsigned int position); /**< Delete a child of this model. Existing child will have reference decreased! */ + Eina_Bool (*insert_at)(Eina_Model *model, unsigned int position, Eina_Model *child); /**< Insert a child into this model, given child will have reference increased! All elements towards the end of the internal list will be shifted to the end to make room for the new child. */ + void (*sort)(Eina_Model *model, Eina_Compare_Cb compare); /**< Reorder children to be sorted respecting comparison function @c compare() */ +}; + +/** + * @brief Compares children using @a iface's comparing function. + * + * @param[in] iface The interface used to compare the properties. + * @param[in] a The first model whose properties will be compared. + * @param[in] b The second model whose properties will be compared. + * @param[out] cmp A pointer to an integer which will contain the result of the + * comparison. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @warning If either model doesn't implement @a iface will do nothing and + * return EINA_FALSE. + * + * @see eina_model_compare() + * @since 1.2 + */ +EAPI Eina_Bool eina_model_interface_children_compare(const Eina_Model_Interface *iface, + const Eina_Model *a, + const Eina_Model *b, + int *cmp) EINA_ARG_NONNULL(1, 2, 3, 4) EINA_WARN_UNUSED_RESULT; +/** + * @brief Loads children using @a iface's loading function. + * @param iface The children interface whose load method will be called. + * @param model The model instance. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @warning If either model doesn't implement @a iface will do nothing and + * return EINA_FALSE. + * + * @see eina_model_load() + * @since 1.2 + */ +EAPI Eina_Bool eina_model_interface_children_load(const Eina_Model_Interface *iface, + Eina_Model *model) EINA_ARG_NONNULL(1, 2); +/** + * @brief Unloads children using @a iface's unloading function. + * @param iface The children interface whose unload method will be called. + * @param model The model instance. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @warning If either model doesn't implement @a iface will do nothing and + * return EINA_FALSE. + * + * @see eina_model_unload() + * @since 1.2 + */ +EAPI Eina_Bool eina_model_interface_children_unload(const Eina_Model_Interface *iface, + Eina_Model *model) EINA_ARG_NONNULL(1, 2); +/** + * @brief Count children using @a iface's counting function. + * @param iface The children interface whose count method will be called. + * @param model The model instance. + * @return Number of children in @a model. + * + * @warning If either model doesn't implement @a iface will do nothing and + * return -1. + * + * @see eina_model_child_count() + * @since 1.2 + */ +EAPI int eina_model_interface_children_count(const Eina_Model_Interface *iface, + const Eina_Model *model) EINA_ARG_NONNULL(1, 2); +/** + * @brief Get child using @a iface's function to get children. + * @param iface The children interface whose get method will be called. + * @param model The model instance. + * @param position Position of child to be retrieved. + * @return The requested child. + * + * @warning If either model doesn't implement @a iface will do nothing and + * return -1. + * + * @see eina_model_child_get() + * @since 1.2 + */ +EAPI Eina_Model *eina_model_interface_children_get(const Eina_Model_Interface *iface, + const Eina_Model *model, + unsigned int position) EINA_ARG_NONNULL(1, 2); +/** + * @brief Set child using @a iface's function to set children. + * @param iface The children interface whose set method will be called. + * @param model The model instance. + * @param position Position of child to be set. + * @param child Value(child) to be set. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @warning If either model doesn't implement @a iface will do nothing and + * return -1. + * + * @see eina_model_child_set() + * @since 1.2 + */ +EAPI Eina_Bool eina_model_interface_children_set(const Eina_Model_Interface *iface, + Eina_Model *model, + unsigned int position, + Eina_Model *child) EINA_ARG_NONNULL(1, 2, 4); +/** + * @brief Delete child using @a iface's function to delete children. + * @param iface The children interface whose delete method will be called. + * @param model The model instance. + * @param position Position of child to be deleted. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @warning If either model doesn't implement @a iface will do nothing and + * return -1. + * + * @see eina_model_child_del() + * @since 1.2 + */ +EAPI Eina_Bool eina_model_interface_children_del(const Eina_Model_Interface *iface, + Eina_Model *model, + unsigned int position) EINA_ARG_NONNULL(1, 2); +/** + * @brief Insert child using @a iface's function to insert children. + * @param iface The children interface whose insert method will be called. + * @param model The model instance. + * @param position Position in which to insert @a child. + * @param child Value(child) to be inserted. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @warning If either model doesn't implement @a iface will do nothing and + * return -1. + * + * @see eina_model_child_insert_at() + * @since 1.2 + */ +EAPI Eina_Bool eina_model_interface_children_insert_at(const Eina_Model_Interface *iface, + Eina_Model *model, + unsigned int position, + Eina_Model *child) EINA_ARG_NONNULL(1, 2, 4); +/** + * @brief Sort children using @a iface's function to sort children. + * @param iface The children interface whose sort method will be called. + * @param model The model instance. + * @param compare Function used to compare children. + * + * @warning If either model doesn't implement @a iface will do nothing and + * return -1. + * + * @see eina_model_child_sort(). + * @since 1.2 + */ +EAPI void eina_model_interface_children_sort(const Eina_Model_Interface *iface, + Eina_Model *model, + Eina_Compare_Cb compare) EINA_ARG_NONNULL(1, 2, 3); + + +/** + * @} + */ + +/** + * @defgroup Eina_Model_Utils_Group Data Model Utilities + * + * Miscellaneous utilities to help usage or debug of @ref Eina_Model_Group. + * + * @{ + */ + +/** + * @brief Checks if @a model is an instance of @a type. + * @param model The model instance. + * @param type The type being checked for. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @see eina_model_new() + * @see _Eina_Model_Type + * @since 1.2 + */ +EAPI Eina_Bool eina_model_instance_check(const Eina_Model *model, + const Eina_Model_Type *type) EINA_ARG_NONNULL(1, 2) EINA_WARN_UNUSED_RESULT EINA_PURE; + +/** + * @brief Checks if @a model implements @a iface. + * @param model The model instance. + * @param iface The interface being checked for. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * @see _Eina_Model_Interface + * @since 1.2 + */ +EAPI Eina_Bool eina_model_interface_implemented(const Eina_Model *model, const Eina_Model_Interface *iface) EINA_ARG_NONNULL(1, 2) EINA_WARN_UNUSED_RESULT EINA_PURE; + +/** + * @brief Returns the number of references to @a model. + * @param model The model to query number of references. + * @return Number of references to model. + * + * @see eina_model_ref() + * @see eina_model_unref() + * @see eina_model_xref() + * @see eina_model_xunref() + * @see eina_model_xrefs_get() + * @since 1.2 + */ +EAPI int eina_model_refcount(const Eina_Model *model) EINA_ARG_NONNULL(1); + +/** + * @typedef Eina_Model_XRef + * Extended reference to model. + * + * This is returned by eina_model_xrefs_get() and should never be + * modified. It is managed by eina_model_xref() and + * eina_model_xunref() when @c EINA_MODEL_DEBUG is set to "1" or + * "backtrace". + * + * @see #_Eina_Model_XRef explains fields. + * @since 1.2 + */ +typedef struct _Eina_Model_XRef Eina_Model_XRef; + +/** + * @struct _Eina_Model_XRef + * Extended reference to model. + * + * This is returned by eina_model_xrefs_get() and should never be + * modified. It is managed by eina_model_xref() and + * eina_model_xunref() when @c EINA_MODEL_DEBUG is set to "1" or + * "backtrace". + * + * @see eina_model_xrefs_get() + * @see eina_models_usage_dump() + * @since 1.2 + */ +struct _Eina_Model_XRef +{ + EINA_INLIST; + const void *id; /**< as given to eina_model_xref() */ + struct { + const void * const *symbols; /**< only if @c EINA_MODEL_DEBUG=backtrace is set, otherwise is @c NULL */ + unsigned int count; /**< only if @c EINA_MODEL_DEBUG=backtrace is set, otherwise is 0 */ + } backtrace; + char label[]; /**< Any given label given to eina_model_xref(). */ +}; + +/** + * @brief Returns the current references of this model. + * @param model The model to query references. + * @return List of reference holders as Eina_Model_XRef. This is the internal + * list for speed purposes, do not modify or free it in anyway! + * + * @note This list only exist if environment variable + * @c EINA_MODEL_DEBUG is set to "1" or "backtrace". + * + * @note The backtrace information is only available if environment + * variable @c EINA_MODEL_DEBUG=backtrace is set. + * @since 1.2 + */ +EAPI const Eina_Inlist *eina_model_xrefs_get(const Eina_Model *model) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT EINA_MALLOC; + +/** + * @brief Dump usage of all existing modules. + * @since 1.2 + */ +EAPI void eina_models_usage_dump(void); + +/** + * @brief Return a list of all live models. + * @return a newly allocated list of Eina_Model. Free using + * eina_models_list_free() + * + * @note this is meant to debug purposes, do not modify the models in + * any way! + * + * @note due performance reasons, this is only @b enabled when + * @c EINA_MODEL_DEBUG is set to "1" or "backtrace". + * + * @since 1.2 + */ +EAPI Eina_List *eina_models_list_get(void); + +/** + * @brief Release list returned by eina_models_list_get() + * @param list the list to release. + */ +EAPI void eina_models_list_free(Eina_List *list); + +/** + * @} + */ + +/** + * @var EINA_MODEL_INTERFACE_CHILDREN_INARRAY + * + * Implements #Eina_Model_Interface_Children + * (#EINA_MODEL_INTERFACE_NAME_CHILDREN) using #Eina_Inarray. It + * should be efficient in space and time for most operations. + * + * @note it may become slow if eina_model_child_insert_at() is used at(or near) + * the beginning of the array as the members from that position + * to the end must be memmove()d. + * + * @since 1.2 + */ +EAPI extern const Eina_Model_Interface *EINA_MODEL_INTERFACE_CHILDREN_INARRAY; + + +/** + * @var EINA_MODEL_TYPE_BASE + * Base type for all eina model types. + * + * @since 1.2 + */ +EAPI extern const Eina_Model_Type *EINA_MODEL_TYPE_BASE; + +/** + * @var EINA_MODEL_TYPE_MIXIN + * + * Type that uses #EINA_MODEL_INTERFACE_NAME_PROPERTIES and + * #EINA_MODEL_INTERFACE_NAME_CHILDREN to manage the model. + * + * This is an abstract type, it does not work out of the box as one + * needs to subclass it and define the interface implementations for + * properties and children, as done by #EINA_MODEL_TYPE_GENERIC + * + * @see EINA_MODEL_TYPE_GENERIC + * + * @since 1.2 + */ +EAPI extern const Eina_Model_Type *EINA_MODEL_TYPE_MIXIN; + +/** + * @var EINA_MODEL_TYPE_GENERIC + * + * Subclass of #EINA_MODEL_TYPE_MIXIN that uses + * #EINA_MODEL_INTERFACE_PROPERTIES_HASH and + * #EINA_MODEL_INTERFACE_CHILDREN_INARRAY. + * + * Should be generic enough to hold lots of items with runtime + * configurable properties of any type. + * + * @see #EINA_MODEL_TYPE_STRUCT + * + * @since 1.2 + */ +EAPI extern const Eina_Model_Type *EINA_MODEL_TYPE_GENERIC; + +/** + * @var EINA_MODEL_TYPE_STRUCT + * + * Subclass of #EINA_MODEL_TYPE_MIXIN that uses + * #EINA_MODEL_INTERFACE_PROPERTIES_STRUCT and + * #EINA_MODEL_INTERFACE_CHILDREN_INARRAY. + * + * Should be struct enough to hold lots of items with compile time + * configurable properties of any type. + * + * @see #EINA_MODEL_TYPE_GENERIC + * + * @since 1.2 + */ +EAPI extern const Eina_Model_Type *EINA_MODEL_TYPE_STRUCT; + +/** + * @brief Create and setup an instance of #EINA_MODEL_TYPE_STRUCT. + * @param desc struct description to use for properties. + * @return newly created and set model, or @c NULL on errors. + * + * @see eina_model_type_struct_new() + * @since 1.2 + */ +EAPI Eina_Model *eina_model_struct_new(const Eina_Value_Struct_Desc *desc) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT EINA_MALLOC; + +/** + * @brief Create and setup an instance of type subclass of #EINA_MODEL_TYPE_STRUCT. + * @param type a type which is subclass of #EINA_MODEL_TYPE_STRUCT. + * @param desc struct description to use for properties. + * @return newly created and set model, or @c NULL on errors. + * + * @see eina_model_struct_new() + * @since 1.2 + */ +EAPI Eina_Model *eina_model_type_struct_new(const Eina_Model_Type *type, + const Eina_Value_Struct_Desc *desc) EINA_ARG_NONNULL(1, 2) EINA_WARN_UNUSED_RESULT EINA_MALLOC; + + +/** + * @brief Configure the internal properties of model implementing #EINA_MODEL_INTERFACE_PROPERTIES_STRUCT. + * @param model The model instance to configure. + * @param desc The structure description to use. + * @param memory If not @c NULL, will be copied by model. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * This will setup the internal pointers so that the given @a desc is + * used to manage the properties of this struct. + * + * If a given memory is provided, it will be copied (including + * members) and no references are taken after this function returns. + * + * @see #EINA_VALUE_TYPE_STRUCT + * + * @since 1.2 + */ +EAPI Eina_Bool eina_model_struct_set(Eina_Model *model, + const Eina_Value_Struct_Desc *desc, + void *memory) EINA_ARG_NONNULL(1, 2); +/** + * @brief Get the internal properties of model implementing #EINA_MODEL_INTERFACE_PROPERTIES_STRUCT. + * @param model the model instance. + * @param p_desc where to return the structure description in use. + * @param p_memory where to return the structure memory in use. + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * No copies are made! The memory and description may be invalidaded + * by calls to eina_model_struct_set() or eina_model_del(). + * + * @since 1.2 + */ +EAPI Eina_Bool eina_model_struct_get(const Eina_Model *model, + const Eina_Value_Struct_Desc **p_desc, + void **p_memory) EINA_ARG_NONNULL(1, 2); + +/** + * @var EINA_MODEL_INTERFACE_NAME_PROPERTIES + * + * Interface that uses #Eina_Model_Interface_Properties as + * #Eina_Model_Interface and can manage the model properties. + * + * @since 1.2 + */ +EAPI extern const char *EINA_MODEL_INTERFACE_NAME_PROPERTIES; + +/** + * @var EINA_MODEL_INTERFACE_PROPERTIES_HASH + * + * Implements #Eina_Model_Interface_Properties + * (#EINA_MODEL_INTERFACE_NAME_PROPERTIES) using #Eina_Hash. + * + * @note This function is generic but uses too much space given the + * hash data type. For huge number of elements it's better to + * use custom implementation instead. + * + * @see EINA_MODEL_INTERFACE_PROPERTIES_STRUCT + * + * @since 1.2 + */ +EAPI extern const Eina_Model_Interface *EINA_MODEL_INTERFACE_PROPERTIES_HASH; + +/** + * @var EINA_MODEL_INTERFACE_PROPERTIES_STRUCT + * + * Implements #Eina_Model_Interface_Properties + * (#EINA_MODEL_INTERFACE_NAME_PROPERTIES) using #Eina_Value_Struct. + * + * The interface private data is #Eina_Value of type + * #EINA_VALUE_TYPE_STRUCT. Properties will be accessed using + * Eina_Value_Struct::desc information that can be set by types such + * as #EINA_MODEL_TYPE_STRUCT + * + * @see EINA_MODEL_INTERFACE_PROPERTIES_HASH + * + * @since 1.2 + */ +EAPI extern const Eina_Model_Interface *EINA_MODEL_INTERFACE_PROPERTIES_STRUCT; + +/** + * @var EINA_MODEL_INTERFACE_NAME_CHILDREN + * + * Interface that uses #Eina_Model_Interface_Children as + * #Eina_Model_Interface and can manage the model children. + * + * @since 1.2 + */ +EAPI extern const char *EINA_MODEL_INTERFACE_NAME_CHILDREN; + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ +#endif diff --git a/libraries/eina/src/lib/eina_model.c b/libraries/eina/src/lib/eina_model.c new file mode 100644 index 0000000..ae06c1b --- /dev/null +++ b/libraries/eina/src/lib/eina_model.c @@ -0,0 +1,5548 @@ +/* EINA - EFL data type library + * Copyright (C) 2012 ProFUSION embedded systems + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; + * if not, see . + */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_ALLOCA_H +# include +#elif defined __GNUC__ +# define alloca __builtin_alloca +#elif defined _AIX +# define alloca __alloca +#elif defined _MSC_VER +# include +# define alloca _alloca +#else +# include +# ifdef __cplusplus +extern "C" +# endif +void *alloca (size_t); +#endif + +#ifdef HAVE_EXECINFO_H +#include +#endif + +#include "eina_config.h" +#include "eina_private.h" +#include "eina_error.h" +#include "eina_log.h" +#include "eina_mempool.h" +#include "eina_lock.h" +#include "eina_inlist.h" +#include "eina_strbuf.h" + +/* undefs EINA_ARG_NONULL() so NULL checks are not compiled out! */ +#include "eina_safety_checks.h" +#include "eina_value.h" /* eina-safety used in inline.x */ +#include "eina_model.h" + +/*============================================================================* + * Local * + *============================================================================*/ + +/** + * @cond LOCAL + */ + +static Eina_Mempool *_eina_model_mp = NULL; +static Eina_Hash *_eina_model_inner_mps = NULL; +static Eina_Lock _eina_model_inner_mps_lock; +static char *_eina_model_mp_choice = NULL; +static Eina_Hash *_eina_model_descriptions = NULL; +static Eina_Lock _eina_model_descriptions_lock; +static int _eina_model_log_dom = -1; +static enum { + EINA_MODEL_DEBUG_NONE = 0, + EINA_MODEL_DEBUG_CHECK = 1, + EINA_MODEL_DEBUG_BACKTRACE = 2, +} _eina_model_debug = EINA_MODEL_DEBUG_NONE; +static Eina_Lock _eina_model_debug_list_lock; +static Eina_List *_eina_model_debug_list = NULL; + +static const char _eina_model_str_deleted[] = "deleted"; +static const char _eina_model_str_freed[] = "freed"; +static const char _eina_model_str_property_set[] = "property,set"; +static const char _eina_model_str_property_del[] = "property,deleted"; +static const char _eina_model_str_children_changed[] = "children,changed"; +static const char _eina_model_str_child_inserted[] = "child,inserted"; +static const char _eina_model_str_child_set[] = "child,set"; +static const char _eina_model_str_child_del[] = "child,deleted"; +static const char _eina_model_str_loaded[] = "loaded"; +static const char _eina_model_str_unloaded[] = "unloaded"; +static const char _eina_model_str_properties_loaded[] = "properties,loaded"; +static const char _eina_model_str_properties_unloaded[] = "properties,unloaded"; +static const char _eina_model_str_children_loaded[] = "children,loaded"; +static const char _eina_model_str_children_unloaded[] = "children,unloaded"; + +#ifdef CRITICAL +#undef CRITICAL +#endif +#define CRITICAL(...) EINA_LOG_DOM_CRIT(_eina_model_log_dom, __VA_ARGS__) + +#ifdef ERR +#undef ERR +#endif +#define ERR(...) EINA_LOG_DOM_ERR(_eina_model_log_dom, __VA_ARGS__) + +#ifdef WRN +#undef WRN +#endif +#define WRN(...) EINA_LOG_DOM_WARN(_eina_model_log_dom, __VA_ARGS__) + +#ifdef INF +#undef INF +#endif +#define INF(...) EINA_LOG_DOM_INFO(_eina_model_log_dom, __VA_ARGS__) + +#ifdef DBG +#undef DBG +#endif +#define DBG(...) EINA_LOG_DOM_DBG(_eina_model_log_dom, __VA_ARGS__) + + +/* convenience sort array of Eina_Model* giving compare Eina_Model* instead of + * Eina_Model** + */ +static unsigned int +_eina_model_array_partition(Eina_Model **array, unsigned int start, unsigned int last, unsigned int pivot, Eina_Compare_Cb compare) +{ + Eina_Model **itr, **itr_end, *tmp, *pivot_value; + + pivot_value = tmp = array[pivot]; + array[pivot] = array[last]; + array[last] = tmp; + + pivot = start; + itr = array + start; + itr_end = array + last; + for (; itr < itr_end; itr++) + { + if (compare(*itr, pivot_value) < 0) + { + tmp = *itr; + *itr = array[pivot]; + array[pivot] = tmp; + pivot++; + } + } + + tmp = array[last]; + array[last] = array[pivot]; + array[pivot] = tmp; + + return pivot; +} + +static void +_eina_model_array_sort(Eina_Model **array, unsigned int start, unsigned int last, Eina_Compare_Cb compare) +{ + unsigned int pivot, new_pivot; + + if (last <= start) + return; + + pivot = start + (last - start) / 2; /* avoid overflow */ + new_pivot = _eina_model_array_partition(array, start, last, pivot, compare); + + if (start + 1 < new_pivot) + _eina_model_array_sort(array, start, new_pivot - 1, compare); + + if (new_pivot + 1 < last) + _eina_model_array_sort(array, new_pivot + 1, last, compare); +} + +/* + * Most of inner allocations are made with internal mempools, types + * and thus instace private data will repeat and it's good to use them. + * + * To save on the number of mempools, they are kept per size, not per + * type. + * + * This is done by means of _eina_model_inner_alloc() and + * _eina_model_inner_free(), both at thread safe. + * + */ +typedef struct _Eina_Model_Inner_Mp Eina_Model_Inner_Mp; +struct _Eina_Model_Inner_Mp +{ + Eina_Mempool *mempool; + int refcount; +}; + +static inline void +_eina_model_inner_mp_dispose(int size, Eina_Model_Inner_Mp *imp) +{ + EINA_SAFETY_ON_FALSE_RETURN(imp->refcount == 0); + + eina_hash_del_by_key(_eina_model_inner_mps, &size); + eina_mempool_del(imp->mempool); + free(imp); +} + +static inline Eina_Model_Inner_Mp * +_eina_model_inner_mp_get(int size) +{ + Eina_Model_Inner_Mp *imp = eina_hash_find(_eina_model_inner_mps, &size); + if (imp) return imp; + + imp = malloc(sizeof(Eina_Model_Inner_Mp)); + if (!imp) + return NULL; + + imp->refcount = 0; + + imp->mempool = eina_mempool_add(_eina_model_mp_choice, + "Eina_Model_Inner_Mp", NULL, size, 128); + if (!imp->mempool) + { + free(imp); + return NULL; + } + + if (!eina_hash_add(_eina_model_inner_mps, &size, imp)) + { + eina_mempool_del(imp->mempool); + free(imp); + return NULL; + } + + return imp; +} + +static inline void * +_eina_model_inner_alloc_internal(int size) +{ + Eina_Model_Inner_Mp *imp; + void *mem; + + imp = _eina_model_inner_mp_get(size); + if (!imp) return NULL; + + mem = eina_mempool_malloc(imp->mempool, size); + if (mem) imp->refcount++; + else if (imp->refcount == 0) _eina_model_inner_mp_dispose(size, imp); + + return mem; +} + +static inline void +_eina_model_inner_free_internal(int size, void *mem) +{ + Eina_Model_Inner_Mp *imp = eina_hash_find(_eina_model_inner_mps, &size); + EINA_SAFETY_ON_NULL_RETURN(imp); + + eina_mempool_free(imp->mempool, mem); + + imp->refcount--; + if (imp->refcount > 0) return; + _eina_model_inner_mp_dispose(size, imp); +} + +static void * +_eina_model_inner_alloc(size_t size) +{ + void *mem; + + if (size > 512) return malloc(size); + + eina_lock_take(&_eina_model_inner_mps_lock); + mem = _eina_model_inner_alloc_internal(size); + eina_lock_release(&_eina_model_inner_mps_lock); + + return mem; +} + +static void +_eina_model_inner_free(size_t size, void *mem) +{ + if (size > 512) + { + free(mem); + return; + } + + eina_lock_take(&_eina_model_inner_mps_lock); + _eina_model_inner_free_internal(size, mem); + eina_lock_release(&_eina_model_inner_mps_lock); +} + + +typedef union _Eina_Model_Provider Eina_Model_Provider; +union _Eina_Model_Provider +{ + const Eina_Model_Type *type; + const Eina_Model_Interface *iface; +}; + +/* store event name to aid searching */ +typedef struct _Eina_Model_Event_Description_Cache Eina_Model_Event_Description_Cache; +struct _Eina_Model_Event_Description_Cache +{ + const char *name; + const Eina_Model_Event_Description *desc; + Eina_Model_Provider provider; +}; + +/* description is an optimized structure for type. It's built at runtime + * to avoid user input errors and help declaration. + * + * lookups (ifaces, events) are sorted for binary search. + * + * recursion is avoided by expansion of every possible value in "cache" + * struct. + * + * the first usable operation is stopred for type at "ops" struct, + * avoiding usage of _eina_model_type_find_offset(). + * + * Get a model type description using _eina_model_description_get(), + * when it's not used anymore use + * _eina_model_description_dispose(). These operations are thread + * safe. + */ +typedef struct _Eina_Model_Description Eina_Model_Description; +struct _Eina_Model_Description +{ + struct { + const Eina_Model_Type **types; /* size = total.types */ + const Eina_Model_Interface **ifaces; /* sorted, size = total.ifaces */ + Eina_Model_Provider *privates; /* size = total.privates (types + ifaces) */ + Eina_Model_Event_Description_Cache *events; /* size = total.events */ + } cache; + struct { + /* ops are the topmost operation to use for type/interface */ + struct { + Eina_Bool (*setup)(Eina_Model *model); + Eina_Bool (*flush)(Eina_Model *model); + Eina_Bool (*constructor)(Eina_Model *model); + Eina_Bool (*destructor)(Eina_Model *model); + Eina_Bool (*copy)(const Eina_Model *src, Eina_Model *dst); + Eina_Bool (*deep_copy)(const Eina_Model *src, Eina_Model *dst); + Eina_Bool (*compare)(const Eina_Model *a, const Eina_Model *b, int *cmp); + Eina_Bool (*load)(Eina_Model *model); + Eina_Bool (*unload)(Eina_Model *model); + Eina_Bool (*property_get)(const Eina_Model *model, const char *name, Eina_Value *value); + Eina_Bool (*property_set)(Eina_Model *model, const char *name, const Eina_Value *value); + Eina_Bool (*property_del)(Eina_Model *model, const char *name); + Eina_List *(*properties_names_list_get)(const Eina_Model *model); + int (*child_count)(const Eina_Model *model); + Eina_Model *(*child_get)(const Eina_Model *model, unsigned int position); + Eina_Bool (*child_set)(Eina_Model *model, unsigned int position, Eina_Model *child); + Eina_Bool (*child_del)(Eina_Model *model, unsigned int position); + Eina_Bool (*child_insert_at)(Eina_Model *model, unsigned int position, Eina_Model *child); + int (*child_find)(const Eina_Model *model, unsigned int start_position, const Eina_Model *other); + int (*child_criteria_match)(const Eina_Model *model, unsigned int start_position, Eina_Each_Cb match, const void *data); + void (*child_sort)(Eina_Model *model, Eina_Compare_Cb compare); + Eina_Iterator *(*child_iterator_get)(Eina_Model *model, unsigned int start, unsigned int count); + Eina_Iterator *(*child_reversed_iterator_get)(Eina_Model *model, unsigned int start, unsigned int count); + Eina_Iterator *(*child_sorted_iterator_get)(Eina_Model *model, unsigned int start, unsigned int count, Eina_Compare_Cb compare); + Eina_Iterator *(*child_filtered_iterator_get)(Eina_Model *model, unsigned int start, unsigned int count, Eina_Each_Cb match, const void *data); + char *(*to_string)(const Eina_Model *model); /**< used to represent model as string, usually for debug purposes or user convenience */ + const void **extension; + } type; + } ops; + struct { + unsigned int types; + unsigned int ifaces; + unsigned int privates; + unsigned int size; /* sum of all private sizes */ + unsigned int events; + } total; + int refcount; +}; + +static Eina_Bool +_eina_model_description_type_fill(Eina_Model_Description *desc, const Eina_Model_Type *type) +{ + const Eina_Model_Type *itr, *last_itr = NULL; + unsigned int count, child_size = 0; + + for (count = 0, itr = type; itr != NULL; itr = itr->parent, count++) + { + if (itr->version != EINA_MODEL_TYPE_VERSION) + { + CRITICAL("Type %p version is %u, expected %u instead.", + itr, itr->version, EINA_MODEL_TYPE_VERSION); + return EINA_FALSE; + } + if (!itr->name) + { + CRITICAL("Type %p provides no name!", itr); + return EINA_FALSE; + } + if (itr->type_size < sizeof(Eina_Model_Type)) + { + CRITICAL("Type %p %s size must be >= sizeof(Eina_Model_Type)!", + itr, itr->name); + return EINA_FALSE; + } + if (child_size == 0) child_size = itr->type_size; + else if (child_size < itr->type_size) + { + CRITICAL("Type %p %s size is bigger than its child type %p %s!", + itr, itr->name, last_itr, last_itr->name); + return EINA_FALSE; + } + last_itr = itr; + +#define DEF_METH(meth) \ + if (!desc->ops.type.meth) desc->ops.type.meth = itr->meth + DEF_METH(setup); + DEF_METH(flush); + DEF_METH(constructor); + DEF_METH(destructor); + DEF_METH(copy); + DEF_METH(deep_copy); + DEF_METH(compare); + DEF_METH(load); + DEF_METH(unload); + DEF_METH(property_get); + DEF_METH(property_set); + DEF_METH(property_del); + DEF_METH(properties_names_list_get); + DEF_METH(child_count); + DEF_METH(child_get); + DEF_METH(child_set); + DEF_METH(child_del); + DEF_METH(child_insert_at); + DEF_METH(child_find); + DEF_METH(child_criteria_match); + DEF_METH(child_sort); + DEF_METH(child_iterator_get); + DEF_METH(child_reversed_iterator_get); + DEF_METH(child_sorted_iterator_get); + DEF_METH(child_filtered_iterator_get); + DEF_METH(to_string); +#undef DEF_METH + + if ((!itr->parent) && (itr != EINA_MODEL_TYPE_BASE)) + { + CRITICAL("Type %p (%s) does not inherit from EINA_MODEL_TYPE_BASE!", + type, type->name); + return EINA_FALSE; + } + } + +#define CK_METH(meth) \ + if (!desc->ops.type.meth) \ + { \ + CRITICAL("Mandatory method "#meth \ + "() was not provided by type %p (%s).", \ + type, type->name); \ + return EINA_FALSE; \ + } + CK_METH(setup); + CK_METH(flush); + CK_METH(constructor); + CK_METH(destructor); + CK_METH(property_get); +#undef CK_METH + + if (child_size <= sizeof(Eina_Model_Type)) + desc->ops.type.extension = NULL; + else + { + unsigned ext_size = child_size - sizeof(Eina_Model_Type); + unsigned ext_count = ext_size / sizeof(void *); + + if (ext_size % sizeof(void *) != 0) + { + CRITICAL("Extension size %u is not multiple of sizeof(void*)", + ext_size); + return EINA_FALSE; + } + + desc->ops.type.extension = calloc(ext_count, sizeof(void *)); + EINA_SAFETY_ON_NULL_RETURN_VAL(desc->ops.type.extension, EINA_FALSE); + + for (itr = type; itr != NULL; itr = itr->parent) + { + unsigned cur_size = itr->type_size - sizeof(Eina_Model_Type); + unsigned i, cur_count = cur_size / sizeof(void *); + const void * const *ptr = (const void **)((const char *)itr + sizeof(Eina_Model_Type)); + + if (cur_size == 0) break; + + for (i = 0; i < cur_count; i++) + { + if (desc->ops.type.extension[i]) continue; + desc->ops.type.extension[i] = ptr[i]; + } + } + } + + desc->cache.types = malloc(count * sizeof(Eina_Model_Type *)); + EINA_SAFETY_ON_NULL_GOTO(desc->cache.types, cache_types_failed); + desc->total.types = count; + + for (count = 0, itr = type; itr != NULL; itr = itr->parent, count++) + desc->cache.types[count] = itr; + + return EINA_TRUE; + + cache_types_failed: + free(desc->ops.type.extension); + return EINA_FALSE; +} + +static inline Eina_Bool +_eina_model_interface_implements(const Eina_Model_Interface *iface, const Eina_Model_Interface *query) +{ + const Eina_Model_Interface **itr; + + if (iface == query) + return EINA_TRUE; + + if (!iface->interfaces) + return EINA_FALSE; + + for (itr = iface->interfaces; *itr != NULL; itr++) + if (_eina_model_interface_implements(*itr, query)) + return EINA_TRUE; + + return EINA_FALSE; +} + +/* apply topological sort and remove duplicates */ +/* + * TODO: Topological sort will only work for linked interfaces, but + * will ignore original ordering provided by types. Consider the + * following: + * + * - A_Type -> X_Iface (name: "MyIface") + * - B_Type -> Y_Iface (name: "MyIface") + * + * Both X_Iface and Y_Iface are different implementations of the + * "MyIface". + * + * B_Type inherits from A_Type, then Y_Iface must be looked up + * first, even though there is no link between Y_Iface and + * X_Iface. + * + * However, the way the current topological sort behaves, the + * roots may come out in any order. We need a stable version + * that sorts roots before removing them from graph. + * + * Thanks to Tasn to report it :-) + */ +static Eina_Bool +_eina_model_description_ifaces_fix(Eina_Model_Description *desc) +{ + struct node { + const Eina_Model_Interface *iface; + unsigned int users; + Eina_List *deps; + } *nodes, **pending, **roots; + unsigned int n_nodes = desc->total.ifaces, n_pending = 0, n_roots = 0, i, j; + Eina_Bool ret = EINA_TRUE; + + nodes = alloca(n_nodes * sizeof(struct node)); + pending = alloca(n_nodes * sizeof(struct node *)); + roots = alloca(n_nodes * sizeof(struct node *)); + + /* populate */ + for (i = 0, j = 0; i < n_nodes; i++) + { + unsigned int k; + for (k = 0; k < j; k++) + { + if (nodes[k].iface == desc->cache.ifaces[i]) + break; + } + if (k < j) + continue; /* already exists */ + + nodes[j].iface = desc->cache.ifaces[i]; + nodes[j].users = 0; + nodes[j].deps = NULL; + j++; + } + n_nodes = j; + + for (i = 0; i < n_nodes; i++) + { + for (j = 0; j < n_nodes; j++) + { + if (i == j) continue; + if (!_eina_model_interface_implements(nodes[j].iface, + nodes[i].iface)) + continue; + + nodes[i].users++; + nodes[j].deps = eina_list_append(nodes[j].deps, nodes + i); + } + } + for (i = 0; i < n_nodes; i++) + { + if (nodes[i].users == 0) + { + roots[n_roots] = nodes + i; + n_roots++; + } + else + { + pending[n_pending] = nodes + i; + n_pending++; + } + } + + /* topological sort */ + desc->total.ifaces = 0; + while (n_roots > 0) + { + struct node *r, *d; + + /* TODO: sort roots using input order? Or at least study if + * it's enough to change roots append to prepend. + * + * See comments above. + */ + n_roots--; + r = roots[n_roots]; + + desc->cache.ifaces[desc->total.ifaces] = r->iface; + desc->total.ifaces++; + + EINA_LIST_FREE(r->deps, d) + { + d->users--; + if (d->users > 0) continue; + + roots[n_roots] = d; + n_roots++; + + /* remove node, it became a root */ + for (j = 0; j < n_pending; j++) + { + if (pending[j] == d) + { + n_pending--; + if (j < n_pending) + pending[j] = pending[n_pending]; + break; + } + } + } + } + + if (n_pending > 0) + { + ERR("Dependency loop found for interfaces!"); + for (i = 0; i < n_pending; i++) + ERR("%p (%s) is part of dependency loop!", + pending[i]->iface, pending[i]->iface->name); + CRITICAL("Cannot use type %p (%s) with broken interfaces!", + desc->cache.types[0], desc->cache.types[0]->name); + free(desc->cache.ifaces); + ret = EINA_FALSE; + } + + /* likely from still pending (dependency loops) */ + for (i = 0; i < n_nodes; i++) + eina_list_free(nodes[i].deps); + + return ret; +} + +static Eina_Bool +_eina_model_description_ifaces_validate_and_count(const Eina_Model_Interface *iface, unsigned int *count) +{ + if (iface->version != EINA_MODEL_INTERFACE_VERSION) + { + CRITICAL("Interface %p version is %u, expected %u instead.", + iface, iface->version, EINA_MODEL_INTERFACE_VERSION); + return EINA_FALSE; + } + + if (!iface->name) + { + CRITICAL("Interface %p provides no name!", iface); + return EINA_FALSE; + } + + if (iface->interfaces) + { + const Eina_Model_Interface **itr = iface->interfaces; + for (; *itr != NULL; itr++) + if (!_eina_model_description_ifaces_validate_and_count(*itr, count)) + return EINA_FALSE; + } + + (*count)++; + return EINA_TRUE; +} + +static void +_eina_model_description_ifaces_populate(Eina_Model_Description *desc, const Eina_Model_Interface *iface) +{ + desc->cache.ifaces[desc->total.ifaces] = iface; + desc->total.ifaces++; + + if (iface->interfaces) + { + const Eina_Model_Interface **itr = iface->interfaces; + for (; *itr != NULL; itr++) + _eina_model_description_ifaces_populate(desc, *itr); + } +} + +static Eina_Bool +_eina_model_description_ifaces_fill(Eina_Model_Description *desc) +{ + const Eina_Model_Type **titr, **titr_end; + unsigned int count; + + titr = desc->cache.types; + titr_end = titr + desc->total.types; + + /* naively count all interfaces, remove duplicates later */ + for (count = 0; titr < titr_end; titr++) + { + const Eina_Model_Type *type = *titr; + const Eina_Model_Interface **iitr = type->interfaces; + if (!type->interfaces) continue; + + for (; *iitr != NULL; iitr++) + if (!_eina_model_description_ifaces_validate_and_count(*iitr, &count)) + return EINA_FALSE; + } + if (count == 0) + { + desc->cache.ifaces = NULL; + desc->total.ifaces = 0; + return EINA_TRUE; + } + + desc->cache.ifaces = malloc(count * sizeof(Eina_Model_Interface *)); + EINA_SAFETY_ON_NULL_RETURN_VAL(desc->cache.ifaces, EINA_FALSE); + + titr = desc->cache.types; + desc->total.ifaces = 0; + for (; titr < titr_end; titr++) + { + const Eina_Model_Type *type = *titr; + const Eina_Model_Interface **iitr = type->interfaces; + + if (!type->interfaces) continue; + + for (; *iitr != NULL; iitr++) + _eina_model_description_ifaces_populate(desc, *iitr); + } + + return _eina_model_description_ifaces_fix(desc); +} + +static Eina_Bool +_eina_model_description_privates_fill(Eina_Model_Description *desc) +{ + unsigned int i; + + desc->total.privates = desc->total.types + desc->total.ifaces; + desc->cache.privates = malloc(desc->total.privates * + sizeof(Eina_Model_Provider)); + EINA_SAFETY_ON_NULL_RETURN_VAL(desc->cache.privates, EINA_FALSE); + + desc->total.size = 0; + + for (i = 0; i < desc->total.types; i++) + { + const Eina_Model_Type *type = desc->cache.types[i]; + desc->cache.privates[i].type = type; + if (type->private_size > 0) + { + unsigned int size = type->private_size; + if (size % sizeof(void *) != 0) + size += sizeof(void *) - (size % sizeof(void *)); + desc->total.size += size; + } + } + + for (i = 0; i < desc->total.ifaces; i++) + { + const Eina_Model_Interface *iface = desc->cache.ifaces[i]; + desc->cache.privates[desc->total.types + i].iface = iface; + if (iface->private_size > 0) + { + unsigned int size = iface->private_size; + if (size % sizeof(void *) != 0) + size += sizeof(void *) - (size % sizeof(void *)); + desc->total.size += size; + } + } + + return EINA_TRUE; +} + +static int +_eina_model_description_events_cmp(const void *pa, const void *pb) +{ + const Eina_Model_Event_Description_Cache *a = pa, *b = pb; + return strcmp(a->name, b->name); +} + +static int +_eina_model_description_events_find(const Eina_Model_Description *desc, const Eina_Model_Event_Description *query) +{ + unsigned int i; + for (i = 0; i < desc->total.events; i++) + { + const Eina_Model_Event_Description_Cache *itr = desc->cache.events + i; + if ((itr->name == query->name) || (strcmp(itr->name, query->name) == 0)) + return i; + } + + return -1; +} + +/* warn and remove duplicates, sort items to speed up lookups */ +static Eina_Bool +_eina_model_description_events_fill(Eina_Model_Description *desc) +{ + unsigned int i, count = 0, type_events; + + for (i = 0; i < desc->total.types; i++) + { + const Eina_Model_Event_Description *itr = desc->cache.types[i]->events; + if (!itr) continue; + for (; itr->name != NULL; itr++) + { + count++; + } + } + type_events = count; + + for (i = 0; i < desc->total.ifaces; i++) + { + const Eina_Model_Event_Description *itr = desc->cache.ifaces[i]->events; + if (!itr) continue; + for (; itr->name != NULL; itr++) + count++; + } + + if (count == 0) + { + desc->cache.events = NULL; + desc->total.events = 0; + return EINA_TRUE; + } + + desc->cache.events = malloc(count * + sizeof(Eina_Model_Event_Description_Cache)); + EINA_SAFETY_ON_NULL_RETURN_VAL(desc->cache.events, EINA_FALSE); + desc->total.events = 0; + + for (i = 0; i < desc->total.types; i++) + { + const Eina_Model_Type *mtype = desc->cache.types[i]; + const Eina_Model_Event_Description *itr = mtype->events; + if (!itr) continue; + for (; itr->name != NULL; itr++) + { + int j = _eina_model_description_events_find(desc, itr); + if (j >= 0) + { + const Eina_Model_Event_Description_Cache *o = desc->cache.events + j; + const Eina_Model_Type *omtype = o->provider.type; + WRN("Ignored duplicated event '%s' (type: '%s') from " + "model type %p (%s): already exists with type '%s' " + "from model type %p (%s)", + itr->name, + itr->type ? itr->type : "", + mtype, mtype->name, + o->desc->type ? o->desc->type : "", + omtype, omtype->name); + continue; + } + + desc->cache.events[desc->total.events].name = itr->name; + desc->cache.events[desc->total.events].desc = itr; + desc->cache.events[desc->total.events].provider.type = mtype; + desc->total.events++; + } + } + + for (i = 0; i < desc->total.ifaces; i++) + { + const Eina_Model_Interface *miface = desc->cache.ifaces[i]; + const Eina_Model_Event_Description *itr = desc->cache.ifaces[i]->events; + if (!itr) continue; + for (; itr->name != NULL; itr++) + { + int j = _eina_model_description_events_find(desc, itr); + if (j >= 0) + { + const Eina_Model_Event_Description_Cache *o = desc->cache.events + j; + if ((unsigned)j < type_events) + { + const Eina_Model_Type *omtype = o->provider.type; + WRN("Ignored duplicated event '%s' (type: '%s') from " + "model interface %p (%s): already exists with " + "type '%s' from model interface %p (%s)", + itr->name, + itr->type ? itr->type : "", + miface, miface->name, + o->desc->type ? o->desc->type : "", + omtype, omtype->name); + } + else + { + const Eina_Model_Interface *omiface = o->provider.iface; + WRN("Ignored duplicated event '%s' (iface: '%s') from " + "model interface %p (%s): already exists with " + "interface '%s' from model interface %p (%s)", + itr->name, + itr->type ? itr->type : "", + miface, miface->name, + o->desc->type ? o->desc->type : "", + omiface, omiface->name); + } + continue; + } + + desc->cache.events[desc->total.events].name = itr->name; + desc->cache.events[desc->total.events].desc = itr; + desc->cache.events[desc->total.events].provider.iface = miface; + desc->total.events++; + } + } + + qsort(desc->cache.events, desc->total.events, + sizeof(Eina_Model_Event_Description_Cache), + _eina_model_description_events_cmp); + + return EINA_TRUE; +} + +static const Eina_Model_Description * +_eina_model_description_get_internal(const Eina_Model_Type *type) +{ + Eina_Model_Description *desc; + + desc = eina_hash_find(_eina_model_descriptions, &type); + if (desc) + { + desc->refcount++; + return desc; + } + + desc = calloc(1, sizeof(Eina_Model_Description)); + EINA_SAFETY_ON_NULL_RETURN_VAL(desc, NULL); + + if (!_eina_model_description_type_fill(desc, type)) goto failed_type; + if (!_eina_model_description_ifaces_fill(desc)) goto failed_ifaces; + if (!_eina_model_description_privates_fill(desc)) goto failed_privates; + if (!_eina_model_description_events_fill(desc)) goto failed_events; + if (!eina_hash_add(_eina_model_descriptions, &type, desc)) goto failed_hash; + + desc->refcount = 1; + return desc; + + failed_hash: + free(desc->cache.events); + failed_events: + free(desc->cache.privates); + failed_privates: + free(desc->cache.ifaces); + failed_ifaces: + free(desc->cache.types); + free(desc->ops.type.extension); + failed_type: + free(desc); + return NULL; +} + +static void +_eina_model_description_dispose_internal(Eina_Model_Description *desc) +{ + const Eina_Model_Type *type; + + EINA_SAFETY_ON_FALSE_RETURN(desc->refcount > 0); + desc->refcount--; + if (desc->refcount > 0) return; + + type = desc->cache.types[0]; + if (!eina_hash_del_by_key(_eina_model_descriptions, &type)) + ERR("Cannot find type %p (%s) in descriptions hash!", + type, type->name); + + INF("Disposed model description for type %p (%s)", type, type->name); + + free(desc->ops.type.extension); + free(desc->cache.types); + free(desc->cache.ifaces); + free(desc->cache.privates); + free(desc->cache.events); + free(desc); +} + +static const Eina_Model_Description * +_eina_model_description_get(const Eina_Model_Type *type) +{ + const Eina_Model_Description *desc; + + eina_lock_take(&_eina_model_descriptions_lock); + desc = _eina_model_description_get_internal(type); + eina_lock_release(&_eina_model_descriptions_lock); + + return desc; +} + +static void +_eina_model_description_dispose(const Eina_Model_Description *desc) +{ + eina_lock_take(&_eina_model_descriptions_lock); + _eina_model_description_dispose_internal((Eina_Model_Description *)desc); + eina_lock_release(&_eina_model_descriptions_lock); +} + +static inline int +_eina_model_description_event_id_find(const Eina_Model_Description *desc, const char *event_name) +{ + const Eina_Model_Event_Description_Cache *cache; + Eina_Model_Event_Description_Cache criteria_match; + + criteria_match.name = event_name; + cache = bsearch(&criteria_match, desc->cache.events, desc->total.events, + sizeof(Eina_Model_Event_Description_Cache), + _eina_model_description_events_cmp); + if (!cache) + { + ERR("No event named %s for type %p (%s)", event_name, + desc->cache.types[0], desc->cache.types[0]->name); + return -1; + } + + return cache - desc->cache.events; +} + +/* + * Model management and book keeping + */ +typedef struct _Eina_Model_Event_Listener Eina_Model_Event_Listener; +struct _Eina_Model_Event_Listener +{ + EINA_INLIST; + Eina_Model_Event_Cb cb; + const void *data; + Eina_Bool deleted:1; +}; + +struct _Eina_Model +{ + const Eina_Model_Description *desc; /**< optimized model description */ + struct { + Eina_Inlist **entries; /**< connected/listeners for each event, array of lists of Eina_Model_Event_Listener */ + Eina_List **deleted; /**< deleted listeners while was walking. array of lists of Eina_Model_Event_Listener with deleted flag */ + int *freeze; /**< freeze count for each event */ + int walking; /**< increased while walking entries lists */ + } listeners; + void **privates; /**< private data per type and interface, each level gets its own stuff */ + Eina_Inlist *xrefs; /**< if EINA_MODEL_DEBUG and eina_model_xref() is used */ + int refcount; /**< number of users of this model instance */ + Eina_Bool deleted:1; /**< if deleted but still have references */ + EINA_MAGIC +}; + +static inline Eina_Bool +_eina_model_type_check(const Eina_Model_Type *type) +{ + EINA_SAFETY_ON_NULL_RETURN_VAL(type, EINA_FALSE); + EINA_SAFETY_ON_FALSE_RETURN_VAL(type->version == EINA_MODEL_TYPE_VERSION, + EINA_FALSE); + return EINA_TRUE; +} + +/* find in type hierarchy the first one that the given offset is not a null + * pointer. Use this to discover which method to call on a parent. + */ +static const void * +_eina_model_type_find_offset(const Eina_Model_Type *type, unsigned int offset) +{ + const unsigned char *ptr = (const unsigned char *)type; + const void **addr = (const void **)(ptr + offset); + + if (*addr) return *addr; + if (!type->parent) return NULL; + return _eina_model_type_find_offset(type->parent, offset); +} + +/* find in interface hierarchy the first one that the given offset is + * not a null pointer. Use this to discover which method to call on a + * parent. + * + * TODO: Keep Eina_Model_Interface_Description with topological sorted + * entries for each interface? + * I smell problems with the current code in more complex + * situations (k-s) + * + * iface1 + * ^ + * | + * .---------+---------. + * | | | + * iface2 iface3 iface4 + * ^ ^ ^ + * | | | + * `---------+---------' + * | + * iface5 + * + * It should look: iface5 -> iface2 -> iface3 -> iface4 -> iface1 + * Now it does: iface5 -> iface2 -> iface1 -> iface3 -> iface1 -> iface4 -> iface1 + * + * + * iface1 + * ^ + * | + * iface2 + * ^ + * | + * .---------+---------. + * | | + * iface3 iface4 + * ^ ^ + * | | + * `---------+---------' + * | + * iface5 + * + * It should look: iface5 -> iface3 -> iface4 -> iface2 -> iface1 + * Now it does: iface5 -> iface3 -> iface2 -> iface1 -> iface4 -> iface2 -> iface1 + * + * + * iface1 iface2 + * ^ ^ + * | | + * `---------+---------' + * | + * iface3 + * + * It should look: iface3 -> iface1 -> iface2 + * Now it does: iface3 -> iface1 -> iface2 + * + * For the common case it should work, let's see. + */ +static const void * +_eina_model_interface_find_offset(const Eina_Model_Interface *iface, unsigned int offset) +{ + const Eina_Model_Interface **itr; + const unsigned char *ptr = (const unsigned char *)iface; + const void **addr = (const void **)(ptr + offset); + + if (offset + sizeof(void *) > iface->interface_size) return NULL; + + if (*addr) return *addr; + if (!iface->interfaces) return NULL; + + for (itr = iface->interfaces; *itr != NULL; itr++) + { + const void *r = _eina_model_interface_find_offset(*itr, offset); + if (r) + return r; + } + + return NULL; +} + +static void +_eina_model_event_callback_free_deleted(Eina_Model *model) +{ + unsigned int i; + + for (i = 0; i < model->desc->total.events; i++) + { + Eina_Model_Event_Listener *el; + EINA_LIST_FREE(model->listeners.deleted[i], el) + { + model->listeners.entries[i] = eina_inlist_remove + (model->listeners.entries[i], EINA_INLIST_GET(el)); + _eina_model_inner_free(sizeof(Eina_Model_Event_Listener), el); + } + } + + _eina_model_inner_free(model->desc->total.events * sizeof(Eina_List *), + model->listeners.deleted); + model->listeners.deleted = NULL; +} + +static inline Eina_Bool +_eina_model_event_callback_call(Eina_Model *model, const char *name, const void *event_info) +{ + Eina_Inlist *lst; + Eina_Model_Event_Listener *el; + const Eina_Model_Event_Description *ev_desc; + int event_id = _eina_model_description_event_id_find(model->desc, name); + + if (event_id < 0) return EINA_FALSE; + if (!model->listeners.entries) return EINA_TRUE; + + if ((model->listeners.freeze) && (model->listeners.freeze[event_id])) + { + DBG("Ignored event callback '%s' of model %p (%s): frozen", + name, model, model->desc->cache.types[0]->name); + return EINA_TRUE; + } + + lst = model->listeners.entries[event_id]; + if (!lst) return EINA_TRUE; + + ev_desc = model->desc->cache.events[event_id].desc; + + model->listeners.walking++; + EINA_INLIST_FOREACH(lst, el) + { + if (el->deleted) continue; + el->cb((void *)el->data, model, ev_desc, (void *)event_info); + } + model->listeners.walking--; + + if ((model->listeners.walking == 0) && (model->listeners.deleted)) + _eina_model_event_callback_free_deleted(model); + + return EINA_FALSE; +} + +static const char EINA_ERROR_MODEL_FAILED_STR[] = "Model check failed."; +static const char EINA_ERROR_MODEL_METHOD_MISSING_STR[] = "Model method is missing."; +static const char EINA_MAGIC_MODEL_STR[] = "Eina Model"; + +static void _eina_model_unref(Eina_Model *model); + +/** + * @endcond + */ + +/* EINA_MODEL_TYPE_BASE: base of all other types **********************/ + +static Eina_Bool +_eina_model_type_base_setup(Eina_Model *model) +{ + DBG("base setup of %p", model); + return EINA_TRUE; +} + +static Eina_Bool +_eina_model_type_base_flush(Eina_Model *model) +{ + DBG("base flush of %p", model); + return EINA_TRUE; +} + +static Eina_Bool +_eina_model_type_base_constructor(Eina_Model *model) +{ + DBG("base constructor of %p", model); + return EINA_TRUE; +} + +static Eina_Bool +_eina_model_type_base_destructor(Eina_Model *model) +{ + DBG("base destructor of %p", model); + return EINA_TRUE; +} + +static Eina_Bool +_eina_model_type_base_properties_copy(const Eina_Model *model, Eina_Model *copy) +{ + Eina_List *l, *props = eina_model_properties_names_list_get(model); + const char *name; + EINA_LIST_FOREACH(props, l, name) + { + Eina_Value tmp; + if (!eina_model_property_get(model, name, &tmp)) + { + ERR("Could not get property %s from model %p (%s)", + name, model, model->desc->cache.types[0]->name); + eina_model_properties_names_list_free(props); + return EINA_FALSE; + } + if (!eina_model_property_set(copy, name, &tmp)) + { + ERR("Could not set property %s on model %p (%s)", + name, copy, copy->desc->cache.types[0]->name); + eina_value_flush(&tmp); + eina_model_properties_names_list_free(props); + return EINA_FALSE; + } + eina_value_flush(&tmp); + } + eina_model_properties_names_list_free(props); + return EINA_TRUE; +} + +static Eina_Bool +_eina_model_type_base_children_copy(const Eina_Model *model, Eina_Model *copy) +{ + int i, count = eina_model_child_count(model); + + if (count < 0) + { + ERR("Could not get children count of model %p (%s)", + model, model->desc->cache.types[0]->name); + return EINA_FALSE; + } + + for (i = 0; i < count; i++) + { + Eina_Model *child = eina_model_child_get(model, i); + Eina_Bool ret; + + if (!child) + { + ERR("Could not get child #%d from model %p (%s)", + i, model, model->desc->cache.types[0]->name); + return EINA_FALSE; + } + + ret = eina_model_child_insert_at(copy, i, child); + _eina_model_unref(child); + + if (!ret) + { + ERR("Could not set child #%d on model %p (%s)", + i, copy, copy->desc->cache.types[0]->name); + return EINA_FALSE; + } + } + + return EINA_TRUE; +} + +static Eina_Bool +_eina_model_type_base_copy(const Eina_Model *model, Eina_Model *copy) +{ + DBG("base copy of %p to %p", model, copy); + + return _eina_model_type_base_properties_copy(model, copy) && + _eina_model_type_base_children_copy(model, copy); +} + +static Eina_Bool +_eina_model_type_base_children_deep_copy(const Eina_Model *model, Eina_Model *copy) +{ + int i, count = eina_model_child_count(model); + + if (count < 0) + { + ERR("Could not get children count of model %p (%s)", + model, model->desc->cache.types[0]->name); + return EINA_FALSE; + } + + for (i = 0; i < count; i++) + { + Eina_Model *child_copy, *child = eina_model_child_get(model, i); + Eina_Bool ret; + + if (!child) + { + ERR("Could not get child #%d from model %p (%s)", + i, model, model->desc->cache.types[0]->name); + return EINA_FALSE; + } + + child_copy = eina_model_deep_copy(child); + if (!child_copy) + { + ERR("Could not deep copy child #%d %p (%s) from model %p (%s)", i, + child, child->desc->cache.types[0]->name, + model, model->desc->cache.types[0]->name); + _eina_model_unref(child); + return EINA_FALSE; + } + _eina_model_unref(child); + + ret = eina_model_child_insert_at(copy, i, child_copy); + _eina_model_unref(child_copy); + + if (!ret) + { + ERR("Could not set child #%d on model %p (%s)", + i, copy, copy->desc->cache.types[0]->name); + return EINA_FALSE; + } + } + + return EINA_TRUE; +} + +static Eina_Bool +_eina_model_type_base_deep_copy(const Eina_Model *model, Eina_Model *copy) +{ + DBG("base deep copy of %p to %p", model, copy); + + return _eina_model_type_base_properties_copy(model, copy) && + _eina_model_type_base_children_deep_copy(model, copy); +} + +static Eina_Bool +_eina_model_type_base_properties_compare(const Eina_Model *a, const Eina_Model *b, int *cmp) +{ + Eina_List *al, *aprops = eina_model_properties_names_list_get(a); + Eina_List *bl, *bprops = eina_model_properties_names_list_get(b); + Eina_List *l, *props = NULL; + const char *aname, *bname, *name; + Eina_Bool ret = EINA_TRUE; + + EINA_LIST_FOREACH(aprops, al, aname) + { + EINA_LIST_FOREACH(bprops, bl, bname) + if (strcmp(aname, bname) == 0) + { + props = eina_list_append(props, aname); + break; + } + } + + *cmp = 0; + EINA_LIST_FOREACH(props, l, name) + { + Eina_Value atmp, btmp; + + if (!eina_model_property_get(a, name, &atmp)) + { + ERR("Could not get property %s from model %p (%s)", + name, a, a->desc->cache.types[0]->name); + ret = EINA_FALSE; + *cmp = -1; + break; + } + + if (!eina_model_property_get(b, name, &btmp)) + { + ERR("Could not get property %s from model %p (%s)", + name, b, b->desc->cache.types[0]->name); + ret = EINA_FALSE; + *cmp = -1; + eina_value_flush(&atmp); + break; + } + + *cmp = eina_value_compare(&atmp, &btmp); + if (eina_error_get() != 0) + { + char *astr = eina_value_to_string(&atmp); + char *bstr = eina_value_to_string(&btmp); + ERR("Could not compare property %s: %s=%s, %s=%s", name, + eina_value_type_name_get(eina_value_type_get(&atmp)), astr, + eina_value_type_name_get(eina_value_type_get(&btmp)), bstr); + free(astr); + free(bstr); + ret = EINA_FALSE; + *cmp = -1; + } + + eina_value_flush(&atmp); + eina_value_flush(&btmp); + + if ((!ret) || (*cmp != 0)) + break; + } + + if ((ret) && (*cmp == 0)) + { + int acount = eina_list_count(aprops); + int bcount = eina_list_count(bprops); + + if (acount < bcount) + *cmp = -1; + else if (acount > bcount) + *cmp = 1; + } + + eina_model_properties_names_list_free(aprops); + eina_model_properties_names_list_free(bprops); + eina_list_free(props); + return ret; +} + +static Eina_Bool +_eina_model_type_base_children_compare(const Eina_Model *a, const Eina_Model *b, int *cmp) +{ + int acount = eina_model_child_count(a); + int bcount = eina_model_child_count(b); + int i, count; + Eina_Bool ret = EINA_TRUE; + + if (acount < 0) + { + ERR("Could not get children count of model %p (%s)", + a, a->desc->cache.types[0]->name); + return EINA_FALSE; + } + if (bcount < 0) + { + ERR("Could not get children count of model %p (%s)", + b, b->desc->cache.types[0]->name); + return EINA_FALSE; + } + + if (acount < bcount) + count = acount; + else + count = bcount; + + for (i = 0; i < count; i++) + { + Eina_Model *achild, *bchild; + + achild = eina_model_child_get(a, i); + if (!achild) + { + ERR("Could not get child #%d from model %p (%s)", + i, a, a->desc->cache.types[0]->name); + *cmp = -1; + return EINA_FALSE; + } + + bchild = eina_model_child_get(b, i); + if (!bchild) + { + ERR("Could not get child #%d from model %p (%s)", + i, b, b->desc->cache.types[0]->name); + *cmp = -1; + _eina_model_unref(achild); + return EINA_FALSE; + } + + *cmp = eina_model_compare(achild, bchild); + if (eina_error_get()) + { + ERR("Could not compare children #%d %p (%s) and %p (%s) " + "from models %p (%s) and %p (%s)", i, + achild, + eina_model_type_name_get(eina_model_type_get(achild)), + bchild, + eina_model_type_name_get(eina_model_type_get(bchild)), + a, a->desc->cache.types[0]->name, + b, b->desc->cache.types[0]->name); + ret = EINA_FALSE; + } + _eina_model_unref(achild); + _eina_model_unref(bchild); + + if ((!ret) || (*cmp != 0)) + break; + } + + if ((ret) && (*cmp == 0)) + { + if (acount < bcount) + *cmp = -1; + else if (acount > bcount) + *cmp = 1; + } + + return ret; +} + +static Eina_Bool +_eina_model_type_base_compare(const Eina_Model *a, const Eina_Model *b, int *cmp) +{ + *cmp = 0; + DBG("base compare of %p and %p", a, b); + + if (!_eina_model_type_base_properties_compare(a, b, cmp)) + return EINA_FALSE; + + if (*cmp != 0) + return EINA_TRUE; + + return _eina_model_type_base_children_compare(a, b, cmp); +} + +static int +_eina_model_type_base_child_count(const Eina_Model *model) +{ + DBG("base child_count of %p", model); + return 0; +} + +static int +_eina_model_type_base_child_find(const Eina_Model *model, unsigned int start_position, const Eina_Model *other) +{ + int x = eina_model_child_count(model); + unsigned int i, count; + + DBG("base child_find of %p, %d children", model, x); + + if (x < 0) + return -1; + + count = x; + for (i = start_position; i < count; i++) + { + Eina_Model *current = eina_model_child_get(model, i); + if (current) + { + _eina_model_unref(current); /* we'll not use it's value anyway */ + if (current == other) + return i; + } + } + + return -1; +} + +static int +_eina_model_type_base_child_criteria_match(const Eina_Model *model, unsigned int start_position, Eina_Each_Cb match, const void *user_data) +{ + int x = eina_model_child_count(model); + unsigned int i, count; + + DBG("base child_criteria_match of %p, %d children", model, x); + + if (x < 0) + return -1; + + count = x; + for (i = start_position; i < count; i++) + { + Eina_Model *current = eina_model_child_get(model, i); + if (current) + { + Eina_Bool r = match(model, current, (void *)user_data); + _eina_model_unref(current); + if (r) + return i; + } + } + + return -1; +} + +typedef struct _Eina_Iterator_Model_Base Eina_Iterator_Model_Base; +struct _Eina_Iterator_Model_Base +{ + Eina_Iterator base; + Eina_Model *model; + unsigned int current; + unsigned int end; +}; + +static Eina_Bool +_eina_model_type_base_child_iterator_next(Eina_Iterator *base, void **data) +{ + Eina_Iterator_Model_Base *it; + + it = (Eina_Iterator_Model_Base *)base; + if (it->current >= it->end) + return EINA_FALSE; + + *data = eina_model_child_get(it->model, it->current); + if (!*data) + return EINA_FALSE; + + it->current++; + return EINA_TRUE; +} + +static void * +_eina_model_type_base_child_iterator_get_container(Eina_Iterator *base) +{ + Eina_Iterator_Model_Base *it; + it = (Eina_Iterator_Model_Base *)base; + return it->model; +} + +static void +_eina_model_type_base_child_iterator_free(Eina_Iterator *base) +{ + Eina_Iterator_Model_Base *it; + it = (Eina_Iterator_Model_Base *)base; + eina_model_xunref(it->model, it); + free(it); +} + +static Eina_Iterator * +_eina_model_type_base_child_iterator_get(Eina_Model *model, unsigned int start, unsigned int count) +{ + Eina_Iterator_Model_Base *it = calloc(1, sizeof(*it)); + EINA_SAFETY_ON_NULL_RETURN_VAL(it, NULL); + + EINA_MAGIC_SET(&it->base, EINA_MAGIC_ITERATOR); + it->base.version = EINA_ITERATOR_VERSION; + it->base.next = _eina_model_type_base_child_iterator_next; + it->base.get_container = _eina_model_type_base_child_iterator_get_container; + it->base.free = _eina_model_type_base_child_iterator_free; + + it->model = eina_model_xref(model, it, "eina_model_child_slice_iterator_get"); + it->current = start; + it->end = start + count; + + return &it->base; +} + +typedef struct _Eina_Iterator_Model_Base_Reversed Eina_Iterator_Model_Base_Reversed; +struct _Eina_Iterator_Model_Base_Reversed +{ + Eina_Iterator base; + Eina_Model *model; + unsigned int current; + unsigned int end; +}; + +static Eina_Bool +_eina_model_type_base_child_reversed_iterator_next(Eina_Iterator *base, void **data) +{ + Eina_Iterator_Model_Base_Reversed *it; + + it = (Eina_Iterator_Model_Base_Reversed *)base; + if (it->current == it->end) + return EINA_FALSE; + + it->current--; + *data = eina_model_child_get(it->model, it->current); + if (!*data) + return EINA_FALSE; + + return EINA_TRUE; +} + +static void * +_eina_model_type_base_child_reversed_iterator_get_container(Eina_Iterator *base) +{ + Eina_Iterator_Model_Base_Reversed *it; + it = (Eina_Iterator_Model_Base_Reversed *)base; + return it->model; +} + +static void +_eina_model_type_base_child_reversed_iterator_free(Eina_Iterator *base) +{ + Eina_Iterator_Model_Base_Reversed *it; + it = (Eina_Iterator_Model_Base_Reversed *)base; + eina_model_xunref(it->model, it); + free(it); +} + +static Eina_Iterator * +_eina_model_type_base_child_reversed_iterator_get(Eina_Model *model, unsigned int start, unsigned int count) +{ + Eina_Iterator_Model_Base_Reversed *it; + int children_count; + + children_count = eina_model_child_count(model); + if (children_count < 0) + return NULL; + + if (start + count > (unsigned int)children_count) + { + if (start >= (unsigned int)children_count) + count = 0; + else + count = children_count - start; + } + + it = calloc(1, sizeof(*it)); + EINA_SAFETY_ON_NULL_RETURN_VAL(it, NULL); + EINA_MAGIC_SET(&it->base, EINA_MAGIC_ITERATOR); + it->base.version = EINA_ITERATOR_VERSION; + it->base.next = _eina_model_type_base_child_reversed_iterator_next; + it->base.get_container = _eina_model_type_base_child_reversed_iterator_get_container; + it->base.free = _eina_model_type_base_child_reversed_iterator_free; + + it->model = eina_model_xref(model, it, "eina_model_child_slice_reversed_iterator_get"); + it->current = start + count; + it->end = start; + + return &it->base; +} + +typedef struct _Eina_Iterator_Model_Base_Sorted Eina_Iterator_Model_Base_Sorted; +struct _Eina_Iterator_Model_Base_Sorted +{ + Eina_Iterator base; + Eina_Model *model; + unsigned int current; + unsigned int count; + Eina_Model *elements[]; +}; + +static Eina_Bool +_eina_model_type_base_child_sorted_iterator_next(Eina_Iterator *base, void **data) +{ + Eina_Iterator_Model_Base_Sorted *it; + + it = (Eina_Iterator_Model_Base_Sorted *)base; + if (it->current == it->count) + return EINA_FALSE; + + *data = eina_model_ref(it->elements[it->current]); + it->current++; + return EINA_TRUE; +} + +static void * +_eina_model_type_base_child_sorted_iterator_get_container(Eina_Iterator *base) +{ + Eina_Iterator_Model_Base_Sorted *it; + it = (Eina_Iterator_Model_Base_Sorted *)base; + return it->model; +} + +static void +_eina_model_type_base_child_sorted_iterator_free(Eina_Iterator *base) +{ + Eina_Iterator_Model_Base_Sorted *it; + unsigned int i; + it = (Eina_Iterator_Model_Base_Sorted *)base; + eina_model_xunref(it->model, it); + + for (i = 0; i < it->count; i++) + _eina_model_unref(it->elements[i]); + + free(it); +} + +static Eina_Iterator * +_eina_model_type_base_child_sorted_iterator_get(Eina_Model *model, unsigned int start, unsigned int count, Eina_Compare_Cb compare) +{ + Eina_Iterator_Model_Base_Sorted *it; + int children_count; + unsigned int i; + + children_count = eina_model_child_count(model); + if (children_count < 0) + return NULL; + + if (start + count > (unsigned int)children_count) + { + if (start >= (unsigned int)children_count) + count = 0; + else + count = children_count - start; + } + + it = calloc(1, sizeof(*it) + count * sizeof(Eina_Model *)); + EINA_SAFETY_ON_NULL_RETURN_VAL(it, NULL); + EINA_MAGIC_SET(&it->base, EINA_MAGIC_ITERATOR); + it->base.version = EINA_ITERATOR_VERSION; + it->base.next = _eina_model_type_base_child_sorted_iterator_next; + it->base.get_container = _eina_model_type_base_child_sorted_iterator_get_container; + it->base.free = _eina_model_type_base_child_sorted_iterator_free; + + it->model = eina_model_xref(model, it, "eina_model_child_slice_sorted_iterator_get"); + it->current = 0; + it->count = count; + + for (i = 0; i < count; i++) + { + it->elements[i] = eina_model_child_get(model, i + start); + if (!it->elements[i]) + { + ERR("Failed to get child %u of model %p (%s)", + i + start, model, model->desc->cache.types[0]->name); + free(it); + return NULL; + } + } + + if (count > 1) + _eina_model_array_sort(it->elements, 0, count - 1, compare); + + return &it->base; +} + +typedef struct _Eina_Iterator_Model_Base_Filtered Eina_Iterator_Model_Base_Filtered; +struct _Eina_Iterator_Model_Base_Filtered +{ + Eina_Iterator base; + Eina_Model *model; + Eina_Each_Cb match; + const void *data; + unsigned int current; + unsigned int count; +}; + +static Eina_Bool +_eina_model_type_base_child_filtered_iterator_next(Eina_Iterator *base, void **data) +{ + Eina_Iterator_Model_Base_Filtered *it; + unsigned int *ret; + int i; + + it = (Eina_Iterator_Model_Base_Filtered *)base; + if (it->count == 0) return EINA_FALSE; + + i = eina_model_child_criteria_match(it->model, it->current, it->match, it->data); + if (i < 0) return EINA_FALSE; + + it->current = i + 1; + it->count--; + ret = (unsigned int *)data; + *ret = i; + return EINA_TRUE; +} + +static void * +_eina_model_type_base_child_filtered_iterator_get_container(Eina_Iterator *base) +{ + Eina_Iterator_Model_Base_Filtered *it; + it = (Eina_Iterator_Model_Base_Filtered *)base; + return it->model; +} + +static void +_eina_model_type_base_child_filtered_iterator_free(Eina_Iterator *base) +{ + Eina_Iterator_Model_Base_Filtered *it; + it = (Eina_Iterator_Model_Base_Filtered *)base; + eina_model_xunref(it->model, it); + free(it); +} + +static Eina_Iterator * +_eina_model_type_base_child_filtered_iterator_get(Eina_Model *model, unsigned int start, unsigned int count, Eina_Each_Cb match, const void *data) +{ + Eina_Iterator_Model_Base_Filtered *it = calloc(1, sizeof(*it)); + EINA_SAFETY_ON_NULL_RETURN_VAL(it, NULL); + + EINA_MAGIC_SET(&it->base, EINA_MAGIC_ITERATOR); + it->base.version = EINA_ITERATOR_VERSION; + it->base.next = _eina_model_type_base_child_filtered_iterator_next; + it->base.get_container = _eina_model_type_base_child_filtered_iterator_get_container; + it->base.free = _eina_model_type_base_child_filtered_iterator_free; + + it->model = eina_model_xref(model, it, "eina_model_child_slice_filtered_iterator_get"); + it->match = match; + it->data = data; + it->current = start; + it->count = count; + + return &it->base; +} + +static char * +_eina_model_type_base_to_string(const Eina_Model *model) +{ + Eina_List *l, *props; + const char *name; + Eina_Strbuf *str; + Eina_Bool first; + int i, count; + char *ret; + + str = eina_strbuf_new(); + EINA_SAFETY_ON_NULL_RETURN_VAL(str, NULL); + + eina_strbuf_append_printf(str, "%s({", model->desc->cache.types[0]->name); + + props = eina_model_properties_names_list_get(model); + props = eina_list_sort(props, 0, EINA_COMPARE_CB(strcmp)); + + first = EINA_TRUE; + EINA_LIST_FOREACH(props, l, name) + { + Eina_Value val; + + if (!first) + eina_strbuf_append_printf(str, ", %s: ", name); + else + { + eina_strbuf_append_printf(str, "%s: ", name); + first = EINA_FALSE; + } + + if (!eina_model_property_get(model, name, &val)) + eina_strbuf_append_char(str, '?'); + else + { + char *tmp = eina_value_to_string(&val); + eina_strbuf_append(str, tmp ? tmp : "?"); + free(tmp); + eina_value_flush(&val); + } + } + eina_list_free(props); + + eina_strbuf_append(str, "}, ["); + + count = eina_model_child_count(model); + first = EINA_TRUE; + for (i = 0; i < count; i++) + { + Eina_Model *c = eina_model_child_get(model, i); + if (!c) + { + if (!first) + eina_strbuf_append(str, ", ?"); + else + { + eina_strbuf_append_char(str, '?'); + first = EINA_FALSE; + } + } + else + { + char *tmp = eina_model_to_string(c); + if (!first) + eina_strbuf_append_printf(str, ", %s", tmp ? tmp : "?"); + else + { + eina_strbuf_append(str, tmp ? tmp : "?"); + first = EINA_FALSE; + } + free(tmp); + _eina_model_unref(c); + } + } + + eina_strbuf_append(str, "])"); + + ret = eina_strbuf_string_steal(str); + eina_strbuf_free(str); + + return ret; +} + +static const Eina_Model_Event_Description _eina_model_type_base_events[] = { + EINA_MODEL_EVENT_DESCRIPTION(_eina_model_str_deleted, "", "model was deleted"), + EINA_MODEL_EVENT_DESCRIPTION(_eina_model_str_freed, "", "model memory was released"), + EINA_MODEL_EVENT_DESCRIPTION(_eina_model_str_property_set, "s", "model data was set, data name given as event information."), + EINA_MODEL_EVENT_DESCRIPTION(_eina_model_str_property_del, "s", "model data was deleted, data name given as event information."), + EINA_MODEL_EVENT_DESCRIPTION(_eina_model_str_children_changed, "", "model children changed (deleted, inserted)."), + EINA_MODEL_EVENT_DESCRIPTION(_eina_model_str_child_inserted, "u", "model child was inserted, child position is given."), + EINA_MODEL_EVENT_DESCRIPTION(_eina_model_str_child_set, "u", "model child was set, child position is given."), + EINA_MODEL_EVENT_DESCRIPTION(_eina_model_str_child_del, "u", "model child was deleted, child position is given."), + EINA_MODEL_EVENT_DESCRIPTION(_eina_model_str_loaded, "", "model was loaded"), + EINA_MODEL_EVENT_DESCRIPTION(_eina_model_str_unloaded, "", "model was unloaded"), + EINA_MODEL_EVENT_DESCRIPTION_SENTINEL +}; + +static const Eina_Model_Type _EINA_MODEL_TYPE_BASE = { + EINA_MODEL_TYPE_VERSION, + 0, /* there is no private data */ + sizeof(Eina_Model_Type), + "Eina_Model_Type_Base", + NULL, /* should be the only type with NULL here! */ + NULL, /* no interfaces implemented */ + _eina_model_type_base_events, + _eina_model_type_base_setup, + _eina_model_type_base_flush, + _eina_model_type_base_constructor, + _eina_model_type_base_destructor, + _eina_model_type_base_copy, + _eina_model_type_base_deep_copy, + _eina_model_type_base_compare, + NULL, /* no load */ + NULL, /* no unload */ + NULL, /* no property value get */ + NULL, /* no property value set */ + NULL, /* no property del */ + NULL, /* no properties names list */ + _eina_model_type_base_child_count, + NULL, /* no child get */ + NULL, /* no child set */ + NULL, /* no child del */ + NULL, /* no child insert */ + _eina_model_type_base_child_find, + _eina_model_type_base_child_criteria_match, + NULL, /* no child sort */ + _eina_model_type_base_child_iterator_get, + _eina_model_type_base_child_reversed_iterator_get, + _eina_model_type_base_child_sorted_iterator_get, + _eina_model_type_base_child_filtered_iterator_get, + _eina_model_type_base_to_string, + NULL, /* extension pointer */ + NULL, /* extension pointer */ + NULL, /* extension pointer */ + NULL /* extension pointer */ +}; + +/* + * EINA_MODEL_TYPE_MIXIN: + * + * Mix-in is a type that uses 2 interfaces, one for properties, + * another for children. Users should inherit this model and implement + * at least onf of the interfaces to get an usable model without + * defining the methods. + */ + +static const char _EINA_MODEL_INTERFACE_NAME_PROPERTIES[] = "Eina_Model_Interface_Properties"; +static const char _EINA_MODEL_INTERFACE_NAME_CHILDREN[] = "Eina_Model_Interface_Children"; + +typedef struct _Eina_Model_Type_Mixin_Data Eina_Model_Type_Mixin_Data; +struct _Eina_Model_Type_Mixin_Data +{ + /* just keep interfaces to avoid lookups */ + const Eina_Model_Interface *if_properties; + const Eina_Model_Interface *if_children; +}; + +static Eina_Bool +_eina_model_type_mixin_setup(Eina_Model *model) +{ + DBG("mix-in setup of %p", model); + return EINA_TRUE; +} + +static Eina_Bool +_eina_model_type_mixin_flush(Eina_Model *model) +{ + DBG("mix-in flush of %p", model); + return EINA_TRUE; +} + +static Eina_Bool +_eina_model_type_mixin_constructor(Eina_Model *model) +{ + Eina_Model_Type_Mixin_Data *priv = eina_model_type_private_data_get + (model, EINA_MODEL_TYPE_MIXIN); + + DBG("mix-in constructor of %p (priv=%p)", model, priv); + + priv->if_properties = eina_model_interface_get + (model, EINA_MODEL_INTERFACE_NAME_PROPERTIES); + if (priv->if_properties) + { + if (!eina_model_interface_constructor(priv->if_properties, model)) + { + ERR("Could not construct properties interface %p of %p (%s)", + model, priv->if_properties, model->desc->cache.types[0]->name); + return EINA_FALSE; + } + } + + priv->if_children = eina_model_interface_get + (model, EINA_MODEL_INTERFACE_NAME_CHILDREN); + if (priv->if_children) + { + if (!eina_model_interface_constructor(priv->if_children, model)) + { + ERR("Could not construct children interface %p of %p (%s)", + model, priv->if_children, model->desc->cache.types[0]->name); + return EINA_FALSE; + } + } + + if ((!priv->if_properties) && (!priv->if_children)) + { + ERR("Mix-in model %p (%s) does not implement properties or children " + "interfaces!", + model, model->desc->cache.types[0]->name); + return EINA_FALSE; + } + + return EINA_TRUE; +} + +#define EINA_MODEL_TYPE_MIXIN_GET(model) \ + Eina_Model_Type_Mixin_Data *priv = eina_model_type_private_data_get \ + (model, EINA_MODEL_TYPE_MIXIN) + +static Eina_Bool +_eina_model_type_mixin_destructor(Eina_Model *model) +{ + EINA_MODEL_TYPE_MIXIN_GET(model); + + DBG("mixin destructor of %p", model); + + if (priv->if_properties) + eina_model_interface_destructor(priv->if_properties, model); + + if (priv->if_children) + eina_model_interface_destructor(priv->if_children, model); + + return EINA_TRUE; +} + +static Eina_Bool +_eina_model_type_mixin_compare(const Eina_Model *a, const Eina_Model *b, int *cmp) +{ + Eina_Bool ret = EINA_TRUE, did_prop = EINA_FALSE, did_child = EINA_FALSE; + + *cmp = 0; + + EINA_MODEL_TYPE_MIXIN_GET(a); + + if (priv->if_properties) + { + Eina_Bool (*compare)(const Eina_Model*, const Eina_Model*, int *) = + _eina_model_interface_find_offset + (priv->if_properties, + offsetof(Eina_Model_Interface_Properties, compare)); + + if (compare) + { + ret &= compare(a, b, cmp); + did_prop = EINA_TRUE; + } + } + + if ((ret) && (*cmp == 0)) + { + if (priv->if_children) + { + Eina_Bool (*compare)(const Eina_Model*, const Eina_Model*, int *) = + _eina_model_interface_find_offset + (priv->if_children, + offsetof(Eina_Model_Interface_Children, compare)); + + if (compare) + { + ret &= compare(a, b, cmp); + did_child = EINA_TRUE; + } + } + } + + if ((!did_prop) && (!did_child)) + return eina_model_type_compare(EINA_MODEL_TYPE_BASE, a, b, cmp); + + return ret; +} + +static Eina_Bool +_eina_model_type_mixin_load(Eina_Model *model) +{ + Eina_Bool ret = EINA_TRUE; + + EINA_MODEL_TYPE_MIXIN_GET(model); + + if (priv->if_properties) + ret &= eina_model_interface_properties_load(priv->if_properties, model); + + if (priv->if_children) + ret &= eina_model_interface_children_load(priv->if_children, model); + + return ret; +} + +static Eina_Bool +_eina_model_type_mixin_unload(Eina_Model *model) +{ + Eina_Bool ret = EINA_TRUE; + + EINA_MODEL_TYPE_MIXIN_GET(model); + + if (priv->if_properties) + ret &= eina_model_interface_properties_unload(priv->if_properties, model); + + if (priv->if_children) + ret &= eina_model_interface_children_unload(priv->if_children, model); + + return ret; +} + +static Eina_Bool +_eina_model_type_mixin_property_get(const Eina_Model *model, const char *name, Eina_Value *value) +{ + Eina_Bool ret = EINA_FALSE; + + EINA_MODEL_TYPE_MIXIN_GET(model); + + if (priv->if_properties) + ret = eina_model_interface_properties_get + (priv->if_properties, model, name, value); + + return ret; +} + +static Eina_Bool +_eina_model_type_mixin_property_set(Eina_Model *model, const char *name, const Eina_Value *value) +{ + Eina_Bool ret = EINA_FALSE; + + EINA_MODEL_TYPE_MIXIN_GET(model); + + if (priv->if_properties) + ret = eina_model_interface_properties_set + (priv->if_properties, model, name, value); + + return ret; +} + +static Eina_Bool +_eina_model_type_mixin_property_del(Eina_Model *model, const char *name) +{ + Eina_Bool ret = EINA_FALSE; + + EINA_MODEL_TYPE_MIXIN_GET(model); + + if (priv->if_properties) + ret = eina_model_interface_properties_del + (priv->if_properties, model, name); + + return ret; +} + +static Eina_List * +_eina_model_type_mixin_properties_names_list_get(const Eina_Model *model) +{ + Eina_List *ret = NULL; + + EINA_MODEL_TYPE_MIXIN_GET(model); + + if (priv->if_properties) + ret = eina_model_interface_properties_names_list_get + (priv->if_properties, model); + + return ret; +} + +static int +_eina_model_type_mixin_child_count(const Eina_Model *model) +{ + EINA_MODEL_TYPE_MIXIN_GET(model); + + if (!priv->if_children) + return 0; + + return eina_model_interface_children_count(priv->if_children, model); +} + +static Eina_Model * +_eina_model_type_mixin_child_get(const Eina_Model *model, unsigned int position) +{ + EINA_MODEL_TYPE_MIXIN_GET(model); + + if (!priv->if_children) + return 0; + + return eina_model_interface_children_get(priv->if_children, model, position); +} + +static Eina_Bool +_eina_model_type_mixin_child_set(Eina_Model *model, unsigned int position, Eina_Model *child) +{ + EINA_MODEL_TYPE_MIXIN_GET(model); + + if (!priv->if_children) + return 0; + + return eina_model_interface_children_set + (priv->if_children, model, position, child); +} + +static Eina_Bool +_eina_model_type_mixin_child_del(Eina_Model *model, unsigned int position) +{ + EINA_MODEL_TYPE_MIXIN_GET(model); + + if (!priv->if_children) + return 0; + + return eina_model_interface_children_del + (priv->if_children, model, position); +} + +static Eina_Bool +_eina_model_type_mixin_child_insert_at(Eina_Model *model, unsigned int position, Eina_Model *child) +{ + EINA_MODEL_TYPE_MIXIN_GET(model); + + if (!priv->if_children) + return 0; + + return eina_model_interface_children_insert_at + (priv->if_children, model, position, child); +} + +static void +_eina_model_type_mixin_child_sort(Eina_Model *model, Eina_Compare_Cb compare) +{ + EINA_MODEL_TYPE_MIXIN_GET(model); + + if (!priv->if_children) + return; + eina_model_interface_children_sort(priv->if_children, model, compare); +} + +static const Eina_Model_Type _EINA_MODEL_TYPE_MIXIN = { + EINA_MODEL_TYPE_VERSION, + sizeof(Eina_Model_Type_Mixin_Data), + sizeof(Eina_Model_Type), + "Eina_Model_Type_Mixin", + &_EINA_MODEL_TYPE_BASE, + NULL, /* no interfaces implemented */ + NULL, /* no extra events */ + _eina_model_type_mixin_setup, + _eina_model_type_mixin_flush, + _eina_model_type_mixin_constructor, + _eina_model_type_mixin_destructor, + NULL, /* no copy, as interface is called automatically */ + NULL, /* no deep copy, as interface is called automatically */ + _eina_model_type_mixin_compare, + _eina_model_type_mixin_load, + _eina_model_type_mixin_unload, + _eina_model_type_mixin_property_get, + _eina_model_type_mixin_property_set, + _eina_model_type_mixin_property_del, + _eina_model_type_mixin_properties_names_list_get, + _eina_model_type_mixin_child_count, + _eina_model_type_mixin_child_get, + _eina_model_type_mixin_child_set, + _eina_model_type_mixin_child_del, + _eina_model_type_mixin_child_insert_at, + NULL, /* use default find */ + NULL, /* use default criteria_match */ + _eina_model_type_mixin_child_sort, + NULL, /* use default iterator get */ + NULL, /* use default reversed iterator get */ + NULL, /* use default sorted iterator get */ + NULL, /* use default filtered iterator get */ + NULL, /* use default to string */ + NULL, /* extension pointer */ + NULL, /* extension pointer */ + NULL, /* extension pointer */ + NULL /* extension pointer */ +}; +#undef EINA_MODEL_TYPE_MIXIN_GET + +/* Events for all Properties interface */ +static const Eina_Model_Event_Description _eina_model_interface_properties_events[] = { + EINA_MODEL_EVENT_DESCRIPTION(_eina_model_str_properties_loaded, "", "model properties were loaded"), + EINA_MODEL_EVENT_DESCRIPTION(_eina_model_str_properties_unloaded, "", "model properties were unloaded"), + EINA_MODEL_EVENT_DESCRIPTION_SENTINEL +}; + +/* EINA_MODEL_INTERFACE_PROPERTIES_HASH ******************************/ + +#define EINA_MODEL_INTERFACE_PROPERTIES_HASH_GET(model) \ + Eina_Hash *priv = *(Eina_Hash **)eina_model_interface_private_data_get \ + (model, EINA_MODEL_INTERFACE_PROPERTIES_HASH) + +static Eina_Bool +_eina_model_interface_properties_hash_setup(Eina_Model *model) +{ + Eina_Hash **p_priv = eina_model_interface_private_data_get + (model, EINA_MODEL_INTERFACE_PROPERTIES_HASH); + + DBG("setup interface properties (hash) at %p model %p (%s)", + p_priv, model, model->desc->cache.types[0]->name); + + *p_priv = eina_hash_string_small_new(NULL); + return !!*p_priv; +} + +static Eina_Bool +_eina_model_interface_properties_hash_flush(Eina_Model *model) +{ + EINA_MODEL_INTERFACE_PROPERTIES_HASH_GET(model); + + DBG("flush interface properties (hash) at %p model %p (%s)", + priv, model, model->desc->cache.types[0]->name); + + if (priv) + { + ERR("interface properties flushed with values! priv=%p, model %p (%s)", + priv, model, model->desc->cache.types[0]->name); + eina_hash_free(priv); + } + + return EINA_TRUE; +} + +static Eina_Bool +_eina_model_interface_properties_hash_constructor(Eina_Model *model) +{ + EINA_MODEL_INTERFACE_PROPERTIES_HASH_GET(model); + + DBG("construct interface properties (hash) at %p model %p (%s)", + priv, model, model->desc->cache.types[0]->name); + + return EINA_TRUE; +} + +static Eina_Bool +_eina_model_interface_properties_hash_destructor_foreach(const Eina_Hash *hash __UNUSED__, const void *key __UNUSED__, void *data, void *fdata __UNUSED__) +{ + eina_value_free(data); + return EINA_TRUE; +} + +static Eina_Bool +_eina_model_interface_properties_hash_destructor(Eina_Model *model) +{ + Eina_Hash **p_priv = eina_model_interface_private_data_get + (model, EINA_MODEL_INTERFACE_PROPERTIES_HASH); + int count = eina_hash_population(*p_priv); + + DBG("destroy interface properties (hash) at %p model %p (%s). %d values.", + *p_priv, model, model->desc->cache.types[0]->name, count); + + eina_hash_foreach + (*p_priv, _eina_model_interface_properties_hash_destructor_foreach, NULL); + eina_hash_free(*p_priv); + *p_priv = NULL; + + return EINA_TRUE; +} + +static Eina_Bool +_eina_model_interface_properties_hash_get(const Eina_Model *model, const char *name, Eina_Value *value) +{ + EINA_MODEL_INTERFACE_PROPERTIES_HASH_GET(model); + const Eina_Value *prop = eina_hash_find(priv, name); + EINA_SAFETY_ON_NULL_RETURN_VAL(prop, EINA_FALSE); + return eina_value_copy(prop, value); +} + +static Eina_Bool +_eina_model_interface_properties_hash_set(Eina_Model *model, const char *name, const Eina_Value *value) +{ + EINA_MODEL_INTERFACE_PROPERTIES_HASH_GET(model); + Eina_Value *prop, *old = eina_hash_find(priv, name); + + prop = eina_value_new(eina_value_type_get(value)); + EINA_SAFETY_ON_NULL_RETURN_VAL(prop, EINA_FALSE); + + eina_value_flush(prop); + if (!eina_value_copy(value, prop)) + { + ERR("Could not copy value '%s' from %p to %p", name, value, prop); + eina_value_free(prop); + return EINA_FALSE; + } + + if (!old) + { + if (!eina_hash_add(priv, name, prop)) + { + ERR("Could not add value %p to hash as key '%s'", prop, name); + eina_value_free(prop); + return EINA_FALSE; + } + } + else + { + eina_value_free(old); + if (!eina_hash_modify(priv, name, prop)) + { + ERR("Could not modify hash key '%s' value from %p to %p", + name, old, prop); + eina_hash_del_by_key(priv, name); + eina_value_free(prop); + return EINA_FALSE; + } + } + + return EINA_TRUE; +} + +static Eina_Bool +_eina_model_interface_properties_hash_del(Eina_Model *model, const char *name) +{ + EINA_MODEL_INTERFACE_PROPERTIES_HASH_GET(model); + Eina_Value *old = eina_hash_find(priv, name); + EINA_SAFETY_ON_NULL_RETURN_VAL(old, EINA_FALSE); + eina_value_free(old); + return eina_hash_del_by_key(priv, name); +} + +static Eina_Bool +_eina_model_interface_properties_hash_names_list_foreach(const Eina_Hash *hash __UNUSED__, const void *key, void *data __UNUSED__, void *fdata) +{ + Eina_List **p_list = fdata; + *p_list = eina_list_append(*p_list, eina_stringshare_add(key)); + return EINA_TRUE; +} + +static Eina_List * +_eina_model_interface_properties_hash_names_list(const Eina_Model *model) +{ + EINA_MODEL_INTERFACE_PROPERTIES_HASH_GET(model); + Eina_List *list = NULL; + eina_hash_foreach + (priv, _eina_model_interface_properties_hash_names_list_foreach, &list); + return list; +} +#undef EINA_MODEL_INTERFACE_PROPERTIES_HASH_GET + +static const Eina_Model_Interface_Properties _EINA_MODEL_INTERFACE_PROPERTIES_HASH = { + { + EINA_MODEL_INTERFACE_VERSION, + sizeof(Eina_Hash *), + sizeof(Eina_Model_Interface_Properties), + _EINA_MODEL_INTERFACE_NAME_PROPERTIES, + NULL, /* no parent interfaces */ + _eina_model_interface_properties_events, + _eina_model_interface_properties_hash_setup, + _eina_model_interface_properties_hash_flush, + _eina_model_interface_properties_hash_constructor, + _eina_model_interface_properties_hash_destructor, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL + }, + EINA_MODEL_INTERFACE_PROPERTIES_VERSION, + NULL, /* no compare */ + NULL, /* no load */ + NULL, /* no unload */ + _eina_model_interface_properties_hash_get, + _eina_model_interface_properties_hash_set, + _eina_model_interface_properties_hash_del, + _eina_model_interface_properties_hash_names_list +}; + +/* EINA_MODEL_INTERFACE_PROPERTIES_STRUCT ******************************/ + +static Eina_Value_Struct * +_eina_model_interface_properties_struct_private_get(const Eina_Model *model) +{ + Eina_Value *val = eina_model_interface_private_data_get + (model, EINA_MODEL_INTERFACE_PROPERTIES_STRUCT); + return eina_value_memory_get(val); +} + +#define EINA_MODEL_INTERFACE_PROPERTIES_STRUCT_GET(model) \ + Eina_Value_Struct *priv = \ + _eina_model_interface_properties_struct_private_get(model) + +static Eina_Bool +_eina_model_interface_properties_struct_setup(Eina_Model *model) +{ + Eina_Value *val = eina_model_interface_private_data_get + (model, EINA_MODEL_INTERFACE_PROPERTIES_STRUCT); + + DBG("setup interface properties (struct) at %p model %p (%s)", + val, model, model->desc->cache.types[0]->name); + + return eina_value_setup(val, EINA_VALUE_TYPE_STRUCT); +} + +static Eina_Bool +_eina_model_interface_properties_struct_flush(Eina_Model *model) +{ + Eina_Value *val = eina_model_interface_private_data_get + (model, EINA_MODEL_INTERFACE_PROPERTIES_STRUCT); + + DBG("flush interface properties (struct) at %p model %p (%s)", + val, model, model->desc->cache.types[0]->name); + + if (val->type) + { + ERR("interface properties flushed with values! val=%p, model %p (%s)", + val, model, model->desc->cache.types[0]->name); + eina_value_flush(val); + } + + return EINA_TRUE; +} + +static Eina_Bool +_eina_model_interface_properties_struct_constructor(Eina_Model *model) +{ + EINA_MODEL_INTERFACE_PROPERTIES_STRUCT_GET(model); + + DBG("construct interface properties (struct) at %p model %p (%s)", + priv, model, model->desc->cache.types[0]->name); + + return EINA_TRUE; +} + +static Eina_Bool +_eina_model_interface_properties_struct_destructor(Eina_Model *model) +{ + Eina_Value *val = eina_model_interface_private_data_get + (model, EINA_MODEL_INTERFACE_PROPERTIES_STRUCT); + + DBG("destroy interface properties (struct) at %p model %p (%s)", + val, model, model->desc->cache.types[0]->name); + + eina_value_flush(val); + val->type = NULL; + + return EINA_TRUE; +} + +static Eina_Bool +_eina_model_interface_properties_struct_get(const Eina_Model *model, const char *name, Eina_Value *val) +{ + const Eina_Value *v = eina_model_interface_private_data_get + (model, EINA_MODEL_INTERFACE_PROPERTIES_STRUCT); + return eina_value_struct_value_get(v, name, val); +} + +static Eina_Bool +_eina_model_interface_properties_struct_set(Eina_Model *model, const char *name, const Eina_Value *val) +{ + Eina_Value *v = eina_model_interface_private_data_get + (model, EINA_MODEL_INTERFACE_PROPERTIES_STRUCT); + return eina_value_struct_value_set(v, name, val); +} + +static Eina_Bool +_eina_model_interface_properties_struct_del(Eina_Model *model __UNUSED__, const char *name __UNUSED__) +{ + return EINA_FALSE; /* not allowed */ +} + +static Eina_List * +_eina_model_interface_properties_struct_names_list(const Eina_Model *model) +{ + EINA_MODEL_INTERFACE_PROPERTIES_STRUCT_GET(model); + const Eina_Value_Struct_Member *itr; + Eina_List *list = NULL; + + EINA_SAFETY_ON_NULL_RETURN_VAL(priv, NULL); + EINA_SAFETY_ON_NULL_RETURN_VAL(priv->desc, NULL); + EINA_SAFETY_ON_NULL_RETURN_VAL(priv->desc->members, NULL); + + itr = priv->desc->members; + if (priv->desc->member_count) + { + const Eina_Value_Struct_Member *end = itr + priv->desc->member_count; + for (; itr < end; itr++) + list = eina_list_append(list, eina_stringshare_add(itr->name)); + } + else + { + for (; itr->name != NULL; itr++) + list = eina_list_append(list, eina_stringshare_add(itr->name)); + } + + return list; +} +#undef EINA_MODEL_INTERFACE_PROPERTIES_STRUCT_GET + +static const Eina_Model_Interface_Properties _EINA_MODEL_INTERFACE_PROPERTIES_STRUCT = { + { + EINA_MODEL_INTERFACE_VERSION, + sizeof(Eina_Value), + sizeof(Eina_Model_Interface_Properties), + _EINA_MODEL_INTERFACE_NAME_PROPERTIES, + NULL, /* no parent interfaces */ + _eina_model_interface_properties_events, + _eina_model_interface_properties_struct_setup, + _eina_model_interface_properties_struct_flush, + _eina_model_interface_properties_struct_constructor, + _eina_model_interface_properties_struct_destructor, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL + }, + EINA_MODEL_INTERFACE_PROPERTIES_VERSION, + NULL, /* no compare */ + NULL, /* no load */ + NULL, /* no unload */ + _eina_model_interface_properties_struct_get, + _eina_model_interface_properties_struct_set, + _eina_model_interface_properties_struct_del, + _eina_model_interface_properties_struct_names_list +}; + +/* Events for all Children interface */ +static const Eina_Model_Event_Description _eina_model_interface_children_events[] = { + EINA_MODEL_EVENT_DESCRIPTION(_eina_model_str_children_loaded, "", "model children were loaded"), + EINA_MODEL_EVENT_DESCRIPTION(_eina_model_str_children_unloaded, "", "model children were unloaded"), + EINA_MODEL_EVENT_DESCRIPTION_SENTINEL +}; + +/* EINA_MODEL_INTERFACE_CHILDREN_INARRAY ******************************/ + +#define EINA_MODEL_INTERFACE_CHILDREN_INARRAY_GET(model) \ + Eina_Inarray *priv = eina_model_interface_private_data_get \ + (model, EINA_MODEL_INTERFACE_CHILDREN_INARRAY) + +static Eina_Bool +_eina_model_interface_children_inarray_setup(Eina_Model *model) +{ + EINA_MODEL_INTERFACE_CHILDREN_INARRAY_GET(model); + + DBG("setup interface children (inarray) at %p model %p (%s)", + priv, model, model->desc->cache.types[0]->name); + + eina_inarray_setup(priv, sizeof(Eina_Model *), 0); + return EINA_TRUE; +} + +static Eina_Bool +_eina_model_interface_children_inarray_flush(Eina_Model *model) +{ + EINA_MODEL_INTERFACE_CHILDREN_INARRAY_GET(model); + int count; + + DBG("flush interface children (inarray) at %p model %p (%s)", + priv, model, model->desc->cache.types[0]->name); + + count = eina_inarray_count(priv); + if (count > 0) + ERR("interface children flushed with %d members! priv=%p, model %p (%s)", + count, priv, model, model->desc->cache.types[0]->name); + + eina_inarray_flush(priv); + return EINA_TRUE; +} + +static Eina_Bool +_eina_model_interface_children_inarray_constructor(Eina_Model *model) +{ + EINA_MODEL_INTERFACE_CHILDREN_INARRAY_GET(model); + + DBG("construct interface children (inarray) at %p model %p (%s)", + priv, model, model->desc->cache.types[0]->name); + + return EINA_TRUE; +} + +static Eina_Bool +_eina_model_interface_children_inarray_destructor(Eina_Model *model) +{ + Eina_Model **itr, **itr_end; + int count; + + EINA_MODEL_INTERFACE_CHILDREN_INARRAY_GET(model); + + count = eina_inarray_count(priv); + + DBG("destroy interface children (inarray) at %p model %p (%s). %d members.", + priv, model, model->desc->cache.types[0]->name, count); + + itr = priv->members; + itr_end = itr + count; + for (; itr < itr_end; itr++) + eina_model_xunref(*itr, EINA_MODEL_INTERFACE_CHILDREN_INARRAY); + eina_inarray_flush(priv); + + return EINA_TRUE; +} + +static int +_eina_model_interface_children_inarray_count(const Eina_Model *model) +{ + EINA_MODEL_INTERFACE_CHILDREN_INARRAY_GET(model); + return eina_inarray_count(priv); +} + +static Eina_Model * +_eina_model_interface_children_inarray_get(const Eina_Model *model, unsigned int position) +{ + EINA_MODEL_INTERFACE_CHILDREN_INARRAY_GET(model); + Eina_Model **child = eina_inarray_nth(priv, position); + if (!child) + return NULL; + return eina_model_ref(*child); +} + +static Eina_Bool +_eina_model_interface_children_inarray_set(Eina_Model *model, unsigned int position, Eina_Model *child) +{ + EINA_MODEL_INTERFACE_CHILDREN_INARRAY_GET(model); + Eina_Model **p_old = eina_inarray_nth(priv, position); + Eina_Model *old; + + if (!p_old) + return EINA_FALSE; + + old = *p_old; + if (!eina_inarray_replace_at(priv, position, &child)) + return EINA_FALSE; + + eina_model_xref(child, EINA_MODEL_INTERFACE_CHILDREN_INARRAY, + "eina_model_child_set"); + eina_model_xunref(old, EINA_MODEL_INTERFACE_CHILDREN_INARRAY); + return EINA_TRUE; +} + +static Eina_Bool +_eina_model_interface_children_inarray_del(Eina_Model *model, unsigned int position) +{ + EINA_MODEL_INTERFACE_CHILDREN_INARRAY_GET(model); + Eina_Model **p_old = eina_inarray_nth(priv, position); + Eina_Model *old; + + if (!p_old) + return EINA_FALSE; + + old = *p_old; + if (!eina_inarray_remove_at(priv, position)) + return EINA_FALSE; + + eina_model_xunref(old, EINA_MODEL_INTERFACE_CHILDREN_INARRAY); + return EINA_TRUE; +} + +static Eina_Bool +_eina_model_interface_children_inarray_insert_at(Eina_Model *model, unsigned int position, Eina_Model *child) +{ + EINA_MODEL_INTERFACE_CHILDREN_INARRAY_GET(model); + + if (!eina_inarray_insert_at(priv, position, &child)) + return EINA_FALSE; + + eina_model_xref(child, EINA_MODEL_INTERFACE_CHILDREN_INARRAY, + "eina_model_child_insert_at"); + return EINA_TRUE; +} + +static void +_eina_model_interface_children_inarray_sort(Eina_Model *model, Eina_Compare_Cb compare) +{ + EINA_MODEL_INTERFACE_CHILDREN_INARRAY_GET(model); + int count = eina_inarray_count(priv); + EINA_SAFETY_ON_FALSE_RETURN(count >= 0); + + if (count > 1) + _eina_model_array_sort(priv->members, 0, count - 1, compare); +} +#undef EINA_MODEL_INTERFACE_CHILDREN_INARRAY_GET + +static const Eina_Model_Interface_Children _EINA_MODEL_INTERFACE_CHILDREN_INARRAY = { + { + EINA_MODEL_INTERFACE_VERSION, + sizeof(Eina_Inarray), + sizeof(Eina_Model_Interface_Children), + _EINA_MODEL_INTERFACE_NAME_CHILDREN, + NULL, /* no parent interfaces */ + _eina_model_interface_children_events, + _eina_model_interface_children_inarray_setup, + _eina_model_interface_children_inarray_flush, + _eina_model_interface_children_inarray_constructor, + _eina_model_interface_children_inarray_destructor, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL + }, + EINA_MODEL_INTERFACE_CHILDREN_VERSION, + NULL, /* no compare */ + NULL, /* no load */ + NULL, /* no unload */ + _eina_model_interface_children_inarray_count, + _eina_model_interface_children_inarray_get, + _eina_model_interface_children_inarray_set, + _eina_model_interface_children_inarray_del, + _eina_model_interface_children_inarray_insert_at, + _eina_model_interface_children_inarray_sort +}; + +/* EINA_MODEL_TYPE_GENERIC ********************************************/ + +static const Eina_Model_Interface *_EINA_MODEL_TYPE_GENERIC_IFACES[] = { + &_EINA_MODEL_INTERFACE_PROPERTIES_HASH.base, + &_EINA_MODEL_INTERFACE_CHILDREN_INARRAY.base, + NULL +}; + +static const Eina_Model_Type _EINA_MODEL_TYPE_GENERIC = + EINA_MODEL_TYPE_INIT_NOPRIVATE("Eina_Model_Type_Generic", + Eina_Model_Type, + &_EINA_MODEL_TYPE_MIXIN, + _EINA_MODEL_TYPE_GENERIC_IFACES, + NULL); + +/* EINA_MODEL_TYPE_STRUCT ********************************************/ + +static const Eina_Model_Interface *_EINA_MODEL_TYPE_STRUCT_IFACES[] = { + &_EINA_MODEL_INTERFACE_PROPERTIES_STRUCT.base, + &_EINA_MODEL_INTERFACE_CHILDREN_INARRAY.base, + NULL +}; + +static const Eina_Model_Type _EINA_MODEL_TYPE_STRUCT = + EINA_MODEL_TYPE_INIT_NOPRIVATE("Eina_Model_Type_Struct", + Eina_Model_Type, + &_EINA_MODEL_TYPE_MIXIN, + _EINA_MODEL_TYPE_STRUCT_IFACES, + NULL); + +/** + */ + +/** + * @internal + * @brief Initialize the model module. + * + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * This function sets up the model module of Eina. It is called + * by eina_init(). + * + * @see eina_init() + */ +Eina_Bool +eina_model_init(void) +{ + const char *choice, *tmp; + + _eina_model_log_dom = eina_log_domain_register("eina_model", + EINA_LOG_COLOR_DEFAULT); + if (_eina_model_log_dom < 0) + { + EINA_LOG_ERR("Could not register log domain: eina_model"); + return EINA_FALSE; + } + + choice = getenv("EINA_MODEL_DEBUG"); + if (choice) + { + if (strcmp(choice, "1") == 0) + _eina_model_debug = EINA_MODEL_DEBUG_CHECK; + else if (strcmp(choice, "backtrace") == 0) + _eina_model_debug = EINA_MODEL_DEBUG_BACKTRACE; + } + +#ifdef EINA_DEFAULT_MEMPOOL + choice = "pass_through"; +#else + choice = "chained_mempool"; +#endif + tmp = getenv("EINA_MEMPOOL"); + if (tmp && tmp[0]) + choice = tmp; + + if (choice) + _eina_model_mp_choice = strdup(choice); + + _eina_model_mp = eina_mempool_add + (_eina_model_mp_choice, "model", NULL, sizeof(Eina_Model), 320); + if (!_eina_model_mp) + { + ERR("Mempool for model cannot be allocated in model init."); + goto on_init_fail_mp; + } + + if (!eina_lock_new(&_eina_model_inner_mps_lock)) + { + ERR("Cannot create inner mempools lock in model init."); + goto on_init_fail_lock_mp; + } + _eina_model_inner_mps = eina_hash_int32_new(NULL); + if (!_eina_model_inner_mps) + { + ERR("Cannot create hash for inner mempools in model init."); + goto on_init_fail_hash_mp; + } + + if (!eina_lock_new(&_eina_model_descriptions_lock)) + { + ERR("Cannot create model descriptions lock in model init."); + goto on_init_fail_lock_desc; + } + _eina_model_descriptions = eina_hash_pointer_new(NULL); + if (!_eina_model_descriptions) + { + ERR("Cannot create model descriptions hash in model init."); + goto on_init_fail_hash_desc; + } + + if (!eina_lock_new(&_eina_model_debug_list_lock)) + { + ERR("Cannot create model debug list lock in model init."); + goto on_init_fail_lock_debug; + } + + EINA_ERROR_MODEL_FAILED = eina_error_msg_static_register( + EINA_ERROR_MODEL_FAILED_STR); + EINA_ERROR_MODEL_METHOD_MISSING = eina_error_msg_static_register( + EINA_ERROR_MODEL_METHOD_MISSING_STR); + + EINA_MODEL_TYPE_BASE = &_EINA_MODEL_TYPE_BASE; + EINA_MODEL_TYPE_MIXIN = &_EINA_MODEL_TYPE_MIXIN; + EINA_MODEL_TYPE_GENERIC = &_EINA_MODEL_TYPE_GENERIC; + EINA_MODEL_TYPE_STRUCT = &_EINA_MODEL_TYPE_STRUCT; + + EINA_MODEL_INTERFACE_PROPERTIES_HASH = &_EINA_MODEL_INTERFACE_PROPERTIES_HASH.base; + EINA_MODEL_INTERFACE_PROPERTIES_STRUCT = &_EINA_MODEL_INTERFACE_PROPERTIES_STRUCT.base; + + EINA_MODEL_INTERFACE_CHILDREN_INARRAY = &_EINA_MODEL_INTERFACE_CHILDREN_INARRAY.base; + + EINA_MODEL_INTERFACE_NAME_PROPERTIES = _EINA_MODEL_INTERFACE_NAME_PROPERTIES; + EINA_MODEL_INTERFACE_NAME_CHILDREN = _EINA_MODEL_INTERFACE_NAME_CHILDREN; + + eina_magic_string_static_set(EINA_MAGIC_MODEL, EINA_MAGIC_MODEL_STR); + + return EINA_TRUE; + + on_init_fail_lock_debug: + eina_hash_free(_eina_model_descriptions); + on_init_fail_hash_desc: + eina_lock_free(&_eina_model_descriptions_lock); + on_init_fail_lock_desc: + eina_hash_free(_eina_model_inner_mps); + _eina_model_inner_mps = NULL; + on_init_fail_hash_mp: + eina_lock_free(&_eina_model_inner_mps_lock); + on_init_fail_lock_mp: + eina_mempool_del(_eina_model_mp); + on_init_fail_mp: + free(_eina_model_mp_choice); + _eina_model_mp_choice = NULL; + eina_log_domain_unregister(_eina_model_log_dom); + _eina_model_log_dom = -1; + return EINA_FALSE; +} + +/** + * @internal + * @brief Shut down the model module. + * + * @return #EINA_TRUE on success, #EINA_FALSE on failure. + * + * This function shuts down the model module set up by + * eina_model_init(). It is called by eina_shutdown(). + * + * @see eina_shutdown() + */ +Eina_Bool +eina_model_shutdown(void) +{ + eina_lock_take(&_eina_model_debug_list_lock); + if (eina_list_count(_eina_model_debug_list) > 0) + ERR("%d models are still alive!", eina_list_count(_eina_model_debug_list)); + eina_lock_release(&_eina_model_debug_list_lock); + eina_lock_free(&_eina_model_debug_list_lock); + + eina_lock_take(&_eina_model_inner_mps_lock); + if (eina_hash_population(_eina_model_inner_mps) != 0) + ERR("Cannot free eina_model internal memory pools -- still in use!"); + else + eina_hash_free(_eina_model_inner_mps); + eina_lock_release(&_eina_model_inner_mps_lock); + eina_lock_free(&_eina_model_inner_mps_lock); + + eina_lock_take(&_eina_model_descriptions_lock); + if (eina_hash_population(_eina_model_descriptions) != 0) + ERR("Cannot free eina_model internal descriptions -- still in use!"); + else + eina_hash_free(_eina_model_descriptions); + eina_lock_release(&_eina_model_descriptions_lock); + eina_lock_free(&_eina_model_descriptions_lock); + + free(_eina_model_mp_choice); + _eina_model_mp_choice = NULL; + eina_mempool_del(_eina_model_mp); + eina_log_domain_unregister(_eina_model_log_dom); + _eina_model_log_dom = -1; + return EINA_TRUE; +} + +/*============================================================================* + * Global * + *============================================================================*/ + +/*============================================================================* + * API * + *============================================================================*/ + + +EAPI Eina_Error EINA_ERROR_MODEL_FAILED = 0; +EAPI Eina_Error EINA_ERROR_MODEL_METHOD_MISSING = 0; + +EAPI const Eina_Model_Type *EINA_MODEL_TYPE_BASE = NULL; +EAPI const Eina_Model_Type *EINA_MODEL_TYPE_MIXIN = NULL; +EAPI const Eina_Model_Type *EINA_MODEL_TYPE_GENERIC = NULL; +EAPI const Eina_Model_Type *EINA_MODEL_TYPE_STRUCT = NULL; + +EAPI const Eina_Model_Interface *EINA_MODEL_INTERFACE_PROPERTIES_HASH = NULL; +EAPI const Eina_Model_Interface *EINA_MODEL_INTERFACE_PROPERTIES_STRUCT = NULL; +EAPI const Eina_Model_Interface *EINA_MODEL_INTERFACE_CHILDREN_INARRAY = NULL; + +EAPI const char *EINA_MODEL_INTERFACE_NAME_PROPERTIES = "Eina_Model_Interface_Properties"; +EAPI const char *EINA_MODEL_INTERFACE_NAME_CHILDREN = "Eina_Model_Interface_Children"; + +EAPI Eina_Model * +eina_model_new(const Eina_Model_Type *type) +{ + const Eina_Model_Description *desc; + Eina_Model *model; + unsigned int i; + + EINA_SAFETY_ON_NULL_RETURN_VAL(type, NULL); + EINA_SAFETY_ON_FALSE_RETURN_VAL(eina_model_type_check(type), NULL); + + desc = _eina_model_description_get(type); + EINA_SAFETY_ON_NULL_RETURN_VAL(desc, NULL); + + model = eina_mempool_malloc(_eina_model_mp, sizeof(Eina_Model)); + EINA_SAFETY_ON_NULL_GOTO(model, failed_model); + + model->desc = desc; + model->listeners.entries = NULL; + model->listeners.deleted = NULL; + model->listeners.freeze = NULL; + model->listeners.walking = 0; + + if (desc->total.size == 0) + model->privates = NULL; + else + { + unsigned char *ptr; + + model->privates = _eina_model_inner_alloc + (desc->total.privates * sizeof(void *) + + desc->total.size); + EINA_SAFETY_ON_NULL_GOTO(model->privates, failed_privates); + + ptr = (unsigned char *)(model->privates + desc->total.privates); + for (i = 0; i < desc->total.privates; i++) + { + unsigned int size; + if (i < desc->total.types) + size = desc->cache.privates[i].type->private_size; + else + size = desc->cache.privates[i].iface->private_size; + + if (size == 0) + { + model->privates[i] = NULL; + continue; + } + + model->privates[i] = ptr; + memset(ptr, 0, size); + + if (size % sizeof(void *) != 0) + size += sizeof(void *) - (size % sizeof(void *)); + ptr += size; + } + } + + model->refcount = 1; + model->xrefs = NULL; + model->deleted = EINA_FALSE; + EINA_MAGIC_SET(model, EINA_MAGIC_MODEL); + + /* call setup of every type in the reverse order, + * they should not call parent's setup. + */ + for (i = desc->total.types; i > 0; i--) + { + if (desc->cache.types[i - 1]->setup) + { + if (!desc->cache.types[i - 1]->setup(model)) + { + ERR("Failed to setup model %p at type %p (%s)", + model, desc->cache.types[i - 1], + desc->cache.types[i - 1]->name); + goto failed_types; + } + } + } + + /* call setup of every interface in the reverse order, + * they should not call parent's setup. + */ + for (i = desc->total.ifaces; i > 0; i--) + { + if (desc->cache.ifaces[i - 1]->setup) + { + if (!desc->cache.ifaces[i - 1]->setup(model)) + { + ERR("Failed to setup model %p at interface %p (%s)", + model, desc->cache.ifaces[i - 1], + desc->cache.ifaces[i - 1]->name); + goto failed_ifaces; + } + } + } + + if (!desc->ops.type.constructor(model)) + { + ERR("Failed to construct model %p, type %p (%s)", + model, desc->cache.types[0], desc->cache.types[0]->name); + goto failed_constructor; + } + + if (EINA_UNLIKELY(_eina_model_debug)) + { + eina_lock_take(&_eina_model_debug_list_lock); + _eina_model_debug_list = eina_list_append + (_eina_model_debug_list, model); + eina_lock_release(&_eina_model_debug_list_lock); + } + + return model; + + failed_constructor: + i = 0; + failed_ifaces: + /* flush every setup interface, natural order */ + for (; i < desc->total.ifaces; i++) + desc->cache.ifaces[i]->flush(model); + i = 0; + failed_types: + /* flush every setup type, natural order */ + for (; i < desc->total.types; i++) + desc->cache.types[i]->flush(model); + + if (model->privates) + _eina_model_inner_free(desc->total.privates * sizeof(void *) + + desc->total.size, + model->privates); + + failed_privates: + EINA_MAGIC_SET(model, EINA_MAGIC_NONE); + eina_mempool_free(_eina_model_mp, model); + failed_model: + _eina_model_description_dispose(desc); + return NULL; +} + +static void +_eina_model_free(Eina_Model *model) +{ + const Eina_Model_Description *desc = model->desc; + unsigned int i; + + DBG("model %p (%s) refcount=%d deleted=%hhu", + model, model->desc->cache.types[0]->name, + model->refcount, model->deleted); + + if (EINA_UNLIKELY(_eina_model_debug)) + { + if (model->xrefs) + { + ERR("Model %p (%s) released with references pending:", + model, model->desc->cache.types[0]->name); + while (model->xrefs) + { + Eina_Model_XRef *ref = (Eina_Model_XRef *)model->xrefs; + model->xrefs = eina_inlist_remove(model->xrefs, model->xrefs); + + ERR("xref: %p '%s'", ref->id, ref->label); + free(ref); + } + } + + eina_lock_take(&_eina_model_debug_list_lock); + _eina_model_debug_list = eina_list_remove + (_eina_model_debug_list, model); + eina_lock_release(&_eina_model_debug_list_lock); + } + + /* flush every interface, natural order */ + for (i = 0; i < desc->total.ifaces; i++) + if (desc->cache.ifaces[i]->flush) + desc->cache.ifaces[i]->flush(model); + + /* flush every type, natural order */ + for (i = 0; i < desc->total.types; i++) + if (desc->cache.types[i]->flush) + desc->cache.types[i]->flush(model); + + model->refcount--; + _eina_model_event_callback_call(model, _eina_model_str_freed, NULL); + + if (model->privates) + _eina_model_inner_free(desc->total.privates * sizeof(void *) + + desc->total.size, + model->privates); + + if (model->listeners.deleted) + _eina_model_event_callback_free_deleted(model); + + if (model->listeners.entries) + { + for (i = 0; i < desc->total.events; i++) + { + Eina_Inlist *lst = model->listeners.entries[i]; + while (lst) + { + void *tmp = lst; + lst = lst->next; + _eina_model_inner_free(sizeof(Eina_Model_Event_Listener), + tmp); + } + } + + _eina_model_inner_free(desc->total.events * sizeof(Eina_Inlist *), + model->listeners.entries); + } + + if (model->listeners.freeze) + _eina_model_inner_free(model->desc->total.events * sizeof(int), + model->listeners.freeze); + + EINA_MAGIC_SET(model, EINA_MAGIC_NONE); + eina_mempool_free(_eina_model_mp, model); + + _eina_model_description_dispose(desc); +} + +static void +_eina_model_del(Eina_Model *model) +{ + const Eina_Model_Description *desc = model->desc; + + DBG("model %p (%s) refcount=%d deleted=%hhu", + model, model->desc->cache.types[0]->name, + model->refcount, model->deleted); + + EINA_SAFETY_ON_TRUE_RETURN(model->deleted); + + model->deleted = EINA_TRUE; + _eina_model_event_callback_call(model, _eina_model_str_deleted, NULL); + + if (!desc->ops.type.destructor(model)) + ERR("Failed to destroy model %p, type %p (%s)", + model, desc->cache.types[0], desc->cache.types[0]->name); +} + +static void +_eina_model_unref(Eina_Model *model) +{ + DBG("model %p (%s) refcount=%d deleted=%hhu", + model, model->desc->cache.types[0]->name, + model->refcount, model->deleted); + + if (model->refcount > 1) + { + model->refcount--; + return; + } + + if (!model->deleted) _eina_model_del(model); + _eina_model_free(model); +} + +#define EINA_MODEL_INSTANCE_CHECK_VAL(inst, retval) \ + do \ + { \ + if (!EINA_MAGIC_CHECK(inst, EINA_MAGIC_MODEL)) \ + { \ + EINA_MAGIC_FAIL(inst, EINA_MAGIC_MODEL); \ + return retval; \ + } \ + EINA_SAFETY_ON_NULL_RETURN_VAL(inst->desc, retval); \ + EINA_SAFETY_ON_FALSE_RETURN_VAL(inst->refcount > 0, retval); \ + EINA_SAFETY_ON_FALSE_RETURN_VAL(inst->desc->refcount > 0, retval); \ + } \ + while (0) + +#define EINA_MODEL_INSTANCE_CHECK(inst) \ + do \ + { \ + if (!EINA_MAGIC_CHECK(inst, EINA_MAGIC_MODEL)) \ + { \ + EINA_MAGIC_FAIL(inst, EINA_MAGIC_MODEL); \ + return; \ + } \ + EINA_SAFETY_ON_NULL_RETURN(inst->desc); \ + EINA_SAFETY_ON_FALSE_RETURN(inst->refcount > 0); \ + EINA_SAFETY_ON_FALSE_RETURN(inst->desc->refcount > 0); \ + } \ + while (0) + +#define EINA_MODEL_TYPE_CALL_OPTIONAL_RETURN(model, method, def_retval, ...) \ + do \ + { \ + eina_error_set(0); \ + if (model->desc->ops.type.method) \ + return model->desc->ops.type.method(model, ## __VA_ARGS__); \ + DBG("Optional method" # method "() not implemented for model %p (%s)", \ + model, model->desc->cache.types[0]->name); \ + return def_retval; \ + } \ + while (0) + +#define EINA_MODEL_TYPE_CALL_OPTIONAL(model, method, ...) \ + do \ + { \ + eina_error_set(0); \ + if (model->desc->ops.type.method) \ + model->desc->ops.type.method(model, ## __VA_ARGS__); \ + else \ + DBG("Optional method" # method "() not implemented for model %p (%s)", \ + model, model->desc->cache.types[0]->name); \ + } \ + while (0) + +#define EINA_MODEL_TYPE_CALL_MANDATORY_RETURN(model, method, def_retval, ...) \ + do \ + { \ + eina_error_set(0); \ + if (model->desc->ops.type.method) \ + return model->desc->ops.type.method(model, ## __VA_ARGS__); \ + eina_error_set(EINA_ERROR_MODEL_METHOD_MISSING); \ + CRITICAL("Mandatory method" # method "() not implemented for model %p (%s)", \ + model, model->desc->cache.types[0]->name); \ + return def_retval; \ + } \ + while (0) + +#define EINA_MODEL_TYPE_CALL_MANDATORY(model, method, ...) \ + do \ + { \ + eina_error_set(0); \ + if (model->desc->ops.type.method) \ + model->desc->ops.type.method(model, ## __VA_ARGS__); \ + else \ + { \ + eina_error_set(EINA_ERROR_MODEL_METHOD_MISSING); \ + CRITICAL("Mandatory method" # method "() not implemented for model %p (%s)", \ + model, model->desc->cache.types[0]->name); \ + } \ + } \ + while (0) + + +#define EINA_MODEL_TYPE_CALL_RETURN(model, method, def_retval, ...) \ + do \ + { \ + eina_error_set(0); \ + if (model->desc->ops.type.method) \ + return model->desc->ops.type.method(model, ## __VA_ARGS__); \ + eina_error_set(EINA_ERROR_MODEL_METHOD_MISSING); \ + ERR("Method" # method "() not implemented for model %p (%s)", \ + model, model->desc->cache.types[0]->name); \ + return def_retval; \ + } \ + while (0) + +#define EINA_MODEL_TYPE_CALL(model, method, ...) \ + do \ + { \ + eina_error_set(0); \ + if (model->desc->ops.type.method) \ + model->desc->ops.type.method(model, ## __VA_ARGS__); \ + else \ + { \ + eina_error_set(EINA_ERROR_MODEL_METHOD_MISSING); \ + ERR("Method" # method "() not implemented for model %p (%s)", \ + model, model->desc->cache.types[0]->name); \ + } \ + } \ + while (0) + +EAPI void +eina_model_del(Eina_Model *model) +{ + EINA_MODEL_INSTANCE_CHECK(model); + _eina_model_del(model); + _eina_model_unref(model); +} + +EAPI const Eina_Model_Type * +eina_model_type_get(const Eina_Model *model) +{ + EINA_MODEL_INSTANCE_CHECK_VAL(model, NULL); + return model->desc->cache.types[0]; +} + +EAPI const Eina_Model_Interface * +eina_model_interface_get(const Eina_Model *model, const char *name) +{ + const Eina_Model_Description *desc; + const Eina_Model_Interface **itr, **itr_end; + + EINA_MODEL_INSTANCE_CHECK_VAL(model, NULL); + EINA_SAFETY_ON_NULL_RETURN_VAL(name, NULL); + + desc = model->desc; + itr = desc->cache.ifaces; + itr_end = itr + desc->total.ifaces; + + /* fallback to strcmp if user is lazy about speed */ + for (; itr < itr_end; itr++) + if (strcmp((*itr)->name, name) == 0) + return *itr; + + return NULL; +} + +static Eina_Bool +_eina_model_instance_check(const Eina_Model *model, const Eina_Model_Type *type) +{ + const Eina_Model_Type **itr, **itr_end; + + itr = model->desc->cache.types; + itr_end = itr + model->desc->total.types; + + for (; itr < itr_end; itr++) + if (*itr == type) + return EINA_TRUE; + + return EINA_FALSE; +} + +EAPI Eina_Bool +eina_model_instance_check(const Eina_Model *model, const Eina_Model_Type *type) +{ + EINA_MODEL_INSTANCE_CHECK_VAL(model, EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(type, EINA_FALSE); + EINA_SAFETY_ON_FALSE_RETURN_VAL(_eina_model_type_check(type), EINA_FALSE); + return _eina_model_instance_check(model, type); +} + +EAPI Eina_Model * +eina_model_ref(Eina_Model *model) +{ + EINA_MODEL_INSTANCE_CHECK_VAL(model, NULL); + DBG("model %p (%s) refcount=%d deleted=%hhu", + model, model->desc->cache.types[0]->name, + model->refcount, model->deleted); + model->refcount++; + return model; +} + +static Eina_Model * +_eina_model_xref_add(Eina_Model *model, const void *id, const char *label) +{ + Eina_Model_XRef *ref; + void *bt[256]; + int btlen, labellen; + + labellen = label ? strlen(label): 0; + btlen = 0; + +#ifdef HAVE_BACKTRACE + if (_eina_model_debug == EINA_MODEL_DEBUG_BACKTRACE) + btlen = backtrace(bt, EINA_C_ARRAY_LENGTH(bt)); +#endif + + ref = calloc(1, sizeof(*ref) + (btlen * sizeof(void *)) + (labellen + 1)); + EINA_SAFETY_ON_NULL_RETURN_VAL(ref, NULL); + + ref->id = id; + memcpy(ref->label, label, labellen); + ref->label[labellen] = '\0'; + ref->backtrace.count = btlen; + if (btlen == 0) ref->backtrace.symbols = NULL; + else + { + void *ptr = (unsigned char *)ref + sizeof(*ref) + (labellen + 1); + ref->backtrace.symbols = ptr; + memcpy(ptr, bt, btlen * sizeof(void *)); + } + + model->xrefs = eina_inlist_append(model->xrefs, EINA_INLIST_GET(ref)); + return model; +} + +EAPI Eina_Model * +eina_model_xref(Eina_Model *model, const void *id, const char *label) +{ + EINA_MODEL_INSTANCE_CHECK_VAL(model, NULL); + DBG("model %p (%s) refcount=%d deleted=%hhu id=%p label=%s", + model, model->desc->cache.types[0]->name, + model->refcount, model->deleted, id, label ? label : ""); + + model->refcount++; + + if (EINA_LIKELY(!_eina_model_debug)) + return model; + + return _eina_model_xref_add(model, id, label); +} + +EAPI void +eina_model_unref(Eina_Model *model) +{ + EINA_MODEL_INSTANCE_CHECK(model); + _eina_model_unref(model); +} + +EAPI void +eina_model_xunref(Eina_Model *model, const void *id) +{ + Eina_Model_XRef *ref; + EINA_MODEL_INSTANCE_CHECK(model); + + if (EINA_LIKELY(!_eina_model_debug)) + { + _eina_model_unref(model); + return; + } + + EINA_INLIST_FOREACH(model->xrefs, ref) + { + if (ref->id != id) continue; + + model->xrefs = eina_inlist_remove(model->xrefs, EINA_INLIST_GET(ref)); + free(ref); + _eina_model_unref(model); + return; + } + + ERR("Could not find existing reference %p to model %p", id, model); +} + +EAPI int +eina_model_refcount(const Eina_Model *model) +{ + EINA_MODEL_INSTANCE_CHECK_VAL(model, -1); + return model->refcount; +} + +EAPI const Eina_Inlist * +eina_model_xrefs_get(const Eina_Model *model) +{ + EINA_MODEL_INSTANCE_CHECK_VAL(model, NULL); + return model->xrefs; +} + +EAPI Eina_Bool +eina_model_event_callback_add(Eina_Model *model, const char *event_name, Eina_Model_Event_Cb cb, const void *data) +{ + const Eina_Model_Description *desc; + Eina_Model_Event_Listener *el; + int event_id; + + EINA_MODEL_INSTANCE_CHECK_VAL(model, EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(event_name, EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(cb, EINA_FALSE); + + desc = model->desc; + event_id = _eina_model_description_event_id_find(desc, event_name); + if (event_id < 0) + { + ERR("No event named %s for model %p (%s)", + event_name, model, model->desc->cache.types[0]->name); + return EINA_FALSE; + } + + if (!model->listeners.entries) + { + model->listeners.entries = _eina_model_inner_alloc + (desc->total.events * sizeof(Eina_Inlist *)); + EINA_SAFETY_ON_NULL_RETURN_VAL(model->listeners.entries, EINA_FALSE); + memset(model->listeners.entries, 0, + desc->total.events * sizeof(Eina_Inlist *)); + } + + el = _eina_model_inner_alloc(sizeof(Eina_Model_Event_Listener)); + EINA_SAFETY_ON_NULL_RETURN_VAL(el, EINA_FALSE); + + el->cb = cb; + el->data = data; + el->deleted = EINA_FALSE; + model->listeners.entries[event_id] = eina_inlist_append + (model->listeners.entries[event_id], EINA_INLIST_GET(el)); + + return EINA_TRUE; +} + +EAPI Eina_Bool +eina_model_event_callback_del(Eina_Model *model, const char *event_name, Eina_Model_Event_Cb cb, const void *data) +{ + int event_id; + Eina_Inlist *lst; + Eina_Model_Event_Listener *el; + + EINA_MODEL_INSTANCE_CHECK_VAL(model, EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(event_name, EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(cb, EINA_FALSE); + + if (!model->listeners.entries) + { + ERR("No event callbacks for model %p (%s)", + model, model->desc->cache.types[0]->name); + return EINA_FALSE; + } + + event_id = _eina_model_description_event_id_find(model->desc, event_name); + if (event_id < 0) + { + ERR("No event named %s for model %p (%s)", + event_name, model, model->desc->cache.types[0]->name); + return EINA_FALSE; + } + + lst = model->listeners.entries[event_id]; + EINA_INLIST_FOREACH(lst, el) + { + if (el->cb != cb) continue; + if ((data) && (el->data != data)) continue; + + if (model->listeners.walking == 0) + { + model->listeners.entries[event_id] = eina_inlist_remove + (model->listeners.entries[event_id], EINA_INLIST_GET(el)); + _eina_model_inner_free(sizeof(Eina_Model_Event_Listener), el); + } + else + { + el->deleted = EINA_TRUE; + if (!model->listeners.deleted) + { + model->listeners.deleted = _eina_model_inner_alloc + (model->desc->total.events * sizeof(Eina_List *)); + EINA_SAFETY_ON_NULL_RETURN_VAL(model->listeners.deleted, + EINA_FALSE); + + memset(model->listeners.deleted, 0, + model->desc->total.events * sizeof(Eina_List *)); + + } + model->listeners.deleted[event_id] = eina_list_append + (model->listeners.deleted[event_id], el); + } + return EINA_TRUE; + } + + ERR("No callback %p data %p found for event named %s for model %p (%s)", + cb, data, event_name, model, model->desc->cache.types[0]->name); + return EINA_FALSE; +} + +EAPI const Eina_Model_Event_Description * +eina_model_event_description_get(const Eina_Model *model, const char *event_name) +{ + const Eina_Model_Description *desc; + int event_id; + + EINA_MODEL_INSTANCE_CHECK_VAL(model, NULL); + EINA_SAFETY_ON_NULL_RETURN_VAL(event_name, NULL); + + desc = model->desc; + event_id = _eina_model_description_event_id_find(desc, event_name); + if (event_id < 0) + return NULL; + + return desc->cache.events[event_id].desc; +} + +EAPI Eina_List * +eina_model_event_names_list_get(const Eina_Model *model) +{ + const Eina_Model_Event_Description_Cache *itr, *itr_end; + Eina_List *lst = NULL; + + EINA_MODEL_INSTANCE_CHECK_VAL(model, NULL); + + itr = model->desc->cache.events; + itr_end = itr + model->desc->total.events; + + for (; itr < itr_end; itr++) + lst = eina_list_append(lst, eina_stringshare_add(itr->name)); + + return lst; +} + +EAPI void +eina_model_event_names_list_free(Eina_List *list) +{ + const char *str; + EINA_LIST_FREE(list, str) + eina_stringshare_del(str); +} + +EAPI Eina_Bool +eina_model_event_callback_call(Eina_Model *model, const char *name, const void *event_info) +{ + EINA_MODEL_INSTANCE_CHECK_VAL(model, EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(name, EINA_FALSE); + return _eina_model_event_callback_call(model, name, event_info); +} + +EAPI int +eina_model_event_callback_freeze(Eina_Model *model, const char *name) +{ + int event_id; + + EINA_MODEL_INSTANCE_CHECK_VAL(model, -1); + EINA_SAFETY_ON_NULL_RETURN_VAL(name, -1); + + event_id = _eina_model_description_event_id_find(model->desc, name); + if (event_id < 0) return -1; + + if (!model->listeners.freeze) + { + model->listeners.freeze = _eina_model_inner_alloc + (model->desc->total.events * sizeof(int)); + EINA_SAFETY_ON_NULL_RETURN_VAL(model->listeners.freeze, -1); + + memset(model->listeners.freeze, 0, + model->desc->total.events * sizeof(int)); + } + + if (model->listeners.freeze[event_id] == 0) + DBG("model %p (%s) event %s frozen", + model, model->desc->cache.types[0]->name, name); + + model->listeners.freeze[event_id]++; + return model->listeners.freeze[event_id]; +} + +EAPI int +eina_model_event_callback_thaw(Eina_Model *model, const char *name) +{ + int event_id; + + EINA_MODEL_INSTANCE_CHECK_VAL(model, -1); + EINA_SAFETY_ON_NULL_RETURN_VAL(name, -1); + EINA_SAFETY_ON_NULL_RETURN_VAL(model->listeners.freeze, -1); + + event_id = _eina_model_description_event_id_find(model->desc, name); + if (event_id < 0) return -1; + + model->listeners.freeze[event_id]--; + if (model->listeners.freeze[event_id] == 0) + DBG("model %p (%s) event %s unfrozen", + model, model->desc->cache.types[0]->name, name); + return model->listeners.freeze[event_id]; +} + +EAPI Eina_Model * +eina_model_copy(const Eina_Model *model) +{ + const Eina_Model_Description *desc; + Eina_Model *copy; + unsigned int i; + + EINA_MODEL_INSTANCE_CHECK_VAL(model, NULL); + desc = model->desc; + copy = eina_model_new(desc->cache.types[0]); + EINA_SAFETY_ON_NULL_RETURN_VAL(copy, NULL); + + /* call copy of every type in the reverse order, + * they should not call parent's copy. + */ + for (i = desc->total.types; i > 0; i--) + { + if (desc->cache.types[i - 1]->copy) + { + if (!desc->cache.types[i - 1]->copy(model, copy)) + goto failed; + } + } + + /* call copy of every interface in the reverse order, + * they should not call parent's copy. + */ + for (i = desc->total.ifaces; i > 0; i--) + { + if (desc->cache.ifaces[i - 1]->copy) + { + if (!desc->cache.ifaces[i - 1]->copy(model, copy)) + goto failed; + } + } + + return copy; + + failed: + ERR("Failed to copy model %p %s", model, desc->cache.types[0]->name); + eina_model_del(copy); + return NULL; +} + +EAPI Eina_Model * +eina_model_deep_copy(const Eina_Model *model) +{ + const Eina_Model_Description *desc; + Eina_Model *deep_copy; + unsigned int i; + + EINA_MODEL_INSTANCE_CHECK_VAL(model, NULL); + desc = model->desc; + deep_copy = eina_model_new(desc->cache.types[0]); + EINA_SAFETY_ON_NULL_RETURN_VAL(deep_copy, NULL); + + /* call deep_copy of every type in the reverse order, + * they should not call parent's deep_copy. + */ + for (i = desc->total.types; i > 0; i--) + { + if (desc->cache.types[i - 1]->deep_copy) + { + if (!desc->cache.types[i - 1]->deep_copy(model, deep_copy)) + goto failed; + } + } + + /* call deep_copy of every interface in the reverse order, + * they should not call parent's deep_copy. + */ + for (i = desc->total.ifaces; i > 0; i--) + { + if (desc->cache.ifaces[i - 1]->deep_copy) + { + if (!desc->cache.ifaces[i - 1]->deep_copy(model, deep_copy)) + goto failed; + } + } + + return deep_copy; + + failed: + ERR("Failed to deep copy model %p %s", model, desc->cache.types[0]->name); + eina_model_del(deep_copy); + return NULL; +} + +EAPI int +eina_model_compare(const Eina_Model *a, const Eina_Model *b) +{ + const Eina_Model_Description *desc_a, *desc_b; + Eina_Bool ok; + int cmp = -1; + + EINA_MODEL_INSTANCE_CHECK_VAL(a, -1); + EINA_MODEL_INSTANCE_CHECK_VAL(b, -1); + desc_a = a->desc; + desc_b = b->desc; + + if ((!desc_a->ops.type.compare) && (!desc_b->ops.type.compare)) + { + ERR("Models %p (%s) and %p (%s) can't compare", + a, desc_a->cache.types[0]->name, + b, desc_b->cache.types[0]->name); + eina_error_set(EINA_ERROR_MODEL_METHOD_MISSING); + return -1; + } + else if ((desc_a->ops.type.compare) && (desc_b->ops.type.compare)) + { + ok = desc_a->ops.type.compare(a, b, &cmp); + if (!ok) + { + ok = desc_b->ops.type.compare(b, a, &cmp); + if (ok) + cmp = -cmp; /* swapped sides! */ + } + } + else if (desc_a->ops.type.compare) + ok = desc_a->ops.type.compare(a, b, &cmp); + else + { + ok = desc_b->ops.type.compare(b, a, &cmp); + if (ok) + cmp = -cmp; /* swapped sides! */ + } + + if (!ok) + { + ERR("Could not compare models %p (%s) and %p (%s)", + a, desc_a->cache.types[0]->name, + b, desc_b->cache.types[0]->name); + eina_error_set(EINA_ERROR_MODEL_FAILED); + return -1; + } + + return cmp; +} + +EAPI Eina_Bool +eina_model_load(Eina_Model *model) +{ + Eina_Bool ret; + + EINA_MODEL_INSTANCE_CHECK_VAL(model, EINA_FALSE); + + eina_error_set(0); + if (model->desc->ops.type.load) + { + ret = model->desc->ops.type.load(model); + if (ret) + _eina_model_event_callback_call(model, _eina_model_str_loaded, NULL); + } + else + { + eina_error_set(EINA_ERROR_MODEL_METHOD_MISSING); + ret = EINA_FALSE; + ERR("Method load() not implemented for model %p (%s)", + model, model->desc->cache.types[0]->name); + } + + return ret; +} + +EAPI Eina_Bool +eina_model_unload(Eina_Model *model) +{ + Eina_Bool ret; + + EINA_MODEL_INSTANCE_CHECK_VAL(model, EINA_FALSE); + + eina_error_set(0); + if (model->desc->ops.type.unload) + { + ret = model->desc->ops.type.unload(model); + if (ret) + _eina_model_event_callback_call + (model, _eina_model_str_unloaded, NULL); + } + else + { + eina_error_set(EINA_ERROR_MODEL_METHOD_MISSING); + ret = EINA_FALSE; + ERR("Method unload() not implemented for model %p (%s)", + model, model->desc->cache.types[0]->name); + } + + return ret; +} + +EAPI Eina_Bool +eina_model_property_get(const Eina_Model *model, const char *name, Eina_Value *value) +{ + EINA_MODEL_INSTANCE_CHECK_VAL(model, EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(name, EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(value, EINA_FALSE); + EINA_MODEL_TYPE_CALL_MANDATORY_RETURN(model, property_get, EINA_FALSE, + name, value); +} + +EAPI Eina_Bool +eina_model_property_set(Eina_Model *model, const const char *name, const Eina_Value *value) +{ + Eina_Bool ret; + + EINA_MODEL_INSTANCE_CHECK_VAL(model, EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(name, EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(value, EINA_FALSE); + EINA_SAFETY_ON_FALSE_RETURN_VAL(eina_value_type_check(value->type), EINA_FALSE); + + eina_error_set(0); + if (model->desc->ops.type.property_set) + { + ret = model->desc->ops.type.property_set(model, name, value); + if (ret) + _eina_model_event_callback_call + (model, _eina_model_str_property_set, name); + } + else + { + eina_error_set(EINA_ERROR_MODEL_METHOD_MISSING); + ret = EINA_FALSE; + ERR("Method property_set() not implemented for model %p (%s)", + model, model->desc->cache.types[0]->name); + } + + return ret; +} + +EAPI Eina_Bool +eina_model_property_del(Eina_Model *model, const char *name) +{ + Eina_Bool ret; + + EINA_MODEL_INSTANCE_CHECK_VAL(model, EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(name, EINA_FALSE); + + eina_error_set(0); + if (model->desc->ops.type.property_del) + { + ret = model->desc->ops.type.property_del(model, name); + if (ret) + _eina_model_event_callback_call + (model, _eina_model_str_property_del, name); + } + else + { + eina_error_set(EINA_ERROR_MODEL_METHOD_MISSING); + ret = EINA_FALSE; + ERR("Method property_del() not implemented for model %p (%s)", + model, model->desc->cache.types[0]->name); + } + + return ret; +} + +EAPI Eina_List * +eina_model_properties_names_list_get(const Eina_Model *model) +{ + EINA_MODEL_INSTANCE_CHECK_VAL(model, NULL); + EINA_MODEL_TYPE_CALL_OPTIONAL_RETURN(model, properties_names_list_get, NULL); +} + +EAPI void +eina_model_properties_names_list_free(Eina_List *list) +{ + const char *str; + EINA_LIST_FREE(list, str) + eina_stringshare_del(str); +} + +EAPI int +eina_model_child_count(const Eina_Model *model) +{ + EINA_MODEL_INSTANCE_CHECK_VAL(model, -1); + EINA_MODEL_TYPE_CALL_OPTIONAL_RETURN(model, child_count, 0); +} + +EAPI Eina_Model * +eina_model_child_get(const Eina_Model *model, unsigned int position) +{ + EINA_MODEL_INSTANCE_CHECK_VAL(model, NULL); + EINA_MODEL_TYPE_CALL_RETURN(model, child_get, NULL, position); +} + +EAPI Eina_Bool +eina_model_child_set(Eina_Model *model, unsigned int position, Eina_Model *child) +{ + Eina_Bool ret; + + EINA_MODEL_INSTANCE_CHECK_VAL(model, EINA_FALSE); + EINA_MODEL_INSTANCE_CHECK_VAL(child, EINA_FALSE); + + eina_error_set(0); + if (model->desc->ops.type.child_set) + { + ret = model->desc->ops.type.child_set(model, position, child); + if (ret) + _eina_model_event_callback_call + (model, _eina_model_str_child_set, &position); + } + else + { + eina_error_set(EINA_ERROR_MODEL_METHOD_MISSING); + ret = EINA_FALSE; + ERR("Method child_set() not implemented for model %p (%s)", + model, model->desc->cache.types[0]->name); + } + + return ret; +} + +EAPI Eina_Bool +eina_model_child_del(Eina_Model *model, unsigned int position) +{ + Eina_Bool ret; + + EINA_MODEL_INSTANCE_CHECK_VAL(model, EINA_FALSE); + + eina_error_set(0); + if (model->desc->ops.type.child_del) + { + ret = model->desc->ops.type.child_del(model, position); + if (ret) + { + _eina_model_event_callback_call + (model, _eina_model_str_child_del, &position); + _eina_model_event_callback_call + (model, _eina_model_str_children_changed, NULL); + } + } + else + { + eina_error_set(EINA_ERROR_MODEL_METHOD_MISSING); + ret = EINA_FALSE; + ERR("Method child_del() not implemented for model %p (%s)", + model, model->desc->cache.types[0]->name); + } + + return ret; +} + +EAPI Eina_Bool +eina_model_child_insert_at(Eina_Model *model, unsigned int position, Eina_Model *child) +{ + Eina_Bool ret; + + EINA_MODEL_INSTANCE_CHECK_VAL(model, EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(child, EINA_FALSE); + + eina_error_set(0); + if (model->desc->ops.type.child_insert_at) + { + ret = model->desc->ops.type.child_insert_at(model, position, child); + if (ret) + { + _eina_model_event_callback_call + (model, _eina_model_str_child_inserted, &position); + _eina_model_event_callback_call + (model, _eina_model_str_children_changed, NULL); + } + } + else + { + eina_error_set(EINA_ERROR_MODEL_METHOD_MISSING); + ret = EINA_FALSE; + ERR("Method child_insert_at() not implemented for model %p (%s)", + model, model->desc->cache.types[0]->name); + } + + return ret; +} + +EAPI int +eina_model_child_append(Eina_Model *model, Eina_Model *child) +{ + Eina_Bool ret; + int position; + + EINA_MODEL_INSTANCE_CHECK_VAL(model, -1); + EINA_SAFETY_ON_NULL_RETURN_VAL(child, -1); + + position = eina_model_child_count(model); + if (position < 0) + return -1; + + eina_error_set(0); + if (model->desc->ops.type.child_insert_at) + { + ret = model->desc->ops.type.child_insert_at(model, position, child); + if (ret) + { + _eina_model_event_callback_call + (model, _eina_model_str_child_inserted, &position); + _eina_model_event_callback_call + (model, _eina_model_str_children_changed, NULL); + } + } + else + { + eina_error_set(EINA_ERROR_MODEL_METHOD_MISSING); + ret = EINA_FALSE; + ERR("Method child_insert_at() not implemented for model %p (%s)", + model, model->desc->cache.types[0]->name); + } + + return ret ? position : -1; +} + +EAPI int +eina_model_child_find(const Eina_Model *model, unsigned int start_position, const Eina_Model *other) +{ + EINA_MODEL_INSTANCE_CHECK_VAL(model, -1); + EINA_SAFETY_ON_NULL_RETURN_VAL(other, -1); + EINA_MODEL_TYPE_CALL_RETURN(model, child_find, -1, start_position, other); +} + +EAPI int +eina_model_child_criteria_match(const Eina_Model *model, unsigned int start_position, Eina_Each_Cb match, const void *data) +{ + EINA_MODEL_INSTANCE_CHECK_VAL(model, -1); + EINA_SAFETY_ON_NULL_RETURN_VAL(match, -1); + EINA_MODEL_TYPE_CALL_RETURN(model, child_criteria_match, -1, + start_position, match, data); +} + +EAPI Eina_Bool +eina_model_child_sort(Eina_Model *model, Eina_Compare_Cb compare) +{ + EINA_MODEL_INSTANCE_CHECK_VAL(model, EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(compare, EINA_FALSE); + EINA_MODEL_TYPE_CALL(model, child_sort, compare); + _eina_model_event_callback_call + (model, _eina_model_str_children_changed, NULL); + return EINA_TRUE; +} + +EAPI Eina_Iterator * +eina_model_child_iterator_get(Eina_Model *model) +{ + int count; + EINA_MODEL_INSTANCE_CHECK_VAL(model, NULL); + count = eina_model_child_count(model); + if (count < 0) + return NULL; + EINA_MODEL_TYPE_CALL_RETURN(model, child_iterator_get, NULL, 0, count); +} + +EAPI Eina_Iterator * +eina_model_child_slice_iterator_get(Eina_Model *model, unsigned int start, unsigned int count) +{ + EINA_MODEL_INSTANCE_CHECK_VAL(model, NULL); + EINA_MODEL_TYPE_CALL_RETURN(model, child_iterator_get, NULL, start, count); +} + +EAPI Eina_Iterator * +eina_model_child_reversed_iterator_get(Eina_Model *model) +{ + int count; + EINA_MODEL_INSTANCE_CHECK_VAL(model, NULL); + count = eina_model_child_count(model); + if (count < 0) + return NULL; + EINA_MODEL_TYPE_CALL_RETURN(model, child_reversed_iterator_get, NULL, + 0, count); +} + +EAPI Eina_Iterator * +eina_model_child_slice_reversed_iterator_get(Eina_Model *model, unsigned int start, unsigned int count) +{ + EINA_MODEL_INSTANCE_CHECK_VAL(model, NULL); + EINA_MODEL_TYPE_CALL_RETURN(model, child_reversed_iterator_get, NULL, + start, count); +} + +EAPI Eina_Iterator * +eina_model_child_sorted_iterator_get(Eina_Model *model, Eina_Compare_Cb compare) +{ + int count; + EINA_MODEL_INSTANCE_CHECK_VAL(model, NULL); + EINA_SAFETY_ON_NULL_RETURN_VAL(compare, NULL); + count = eina_model_child_count(model); + if (count < 0) + return NULL; + EINA_MODEL_TYPE_CALL_RETURN(model, child_sorted_iterator_get, NULL, + 0, count, compare); +} + +EAPI Eina_Iterator * +eina_model_child_slice_sorted_iterator_get(Eina_Model *model, unsigned int start, unsigned int count, Eina_Compare_Cb compare) +{ + EINA_MODEL_INSTANCE_CHECK_VAL(model, NULL); + EINA_SAFETY_ON_NULL_RETURN_VAL(compare, NULL); + EINA_MODEL_TYPE_CALL_RETURN(model, child_sorted_iterator_get, NULL, + start, count, compare); +} + +EAPI Eina_Iterator * +eina_model_child_filtered_iterator_get(Eina_Model *model, Eina_Each_Cb match, const void *data) +{ + int count; + EINA_MODEL_INSTANCE_CHECK_VAL(model, NULL); + EINA_SAFETY_ON_NULL_RETURN_VAL(match, NULL); + count = eina_model_child_count(model); + if (count < 0) + return NULL; + EINA_MODEL_TYPE_CALL_RETURN(model, child_filtered_iterator_get, NULL, + 0, count, match, data); +} + +EAPI Eina_Iterator * +eina_model_child_slice_filtered_iterator_get(Eina_Model *model, unsigned int start, unsigned int count, Eina_Each_Cb match, const void *data) +{ + EINA_MODEL_INSTANCE_CHECK_VAL(model, NULL); + EINA_SAFETY_ON_NULL_RETURN_VAL(match, NULL); + EINA_MODEL_TYPE_CALL_RETURN(model, child_filtered_iterator_get, NULL, + start, count, match, data); +} + +EAPI char * +eina_model_to_string(const Eina_Model *model) +{ + EINA_MODEL_INSTANCE_CHECK_VAL(model, NULL); + EINA_MODEL_TYPE_CALL_RETURN(model, to_string, NULL); +} + +/* type functions *****************************************************/ + +EAPI Eina_Bool +eina_model_type_check(const Eina_Model_Type *type) +{ + EINA_SAFETY_ON_NULL_RETURN_VAL(type, EINA_FALSE); + return _eina_model_type_check(type); +} + +EAPI const char * +eina_model_type_name_get(const Eina_Model_Type *type) +{ + EINA_SAFETY_ON_NULL_RETURN_VAL(type, NULL); + EINA_SAFETY_ON_FALSE_RETURN_VAL(_eina_model_type_check(type), NULL); + return type->name; +} + +EAPI const Eina_Model_Type * +eina_model_type_parent_get(const Eina_Model_Type *type) +{ + EINA_SAFETY_ON_NULL_RETURN_VAL(type, NULL); + EINA_SAFETY_ON_FALSE_RETURN_VAL(_eina_model_type_check(type), NULL); + return type->parent; +} + +#define EINA_MODEL_TYPE_INSTANCE_CHECK(type, model) \ + EINA_SAFETY_ON_NULL_RETURN(type); \ + EINA_SAFETY_ON_FALSE_RETURN(_eina_model_type_check(type)); \ + EINA_MODEL_INSTANCE_CHECK(model); \ + EINA_SAFETY_ON_FALSE_RETURN(_eina_model_instance_check(model, type)); + +#define EINA_MODEL_TYPE_INSTANCE_CHECK_VAL(type, model, retval) \ + EINA_SAFETY_ON_NULL_RETURN_VAL(type, retval); \ + EINA_SAFETY_ON_FALSE_RETURN_VAL(_eina_model_type_check(type), retval); \ + EINA_MODEL_INSTANCE_CHECK_VAL(model, retval); \ + EINA_SAFETY_ON_FALSE_RETURN_VAL(_eina_model_instance_check(model, type), retval); + +EAPI Eina_Bool +eina_model_type_constructor(const Eina_Model_Type *type, Eina_Model *model) +{ + Eina_Bool (*constructor)(Eina_Model *); + + EINA_MODEL_TYPE_INSTANCE_CHECK_VAL(type, model, EINA_FALSE); + + constructor = _eina_model_type_find_offset + (type, offsetof(Eina_Model_Type, constructor)); + EINA_SAFETY_ON_NULL_RETURN_VAL(constructor, EINA_FALSE); + + return constructor(model); +} + +EAPI Eina_Bool +eina_model_type_destructor(const Eina_Model_Type *type, Eina_Model *model) +{ + Eina_Bool (*destructor)(Eina_Model *); + + EINA_MODEL_TYPE_INSTANCE_CHECK_VAL(type, model, EINA_FALSE); + + destructor = _eina_model_type_find_offset + (type, offsetof(Eina_Model_Type, destructor)); + EINA_SAFETY_ON_NULL_RETURN_VAL(destructor, EINA_FALSE); + + return destructor(model); +} + +EAPI Eina_Bool +eina_model_type_copy(const Eina_Model_Type *type, const Eina_Model *src, Eina_Model *dst) +{ + Eina_Bool (*copy)(const Eina_Model *, Eina_Model *); + + EINA_MODEL_TYPE_INSTANCE_CHECK_VAL(type, src, EINA_FALSE); + EINA_MODEL_TYPE_INSTANCE_CHECK_VAL(type, dst, EINA_FALSE); + + copy = _eina_model_type_find_offset + (type, offsetof(Eina_Model_Type, copy)); + EINA_SAFETY_ON_NULL_RETURN_VAL(copy, EINA_FALSE); + + return copy(src, dst); +} + +EAPI Eina_Bool +eina_model_type_deep_copy(const Eina_Model_Type *type, const Eina_Model *src, Eina_Model *dst) +{ + Eina_Bool (*deep_copy)(const Eina_Model *, Eina_Model *); + + EINA_MODEL_TYPE_INSTANCE_CHECK_VAL(type, src, EINA_FALSE); + EINA_MODEL_TYPE_INSTANCE_CHECK_VAL(type, dst, EINA_FALSE); + + deep_copy = _eina_model_type_find_offset + (type, offsetof(Eina_Model_Type, deep_copy)); + EINA_SAFETY_ON_NULL_RETURN_VAL(deep_copy, EINA_FALSE); + + return deep_copy(src, dst); +} + +EAPI Eina_Bool +eina_model_type_compare(const Eina_Model_Type *type, const Eina_Model *a, const Eina_Model *b, int *cmp) +{ + Eina_Bool (*compare)(const Eina_Model *, const Eina_Model *, int *); + + EINA_SAFETY_ON_NULL_RETURN_VAL(cmp, EINA_FALSE); + *cmp = 0; + + EINA_MODEL_TYPE_INSTANCE_CHECK_VAL(type, a, EINA_FALSE); + EINA_MODEL_TYPE_INSTANCE_CHECK_VAL(type, b, EINA_FALSE); + + compare = _eina_model_type_find_offset + (type, offsetof(Eina_Model_Type, compare)); + EINA_SAFETY_ON_NULL_RETURN_VAL(compare, EINA_FALSE); + + return compare(a, b, cmp); +} + +EAPI Eina_Bool +eina_model_type_load(const Eina_Model_Type *type, Eina_Model *model) +{ + Eina_Bool (*load)(Eina_Model *); + + EINA_MODEL_TYPE_INSTANCE_CHECK_VAL(type, model, EINA_FALSE); + + load = _eina_model_type_find_offset + (type, offsetof(Eina_Model_Type, load)); + EINA_SAFETY_ON_NULL_RETURN_VAL(load, EINA_FALSE); + + return load(model); +} + +EAPI Eina_Bool +eina_model_type_unload(const Eina_Model_Type *type, Eina_Model *model) +{ + Eina_Bool (*unload)(Eina_Model *); + + EINA_MODEL_TYPE_INSTANCE_CHECK_VAL(type, model, EINA_FALSE); + + unload = _eina_model_type_find_offset + (type, offsetof(Eina_Model_Type, unload)); + EINA_SAFETY_ON_NULL_RETURN_VAL(unload, EINA_FALSE); + + return unload(model); +} + +EAPI Eina_Bool +eina_model_type_property_get(const Eina_Model_Type *type, const Eina_Model *model, const char *name, Eina_Value *value) +{ + Eina_Bool (*property_get)(const Eina_Model *, const char *, Eina_Value *); + + EINA_SAFETY_ON_NULL_RETURN_VAL(name, EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(value, EINA_FALSE); + + EINA_MODEL_TYPE_INSTANCE_CHECK_VAL(type, model, EINA_FALSE); + + property_get = _eina_model_type_find_offset + (type, offsetof(Eina_Model_Type, property_get)); + EINA_SAFETY_ON_NULL_RETURN_VAL(property_get, EINA_FALSE); + + return property_get(model, name, value); +} + +EAPI Eina_Bool +eina_model_type_property_set(const Eina_Model_Type *type, Eina_Model *model, const char *name, const Eina_Value *value) +{ + Eina_Bool (*property_set)(Eina_Model *, const char *, const Eina_Value *); + + EINA_SAFETY_ON_NULL_RETURN_VAL(name, EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(value, EINA_FALSE); + EINA_SAFETY_ON_FALSE_RETURN_VAL(eina_value_type_check(value->type), EINA_FALSE); + + EINA_MODEL_TYPE_INSTANCE_CHECK_VAL(type, model, EINA_FALSE); + + property_set = _eina_model_type_find_offset + (type, offsetof(Eina_Model_Type, property_set)); + EINA_SAFETY_ON_NULL_RETURN_VAL(property_set, EINA_FALSE); + + return property_set(model, name, value); +} + +EAPI Eina_Bool +eina_model_type_property_del(const Eina_Model_Type *type, Eina_Model *model, const char *name) +{ + Eina_Bool (*property_del)(const Eina_Model *, const char *); + + EINA_SAFETY_ON_NULL_RETURN_VAL(name, EINA_FALSE); + EINA_MODEL_TYPE_INSTANCE_CHECK_VAL(type, model, EINA_FALSE); + + property_del = _eina_model_type_find_offset + (type, offsetof(Eina_Model_Type, property_del)); + EINA_SAFETY_ON_NULL_RETURN_VAL(property_del, EINA_FALSE); + + return property_del(model, name); +} + +EAPI Eina_List * +eina_model_type_properties_names_list_get(const Eina_Model_Type *type, const Eina_Model *model) +{ + Eina_List *(*properties_names_list_get)(const Eina_Model *); + + EINA_MODEL_TYPE_INSTANCE_CHECK_VAL(type, model, NULL); + + properties_names_list_get = _eina_model_type_find_offset + (type, offsetof(Eina_Model_Type, properties_names_list_get)); + EINA_SAFETY_ON_NULL_RETURN_VAL(properties_names_list_get, NULL); + + return properties_names_list_get(model); +} + +EAPI int +eina_model_type_child_count(const Eina_Model_Type *type, const Eina_Model *model) +{ + int (*child_count)(const Eina_Model *); + + EINA_MODEL_TYPE_INSTANCE_CHECK_VAL(type, model, -1); + + child_count = _eina_model_type_find_offset + (type, offsetof(Eina_Model_Type, child_count)); + EINA_SAFETY_ON_NULL_RETURN_VAL(child_count, -1); + + return child_count(model); +} + +EAPI Eina_Model * +eina_model_type_child_get(const Eina_Model_Type *type, const Eina_Model *model, unsigned int position) +{ + Eina_Model *(*child_get)(const Eina_Model *, unsigned int); + + EINA_MODEL_TYPE_INSTANCE_CHECK_VAL(type, model, NULL); + + child_get = _eina_model_type_find_offset + (type, offsetof(Eina_Model_Type, child_get)); + EINA_SAFETY_ON_NULL_RETURN_VAL(child_get, NULL); + + return child_get(model, position); +} + +EAPI Eina_Bool +eina_model_type_child_set(const Eina_Model_Type *type, Eina_Model *model, unsigned int position, Eina_Model *child) +{ + Eina_Bool (*child_set)(Eina_Model *, unsigned int, Eina_Model *); + + EINA_MODEL_TYPE_INSTANCE_CHECK_VAL(type, model, EINA_FALSE); + EINA_MODEL_INSTANCE_CHECK_VAL(child, EINA_FALSE); + + child_set = _eina_model_type_find_offset + (type, offsetof(Eina_Model_Type, child_set)); + EINA_SAFETY_ON_NULL_RETURN_VAL(child_set, EINA_FALSE); + + return child_set(model, position, child); +} + +EAPI Eina_Bool +eina_model_type_child_del(const Eina_Model_Type *type, Eina_Model *model, unsigned int position) +{ + Eina_Bool (*child_del)(Eina_Model *, unsigned int); + + EINA_MODEL_TYPE_INSTANCE_CHECK_VAL(type, model, EINA_FALSE); + + child_del = _eina_model_type_find_offset + (type, offsetof(Eina_Model_Type, child_del)); + EINA_SAFETY_ON_NULL_RETURN_VAL(child_del, EINA_FALSE); + + return child_del(model, position); +} + +EAPI Eina_Bool +eina_model_type_child_insert_at(const Eina_Model_Type *type, Eina_Model *model, unsigned int position, Eina_Model *child) +{ + Eina_Bool (*child_insert_at)(Eina_Model *, unsigned int, Eina_Model *); + + EINA_MODEL_TYPE_INSTANCE_CHECK_VAL(type, model, EINA_FALSE); + EINA_MODEL_INSTANCE_CHECK_VAL(child, EINA_FALSE); + + child_insert_at = _eina_model_type_find_offset + (type, offsetof(Eina_Model_Type, child_insert_at)); + EINA_SAFETY_ON_NULL_RETURN_VAL(child_insert_at, EINA_FALSE); + + return child_insert_at(model, position, child); +} + +EAPI int +eina_model_type_child_find(const Eina_Model_Type *type, const Eina_Model *model, unsigned int start_position, const Eina_Model *other) +{ + int (*child_find)(const Eina_Model *, unsigned int, const Eina_Model *); + + EINA_MODEL_TYPE_INSTANCE_CHECK_VAL(type, model, -1); + EINA_MODEL_INSTANCE_CHECK_VAL(other, -1); + + child_find = _eina_model_type_find_offset + (type, offsetof(Eina_Model_Type, child_find)); + EINA_SAFETY_ON_NULL_RETURN_VAL(child_find, -1); + + return child_find(model, start_position, other); +} + +EAPI int +eina_model_type_child_criteria_match(const Eina_Model_Type *type, const Eina_Model *model, unsigned int start_position, Eina_Each_Cb match, const void *data) +{ + int (*child_criteria_match)(const Eina_Model *, unsigned int, Eina_Each_Cb, const void *); + + EINA_SAFETY_ON_NULL_RETURN_VAL(match, -1); + EINA_MODEL_TYPE_INSTANCE_CHECK_VAL(type, model, -1); + + child_criteria_match = _eina_model_type_find_offset + (type, offsetof(Eina_Model_Type, child_criteria_match)); + EINA_SAFETY_ON_NULL_RETURN_VAL(child_criteria_match, -1); + + return child_criteria_match(model, start_position, match, data); +} + +EAPI void +eina_model_type_child_sort(const Eina_Model_Type *type, Eina_Model *model, Eina_Compare_Cb compare) +{ + void (*child_sort)(Eina_Model *, Eina_Compare_Cb); + + EINA_SAFETY_ON_NULL_RETURN(compare); + EINA_MODEL_TYPE_INSTANCE_CHECK(type, model); + + child_sort = _eina_model_type_find_offset + (type, offsetof(Eina_Model_Type, child_sort)); + EINA_SAFETY_ON_NULL_RETURN(child_sort); + + return child_sort(model, compare); +} + +EAPI Eina_Iterator * +eina_model_type_child_iterator_get(const Eina_Model_Type *type, Eina_Model *model, unsigned int start, unsigned int count) +{ + Eina_Iterator *(*child_iterator_get)(const Eina_Model *, unsigned int, unsigned int); + + EINA_MODEL_TYPE_INSTANCE_CHECK_VAL(type, model, NULL); + + child_iterator_get = _eina_model_type_find_offset + (type, offsetof(Eina_Model_Type, child_iterator_get)); + EINA_SAFETY_ON_NULL_RETURN_VAL(child_iterator_get, NULL); + + return child_iterator_get(model, start, count); +} + +EAPI Eina_Iterator * +eina_model_type_child_reversed_iterator_get(const Eina_Model_Type *type, Eina_Model *model, unsigned int start, unsigned int count) +{ + Eina_Iterator *(*child_reversed_iterator_get)(const Eina_Model *, unsigned int, unsigned int); + + EINA_MODEL_TYPE_INSTANCE_CHECK_VAL(type, model, NULL); + + child_reversed_iterator_get = _eina_model_type_find_offset + (type, offsetof(Eina_Model_Type, child_reversed_iterator_get)); + EINA_SAFETY_ON_NULL_RETURN_VAL(child_reversed_iterator_get, NULL); + + return child_reversed_iterator_get(model, start, count); +} + +EAPI Eina_Iterator * +eina_model_type_child_sorted_iterator_get(const Eina_Model_Type *type, Eina_Model *model, unsigned int start, unsigned int count, Eina_Compare_Cb compare) +{ + Eina_Iterator *(*child_sorted_iterator_get)(const Eina_Model *, unsigned int, unsigned int, Eina_Compare_Cb); + + EINA_SAFETY_ON_NULL_RETURN_VAL(compare, NULL); + EINA_MODEL_TYPE_INSTANCE_CHECK_VAL(type, model, NULL); + + child_sorted_iterator_get = _eina_model_type_find_offset + (type, offsetof(Eina_Model_Type, child_sorted_iterator_get)); + EINA_SAFETY_ON_NULL_RETURN_VAL(child_sorted_iterator_get, NULL); + + return child_sorted_iterator_get(model, start, count, compare); +} + +EAPI Eina_Iterator * +eina_model_type_child_filtered_iterator_get(const Eina_Model_Type *type, Eina_Model *model, unsigned int start, unsigned int count, Eina_Each_Cb match, const void *data) +{ + Eina_Iterator *(*child_filtered_iterator_get)(const Eina_Model *, unsigned int, unsigned int, Eina_Each_Cb, const void *); + + EINA_SAFETY_ON_NULL_RETURN_VAL(match, NULL); + EINA_MODEL_TYPE_INSTANCE_CHECK_VAL(type, model, NULL); + + child_filtered_iterator_get = _eina_model_type_find_offset + (type, offsetof(Eina_Model_Type, child_filtered_iterator_get)); + EINA_SAFETY_ON_NULL_RETURN_VAL(child_filtered_iterator_get, NULL); + + return child_filtered_iterator_get(model, start, count, match, data); +} + +EAPI char * +eina_model_type_to_string(const Eina_Model_Type *type, const Eina_Model *model) +{ + char *(*to_string)(const Eina_Model *); + + EINA_MODEL_TYPE_INSTANCE_CHECK_VAL(type, model, NULL); + + to_string = _eina_model_type_find_offset + (type, offsetof(Eina_Model_Type, to_string)); + EINA_SAFETY_ON_NULL_RETURN_VAL(to_string, NULL); + + return to_string(model); +} + +EAPI Eina_Bool +eina_model_type_subclass_setup(Eina_Model_Type *type, const Eina_Model_Type *parent) +{ + EINA_SAFETY_ON_NULL_RETURN_VAL(type, EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(parent, EINA_FALSE); + EINA_SAFETY_ON_FALSE_RETURN_VAL(_eina_model_type_check(parent), EINA_FALSE); + EINA_SAFETY_ON_FALSE_RETURN_VAL(type->version == EINA_MODEL_TYPE_VERSION, + EINA_FALSE); + + type->parent = parent; + type->type_size = parent->type_size; + type->interfaces = NULL; + type->events = NULL; + + type->setup = NULL; + type->flush = NULL; + type->constructor = NULL; + type->destructor = NULL; + type->copy = NULL; + type->deep_copy = NULL; + type->compare = NULL; + type->load = NULL; + type->unload = NULL; + type->property_get = NULL; + type->property_set = NULL; + type->property_del = NULL; + type->properties_names_list_get = NULL; + type->child_count = NULL; + type->child_get = NULL; + type->child_set = NULL; + type->child_del = NULL; + type->child_insert_at = NULL; + type->child_find = NULL; + type->child_criteria_match = NULL; + type->child_sort = NULL; + type->child_iterator_get = NULL; + type->child_reversed_iterator_get = NULL; + type->child_sorted_iterator_get = NULL; + type->child_filtered_iterator_get = NULL; + type->to_string = NULL; + type->__extension_ptr0 = NULL; + type->__extension_ptr1 = NULL; + type->__extension_ptr2 = NULL; + type->__extension_ptr3 = NULL; + + if (type->type_size > sizeof(Eina_Model_Type)) + { + unsigned char *p = (unsigned char *)type; + p += sizeof(Eina_Model_Type); + memset(p, 0, type->type_size - sizeof(Eina_Model_Type)); + } + + return EINA_TRUE; +} + +EAPI Eina_Bool +eina_model_type_subclass_check(const Eina_Model_Type *type, const Eina_Model_Type *self_or_parent) +{ + EINA_SAFETY_ON_NULL_RETURN_VAL(type, EINA_FALSE); + EINA_SAFETY_ON_FALSE_RETURN_VAL(_eina_model_type_check(type), EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(self_or_parent, EINA_FALSE); + + for (; type != NULL; type = type->parent) + { + if (type == self_or_parent) + return EINA_TRUE; + } + + return EINA_FALSE; +} + +static inline const Eina_Model_Interface * +_eina_model_type_interface_get(const Eina_Model_Type *type, const char *name, Eina_Bool ptr_cmp __UNUSED__) +{ + const Eina_Model_Interface **itr; + + if (!type) + return NULL; + + if (!type->interfaces) + return _eina_model_type_interface_get(type->parent, name, ptr_cmp); + + { + for (itr = type->interfaces ; itr != NULL ; itr++) + if (strcmp((*itr)->name, name) == 0) + return *itr; + } + + return NULL; +} + +static inline Eina_Bool +_eina_model_interface_check(const Eina_Model_Interface *iface) +{ + EINA_SAFETY_ON_FALSE_RETURN_VAL + (iface->version == EINA_MODEL_INTERFACE_VERSION, EINA_FALSE); + return EINA_TRUE; +} + +EAPI const Eina_Model_Interface * +eina_model_type_interface_get(const Eina_Model_Type *type, const char *name) +{ + const Eina_Model_Interface *iface; + + EINA_SAFETY_ON_NULL_RETURN_VAL(type, NULL); + EINA_SAFETY_ON_FALSE_RETURN_VAL(_eina_model_type_check(type), NULL); + + /* search for pointer, make speed-aware users fast */ + iface = _eina_model_type_interface_get(type, name, EINA_TRUE); + + if (!iface) + { + /* search using strcmp(), slow users don't care */ + iface = _eina_model_type_interface_get(type, name, EINA_FALSE); + } + else if (!_eina_model_interface_check(iface)) + iface = NULL; + + return iface; +} + +EAPI void * +eina_model_type_private_data_get(const Eina_Model *model, const Eina_Model_Type *type) +{ + const Eina_Model_Description *desc; + unsigned int i; + + EINA_MODEL_INSTANCE_CHECK_VAL(model, NULL); + EINA_SAFETY_ON_NULL_RETURN_VAL(type, NULL); + EINA_SAFETY_ON_FALSE_RETURN_VAL(_eina_model_type_check(type), NULL); + + desc = model->desc; + + for (i = 0; i < desc->total.types; i++) + if (desc->cache.types[i] == type) + return model->privates[i]; + + CRITICAL("Model %p (%s) is not an instance of type %p (%s)", + model, desc->cache.types[0]->name, + type, type->name); + return NULL; +} + +EAPI const void * +eina_model_method_offset_resolve(const Eina_Model *model, unsigned int offset) +{ + const Eina_Model_Description *desc; + + EINA_MODEL_INSTANCE_CHECK_VAL(model, NULL); + EINA_SAFETY_ON_FALSE_RETURN_VAL(offset >= sizeof(Eina_Model_Type), NULL); + EINA_SAFETY_ON_FALSE_RETURN_VAL(offset % sizeof(void *) == 0, NULL); + + desc = model->desc; + EINA_SAFETY_ON_FALSE_RETURN_VAL + (offset + sizeof(void *) <= desc->cache.types[0]->type_size, NULL); + + offset -= sizeof(Eina_Model_Type); + offset /= sizeof(void *); + return desc->ops.type.extension[offset]; +} + +EAPI const void * +eina_model_type_method_offset_resolve(const Eina_Model_Type *type, const Eina_Model *model, unsigned int offset) +{ + const Eina_Model_Description *desc; + + EINA_MODEL_TYPE_INSTANCE_CHECK_VAL(type, model, NULL); + EINA_SAFETY_ON_FALSE_RETURN_VAL(offset >= sizeof(Eina_Model_Type), NULL); + EINA_SAFETY_ON_FALSE_RETURN_VAL(offset % sizeof(void *) == 0, NULL); + + desc = model->desc; + EINA_SAFETY_ON_FALSE_RETURN_VAL + (offset + sizeof(void *) <= desc->cache.types[0]->type_size, NULL); + + return _eina_model_type_find_offset(type, offset); +} + +/* interface functions ************************************************/ + +EAPI Eina_Bool +eina_model_interface_check(const Eina_Model_Interface *iface) +{ + EINA_SAFETY_ON_NULL_RETURN_VAL(iface, EINA_FALSE); + return _eina_model_interface_check(iface); +} + +EAPI void * +eina_model_interface_private_data_get(const Eina_Model *model, const Eina_Model_Interface *iface) +{ + const Eina_Model_Description *desc; + unsigned int i; + + EINA_MODEL_INSTANCE_CHECK_VAL(model, NULL); + EINA_SAFETY_ON_NULL_RETURN_VAL(iface, NULL); + EINA_SAFETY_ON_FALSE_RETURN_VAL(_eina_model_interface_check(iface), NULL); + + desc = model->desc; + + for (i = 0; i < desc->total.ifaces; i++) + if (desc->cache.ifaces[i] == iface) + return model->privates[desc->total.types + i]; + + CRITICAL("Model %p (%s) does not implement interface %p (%s)", + model, desc->cache.types[0]->name, + iface, iface->name); + return NULL; +} + +static Eina_Bool +_eina_model_interface_implemented(const Eina_Model *model, const Eina_Model_Interface *iface) +{ + const Eina_Model_Interface **itr, **itr_end; + + itr = model->desc->cache.ifaces; + itr_end = itr + model->desc->total.ifaces; + + for (; itr < itr_end; itr++) + if (*itr == iface) + return EINA_TRUE; + + return EINA_FALSE; +} + +EAPI Eina_Bool +eina_model_interface_implemented(const Eina_Model *model, const Eina_Model_Interface *iface) +{ + EINA_MODEL_INSTANCE_CHECK_VAL(model, EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(iface, EINA_FALSE); + EINA_SAFETY_ON_FALSE_RETURN_VAL(_eina_model_interface_check(iface), + EINA_FALSE); + return _eina_model_interface_implemented(model, iface); +} + +#define EINA_MODEL_INTERFACE_IMPLEMENTED_CHECK(iface, model) \ + EINA_SAFETY_ON_NULL_RETURN(iface); \ + EINA_SAFETY_ON_FALSE_RETURN(_eina_model_interface_check(iface)); \ + EINA_MODEL_INSTANCE_CHECK(model); \ + EINA_SAFETY_ON_FALSE_RETURN(_eina_model_interface_implemented(model, iface)); + +#define EINA_MODEL_INTERFACE_IMPLEMENTED_CHECK_VAL(iface, model, retval) \ + EINA_SAFETY_ON_NULL_RETURN_VAL(iface, retval); \ + EINA_SAFETY_ON_FALSE_RETURN_VAL(_eina_model_interface_check(iface), retval); \ + EINA_MODEL_INSTANCE_CHECK_VAL(model, retval); \ + EINA_SAFETY_ON_FALSE_RETURN_VAL(_eina_model_interface_implemented(model, iface), retval); + + +EAPI Eina_Bool +eina_model_interface_constructor(const Eina_Model_Interface *iface, Eina_Model *model) +{ + Eina_Bool (*constructor)(Eina_Model *); + + EINA_MODEL_INTERFACE_IMPLEMENTED_CHECK_VAL(iface, model, EINA_FALSE); + + constructor = _eina_model_interface_find_offset + (iface, offsetof(Eina_Model_Interface, constructor)); + EINA_SAFETY_ON_NULL_RETURN_VAL(constructor, EINA_FALSE); + return constructor(model); +} + +EAPI Eina_Bool +eina_model_interface_destructor(const Eina_Model_Interface *iface, Eina_Model *model) +{ + Eina_Bool (*destructor)(Eina_Model *); + + EINA_MODEL_INTERFACE_IMPLEMENTED_CHECK_VAL(iface, model, EINA_FALSE); + + destructor = _eina_model_interface_find_offset + (iface, offsetof(Eina_Model_Interface, destructor)); + EINA_SAFETY_ON_NULL_RETURN_VAL(destructor, EINA_FALSE); + return destructor(model); +} + +EAPI Eina_Bool +eina_model_interface_copy(const Eina_Model_Interface *iface, const Eina_Model *src, Eina_Model *dst) +{ + Eina_Bool (*copy)(const Eina_Model *, Eina_Model *); + + EINA_MODEL_INTERFACE_IMPLEMENTED_CHECK_VAL(iface, src, EINA_FALSE); + EINA_MODEL_INTERFACE_IMPLEMENTED_CHECK_VAL(iface, dst, EINA_FALSE); + + copy = _eina_model_interface_find_offset + (iface, offsetof(Eina_Model_Interface, copy)); + EINA_SAFETY_ON_NULL_RETURN_VAL(copy, EINA_FALSE); + return copy(src, dst); +} + +EAPI Eina_Bool +eina_model_interface_deep_copy(const Eina_Model_Interface *iface, const Eina_Model *src, Eina_Model *dst) +{ + Eina_Bool (*deep_copy)(const Eina_Model *, Eina_Model *); + + EINA_MODEL_INTERFACE_IMPLEMENTED_CHECK_VAL(iface, src, EINA_FALSE); + EINA_MODEL_INTERFACE_IMPLEMENTED_CHECK_VAL(iface, dst, EINA_FALSE); + + deep_copy = _eina_model_interface_find_offset + (iface, offsetof(Eina_Model_Interface, deep_copy)); + EINA_SAFETY_ON_NULL_RETURN_VAL(deep_copy, EINA_FALSE); + return deep_copy(src, dst); +} + +EAPI const void +*eina_model_interface_method_offset_resolve(const Eina_Model_Interface *iface, const Eina_Model *model, unsigned int offset) +{ + EINA_MODEL_INTERFACE_IMPLEMENTED_CHECK_VAL(iface, model, NULL); + EINA_SAFETY_ON_FALSE_RETURN_VAL(offset >= sizeof(Eina_Model_Interface), NULL); + EINA_SAFETY_ON_FALSE_RETURN_VAL(offset % sizeof(void *) == 0, NULL); + return _eina_model_interface_find_offset(iface, offset); +} + + +/* Eina_Model_Interface_Properties ************************************/ + +EAPI Eina_Bool +eina_model_interface_properties_compare(const Eina_Model_Interface *iface, const Eina_Model *a, const Eina_Model *b, int *cmp) +{ + Eina_Bool (*compare)(const Eina_Model *, const Eina_Model *, int *cmp); + + EINA_SAFETY_ON_NULL_RETURN_VAL(cmp, EINA_FALSE); + + *cmp = 0; + EINA_MODEL_INTERFACE_IMPLEMENTED_CHECK_VAL(iface, a, EINA_FALSE); + EINA_MODEL_INTERFACE_IMPLEMENTED_CHECK_VAL(iface, b, EINA_FALSE); + + compare = _eina_model_interface_find_offset + (iface, offsetof(Eina_Model_Interface_Properties, compare)); + EINA_SAFETY_ON_NULL_RETURN_VAL(compare, EINA_FALSE); + return compare(a, b, cmp); +} + +EAPI Eina_Bool +eina_model_interface_properties_load(const Eina_Model_Interface *iface, Eina_Model *model) +{ + Eina_Bool (*load)(Eina_Model *); + Eina_Bool ret; + + EINA_MODEL_INTERFACE_IMPLEMENTED_CHECK_VAL(iface, model, EINA_FALSE); + + load = _eina_model_interface_find_offset + (iface, offsetof(Eina_Model_Interface_Properties, load)); + EINA_SAFETY_ON_NULL_RETURN_VAL(load, EINA_FALSE); + ret = load(model); + + if (ret) + _eina_model_event_callback_call + (model, _eina_model_str_properties_loaded, NULL); + + return ret; +} + +EAPI Eina_Bool +eina_model_interface_properties_unload(const Eina_Model_Interface *iface, Eina_Model *model) +{ + Eina_Bool (*unload)(Eina_Model *); + Eina_Bool ret; + + EINA_MODEL_INTERFACE_IMPLEMENTED_CHECK_VAL(iface, model, EINA_FALSE); + + unload = _eina_model_interface_find_offset + (iface, offsetof(Eina_Model_Interface_Properties, unload)); + EINA_SAFETY_ON_NULL_RETURN_VAL(unload, EINA_FALSE); + ret = unload(model); + + if (ret) + _eina_model_event_callback_call + (model, _eina_model_str_properties_unloaded, NULL); + + return ret; +} + +EAPI Eina_Bool +eina_model_interface_properties_get(const Eina_Model_Interface *iface, const Eina_Model *model, const char *name, Eina_Value *value) +{ + Eina_Bool (*get)(const Eina_Model *, const char *, Eina_Value *); + + EINA_SAFETY_ON_NULL_RETURN_VAL(name, EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(value, EINA_FALSE); + EINA_MODEL_INTERFACE_IMPLEMENTED_CHECK_VAL(iface, model, EINA_FALSE); + + get = _eina_model_interface_find_offset + (iface, offsetof(Eina_Model_Interface_Properties, get)); + EINA_SAFETY_ON_NULL_RETURN_VAL(get, EINA_FALSE); + return get(model, name, value); +} + +EAPI Eina_Bool +eina_model_interface_properties_set(const Eina_Model_Interface *iface, Eina_Model *model, const char *name, const Eina_Value *value) +{ + Eina_Bool (*set)(Eina_Model *, const char *, const Eina_Value *); + + EINA_SAFETY_ON_NULL_RETURN_VAL(name, EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(value, EINA_FALSE); + EINA_SAFETY_ON_FALSE_RETURN_VAL(eina_value_type_check(value->type), EINA_FALSE); + EINA_MODEL_INTERFACE_IMPLEMENTED_CHECK_VAL(iface, model, EINA_FALSE); + + set = _eina_model_interface_find_offset + (iface, offsetof(Eina_Model_Interface_Properties, set)); + EINA_SAFETY_ON_NULL_RETURN_VAL(set, EINA_FALSE); + return set(model, name, value); +} + +EAPI Eina_Bool +eina_model_interface_properties_del(const Eina_Model_Interface *iface, Eina_Model *model, const char *name) +{ + Eina_Bool (*del)(Eina_Model *, const char *); + + EINA_SAFETY_ON_NULL_RETURN_VAL(name, EINA_FALSE); + EINA_MODEL_INTERFACE_IMPLEMENTED_CHECK_VAL(iface, model, EINA_FALSE); + + del = _eina_model_interface_find_offset + (iface, offsetof(Eina_Model_Interface_Properties, del)); + EINA_SAFETY_ON_NULL_RETURN_VAL(del, EINA_FALSE); + return del(model, name); +} + +EAPI Eina_List * +eina_model_interface_properties_names_list_get(const Eina_Model_Interface *iface, const Eina_Model *model) +{ + Eina_List *(*names_list_get)(const Eina_Model *); + + EINA_MODEL_INTERFACE_IMPLEMENTED_CHECK_VAL(iface, model, NULL); + + names_list_get = _eina_model_interface_find_offset + (iface, offsetof(Eina_Model_Interface_Properties, names_list_get)); + EINA_SAFETY_ON_NULL_RETURN_VAL(names_list_get, NULL); + return names_list_get(model); +} + +/* Eina_Model_Interface_Children **************************************/ + +EAPI Eina_Bool +eina_model_interface_children_compare(const Eina_Model_Interface *iface, const Eina_Model *a, const Eina_Model *b, int *cmp) +{ + Eina_Bool (*compare)(const Eina_Model *, const Eina_Model *, int *); + + EINA_SAFETY_ON_NULL_RETURN_VAL(cmp, EINA_FALSE); + + *cmp = 0; + + EINA_MODEL_INTERFACE_IMPLEMENTED_CHECK_VAL(iface, a, EINA_FALSE); + EINA_MODEL_INTERFACE_IMPLEMENTED_CHECK_VAL(iface, b, EINA_FALSE); + + compare = _eina_model_interface_find_offset + (iface, offsetof(Eina_Model_Interface_Children, compare)); + EINA_SAFETY_ON_NULL_RETURN_VAL(compare, EINA_FALSE); + return compare(a, b, cmp); +} + +EAPI Eina_Bool +eina_model_interface_children_load(const Eina_Model_Interface *iface, Eina_Model *model) +{ + Eina_Bool (*load)(Eina_Model *); + Eina_Bool ret; + + EINA_MODEL_INTERFACE_IMPLEMENTED_CHECK_VAL(iface, model, EINA_FALSE); + + load = _eina_model_interface_find_offset + (iface, offsetof(Eina_Model_Interface_Children, load)); + EINA_SAFETY_ON_NULL_RETURN_VAL(load, EINA_FALSE); + ret = load(model); + + if (ret) + _eina_model_event_callback_call + (model, _eina_model_str_children_loaded, NULL); + + return ret; +} + +EAPI Eina_Bool +eina_model_interface_children_unload(const Eina_Model_Interface *iface, Eina_Model *model) +{ + Eina_Bool (*unload)(Eina_Model *); + Eina_Bool ret; + + EINA_MODEL_INTERFACE_IMPLEMENTED_CHECK_VAL(iface, model, EINA_FALSE); + + unload = _eina_model_interface_find_offset + (iface, offsetof(Eina_Model_Interface_Children, unload)); + EINA_SAFETY_ON_NULL_RETURN_VAL(unload, EINA_FALSE); + ret = unload(model); + + if (ret) + _eina_model_event_callback_call + (model, _eina_model_str_children_unloaded, NULL); + + return ret; +} + +EAPI int +eina_model_interface_children_count(const Eina_Model_Interface *iface, const Eina_Model *model) +{ + int (*count)(const Eina_Model *); + + EINA_MODEL_INTERFACE_IMPLEMENTED_CHECK_VAL(iface, model, -1); + + count = _eina_model_interface_find_offset + (iface, offsetof(Eina_Model_Interface_Children, count)); + EINA_SAFETY_ON_NULL_RETURN_VAL(count, -1); + return count(model); +} + +EAPI Eina_Model * +eina_model_interface_children_get(const Eina_Model_Interface *iface, const Eina_Model *model, unsigned int position) +{ + Eina_Model *(*get)(const Eina_Model *, unsigned int); + + EINA_MODEL_INTERFACE_IMPLEMENTED_CHECK_VAL(iface, model, NULL); + + get = _eina_model_interface_find_offset + (iface, offsetof(Eina_Model_Interface_Children, get)); + EINA_SAFETY_ON_NULL_RETURN_VAL(get, NULL); + return get(model, position); +} + +EAPI Eina_Bool eina_model_interface_children_set(const Eina_Model_Interface *iface, Eina_Model *model, unsigned int position, Eina_Model *child) +{ + Eina_Bool (*set)(const Eina_Model *, unsigned int, Eina_Model *); + + EINA_MODEL_INTERFACE_IMPLEMENTED_CHECK_VAL(iface, model, EINA_FALSE); + EINA_MODEL_INSTANCE_CHECK_VAL(child, EINA_FALSE); + + set = _eina_model_interface_find_offset + (iface, offsetof(Eina_Model_Interface_Children, set)); + EINA_SAFETY_ON_NULL_RETURN_VAL(set, EINA_FALSE); + return set(model, position, child); +} + +EAPI Eina_Bool +eina_model_interface_children_del(const Eina_Model_Interface *iface, Eina_Model *model, unsigned int position) +{ + Eina_Bool (*del)(Eina_Model *, unsigned int); + + EINA_MODEL_INTERFACE_IMPLEMENTED_CHECK_VAL(iface, model, EINA_FALSE); + + del = _eina_model_interface_find_offset + (iface, offsetof(Eina_Model_Interface_Children, del)); + EINA_SAFETY_ON_NULL_RETURN_VAL(del, EINA_FALSE); + return del(model, position); +} + + +EAPI Eina_Bool +eina_model_interface_children_insert_at(const Eina_Model_Interface *iface, Eina_Model *model, unsigned int position, Eina_Model *child) +{ + Eina_Bool (*insert_at)(const Eina_Model *, unsigned int, Eina_Model *); + + EINA_MODEL_INTERFACE_IMPLEMENTED_CHECK_VAL(iface, model, EINA_FALSE); + EINA_MODEL_INSTANCE_CHECK_VAL(child, EINA_FALSE); + + insert_at = _eina_model_interface_find_offset + (iface, offsetof(Eina_Model_Interface_Children, insert_at)); + EINA_SAFETY_ON_NULL_RETURN_VAL(insert_at, EINA_FALSE); + return insert_at(model, position, child); +} + +EAPI void +eina_model_interface_children_sort(const Eina_Model_Interface *iface, Eina_Model *model, Eina_Compare_Cb compare) +{ + void (*sort)(const Eina_Model *, Eina_Compare_Cb); + + EINA_SAFETY_ON_NULL_RETURN(compare); + EINA_MODEL_INTERFACE_IMPLEMENTED_CHECK(iface, model); + + sort = _eina_model_interface_find_offset + (iface, offsetof(Eina_Model_Interface_Children, sort)); + EINA_SAFETY_ON_NULL_RETURN(sort); + return sort(model, compare); +} + +static Eina_Bool +_eina_model_struct_set(Eina_Model *m, const Eina_Value_Struct_Desc *desc, void *memory) +{ + Eina_Value_Struct st = {desc, memory}; + Eina_Value *val = eina_model_interface_private_data_get + (m, &_EINA_MODEL_INTERFACE_PROPERTIES_STRUCT.base); + return eina_value_pset(val, &st); +} + +EAPI Eina_Model * +eina_model_struct_new(const Eina_Value_Struct_Desc *desc) +{ + Eina_Model *m; + + EINA_SAFETY_ON_NULL_RETURN_VAL(desc, NULL); + EINA_SAFETY_ON_FALSE_RETURN_VAL + (desc->version == EINA_VALUE_STRUCT_DESC_VERSION, NULL); + + m = eina_model_new(EINA_MODEL_TYPE_STRUCT); + EINA_SAFETY_ON_NULL_RETURN_VAL(m, NULL); + + EINA_SAFETY_ON_FALSE_GOTO(_eina_model_struct_set(m, desc, NULL), error); + return m; + + error: + eina_model_del(m); + return NULL; +} + +EAPI Eina_Model * +eina_model_type_struct_new(const Eina_Model_Type *type, const Eina_Value_Struct_Desc *desc) +{ + Eina_Model *m; + + EINA_SAFETY_ON_FALSE_RETURN_VAL + (eina_model_type_subclass_check(type, EINA_MODEL_TYPE_STRUCT), NULL); + EINA_SAFETY_ON_NULL_RETURN_VAL(desc, NULL); + EINA_SAFETY_ON_FALSE_RETURN_VAL + (desc->version == EINA_VALUE_STRUCT_DESC_VERSION, NULL); + + m = eina_model_new(type); + EINA_SAFETY_ON_NULL_RETURN_VAL(m, NULL); + + EINA_SAFETY_ON_FALSE_GOTO(_eina_model_struct_set(m, desc, NULL), error); + return m; + + error: + eina_model_del(m); + return NULL; +} + +EAPI Eina_Bool +eina_model_struct_set(Eina_Model *model, const Eina_Value_Struct_Desc *desc, void *memory) +{ + EINA_SAFETY_ON_NULL_RETURN_VAL(desc, EINA_FALSE); + EINA_SAFETY_ON_FALSE_RETURN_VAL + (desc->version == EINA_VALUE_STRUCT_DESC_VERSION, EINA_FALSE); + EINA_MODEL_INTERFACE_IMPLEMENTED_CHECK_VAL + (&_EINA_MODEL_INTERFACE_PROPERTIES_STRUCT.base, model, EINA_FALSE); + + return _eina_model_struct_set(model, desc, memory); +} + +EAPI Eina_Bool +eina_model_struct_get(const Eina_Model *model, const Eina_Value_Struct_Desc **p_desc, void **p_memory) +{ + const Eina_Value *val; + Eina_Value_Struct st; + + EINA_SAFETY_ON_NULL_RETURN_VAL(p_desc, EINA_FALSE); + + *p_desc = NULL; + if (p_memory) *p_memory = NULL; + + EINA_MODEL_INTERFACE_IMPLEMENTED_CHECK_VAL + (&_EINA_MODEL_INTERFACE_PROPERTIES_STRUCT.base, model, EINA_FALSE); + + val = eina_model_interface_private_data_get + (model, &_EINA_MODEL_INTERFACE_PROPERTIES_STRUCT.base); + + EINA_SAFETY_ON_FALSE_RETURN_VAL(eina_value_pget(val, &st), EINA_FALSE); + + *p_desc = st.desc; + if (p_memory) *p_memory = st.memory; + return EINA_FALSE; +} + +EAPI void +eina_models_usage_dump(void) +{ + const Eina_List *l; + const Eina_Model *m; + + eina_lock_take(&_eina_model_debug_list_lock); + + puts("DDD: model refs info (type, holders, backtrace)"); + puts("DDD: -------------- -------------- ---------------------------------"); + + EINA_LIST_FOREACH(_eina_model_debug_list, l, m) + { + Eina_Model_XRef *ref; + + printf("DDD: %14p %14d %s\n", + m, m->refcount, m->desc->cache.types[0]->name); + + EINA_INLIST_FOREACH(m->xrefs, ref) + { + printf("DDD: id: %p '%s'\n", + ref->id, ref->label); + if (ref->backtrace.count) + { + char **symbols; + unsigned int i; + +#ifdef HAVE_BACKTRACE_SYMBOLS + symbols = backtrace_symbols((void * const *)ref->backtrace.symbols, + ref->backtrace.count); +#else + symbols = NULL; +#endif + + printf("DDD: Backtrace: Address Symbol\n"); + for (i = 0; i < ref->backtrace.count; i++) + printf("DDD: %14p %s\n", + ref->backtrace.symbols[i], + symbols ? symbols[i] : "???"); + + free(symbols); + puts("DDD:"); + } + } + } + + eina_lock_release(&_eina_model_debug_list_lock); +} + +EAPI Eina_List * +eina_models_list_get(void) +{ + const Eina_List *l; + Eina_Model *m; + Eina_List *ret = NULL; + + eina_lock_take(&_eina_model_debug_list_lock); + + EINA_LIST_FOREACH(_eina_model_debug_list, l, m) + { + ret = eina_list_append + (ret, eina_model_xref + (m, eina_models_list_get, "eina_models_list_get")); + } + + eina_lock_release(&_eina_model_debug_list_lock); + + return ret; +} + +EAPI void +eina_models_list_free(Eina_List *list) +{ + Eina_Model *m; + + EINA_LIST_FREE(list, m) + eina_model_xunref(m, eina_models_list_get); +} diff --git a/libraries/eina/src/tests/eina_test_model.c b/libraries/eina/src/tests/eina_test_model.c new file mode 100644 index 0000000..54a2258 --- /dev/null +++ b/libraries/eina/src/tests/eina_test_model.c @@ -0,0 +1,1288 @@ +/* EINA - EFL data type library + * Copyright (C) 2012 ProFUSION embedded systems + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; + * if not, see . + */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include +#include + +#include "eina_suite.h" +#include "Eina.h" + +static void +_eina_test_model_check_safety_null(const Eina_Log_Domain *d, Eina_Log_Level level, const char *file, const char *fnc, int line, const char *fmt, void *data, va_list args) +{ + Eina_Bool *ck = data; + + if ((level == EINA_LOG_LEVEL_ERR) && (strcmp(fmt, "%s") == 0)) + { + const char *str; + va_list cp_args; + + va_copy(cp_args, args); + str = va_arg(cp_args, const char *); + va_end(cp_args); + if (eina_str_has_prefix(str, "safety check failed: ") && + eina_str_has_suffix(str, " == NULL")) + { + *ck = EINA_TRUE; + return; + } + } + *ck = EINA_FALSE; + eina_log_print_cb_stderr(d, level, file, fnc, line, fmt, NULL, args); +} + +static void +_eina_test_model_check_safety_false(const Eina_Log_Domain *d, Eina_Log_Level level, const char *file, const char *fnc, int line, const char *fmt, void *data, va_list args) +{ + Eina_Bool *ck = data; + + if ((level == EINA_LOG_LEVEL_ERR) && (strcmp(fmt, "%s") == 0)) + { + const char *str; + va_list cp_args; + + va_copy(cp_args, args); + str = va_arg(cp_args, const char *); + va_end(cp_args); + if (eina_str_has_prefix(str, "safety check failed: ") && + eina_str_has_suffix(str, " is false")) + { + *ck = EINA_TRUE; + return; + } + } + *ck = EINA_FALSE; + eina_log_print_cb_stderr(d, level, file, fnc, line, fmt, NULL, args); +} + +static void +_eina_test_model_cb_count(void *data, Eina_Model *model, const Eina_Model_Event_Description *desc, void *event_info) +{ + unsigned *count = data; + (*count)++; +#if SHOW_LOG + if ((desc->type) && (strcmp(desc->type, "u") == 0)) + { + unsigned *pos = event_info; + printf("%2u %p %s at %u\n", *count, model, desc->name, *pos); + } + else + printf("%2u %p %s\n", *count, model, desc->name); +#else + (void)model; + (void)desc; + (void)event_info; +#endif +} + +START_TEST(eina_model_test_properties) +{ + unsigned int count_del = 0, count_pset = 0, count_pdel = 0; + Eina_Model *m; + Eina_Value inv, outv; + int i; + char *s; + Eina_List *lst; + Eina_Bool ck; + + eina_init(); + + m = eina_model_new(EINA_MODEL_TYPE_GENERIC); + fail_unless(m != NULL); + + eina_model_event_callback_add + (m, "deleted", _eina_test_model_cb_count, &count_del); + eina_model_event_callback_add + (m, "property,set", _eina_test_model_cb_count, &count_pset); + eina_model_event_callback_add + (m, "property,deleted", _eina_test_model_cb_count, &count_pdel); + + fail_unless(eina_value_setup(&inv, EINA_VALUE_TYPE_INT)); + fail_unless(eina_value_set(&inv, 1234)); + fail_unless(eina_value_get(&inv, &i)); + ck_assert_int_eq(i, 1234); + + fail_unless(eina_model_property_set(m, "abc", &inv)); + + fail_unless(eina_value_set(&inv, 5678)); + fail_unless(eina_model_property_set(m, "xyz", &inv)); + + fail_unless(eina_value_set(&inv, 171)); + fail_unless(eina_model_property_set(m, "value", &inv)); + + lst = eina_model_properties_names_list_get(m); + ck_assert_int_eq(eina_list_count(lst), 3); + + lst = eina_list_sort(lst, 0, EINA_COMPARE_CB(strcmp)); + ck_assert_str_eq("abc", eina_list_nth(lst, 0)); + ck_assert_str_eq("value", eina_list_nth(lst, 1)); + ck_assert_str_eq("xyz", eina_list_nth(lst, 2)); + + eina_model_properties_names_list_free(lst); + + fail_unless(eina_model_property_get(m, "abc", &outv)); + fail_unless(eina_value_get(&outv, &i)); + ck_assert_int_eq(i, 1234); + eina_value_flush(&outv); + + fail_unless(eina_model_property_get(m, "xyz", &outv)); + fail_unless(eina_value_get(&outv, &i)); + ck_assert_int_eq(i, 5678); + eina_value_flush(&outv); + + fail_unless(eina_model_property_get(m, "value", &outv)); + fail_unless(eina_value_get(&outv, &i)); + ck_assert_int_eq(i, 171); + eina_value_flush(&outv); + + fail_unless(eina_value_set(&inv, 666)); + fail_unless(eina_model_property_set(m, "value", &inv)); + fail_unless(eina_model_property_get(m, "value", &outv)); + fail_unless(eina_value_get(&outv, &i)); + ck_assert_int_eq(i, 666); + + eina_value_flush(&outv); + eina_value_flush(&inv); + + fail_unless(eina_value_setup(&inv, EINA_VALUE_TYPE_STRING)); + fail_unless(eina_value_set(&inv, "Hello world!")); + fail_unless(eina_model_property_set(m, "string", &inv)); + + fail_unless(eina_model_property_get(m, "string", &outv)); + fail_unless(eina_value_get(&outv, &s)); + fail_unless(s != NULL); + ck_assert_str_eq(s, "Hello world!"); + + eina_value_flush(&outv); + eina_value_flush(&inv); + + fail_unless(eina_value_setup(&inv, EINA_VALUE_TYPE_STRINGSHARE)); + fail_unless(eina_value_set(&inv, "Hello world-STRINGSHARED!")); + fail_unless(eina_model_property_set(m, "stringshare", &inv)); + /* set twice to see if references drop to zero before new add, shouldn't */ + fail_unless(eina_model_property_set(m, "stringshare", &inv)); + + fail_unless(eina_model_property_get(m, "stringshare", &outv)); + fail_unless(eina_value_get(&outv, &s)); + fail_unless(s != NULL); + ck_assert_str_eq(s, "Hello world-STRINGSHARED!"); + + eina_value_flush(&outv); + eina_value_flush(&inv); + + s = eina_model_to_string(m); + fail_unless(s != NULL); + ck_assert_str_eq(s, "Eina_Model_Type_Generic({abc: 1234, string: Hello world!, stringshare: Hello world-STRINGSHARED!, value: 666, xyz: 5678}, [])"); + free(s); + + fail_unless(eina_model_property_del(m, "value")); + + /* negative test (check safety was displayed by using print_cb) */ + eina_log_print_cb_set(_eina_test_model_check_safety_null, &ck); + + ck = EINA_FALSE; + fail_if(eina_model_property_get(m, "non-existent", &outv)); + fail_unless(ck == EINA_TRUE); + + ck = EINA_FALSE; + fail_if(eina_model_property_get(m, NULL, &outv)); + fail_unless(ck == EINA_TRUE); + + ck = EINA_FALSE; + fail_if(eina_model_property_del(m, "value")); + fail_unless(ck == EINA_TRUE); + + /* revert print_cb to default */ + eina_log_print_cb_set(eina_log_print_cb_stderr, NULL); + + ck_assert_int_eq(eina_model_refcount(m), 1); + + eina_model_unref(m); + ck_assert_int_eq(count_del, 1); + ck_assert_int_eq(count_pset, 7); + ck_assert_int_eq(count_pdel, 1); + eina_shutdown(); +} +END_TEST + +static int +eina_model_test_children_reverse_cmp(const Eina_Model *a, const Eina_Model *b) +{ + return - eina_model_compare(a, b); +} + +START_TEST(eina_model_test_children) +{ + unsigned int count_del = 0, count_cset = 0, count_cins = 0, count_cdel = 0; + Eina_Model *m, *c; + char *s; + int i; + + eina_init(); + + m = eina_model_new(EINA_MODEL_TYPE_GENERIC); + fail_unless(m != NULL); + + eina_model_event_callback_add + (m, "deleted", _eina_test_model_cb_count, &count_del); + eina_model_event_callback_add + (m, "child,set", _eina_test_model_cb_count, &count_cset); + eina_model_event_callback_add + (m, "child,inserted", _eina_test_model_cb_count, &count_cins); + eina_model_event_callback_add + (m, "child,deleted", _eina_test_model_cb_count, &count_cdel); + + for (i = 0; i < 10; i++) + { + Eina_Value val; + + c = eina_model_new(EINA_MODEL_TYPE_GENERIC); + fail_unless(c != NULL); + + eina_model_event_callback_add + (c, "deleted", _eina_test_model_cb_count, &count_del); + eina_model_event_callback_add + (c, "child,set", _eina_test_model_cb_count, &count_cset); + eina_model_event_callback_add + (c, "child,inserted", _eina_test_model_cb_count, &count_cins); + eina_model_event_callback_add + (c, "child,deleted", _eina_test_model_cb_count, &count_cdel); + + fail_unless(eina_value_setup(&val, EINA_VALUE_TYPE_INT)); + fail_unless(eina_value_set(&val, i)); + fail_unless(eina_model_property_set(c, "value", &val)); + + fail_unless(eina_model_child_append(m, c) >= 0); + ck_assert_int_eq(eina_model_refcount(c), 2); + + eina_value_flush(&val); + eina_model_unref(c); + } + + ck_assert_int_eq(eina_model_child_count(m), 10); + + for (i = 0; i < 10; i++) + { + Eina_Value val; + int x; + + c = eina_model_child_get(m, i); + fail_unless(c != NULL); + ck_assert_int_eq(eina_model_refcount(c), 2); + + fail_unless(eina_model_property_get(c, "value", &val)); + fail_unless(eina_value_get(&val, &x)); + ck_assert_int_eq(x, i); + + eina_value_flush(&val); + eina_model_unref(c); + } + + eina_model_child_sort(m, EINA_COMPARE_CB(eina_model_test_children_reverse_cmp)); + + for (i = 0; i < 10; i++) + { + Eina_Value val; + int x; + + c = eina_model_child_get(m, i); + fail_unless(c != NULL); + ck_assert_int_eq(eina_model_refcount(c), 2); + + fail_unless(eina_model_property_get(c, "value", &val)); + fail_unless(eina_value_get(&val, &x)); + ck_assert_int_eq(x, 10 - i - 1); + + eina_value_flush(&val); + eina_model_unref(c); + } + + eina_model_child_sort(m, EINA_COMPARE_CB(eina_model_compare)); + + s = eina_model_to_string(m); + fail_unless(s != NULL); + ck_assert_str_eq(s, "Eina_Model_Type_Generic({}, [Eina_Model_Type_Generic({value: 0}, []), Eina_Model_Type_Generic({value: 1}, []), Eina_Model_Type_Generic({value: 2}, []), Eina_Model_Type_Generic({value: 3}, []), Eina_Model_Type_Generic({value: 4}, []), Eina_Model_Type_Generic({value: 5}, []), Eina_Model_Type_Generic({value: 6}, []), Eina_Model_Type_Generic({value: 7}, []), Eina_Model_Type_Generic({value: 8}, []), Eina_Model_Type_Generic({value: 9}, [])])"); + free(s); + + c = eina_model_child_get(m, 0); + eina_model_child_set(m, 1, c); + eina_model_unref(c); + + eina_model_child_del(m, 0); + eina_model_child_del(m, 8); + + s = eina_model_to_string(m); + fail_unless(s != NULL); + ck_assert_str_eq(s, "Eina_Model_Type_Generic({}, [Eina_Model_Type_Generic({value: 0}, []), Eina_Model_Type_Generic({value: 2}, []), Eina_Model_Type_Generic({value: 3}, []), Eina_Model_Type_Generic({value: 4}, []), Eina_Model_Type_Generic({value: 5}, []), Eina_Model_Type_Generic({value: 6}, []), Eina_Model_Type_Generic({value: 7}, []), Eina_Model_Type_Generic({value: 8}, [])])"); + free(s); + + ck_assert_int_eq(eina_model_refcount(m), 1); + eina_model_unref(m); + + ck_assert_int_eq(count_del, 11); + ck_assert_int_eq(count_cins, 10); + ck_assert_int_eq(count_cset, 1); + ck_assert_int_eq(count_cdel, 2); + + eina_shutdown(); +} +END_TEST + +START_TEST(eina_model_test_copy) +{ + unsigned int count_del = 0; + Eina_Model *m, *cp; + char *s1, *s2; + int i; + + eina_init(); + + m = eina_model_new(EINA_MODEL_TYPE_GENERIC); + fail_unless(m != NULL); + + eina_model_event_callback_add + (m, "deleted", _eina_test_model_cb_count, &count_del); + + for (i = 0; i < 5; i++) + { + Eina_Value val; + char name[2] = {'a'+ i, 0}; + fail_unless(eina_value_setup(&val, EINA_VALUE_TYPE_INT)); + fail_unless(eina_value_set(&val, i)); + fail_unless(eina_model_property_set(m, name, &val)); + eina_value_flush(&val); + } + + for (i = 0; i < 5; i++) + { + Eina_Value val; + Eina_Model *c = eina_model_new(EINA_MODEL_TYPE_GENERIC); + fail_unless(c != NULL); + fail_unless(eina_value_setup(&val, EINA_VALUE_TYPE_INT)); + fail_unless(eina_value_set(&val, i)); + fail_unless(eina_model_property_set(c, "x", &val)); + + eina_model_event_callback_add + (c, "deleted", _eina_test_model_cb_count, &count_del); + + fail_unless(eina_model_child_append(m, c) >= 0); + eina_model_unref(c); + eina_value_flush(&val); + } + + s1 = eina_model_to_string(m); + fail_unless(s1 != NULL); + ck_assert_str_eq(s1, "Eina_Model_Type_Generic({a: 0, b: 1, c: 2, d: 3, e: 4}, [Eina_Model_Type_Generic({x: 0}, []), Eina_Model_Type_Generic({x: 1}, []), Eina_Model_Type_Generic({x: 2}, []), Eina_Model_Type_Generic({x: 3}, []), Eina_Model_Type_Generic({x: 4}, [])])"); + + cp = eina_model_copy(m); + fail_unless(cp != NULL); + fail_unless(cp != m); + + eina_model_event_callback_add + (cp, "deleted", _eina_test_model_cb_count, &count_del); + + s2 = eina_model_to_string(cp); + fail_unless(s2 != NULL); + ck_assert_str_eq(s1, s2); + + for (i = 0; i < 5; i++) + { + Eina_Model *c1 = eina_model_child_get(m, i); + Eina_Model *c2 = eina_model_child_get(cp, i); + + fail_unless(c1 != NULL); + fail_unless(c1 == c2); + ck_assert_int_eq(eina_model_refcount(c1), 4); + + eina_model_unref(c1); + eina_model_unref(c2); + } + + free(s1); + free(s2); + + ck_assert_int_eq(eina_model_refcount(m), 1); + eina_model_unref(m); + + ck_assert_int_eq(eina_model_refcount(cp), 1); + eina_model_unref(cp); + + ck_assert_int_eq(count_del, 2 + 5); + + eina_shutdown(); +} +END_TEST + +START_TEST(eina_model_test_deep_copy) +{ + unsigned int count_del = 0; + Eina_Model *m, *cp; + char *s1, *s2; + int i; + + eina_init(); + + m = eina_model_new(EINA_MODEL_TYPE_GENERIC); + fail_unless(m != NULL); + + eina_model_event_callback_add + (m, "deleted", _eina_test_model_cb_count, &count_del); + + for (i = 0; i < 5; i++) + { + Eina_Value val; + char name[2] = {'a'+ i, 0}; + fail_unless(eina_value_setup(&val, EINA_VALUE_TYPE_INT)); + fail_unless(eina_value_set(&val, i)); + fail_unless(eina_model_property_set(m, name, &val)); + eina_value_flush(&val); + } + + for (i = 0; i < 5; i++) + { + Eina_Value val; + Eina_Model *c = eina_model_new(EINA_MODEL_TYPE_GENERIC); + fail_unless(c != NULL); + fail_unless(eina_value_setup(&val, EINA_VALUE_TYPE_INT)); + fail_unless(eina_value_set(&val, i)); + fail_unless(eina_model_property_set(c, "x", &val)); + + eina_model_event_callback_add + (c, "deleted", _eina_test_model_cb_count, &count_del); + + fail_unless(eina_model_child_append(m, c) >= 0); + eina_model_unref(c); + eina_value_flush(&val); + } + + s1 = eina_model_to_string(m); + fail_unless(s1 != NULL); + ck_assert_str_eq(s1, "Eina_Model_Type_Generic({a: 0, b: 1, c: 2, d: 3, e: 4}, [Eina_Model_Type_Generic({x: 0}, []), Eina_Model_Type_Generic({x: 1}, []), Eina_Model_Type_Generic({x: 2}, []), Eina_Model_Type_Generic({x: 3}, []), Eina_Model_Type_Generic({x: 4}, [])])");; + + cp = eina_model_deep_copy(m); + fail_unless(cp != NULL); + fail_unless(cp != m); + + eina_model_event_callback_add + (cp, "deleted", _eina_test_model_cb_count, &count_del); + + s2 = eina_model_to_string(cp); + fail_unless(s2 != NULL); + ck_assert_str_eq(s1, s2); + + for (i = 0; i < 5; i++) + { + Eina_Model *c1 = eina_model_child_get(m, i); + Eina_Model *c2 = eina_model_child_get(cp, i); + + fail_unless(c1 != NULL); + fail_unless(c1 != c2); + ck_assert_int_eq(eina_model_refcount(c1), 2); + ck_assert_int_eq(eina_model_refcount(c2), 2); + + eina_model_event_callback_add + (c2, "deleted", _eina_test_model_cb_count, &count_del); + + eina_model_unref(c1); + eina_model_unref(c2); + } + + free(s1); + free(s2); + + ck_assert_int_eq(eina_model_refcount(m), 1); + eina_model_unref(m); + + ck_assert_int_eq(eina_model_refcount(cp), 1); + eina_model_unref(cp); + + ck_assert_int_eq(count_del, 2 + 10); + + eina_shutdown(); +} +END_TEST + +static Eina_Model * +eina_model_test_iterator_setup(unsigned int *count_del) +{ + Eina_Model *m; + int i; + + m = eina_model_new(EINA_MODEL_TYPE_GENERIC); + fail_unless(m != NULL); + + eina_model_event_callback_add + (m, "deleted", _eina_test_model_cb_count, count_del); + + for (i = 0; i < 5; i++) + { + Eina_Value val; + Eina_Model *c = eina_model_new(EINA_MODEL_TYPE_GENERIC); + fail_unless(c != NULL); + fail_unless(eina_value_setup(&val, EINA_VALUE_TYPE_INT)); + fail_unless(eina_value_set(&val, i)); + fail_unless(eina_model_property_set(c, "x", &val)); + + eina_model_event_callback_add + (c, "deleted", _eina_test_model_cb_count, count_del); + + fail_unless(eina_model_child_append(m, c) >= 0); + eina_model_unref(c); + eina_value_flush(&val); + } + + return m; +} + +START_TEST(eina_model_test_child_iterator) +{ + unsigned int count_del = 0; + Eina_Iterator *it; + Eina_Model *m, *c; + int i = 0; + + eina_init(); + + m = eina_model_test_iterator_setup(&count_del); + + it = eina_model_child_iterator_get(m); + fail_unless(it != NULL); + EINA_ITERATOR_FOREACH(it, c) + { + Eina_Value tmp; + int x; + + ck_assert_int_eq(eina_model_refcount(c), 2); + fail_unless(eina_model_property_get(c, "x", &tmp)); + fail_unless(eina_value_get(&tmp, &x)); + ck_assert_int_eq(x, i); + + eina_model_unref(c); + i++; + } + ck_assert_int_eq(i, 5); + eina_iterator_free(it); + + ck_assert_int_eq(eina_model_refcount(m), 1); + eina_model_unref(m); + ck_assert_int_eq(count_del, 6); + eina_shutdown(); +} +END_TEST + +START_TEST(eina_model_test_child_reversed_iterator) +{ + unsigned int count_del = 0; + Eina_Iterator *it; + Eina_Model *m, *c; + int i = 4; + + eina_init(); + + m = eina_model_test_iterator_setup(&count_del); + + it = eina_model_child_reversed_iterator_get(m); + fail_unless(it != NULL); + EINA_ITERATOR_FOREACH(it, c) + { + Eina_Value tmp; + int x; + + ck_assert_int_eq(eina_model_refcount(c), 2); + fail_unless(eina_model_property_get(c, "x", &tmp)); + fail_unless(eina_value_get(&tmp, &x)); + ck_assert_int_eq(x, i); + + eina_model_unref(c); + i--; + } + ck_assert_int_eq(i, -1); + eina_iterator_free(it); + + ck_assert_int_eq(eina_model_refcount(m), 1); + eina_model_unref(m); + ck_assert_int_eq(count_del, 6); + eina_shutdown(); +} +END_TEST + +START_TEST(eina_model_test_child_sorted_iterator) +{ + unsigned int count_del = 0; + Eina_Iterator *it; + Eina_Model *m, *c; + int i = 4; + + eina_init(); + + m = eina_model_test_iterator_setup(&count_del); + + it = eina_model_child_sorted_iterator_get + (m, EINA_COMPARE_CB(eina_model_test_children_reverse_cmp)); + fail_unless(it != NULL); + EINA_ITERATOR_FOREACH(it, c) + { + Eina_Value tmp; + int x; + + /* 3 because sort takes an extra reference for its temp array */ + ck_assert_int_eq(eina_model_refcount(c), 3); + fail_unless(eina_model_property_get(c, "x", &tmp)); + fail_unless(eina_value_get(&tmp, &x)); + ck_assert_int_eq(x, i); + + eina_model_unref(c); + i--; + } + ck_assert_int_eq(i, -1); + eina_iterator_free(it); + + it = eina_model_child_sorted_iterator_get + (m, EINA_COMPARE_CB(eina_model_compare)); + fail_unless(it != NULL); + i = 0; + EINA_ITERATOR_FOREACH(it, c) + { + Eina_Value tmp; + int x; + + /* 3 because sort takes an extra reference for its temp array */ + ck_assert_int_eq(eina_model_refcount(c), 3); + fail_unless(eina_model_property_get(c, "x", &tmp)); + fail_unless(eina_value_get(&tmp, &x)); + ck_assert_int_eq(x, i); + + eina_model_unref(c); + i++; + } + ck_assert_int_eq(i, 5); + eina_iterator_free(it); + + ck_assert_int_eq(eina_model_refcount(m), 1); + eina_model_unref(m); + ck_assert_int_eq(count_del, 6); + eina_shutdown(); +} +END_TEST + +static Eina_Bool +eina_model_test_filter_event(const void *m, void *c, void *fdata) +{ + Eina_Value tmp; + int x; + fail_unless(m == fdata); + fail_unless(eina_model_property_get(c, "x", &tmp)); + fail_unless(eina_value_get(&tmp, &x)); + eina_value_flush(&tmp); + return x % 2 == 0; +} + +START_TEST(eina_model_test_child_filtered_iterator) +{ + unsigned int count_del = 0; + Eina_Iterator *it; + Eina_Model *m; + int i = 0, idx; + + eina_init(); + + m = eina_model_test_iterator_setup(&count_del); + + it = eina_model_child_filtered_iterator_get + (m, eina_model_test_filter_event, m); + fail_unless(it != NULL); + EINA_ITERATOR_FOREACH(it, idx) + { + Eina_Model *c; + Eina_Value tmp; + int x; + + ck_assert_int_eq(idx % 2, 0); + ck_assert_int_eq(idx, i); + + c = eina_model_child_get(m, idx); + fail_unless(c != NULL); + ck_assert_int_eq(eina_model_refcount(c), 2); + fail_unless(eina_model_property_get(c, "x", &tmp)); + fail_unless(eina_value_get(&tmp, &x)); + ck_assert_int_eq(x, i); + + eina_model_unref(c); + i += 2; + } + ck_assert_int_eq(i, 6); + eina_iterator_free(it); + + ck_assert_int_eq(eina_model_refcount(m), 1); + eina_model_unref(m); + ck_assert_int_eq(count_del, 6); + eina_shutdown(); +} +END_TEST + +START_TEST(eina_model_test_struct) +{ + unsigned int count_del = 0, count_pset = 0, count_pdel = 0; + Eina_Model *m; + struct myst { + int i; + char c; + }; + const Eina_Value_Struct_Member myst_members[] = { + EINA_VALUE_STRUCT_MEMBER(EINA_VALUE_TYPE_INT, struct myst, i), + EINA_VALUE_STRUCT_MEMBER(EINA_VALUE_TYPE_CHAR, struct myst, c) + }; + const Eina_Value_Struct_Desc myst_desc = { + EINA_VALUE_STRUCT_DESC_VERSION, + NULL, myst_members, EINA_C_ARRAY_LENGTH(myst_members), sizeof(struct myst) + }; + Eina_Value inv, outv; + int i; + char c, *s; + Eina_List *lst; + Eina_Bool ck; + + eina_init(); + + m = eina_model_struct_new(&myst_desc); + fail_unless(m != NULL); + + eina_model_event_callback_add + (m, "deleted", _eina_test_model_cb_count, &count_del); + eina_model_event_callback_add + (m, "property,set", _eina_test_model_cb_count, &count_pset); + eina_model_event_callback_add + (m, "property,deleted", _eina_test_model_cb_count, &count_pdel); + + fail_unless(eina_value_setup(&inv, EINA_VALUE_TYPE_INT)); + fail_unless(eina_value_set(&inv, 1234)); + fail_unless(eina_value_get(&inv, &i)); + ck_assert_int_eq(i, 1234); + fail_unless(eina_model_property_set(m, "i", &inv)); + + eina_value_flush(&inv); + fail_unless(eina_value_setup(&inv, EINA_VALUE_TYPE_CHAR)); + fail_unless(eina_value_set(&inv, 33)); + fail_unless(eina_value_get(&inv, &c)); + ck_assert_int_eq(c, 33); + fail_unless(eina_model_property_set(m, "c", &inv)); + + lst = eina_model_properties_names_list_get(m); + ck_assert_int_eq(eina_list_count(lst), 2); + + lst = eina_list_sort(lst, 0, EINA_COMPARE_CB(strcmp)); + ck_assert_str_eq("c", eina_list_nth(lst, 0)); + ck_assert_str_eq("i", eina_list_nth(lst, 1)); + + eina_model_properties_names_list_free(lst); + + fail_unless(eina_model_property_get(m, "i", &outv)); + fail_unless(outv.type == EINA_VALUE_TYPE_INT); + fail_unless(eina_value_get(&outv, &i)); + ck_assert_int_eq(i, 1234); + eina_value_flush(&outv); + + fail_unless(eina_model_property_get(m, "c", &outv)); + fail_unless(outv.type == EINA_VALUE_TYPE_CHAR); + fail_unless(eina_value_get(&outv, &c)); + ck_assert_int_eq(c, 33); + eina_value_flush(&outv); + + eina_value_flush(&inv); + + /* negative test (check safety was displayed by using print_cb) */ + eina_log_print_cb_set(_eina_test_model_check_safety_null, &ck); + + fail_if(eina_model_property_get(m, "non-existent", &outv)); + + ck = EINA_FALSE; + fail_if(eina_model_property_get(m, NULL, &outv)); + fail_unless(ck == EINA_TRUE); + + fail_unless(eina_value_setup(&inv, EINA_VALUE_TYPE_STRING)); + fail_unless(eina_value_set(&inv, "hello world")); + + eina_log_print_cb_set(_eina_test_model_check_safety_false, &ck); + + ck = EINA_FALSE; + fail_if(eina_model_property_set(m, "i", &inv)); + fail_unless(ck == EINA_TRUE); + + ck = EINA_FALSE; + fail_if(eina_model_property_set(m, "c", &inv)); + fail_unless(ck == EINA_TRUE); + + /* revert print_cb to default */ + eina_log_print_cb_set(eina_log_print_cb_stderr, NULL); + + fail_if(eina_model_property_del(m, "value")); + fail_if(eina_model_property_del(m, "i")); + fail_if(eina_model_property_del(m, "c")); + + eina_value_flush(&inv); + + s = eina_model_to_string(m); + fail_unless(s != NULL); + ck_assert_str_eq(s, "Eina_Model_Type_Struct({c: 33, i: 1234}, [])"); + free(s); + + ck_assert_int_eq(eina_model_refcount(m), 1); + + eina_model_unref(m); + ck_assert_int_eq(count_del, 1); + ck_assert_int_eq(count_pset, 2); + ck_assert_int_eq(count_pdel, 0); + eina_shutdown(); +} +END_TEST + +static Eina_Bool +_struct_complex_members_constructor(Eina_Model *m) +{ + struct myst { + Eina_Value_Array a; + Eina_Value_List l; + Eina_Value_Hash h; + Eina_Value_Struct s; + } st; + struct subst { + int i, j; + }; + static Eina_Value_Struct_Member myst_members[] = { + EINA_VALUE_STRUCT_MEMBER(NULL, struct myst, a), + EINA_VALUE_STRUCT_MEMBER(NULL, struct myst, l), + EINA_VALUE_STRUCT_MEMBER(NULL, struct myst, h), + EINA_VALUE_STRUCT_MEMBER(NULL, struct myst, s) + }; + static Eina_Value_Struct_Desc myst_desc = { + EINA_VALUE_STRUCT_DESC_VERSION, + NULL, myst_members, EINA_C_ARRAY_LENGTH(myst_members), sizeof(struct myst) + }; + static Eina_Value_Struct_Member subst_members[] = { + EINA_VALUE_STRUCT_MEMBER(NULL, struct subst, i), + EINA_VALUE_STRUCT_MEMBER(NULL, struct subst, j) + }; + static Eina_Value_Struct_Desc subst_desc = { + EINA_VALUE_STRUCT_DESC_VERSION, + NULL, subst_members, EINA_C_ARRAY_LENGTH(subst_members), + sizeof(struct subst) + }; + + if (!myst_members[0].type) + { + myst_members[0].type = EINA_VALUE_TYPE_ARRAY; + myst_members[1].type = EINA_VALUE_TYPE_LIST; + myst_members[2].type = EINA_VALUE_TYPE_HASH; + myst_members[3].type = EINA_VALUE_TYPE_STRUCT; + } + + if (!subst_members[0].type) + { + subst_members[0].type = EINA_VALUE_TYPE_INT; + subst_members[1].type = EINA_VALUE_TYPE_INT; + } + + if (!eina_model_type_constructor(EINA_MODEL_TYPE_STRUCT, m)) + return EINA_FALSE; + + memset(&st, 0, sizeof(st)); + + st.a.subtype = EINA_VALUE_TYPE_STRING; + st.l.subtype = EINA_VALUE_TYPE_STRING; + st.h.subtype = EINA_VALUE_TYPE_STRING; + st.s.desc = &subst_desc; + if (!eina_model_struct_set(m, &myst_desc, &st)) + return EINA_FALSE; + + return EINA_TRUE; +} + +START_TEST(eina_model_test_struct_complex_members) +{ + Eina_Model *m; + Eina_Value outv; + char *s; + Eina_Model_Type type = EINA_MODEL_TYPE_INIT_NOPRIVATE + ("struct_complex_members", Eina_Model_Type, NULL, NULL, NULL); + + eina_init(); + + type.constructor = _struct_complex_members_constructor; + type.parent = EINA_MODEL_TYPE_STRUCT; + + m = eina_model_new(&type); + fail_unless(m != NULL); + + fail_unless(eina_model_property_get(m, "a", &outv)); + fail_unless(eina_value_array_append(&outv, "Hello")); + fail_unless(eina_value_array_append(&outv, "World")); + fail_unless(eina_model_property_set(m, "a", &outv)); + eina_value_flush(&outv); + + fail_unless(eina_model_property_get(m, "l", &outv)); + fail_unless(eina_value_list_append(&outv, "Some")); + fail_unless(eina_value_list_append(&outv, "Thing")); + fail_unless(eina_model_property_set(m, "l", &outv)); + eina_value_flush(&outv); + + fail_unless(eina_model_property_get(m, "h", &outv)); + fail_unless(eina_value_hash_set(&outv, "key", "value")); + fail_unless(eina_model_property_set(m, "h", &outv)); + eina_value_flush(&outv); + + fail_unless(eina_model_property_get(m, "s", &outv)); + fail_unless(eina_value_struct_set(&outv, "i", 1234)); + fail_unless(eina_value_struct_set(&outv, "j", 44)); + fail_unless(eina_model_property_set(m, "s", &outv)); + eina_value_flush(&outv); + + s = eina_model_to_string(m); + fail_unless(s != NULL); + ck_assert_str_eq(s, "struct_complex_members({a: [Hello, World], h: {key: value}, l: [Some, Thing], s: {i: 1234, j: 44}}, [])"); + free(s); + + ck_assert_int_eq(eina_model_refcount(m), 1); + + eina_model_unref(m); + eina_shutdown(); +} +END_TEST + +typedef struct _Animal_Type +{ + Eina_Model_Type parent_class; + void (*eat)(Eina_Model *mdl); +} Animal_Type; + +typedef struct _Human_Type +{ + Animal_Type parent_class; + void (*talk)(Eina_Model *mdl); +} Human_Type; + +typedef struct _Pooper_Interface +{ + Eina_Model_Interface base_interface; + void (*poop)(Eina_Model *mdl); +} Pooper_Interface; + +#define ANIMAL_TYPE(x) ((Animal_Type *) x) +#define HUMAN_TYPE(x) ((Human_Type *) x) +#define POOPER_IFACE(x) ((Pooper_Interface *) x) +#define POOPER_IFACE_NAME "Pooper_Interace" + +#define INHER_CB_COUNT(prefix) \ +static int prefix ## _count = 0; \ +static void \ +prefix (Eina_Model *mdl) \ +{ \ + (void) mdl; \ + (prefix ## _count)++; \ +} + +static void +animal_eat(Eina_Model *mdl) +{ + void (*pf)(Eina_Model *mdl); + pf = eina_model_method_resolve(mdl, Animal_Type, eat); + EINA_SAFETY_ON_NULL_RETURN(pf); + pf(mdl); +} + +static void +pooper_poop(Eina_Model *mdl) +{ + const Eina_Model_Interface *iface = NULL; + iface = eina_model_interface_get(mdl, POOPER_IFACE_NAME); + + EINA_SAFETY_ON_NULL_RETURN(iface); + + void (*pf)(Eina_Model *); + + pf = eina_model_interface_method_resolve(iface, mdl, Pooper_Interface, poop); + EINA_SAFETY_ON_NULL_RETURN(pf); + pf(mdl); +} + +INHER_CB_COUNT(_animal_poop); +INHER_CB_COUNT(_human_poop); +INHER_CB_COUNT(_animal_eat); +INHER_CB_COUNT(_human_eat); + +START_TEST(eina_model_test_inheritance) +{ + eina_init(); + + Pooper_Interface _ANIMAL_POOPER_IFACE; + Eina_Model_Interface *ANIMAL_POOPER_IFACE = (Eina_Model_Interface *) &_ANIMAL_POOPER_IFACE; + memset(&_ANIMAL_POOPER_IFACE, 0, sizeof(_ANIMAL_POOPER_IFACE)); + ANIMAL_POOPER_IFACE->version = EINA_MODEL_INTERFACE_VERSION; + ANIMAL_POOPER_IFACE->interface_size = sizeof(Pooper_Interface); + ANIMAL_POOPER_IFACE->name = POOPER_IFACE_NAME; + POOPER_IFACE(ANIMAL_POOPER_IFACE)->poop = _animal_poop; + + Pooper_Interface _HUMAN_POOPER_IFACE; + Eina_Model_Interface *HUMAN_POOPER_IFACE = (Eina_Model_Interface *) &_HUMAN_POOPER_IFACE; + const Eina_Model_Interface *HUMAN_POOPER_IFACES[] = { + ANIMAL_POOPER_IFACE, NULL + }; + memset(&_HUMAN_POOPER_IFACE, 0, sizeof(_HUMAN_POOPER_IFACE)); + HUMAN_POOPER_IFACE->version = EINA_MODEL_INTERFACE_VERSION; + HUMAN_POOPER_IFACE->interface_size = sizeof(Pooper_Interface); + HUMAN_POOPER_IFACE->name = POOPER_IFACE_NAME; + HUMAN_POOPER_IFACE->interfaces = HUMAN_POOPER_IFACES; + POOPER_IFACE(HUMAN_POOPER_IFACE)->poop = _human_poop; + + const Eina_Model_Interface *ANIMAL_IFACES[] = {ANIMAL_POOPER_IFACE, NULL}; + const Eina_Model_Interface *HUMAN_IFACES[] = {HUMAN_POOPER_IFACE, NULL}; + + /* Init Animal Type */ + Animal_Type _ANIMAL_TYPE; + Eina_Model_Type *ANIMAL_TYPE = (Eina_Model_Type *) &_ANIMAL_TYPE; + + memset(&_ANIMAL_TYPE, 0, sizeof(_ANIMAL_TYPE)); + Eina_Model_Type *type = (Eina_Model_Type *) &_ANIMAL_TYPE; + type->version = EINA_MODEL_TYPE_VERSION; + type->parent = EINA_MODEL_TYPE_BASE; + type->type_size = sizeof(Animal_Type); + type->name = "Animal_Type"; + type->parent = EINA_MODEL_TYPE_GENERIC; + type->interfaces = ANIMAL_IFACES; + + ANIMAL_TYPE(type)->eat = _animal_eat; + + /* Init Human Type */ + Animal_Type _HUMAN_TYPE; + Eina_Model_Type *HUMAN_TYPE = (Eina_Model_Type *) &_HUMAN_TYPE; + memset(&_HUMAN_TYPE, 0, sizeof(_HUMAN_TYPE)); + type = (Eina_Model_Type *) &_HUMAN_TYPE; + type->version = EINA_MODEL_TYPE_VERSION; + type->parent = ANIMAL_TYPE; + type->type_size = sizeof(Human_Type); + type->name = "Human_Type"; + type->interfaces = HUMAN_IFACES; + + ANIMAL_TYPE(type)->eat = _human_eat; + + Eina_Model *hm, *am; + am = eina_model_new(ANIMAL_TYPE); + hm = eina_model_new(HUMAN_TYPE); + + animal_eat(am); + ck_assert_int_eq(_animal_eat_count, 1); + animal_eat(hm); + ck_assert_int_eq(_human_eat_count, 1); + + pooper_poop(am); + ck_assert_int_eq(_animal_poop_count, 1); + pooper_poop(hm); + ck_assert_int_eq(_human_poop_count, 1); + + ck_assert_int_eq(_animal_eat_count, 1); + ck_assert_int_eq(_human_eat_count, 1); + ck_assert_int_eq(_animal_poop_count, 1); + ck_assert_int_eq(_human_poop_count, 1); + + ck_assert_int_eq(eina_model_refcount(am), 1); + ck_assert_int_eq(eina_model_refcount(hm), 1); + + eina_model_unref(am); + eina_model_unref(hm); + + eina_shutdown(); +} +END_TEST + +static Eina_Bool +_myproperties_load(Eina_Model *m) +{ + Eina_Value v; + Eina_Bool ret; + int count; + + if (!eina_model_property_get(m, "load_count", &v)) + return EINA_FALSE; + + eina_value_get(&v, &count); + count++; + eina_value_set(&v, count); + + ret = eina_model_property_set(m, "load_count", &v); + eina_value_flush(&v); + + return ret; +} + +static Eina_Bool +_myproperties_unload(Eina_Model *m) +{ + Eina_Value v; + Eina_Bool ret; + int count; + + if (!eina_model_property_get(m, "load_count", &v)) + return EINA_FALSE; + + eina_value_get(&v, &count); + count--; + eina_value_set(&v, count); + + ret = eina_model_property_set(m, "load_count", &v); + eina_value_flush(&v); + + return ret; +} + +static Eina_Bool +_mychildren_load(Eina_Model *m) +{ + Eina_Model *c = eina_model_new(EINA_MODEL_TYPE_GENERIC); + int ret = eina_model_child_append(m, c); + eina_model_unref(c); + return ret >= 0; +} + +static Eina_Bool +_mychildren_unload(Eina_Model *m) +{ + int count = eina_model_child_count(m); + EINA_SAFETY_ON_FALSE_RETURN_VAL(count > 0, EINA_FALSE); + return eina_model_child_del(m, count - 1); +} + +START_TEST(eina_model_test_ifaces_load_unload) +{ + unsigned int count_loaded = 0, count_unloaded = 0; + unsigned int count_ploaded = 0, count_punloaded = 0; + unsigned int count_cloaded = 0, count_cunloaded = 0; + static Eina_Model_Interface_Properties piface; + static Eina_Model_Interface_Children ciface; + static const Eina_Model_Interface *piface_parents[2] = {NULL, NULL}; + static const Eina_Model_Interface *ciface_parents[2] = {NULL, NULL}; + static const Eina_Model_Interface *type_ifaces[3] = { + &piface.base, &ciface.base, NULL + }; + static Eina_Model_Type type; + Eina_Model *m; + Eina_Value v; + int count; + + eina_init(); + + /* do after eina_init() otherwise interfaces are not set */ + piface_parents[0] = EINA_MODEL_INTERFACE_PROPERTIES_HASH; + ciface_parents[0] = EINA_MODEL_INTERFACE_CHILDREN_INARRAY; + + memset(&piface, 0, sizeof(piface)); + piface.base.version = EINA_MODEL_INTERFACE_VERSION; + piface.base.interface_size = sizeof(piface); + piface.base.name = EINA_MODEL_INTERFACE_NAME_PROPERTIES; + piface.base.interfaces = piface_parents; + piface.load = _myproperties_load; + piface.unload = _myproperties_unload; + + memset(&ciface, 0, sizeof(ciface)); + ciface.base.version = EINA_MODEL_INTERFACE_VERSION; + ciface.base.interface_size = sizeof(ciface); + ciface.base.name = EINA_MODEL_INTERFACE_NAME_CHILDREN; + ciface.base.interfaces = ciface_parents; + ciface.load = _mychildren_load; + ciface.unload = _mychildren_unload; + + type.version = EINA_MODEL_TYPE_VERSION; + type.private_size = 0; + type.name = "MyType"; + eina_model_type_subclass_setup(&type, EINA_MODEL_TYPE_GENERIC); + type.interfaces = type_ifaces; + + m = eina_model_new(&type); + fail_unless(m != NULL); + + eina_model_event_callback_add + (m, "loaded", _eina_test_model_cb_count, &count_loaded); + eina_model_event_callback_add + (m, "unloaded", _eina_test_model_cb_count, &count_unloaded); + + eina_model_event_callback_add + (m, "properties,loaded", _eina_test_model_cb_count, &count_ploaded); + eina_model_event_callback_add + (m, "properties,unloaded", _eina_test_model_cb_count, &count_punloaded); + + eina_model_event_callback_add + (m, "children,loaded", _eina_test_model_cb_count, &count_cloaded); + eina_model_event_callback_add + (m, "children,unloaded", _eina_test_model_cb_count, &count_cunloaded); + + fail_unless(eina_value_setup(&v, EINA_VALUE_TYPE_INT)); + fail_unless(eina_value_set(&v, 0)); + fail_unless(eina_model_property_set(m, "load_count", &v)); + eina_value_flush(&v); + + fail_unless(eina_model_load(m)); + fail_unless(eina_model_load(m)); + fail_unless(eina_model_load(m)); + + /* each load increments one for load_count property */ + fail_unless(eina_model_property_get(m, "load_count", &v)); + fail_unless(eina_value_pget(&v, &count)); + ck_assert_int_eq(count, 3); + eina_value_flush(&v); + + /* each load adds one child */ + ck_assert_int_eq(eina_model_child_count(m), 3); + + fail_unless(eina_model_unload(m)); + fail_unless(eina_model_unload(m)); + fail_unless(eina_model_unload(m)); + + ck_assert_int_eq(count_loaded, 3); + ck_assert_int_eq(count_unloaded, 3); + + ck_assert_int_eq(count_ploaded, 3); + ck_assert_int_eq(count_punloaded, 3); + + ck_assert_int_eq(count_cloaded, 3); + ck_assert_int_eq(count_cunloaded, 3); + + ck_assert_int_eq(eina_model_refcount(m), 1); + eina_model_unref(m); + + eina_shutdown(); +} +END_TEST + +void +eina_test_model(TCase *tc) +{ + tcase_add_test(tc, eina_model_test_properties); + tcase_add_test(tc, eina_model_test_children); + tcase_add_test(tc, eina_model_test_copy); + tcase_add_test(tc, eina_model_test_deep_copy); + tcase_add_test(tc, eina_model_test_child_iterator); + tcase_add_test(tc, eina_model_test_child_reversed_iterator); + tcase_add_test(tc, eina_model_test_child_sorted_iterator); + tcase_add_test(tc, eina_model_test_child_filtered_iterator); + tcase_add_test(tc, eina_model_test_struct); + tcase_add_test(tc, eina_model_test_struct_complex_members); + tcase_add_test(tc, eina_model_test_inheritance); + tcase_add_test(tc, eina_model_test_ifaces_load_unload); +} -- cgit v1.1