Merge: doc: fixed some typos and other misc. corrections
[nit.git] / tests / test_ffi_cpp_callbacks.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2013 Alexis Laferrière <alexis.laf@xymus.net>
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 # Module wrapping the C++ class vector<int>
18 module test_ffi_cpp_callbacks
19
20 in "C++ Header" `{
21 #include <vector>
22
23 using namespace std;
24 `}
25
26 # Nit class over C++'s vector<int>
27 extern class CppVector in "C++" `{vector<int>*`}
28
29 # Extern constructor
30 new in "C++" `{
31 return new vector<int>();
32 `}
33
34 # Adds an element to the end of the vector
35 fun push(v: Int) in "C++" `{
36 self->push_back(v);
37 `}
38
39 # Pops an element from the end of the vector
40 fun pop: Int in "C++" `{
41 long val = self->back();
42 self->pop_back();
43 return val;
44 `}
45
46 fun safe_pop_with_default(default_return: Int): Int import report_error in "C++" `{
47 if (self->empty()) {
48 CppVector_report_error(self);
49 return default_return;
50 } else {
51 long val = self->back();
52 self->pop_back();
53 return val;
54 }
55 `}
56
57 private fun report_error do print "Vector is empty!"
58 end
59
60 var a = new CppVector
61 a.push(1)
62 a.push(2)
63 a.push(3)
64 a.push(4)
65 print a.pop
66 print a.pop
67 print a.pop
68 print a.safe_pop_with_default(1234)
69 print a.safe_pop_with_default(1234)
70 print a.safe_pop_with_default(1234)
71 print a.safe_pop_with_default(1234)