Michele De Stefano's C++ Utilities
dict_usage.i
1 // dict_usage.i
2 //
3 // Copyright (c) 2014 - Michele De Stefano (micdestefano@users.sourceforge.net)
4 //
5 // Distributed under the MIT License (See accompanying file LICENSE)
6 
7 /*
8  * Instructions for generating and building the extension:
9  *
10  * 1. swig -c++ -Wall -python -I../../../include -o dict_usage_wrap.cpp dict_usage.i
11  *
12  * 2. python setup.py build
13  */
14 
15 /**
16  * \example dict_usage.i
17  *
18  * A simple SWIG interface for a Python extension module that shows the
19  * mds_utils::python::Dictionary usage.
20  *
21  * \remarks Here I've used SWIG for convenience only. The
22  * mds_utils::python::Dictionary class does not impose you
23  * this choice.
24  */
25 
26 %module dict_usage
27 
28 // Include the following interface files for having the proper typemaps
29 %include "std_string.i"
30 %include "mds_utils/python/common.i"
31 %include "mds_utils/python/list.i"
32 %include "mds_utils/python/dictionary.i"
33 
34 %header %{
35 #include <mds_utils/python/conversion.hpp>
36 #include <mds_utils/python/dictionary.hpp>
37 #include <iostream>
38 
39 namespace mdspy = mds_utils::python;
40 %}
41 
42 
43 %inline %{
44 
45 // Tests the copy of the dictionary
46 mds_utils::python::Dictionary copy_dict(mds_utils::python::Dictionary d) {
47 
48  mdspy::Dictionary out;
49 
50  out.copy(d);
51 
52  return out;
53 }
54 
55 // Tests the clear method
56 void clear_dict(mds_utils::python::Dictionary d) {
57  d.clear();
58 }
59 
60 // Tests the contains method
61 bool dict_contains(mds_utils::python::Dictionary d,mds_utils::python::Obj key) {
62  return d.contains(key);
63 }
64 
65 // Tests the contains method, with integer key
66 bool dict_contains_int_key(mds_utils::python::Dictionary d,int key) {
67  return d.contains(key);
68 }
69 
70 // Tests the contains method, with string key
71 bool dict_contains_str_key(mds_utils::python::Dictionary d,std::string key) {
72  return d.contains(key);
73 }
74 
75 // Tests the set_item method
76 void dict_set_item(mds_utils::python::Dictionary d,mds_utils::python::Obj key,mds_utils::python::Obj val) {
77  d.set_item(key,val);
78 }
79 
80 // Tests the set_item method, with string key and integer value
81 void dict_set_item_str_int(mds_utils::python::Dictionary d,std::string key,int val) {
82  d.set_item(key,val);
83 }
84 
85 // Tests the del_item method
86 void dict_del_item(mds_utils::python::Dictionary d,mds_utils::python::Obj key) {
87  d.del_item(key);
88 }
89 
90 
91 // Tests the get_item method
92 mds_utils::python::Obj dict_get_item(mds_utils::python::Dictionary d,mds_utils::python::Obj key) {
93  return d.get_item(key);
94 }
95 
96 // Tests the items method
97 mds_utils::python::List dict_items(mds_utils::python::Dictionary d) {
98  return d.items();
99 }
100 
101 // Tests the keys method
102 mds_utils::python::List dict_keys(mds_utils::python::Dictionary d) {
103  return d.keys();
104 }
105 
106 // Tests the values method
107 mds_utils::python::List dict_values(mds_utils::python::Dictionary d) {
108  return d.values();
109 }
110 
111 // Tests the len method
112 size_t dict_len(mds_utils::python::Dictionary d) {
113  return d.len();
114 }
115 
116 
117 // Tests the update method
118 void dict_update(mds_utils::python::Dictionary a,mds_utils::python::Dictionary b) {
119  a.update(b);
120 }
121 
122 
123 %}