1 /*
2  * Copyright 2018      Sven Verdoolaege
3  *
4  * Use of this software is governed by the MIT license
5  *
6  * Written by Sven Verdoolaege.
7  */
8 
9 #include "cpp.h"
10 #include "cpp_conversion.h"
11 
12 /* Print a function called "function" for converting objects of
13  * type "name" from the "from" bindings to the "to" bindings.
14  */
15 static void convert(const char *name, const char *from, const char *to,
16 	const char *function)
17 {
18 	printf("%s%s %s(%s%s obj) {\n", to, name, function, from, name);
19 	printf("\t""return %s""manage(obj.copy());\n", to);
20 	printf("}\n");
21 	printf("\n");
22 }
23 
24 /* Print functions for converting objects of "clazz"
25  * between the default and the checked C++ bindings.
26  *
27  * The conversion from default to checked is called "check".
28  * The inverse conversion is called "uncheck".
29  * For example, to "set", the following two functions are generated:
30  *
31  *	checked::set check(set obj) {
32  *		return checked::manage(obj.copy());
33  *	}
34  *
35  *	set uncheck(checked::set obj) {
36  *		return manage(obj.copy());
37  *	}
38  */
39 static void print(const isl_class &clazz)
40 {
41 	string name = cpp_generator::type2cpp(clazz.name);
42 
43 	convert(name.c_str(), "", "checked::", "check");
44 	convert(name.c_str(), "checked::", "", "uncheck");
45 }
46 
47 /* Generate conversion functions for converting objects between
48  * the default and the checked C++ bindings.
49  * Do this for each exported class.
50  */
51 void cpp_conversion_generator::generate()
52 {
53 	map<string, isl_class>::iterator ci;
54 
55 	printf("namespace isl {\n\n");
56 	for (ci = classes.begin(); ci != classes.end(); ++ci)
57 		print(ci->second);
58 	printf("} // namespace isl\n");
59 }
60