ajwillia-ms pushed a commit to branch master.

http://git.enlightenment.org/tools/examples.git/commit/?id=c2fd813ea64c270f2dc58beb72d070e8c9a0c5a8

commit c2fd813ea64c270f2dc58beb72d070e8c9a0c5a8
Author: Andy Williams <a...@andywilliams.me>
Date:   Tue Nov 7 17:35:34 2017 +0000

    eo-classes: Add an example using eolian to define a class heirarchy
---
 c-eo-classes/meson.build              | 14 ++++++++
 c-eo-classes/src/eo_classes_main.c    | 57 +++++++++++++++++++++++++++++++
 c-eo-classes/src/example_rectangle.c  | 51 ++++++++++++++++++++++++++++
 c-eo-classes/src/example_rectangle.eo | 31 +++++++++++++++++
 c-eo-classes/src/example_shape.c      |  5 +++
 c-eo-classes/src/example_shape.eo     | 24 +++++++++++++
 c-eo-classes/src/example_square.c     | 64 +++++++++++++++++++++++++++++++++++
 c-eo-classes/src/example_square.eo    | 23 +++++++++++++
 c-eo-classes/src/meson.build          | 32 ++++++++++++++++++
 9 files changed, 301 insertions(+)

diff --git a/c-eo-classes/meson.build b/c-eo-classes/meson.build
new file mode 100644
index 0000000..a84c7ba
--- /dev/null
+++ b/c-eo-classes/meson.build
@@ -0,0 +1,14 @@
+project(
+  'efl-example-eo-classes', 'c',
+  version : '0.0.1',
+  default_options: [ 'c_std=gnu99', 'warning_level=2' ],
+  meson_version : '>= 0.38.0')
+
+eina = dependency('eina', version : '>=1.20.99')
+efl = dependency('efl', version : '>=1.20.99')
+elm = dependency('elementary', version : '>=1.20.99')
+eolian = dependency('eolian', version : '>=1.20.99')
+
+inc = include_directories('.')
+subdir('src')
+
diff --git a/c-eo-classes/src/eo_classes_main.c 
b/c-eo-classes/src/eo_classes_main.c
new file mode 100644
index 0000000..aaf3a0c
--- /dev/null
+++ b/c-eo-classes/src/eo_classes_main.c
@@ -0,0 +1,57 @@
+#define EFL_EO_API_SUPPORT 1
+#define EFL_BETA_API_SUPPORT 1
+
+#include <Eina.h>
+#include <Efl.h>
+#include <Elementary.h>
+
+#include "example_shape.eo.h"
+#include "example_rectangle.eo.h"
+#include "example_square.eo.h"
+
+Example_Shape *
+_rect_create()
+{
+   Example_Rectangle *rectangle;
+
+   rectangle = efl_add(EXAMPLE_RECTANGLE_CLASS, NULL,
+                       efl_name_set(efl_added, "Rectangle"),
+                       example_rectangle_width_set(efl_added, 5),
+                       example_rectangle_height_set(efl_added, 10));
+
+   return rectangle;
+}
+
+Example_Shape *
+_square_create()
+{
+   Example_Square *square;
+
+   square = efl_add(EXAMPLE_SQUARE_CLASS, NULL,
+                    efl_name_set(efl_added, "Square"),
+                    example_square_side_set(efl_added, 7));
+
+   return square;
+}
+
+void
+_shape_print(Example_Shape *shape)
+{
+   printf("Shape named %s has area %d\n", efl_name_get(shape), 
example_shape_area(shape));
+}
+
+EAPI_MAIN void
+efl_main(void *data EINA_UNUSED, const Efl_Event *ev EINA_UNUSED)
+{
+   Eo *shape;
+
+   shape = _rect_create();
+   _shape_print(shape);
+
+   shape = _square_create();
+   _shape_print(shape);
+
+   efl_exit(0);
+}
+EFL_MAIN()
+
diff --git a/c-eo-classes/src/example_rectangle.c 
b/c-eo-classes/src/example_rectangle.c
new file mode 100644
index 0000000..c268bc1
--- /dev/null
+++ b/c-eo-classes/src/example_rectangle.c
@@ -0,0 +1,51 @@
+#define EFL_BETA_API_SUPPORT
+#include <Eo.h>
+#include "example_rectangle.eo.h"
+#include "example_shape.eo.h"
+
+typedef struct
+{
+   int width, height;
+} Example_Rectangle_Data;
+
+EOLIAN static void
+_example_rectangle_width_set(Eo *obj EINA_UNUSED, Example_Rectangle_Data *pd, 
int width)
+{
+   pd->width = width;
+}
+
+EOLIAN static int
+_example_rectangle_width_get(Eo *obj EINA_UNUSED, Example_Rectangle_Data *pd)
+{
+   return pd->width;
+}
+
+EOLIAN static void
+_example_rectangle_height_set(Eo *obj EINA_UNUSED, Example_Rectangle_Data *pd, 
int height)
+{
+   pd->height = height;
+}
+
+EOLIAN static int
+_example_rectangle_height_get(Eo *obj EINA_UNUSED, Example_Rectangle_Data *pd)
+{
+   return pd->height;
+}
+
+EOLIAN static int
+_example_rectangle_example_shape_area(Eo *obj EINA_UNUSED, 
Example_Rectangle_Data *pd)
+{
+   return pd->width * pd->height;
+}
+
+EOLIAN static void
+_example_rectangle_class_constructor(Efl_Class *klass EINA_UNUSED)
+{
+}
+
+EOLIAN static void
+_example_rectangle_class_destructor(Efl_Class *klass EINA_UNUSED)
+{
+}
+
+#include "example_rectangle.eo.c"
diff --git a/c-eo-classes/src/example_rectangle.eo 
b/c-eo-classes/src/example_rectangle.eo
new file mode 100644
index 0000000..35ce9fc
--- /dev/null
+++ b/c-eo-classes/src/example_rectangle.eo
@@ -0,0 +1,31 @@
+class Example.Rectangle (Efl.Object, Example.Shape) {
+   [[A rectangle shape object]]
+   eo_prefix: example_rectangle;
+   methods {
+      @property width {
+         [[The width of this rectangle]]
+         set {
+         }
+         get {
+         }
+         values {
+            width: int; [[Rectangle width]]
+         }
+      }
+      @property height {
+         [[The height of this rectangle]]
+         set {
+         }
+         get {
+         }
+         values {
+            height: int; [[Rectangle height]]
+         }
+      }
+   }
+   implements {
+      class.constructor;
+      class.destructor;
+      Example.Shape.area;
+   }
+}
diff --git a/c-eo-classes/src/example_shape.c b/c-eo-classes/src/example_shape.c
new file mode 100644
index 0000000..7ed49aa
--- /dev/null
+++ b/c-eo-classes/src/example_shape.c
@@ -0,0 +1,5 @@
+#define EFL_BETA_API_SUPPORT
+#include <Eo.h>
+#include "example_shape.eo.h"
+
+#include "example_shape.eo.c"
diff --git a/c-eo-classes/src/example_shape.eo 
b/c-eo-classes/src/example_shape.eo
new file mode 100644
index 0000000..9cfc299
--- /dev/null
+++ b/c-eo-classes/src/example_shape.eo
@@ -0,0 +1,24 @@
+interface Example.Shape {
+   [[A generic shape object]]
+   eo_prefix: example_shape;
+   methods {
+      @property color {
+         [[The colour that our shape should be filled with]]
+         set {
+         }
+         get {
+         }
+         values {
+            r: int; [[Red component color]]
+            g: int; [[Green component color]]
+            b: int; [[Blue component color]]
+         }
+      }
+      area {
+         [[Calculate the area of the shape.]]
+         params {
+         }
+         return: int;
+      }
+   }
+}
diff --git a/c-eo-classes/src/example_square.c 
b/c-eo-classes/src/example_square.c
new file mode 100644
index 0000000..bbb904d
--- /dev/null
+++ b/c-eo-classes/src/example_square.c
@@ -0,0 +1,64 @@
+#define EFL_BETA_API_SUPPORT
+#include <Eo.h>
+#include "example_square.eo.h"
+#include "example_rectangle.eo.h"
+#include "example_shape.eo.h"
+
+typedef struct
+{
+   int size;
+} Example_Square_Data;
+
+EOLIAN static void
+_example_square_side_set(Eo *obj EINA_UNUSED, Example_Square_Data *pd, int 
size)
+{
+   pd->size = size;
+}
+
+EOLIAN static int
+_example_square_side_get(Eo *obj EINA_UNUSED, Example_Square_Data *pd)
+{
+   return pd->size;
+}
+
+EOLIAN static void
+_example_square_example_rectangle_width_set(Eo *obj EINA_UNUSED, 
Example_Square_Data *pd, int width)
+{
+   pd->size = width;
+}
+
+EOLIAN static int
+_example_square_example_rectangle_width_get(Eo *obj EINA_UNUSED, 
Example_Square_Data *pd)
+{
+   return pd->size;
+}
+
+EOLIAN static void
+_example_square_example_rectangle_height_set(Eo *obj EINA_UNUSED, 
Example_Square_Data *pd, int height)
+{
+   pd->size = height;
+}
+
+EOLIAN static int
+_example_square_example_rectangle_height_get(Eo *obj EINA_UNUSED, 
Example_Square_Data *pd)
+{
+   return pd->size;
+}
+
+EOLIAN static int
+_example_square_example_shape_area(Eo *obj EINA_UNUSED, Example_Square_Data 
*pd)
+{
+   return pd->size * pd->size;
+}
+
+EOLIAN static void
+_example_square_class_constructor(Efl_Class *klass EINA_UNUSED)
+{
+}
+
+EOLIAN static void
+_example_square_class_destructor(Efl_Class *klass EINA_UNUSED)
+{
+}
+
+#include "example_square.eo.c"
diff --git a/c-eo-classes/src/example_square.eo 
b/c-eo-classes/src/example_square.eo
new file mode 100644
index 0000000..c7e885e
--- /dev/null
+++ b/c-eo-classes/src/example_square.eo
@@ -0,0 +1,23 @@
+class Example.Square (Example.Rectangle) {
+   [[A square shape object]]
+   eo_prefix: example_square;
+   methods {
+      @property side {
+         [[The length of both sides of this square]]
+         set {
+         }
+         get {
+         }
+         values {
+            size: int; [[Square side length]]
+         }
+      }
+   }
+   implements {
+      class.constructor;
+      class.destructor;
+      Example.Shape.area;
+      Example.Rectangle.width {get; set;}
+      Example.Rectangle.height {get; set;}
+   }
+}
diff --git a/c-eo-classes/src/meson.build b/c-eo-classes/src/meson.build
new file mode 100644
index 0000000..0b7f6ca
--- /dev/null
+++ b/c-eo-classes/src/meson.build
@@ -0,0 +1,32 @@
+eolian_gen = find_program('eolian_gen')
+
+eo_src = ['example_shape', 'example_rectangle', 'example_square']
+eo_csrc = []
+eo_gen = []
+
+foreach eo : eo_src
+  eo_file = eo + '.eo'
+  eo_csrc += eo + '.c'
+
+  eo_gen += custom_target('eolian_gen_' + eo_file,
+    input : eo_file,
+    output : [eo_file + '.h'],
+    command : [eolian_gen, '-I', eolian.get_pkgconfig_variable('eoincludedir'),
+                           '-I', '../src',
+                           '-gchi', '-o', 'h:' + eo_file + '.h',
+                           '-o', 'i:../src/' + eo + '.c',
+                           '-o', 'c:' + eo_file + '.c', '@INPUT@'])
+endforeach
+
+src = files([
+  'eo_classes_main.c',
+])
+
+deps = [eina, efl, elm]
+
+executable('efl_example_eo_classes', src, eo_csrc, eo_gen,
+  dependencies : deps,
+  include_directories : inc,
+  install : true
+)
+

-- 


Reply via email to