1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
// Copyright (C) by Josh Blum. See LICENSE.txt for licensing information.
////////////////////////////////////////////////////////////////////////
// http://www.swig.org/Doc2.0/Library.html#Library_stl_exceptions
////////////////////////////////////////////////////////////////////////
%include "exception.i"
%exception
{
try
{
$action
}
catch (const std::exception& e)
{
SWIG_exception(SWIG_RuntimeError, e.what());
}
}
%{
#include <gras/hier_block.hpp>
#include <gras/top_block.hpp>
#include <boost/make_shared.hpp>
%}
////////////////////////////////////////////////////////////////////////
// pull in hier and top interface
////////////////////////////////////////////////////////////////////////
%include <gras/element.i>
%include <gras/hier_block.i>
%include <gras/top_block.hpp>
%include "GRAS_Utils.i"
////////////////////////////////////////////////////////////////////////
// weak element overload for python
////////////////////////////////////////////////////////////////////////
%{
struct WeakContainerPyObject : gras::WeakContainer
{
WeakContainerPyObject(PyObject *o):
o(o)
{
//NOP
}
boost::shared_ptr<const void> lock(void)
{
return boost::make_shared<PyObjectRefHolder>(o);
}
PyObject *o;
};
inline void set_weak_py_self(gras::Element &elem, PyObject *o)
{
elem.set_container(new WeakContainerPyObject(o));
}
%}
void set_weak_py_self(gras::Element &elem, PyObject *o);
////////////////////////////////////////////////////////////////////////
// Make a special top block with python safe unlocking wait
////////////////////////////////////////////////////////////////////////
%inline %{
namespace gras
{
struct TopBlockPython : TopBlock
{
TopBlockPython(const std::string &name):
TopBlock(name)
{
//NOP
}
virtual ~TopBlockPython(void)
{
PyTSPhondler phil;
this->reset();
}
void start(void)
{
PyTSPhondler phil;
TopBlock::start();
}
void stop(void)
{
PyTSPhondler phil;
TopBlock::stop();
}
void wait(void)
{
PyTSPhondler phil;
TopBlock::wait();
}
bool wait(const double timeout)
{
PyTSPhondler phil;
return TopBlock::wait(timeout);
}
std::string query(const std::string &args)
{
PyTSPhondler phil;
return TopBlock::query(args);
}
};
struct HierBlockPython : HierBlock
{
HierBlockPython(const std::string &name):
HierBlock(name)
{
//NOP
}
virtual ~HierBlockPython(void)
{
PyTSPhondler phil;
this->reset();
}
};
}
%}
////////////////////////////////////////////////////////////////////////
// Remake top block and hier block for multi-arg connnect
////////////////////////////////////////////////////////////////////////
%pythoncode %{
def to_element(obj):
if isinstance(obj, Element): return obj
try:
elem = obj.to_element()
set_weak_py_self(elem, obj)
return elem
except: raise Exception('cant coerce obj %s to element'%(obj))
def internal_connect__(fcn, obj, *args):
if len(args) == 1:
elem = to_element(args[0])
fcn(obj, elem)
return
for src, sink in zip(args, args[1:]):
try: src, src_index = src
except: src_index = 0
src = to_element(src)
try: sink, sink_index = sink
except: sink_index = 0
sink = to_element(sink)
fcn(obj, src, src_index, sink, sink_index)
class TopBlock(TopBlockPython):
def __init__(self, name="Top"):
TopBlockPython.__init__(self, name)
def connect(self, *args):
return internal_connect__(TopBlockPython.connect, self, *args)
def disconnect(self, *args):
return internal_connect__(TopBlockPython.disconnect, self, *args)
class HierBlock(HierBlockPython):
def __init__(self, name="Hier"):
HierBlockPython.__init__(self, name)
def connect(self, *args):
return internal_connect__(HierBlockPython.connect, self, *args)
def disconnect(self, *args):
return internal_connect__(HierBlockPython.disconnect, self, *args)
%}
|