Michele De Stefano's C++ Utilities
list_usage.i
1 // list_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 list_usage_wrap.cpp list_usage.i
11  *
12  * 2. python setup.py build
13  */
14 
15 /**
16  * \example list_usage.i
17  *
18  * A simple SWIG interface for a Python extension module that shows the
19  * mds_utils::python::List usage.
20  *
21  * \remarks Here I've used SWIG for convenience only. The
22  * mds_utils::python::List class does not impose you
23  * this choice.
24  */
25 
26 %module list_usage
27 
28 // Include the following interface files for having the proper typemaps
29 %include "mds_utils/python/common.i"
30 %include "mds_utils/python/list.i"
31 
32 %header %{
33 #include <mds_utils/python/conversion.hpp>
34 #include <mds_utils/python/list.hpp>
35 #include <mds_utils/python/sequence_iterator.hpp>
36 #include <boost/fusion/container/vector.hpp>
37 #include <iostream>
38 
39 namespace mdspy = mds_utils::python;
40 %}
41 
42 
43 %inline %{
44 
45 // Tests the call to List::set(mds_utils::python::Obj)
46 PyObject* create_list(mds_utils::python::Obj o) {
47 
48  using namespace std;
49 
50  mdspy::List t;
51 
52  t.set(o);
53 
54  cout << "Built list length: " << t.len() << endl;
55 
56  PyObject *pret(t.transfer());
57 
58  return pret;
59 }
60 
61 
62 // Tests the call to List::set(const seq_T&)
63 PyObject* create_from_fusion() {
64 
65  namespace fus = boost::fusion;
66 
67  mdspy::List t;
68 
69  fus::vector<int,double,std::string>
70  v(1,2.2,"aaa");
71 
72  t.set(v);
73 
74  return t.transfer();
75 }
76 
77 
78 // Tests the List::set(FwIt,FwIt) method
79 PyObject* create_from_seq(mds_utils::python::Obj o) {
80 
81  mdspy::PySequenceIterator<mdspy::Obj>
82  b(o),
83  e(o,true);
84 
85  mdspy::List t;
86 
87  t.set(b,e);
88 
89  return t.transfer();
90 }
91 
92 
93 // Like the previous one, but returning directly a mds_utils::python::List
94 // This tests the output typemap
95 mds_utils::python::List create_from_seq2(mds_utils::python::Obj o) {
96 
97  mdspy::PySequenceIterator<mdspy::Obj>
98  b(o),
99  e(o,true);
100 
101  mdspy::List t;
102 
103  t.set(b,e);
104 
105  return t;
106 }
107 
108 // Test for element access with operator []
109 mds_utils::python::Obj access_element(mds_utils::python::List l,long i) {
110  return l[i];
111 }
112 
113 // Test for element assignment through operator []
114 void set_element(mds_utils::python::List l,long i,mds_utils::python::Obj v) {
115  l[i] = v;
116 }
117 
118 // Test the append method
119 void append_element(mds_utils::python::List l,mds_utils::python::Obj v) {
120  l.append(v);
121 }
122 
123 // Test directly appending a basic type
124 void append_double(mds_utils::python::List l) {
125  l.append(1.1);
126 }
127 
128 
129 // Test the insert method
130 void insert_element(mds_utils::python::List l,long i,mds_utils::python::Obj v) {
131  l.insert(i,v);
132 }
133 
134 
135 // Test the del method
136 void del_element(mds_utils::python::List l,long i) {
137  l.del(i);
138 }
139 
140 
141 // Test the sort method
142 void sort_list(mds_utils::python::List l) {
143  l.sort();
144 }
145 
146 
147 // Test the reverse method
148 void reverse_list(mds_utils::python::List l) {
149  l.reverse();
150 }
151 
152 
153 // Tests the copy-constructor
154 // This function does not actually duplicate the list
155 // It simply creates another Python object pointing to the same
156 // list
157 mds_utils::python::List dup_list(mds_utils::python::List l) {
158  mdspy::List retval(l);
159  return retval;
160 }
161 
162 
163 // Tests the copy-assignment
164 // This function does not actually duplicate the list
165 // It simply creates another Python object pointing to the same
166 // list
167 mds_utils::python::List dup_list2(mds_utils::python::List l) {
168  mdspy::List retval;
169 
170  retval = l;
171 
172  return retval;
173 }
174 
175 %}