Skip to content

Commit

Permalink
Fixing a type issue in the blackbox, refer #187.
Browse files Browse the repository at this point in the history
Also adding better printing for python object tendrils. This is the
inspiration for #194
  • Loading branch information
ethanrublee committed Oct 2, 2011
1 parent 2ce54f2 commit 5a6ea86
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 16 deletions.
2 changes: 1 addition & 1 deletion include/ecto/tendril.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ namespace ecto
*/
std::string
type_name() const;
const char* type_id() const {return type_ID_;}

/**
* \brief A doc string for this tendril, "foo is for the input
Expand Down Expand Up @@ -500,7 +501,6 @@ namespace ecto
t->set_holder<T>();
return t;
}

private:
template<class Archive>
void
Expand Down
18 changes: 12 additions & 6 deletions python/ecto/blackbox.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import ecto

class _SpecialTendrils(object):
class BlackBoxTendrils(object):
'''These look like tendrils with a few extra bits of functionality for BlackBoxes.
'''
tt_key = {ecto.tendril_type.INPUT:'inputs', ecto.tendril_type.PARAMETER:'params', ecto.tendril_type.OUTPUT:'outputs'}

def __init__(self, bb, tendril_type):
self._tendrils = ecto.Tendrils()
self.tt = tendril_type
self.tt_key = _SpecialTendrils.tt_key[self.tt]
self.tt_key = BlackBoxTendrils.tt_key[self.tt]
self.bb = bb
self.forwards = {}

Expand Down Expand Up @@ -43,6 +45,10 @@ def _append(self, cell_name, key, cell_key):
self.forwards[cell_name] = l

def forward_all(self, cell_name):
'''
Given the name of a class variable that is a cell, forward declare all
its tendrils (params, inputs,outputs depending on context).
'''
inst = self._get_cell_template(cell_name)
for key, val in getattr(inst, self.tt_key):
self._append(cell_name, key, key)
Expand All @@ -66,7 +72,7 @@ def solidify_forward_declares(self):
ctendrils = getattr(cell, self.tt_key)
for key, cell_key in keys:
tendrils.declare(key, ctendrils.at(cell_key))
setattr(tendrils, key, self._tendrils[key])#set the cells to parameters.
tendrils.at(key).copy_value(self._tendrils.at(key))#set the cells to parameters.
for key, tendril in self._tendrils:
if not key in tendrils:
tendrils.declare(key, tendril)
Expand All @@ -87,9 +93,9 @@ def __init__(self, *args, **kwargs):
'''
self.niter = kwargs.get('niter', 1)

self._params = _SpecialTendrils(self, ecto.tendril_type.PARAMETER)
self._inputs = _SpecialTendrils(self, ecto.tendril_type.INPUT)
self._outputs = _SpecialTendrils(self, ecto.tendril_type.OUTPUT)
self._params = BlackBoxTendrils(self, ecto.tendril_type.PARAMETER)
self._inputs = BlackBoxTendrils(self, ecto.tendril_type.INPUT)
self._outputs = BlackBoxTendrils(self, ecto.tendril_type.OUTPUT)

self.declare_params(self._params)

Expand Down
38 changes: 29 additions & 9 deletions src/lib/tendrils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <iostream>
namespace ecto
{
struct PrintFunctions
namespace
{
template<typename T>
static void
Expand All @@ -14,22 +14,36 @@ namespace ecto
out << x.get<T>();
}

typedef boost::function<void(std::ostream& out, const tendril& x)> function_t;
typedef std::map<std::string,function_t> ProcMap;
template<>
void
print<boost::python::api::object>(std::ostream& out, const tendril& x)
{
namespace bp = boost::python;
bp::object o;
x >> o;
out << std::string( bp::extract<std::string>(bp::str(o)));
}
}
struct PrintFunctions
{

typedef boost::function<void(std::ostream& out, const tendril& x)> function_t;
typedef std::map<const char*,function_t> ProcMap;
ProcMap processes;
PrintFunctions()
{
processes[ecto::name_of<int>()] = function_t(&PrintFunctions::print<int>);
processes[ecto::name_of<float>()] = function_t(&PrintFunctions::print<float>);
processes[ecto::name_of<double>()] = function_t(&PrintFunctions::print<double>);
processes[ecto::name_of<bool>()] = function_t(&PrintFunctions::print<bool>);
processes[ecto::name_of<std::string>()] = function_t(&PrintFunctions::print<std::string>);
processes[ecto::name_of<int>().c_str()] = function_t(print<int>);
processes[ecto::name_of<float>().c_str()] = function_t(print<float>);
processes[ecto::name_of<double>().c_str()] = function_t(print<double>);
processes[ecto::name_of<bool>().c_str()] = function_t(print<bool>);
processes[ecto::name_of<std::string>().c_str()] = function_t(print<std::string>);
processes[ecto::name_of<boost::python::api::object>().c_str()] = function_t(print<boost::python::api::object>);
}

void
print_tendril(std::ostream& out, const tendril& t) const
{
ProcMap::const_iterator it = processes.find(t.type_name());
ProcMap::const_iterator it = processes.find(t.type_id());
if (it != processes.end())
{
it->second(out, t);
Expand Down Expand Up @@ -64,6 +78,12 @@ namespace ecto
void
operator()(const std::pair<std::string, ecto::tendril::ptr>& tp)
{
//TODO
//EAR: Seems like we would like to have the ability to customize the
// str representation and type_name for any tendril?
// Also these seem like they belong in the tendril itself.
// This could go into a type registry system. All the more reason for us to have one.

std::stringstream tss;
pf.print_tendril(tss, *tp.second);
//default value
Expand Down

0 comments on commit 5a6ea86

Please sign in to comment.