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