We now use real structs to wrap objects.
[ruby-ecore.git] / src / ecore_job / rb_job.c
1 /*
2  * $Id: rb_job.c 50 2004-08-01 10:18:39Z tilman $
3  *
4  * Copyright (C) 2004 Tilman Sauerbeck (tilman at code-monkey de)
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <ruby.h>
22
23 #include <Ecore.h>
24 #include <Ecore_Job.h>
25 #include <stdbool.h>
26
27 #include "rb_ecore_job.h"
28
29 typedef struct {
30         Ecore_Job *real;
31         VALUE callback;
32         bool deleted;
33 } RbJob;
34
35 static void on_job (void *data)
36 {
37         RbJob *job = data;
38
39         rb_funcall (job->callback, rb_intern ("call"), 0);
40         job->deleted = true;
41 }
42
43 static void c_mark (RbJob *job)
44 {
45         rb_gc_mark (job->callback);
46 }
47
48 static void c_free (RbJob *job)
49 {
50         if (job->real && !job->deleted)
51                 ecore_job_del (job->real);
52
53         ecore_shutdown ();
54
55         free (job);
56 }
57
58 static VALUE c_new (VALUE klass)
59 {
60         VALUE self;
61         RbJob *job;
62
63         if (!rb_block_given_p ())
64                 return Qnil;
65
66         self = Data_Make_Struct (klass, RbJob, c_mark, c_free, job);
67
68         ecore_init ();
69
70         job->callback = rb_block_proc ();
71         job->real = ecore_job_add (on_job, job);
72
73         rb_obj_call_init (self, 0, NULL);
74
75         return self;
76 }
77
78 static VALUE c_delete (VALUE self)
79 {
80         VALUE ret = Qfalse;
81         RbJob *job = NULL;
82
83         Data_Get_Struct (self, RbJob, job);
84
85         if (job->real && !job->deleted) {
86                 ecore_job_del (job->real);
87                 job->deleted = true;
88                 job->real = NULL;
89                 ret = Qtrue;
90         }
91
92         return ret;
93 }
94
95 void Init_Job (void)
96 {
97         VALUE c = rb_define_class_under (mJob, "Job", rb_cObject);
98
99         rb_define_singleton_method (c, "new", c_new, 0);
100         rb_define_method (c, "delete", c_delete, 0);
101 }