misc/vim: inform the user when no results are found
[nit.git] / examples / callback_monkey.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2013 Matthieu Lucas <lucasmatthieu@gmail.com>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # This sample has been implemented to show you how simple is it to play
18 # with native callbacks (C) through an high level with NIT program.
19
20 module callback_monkey
21
22 in "C header" `{
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 typedef struct {
27 int id;
28 int age;
29 } CMonkey;
30
31 typedef struct {
32 MonkeyActionCallable toCall;
33 Object message;
34 } MonkeyAction;
35 `}
36
37 in "C body" `{
38 // Method which reproduce a callback answer
39 // Please note that a function pointer is only used to reproduce the callback
40 void cbMonkey(CMonkey *mkey, void callbackFunc(CMonkey*, MonkeyAction*), MonkeyAction *data)
41 {
42 sleep(2);
43 callbackFunc( mkey, data );
44 }
45
46 // Back of background treatment, will be redirected to callback function
47 void nit_monkey_callback_func( CMonkey *mkey, MonkeyAction *data )
48 {
49 // To call a your method, the signature must be written like this :
50 // <Interface Name>_<Method>...
51 MonkeyActionCallable_wokeUp( data->toCall, mkey, data->message );
52 }
53 `}
54
55 # Implementable interface to get callback in defined methods
56 interface MonkeyActionCallable
57 fun wokeUp( sender:Monkey, message: Object) is abstract
58 end
59
60 # Defining my object type Monkey, which is, in a low level, a pointer to a C struct (CMonkey)
61 extern class Monkey `{ CMonkey * `}
62
63 new `{
64 CMonkey *monkey = malloc( sizeof(CMonkey) );
65 monkey->age = 10;
66 monkey->id = 1;
67 return monkey;
68 `}
69
70 # Object method which will get a callback in wokeUp method, defined in MonkeyActionCallable interface
71 # Must be defined as Nit/C method because of C call inside
72 fun wokeUpAction( toCall: MonkeyActionCallable, message: Object ) is extern import MonkeyActionCallable.wokeUp `{
73
74 // Allocating memory to keep reference of received parameters :
75 // - Object receiver
76 // - Message
77 MonkeyAction *data = malloc( sizeof(MonkeyAction) );
78
79 // Incrementing reference counter to prevent from releasing
80 MonkeyActionCallable_incr_ref( toCall );
81 Object_incr_ref( message );
82
83 data->toCall = toCall;
84 data->message = message;
85
86 // Calling method which reproduce a callback by passing :
87 // - Receiver
88 // - Function pointer to object return method
89 // - Datas
90 cbMonkey( recv, &nit_monkey_callback_func, data );
91 `}
92 end