summaryrefslogtreecommitdiff
path: root/python/gras/GRAS_PyHierBlocks.i
diff options
context:
space:
mode:
authorJosh Blum2013-06-24 01:30:02 -0700
committerJosh Blum2013-06-24 01:30:02 -0700
commitc06fbef3cfa47fde6fec4786f0831c8452f877ac (patch)
tree0b6af6ef21c50be0718e968b2fc75a540c6afd28 /python/gras/GRAS_PyHierBlocks.i
parentc2afec624b427034773bfafcfdcf9e3947822329 (diff)
downloadsandhi-c06fbef3cfa47fde6fec4786f0831c8452f877ac.tar.gz
sandhi-c06fbef3cfa47fde6fec4786f0831c8452f877ac.tar.bz2
sandhi-c06fbef3cfa47fde6fec4786f0831c8452f877ac.zip
gras: fun w/ swig module work
Diffstat (limited to 'python/gras/GRAS_PyHierBlocks.i')
-rw-r--r--python/gras/GRAS_PyHierBlocks.i197
1 files changed, 197 insertions, 0 deletions
diff --git a/python/gras/GRAS_PyHierBlocks.i b/python/gras/GRAS_PyHierBlocks.i
new file mode 100644
index 0000000..a769e2b
--- /dev/null
+++ b/python/gras/GRAS_PyHierBlocks.i
@@ -0,0 +1,197 @@
+// 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
+////////////////////////////////////////////////////////////////////////
+%import <gras/element.i>
+%import <gras/hier_block.i>
+%import <gras/top_block.i>
+
+%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 %{
+
+from GRAS_Element import Element
+
+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 PyTopBlock(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)
+
+ def query(self, args):
+ if isinstance(args, str): return TopBlockPython.query(self, args)
+ else:
+ import json
+ query_str = json.dumps(args)
+ result_str = TopBlockPython.query(self, query_str)
+ return json.loads(result_str)
+
+class PyHierBlock(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)
+
+%}