From 5a6ea86c7537df20fe975828758db8dc42db5d8b Mon Sep 17 00:00:00 2001 From: Ethan Rublee Date: Sun, 2 Oct 2011 11:55:12 -0700 Subject: [PATCH] Fixing a type issue in the blackbox, refer #187. Also adding better printing for python object tendrils. This is the inspiration for #194 --- include/ecto/tendril.hpp | 2 +- python/ecto/blackbox.py | 18 ++++++++++++------ src/lib/tendrils.cpp | 38 +++++++++++++++++++++++++++++--------- 3 files changed, 42 insertions(+), 16 deletions(-) diff --git a/include/ecto/tendril.hpp b/include/ecto/tendril.hpp index 23afa372..b6d2055b 100644 --- a/include/ecto/tendril.hpp +++ b/include/ecto/tendril.hpp @@ -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 @@ -500,7 +501,6 @@ namespace ecto t->set_holder(); return t; } - private: template void diff --git a/python/ecto/blackbox.py b/python/ecto/blackbox.py index 34fcfee0..a56eec47 100644 --- a/python/ecto/blackbox.py +++ b/python/ecto/blackbox.py @@ -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 = {} @@ -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) @@ -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) @@ -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) diff --git a/src/lib/tendrils.cpp b/src/lib/tendrils.cpp index 5e2e4591..17df5b10 100644 --- a/src/lib/tendrils.cpp +++ b/src/lib/tendrils.cpp @@ -5,7 +5,7 @@ #include namespace ecto { - struct PrintFunctions + namespace { template static void @@ -14,22 +14,36 @@ namespace ecto out << x.get(); } - typedef boost::function function_t; - typedef std::map ProcMap; + template<> + void + print(std::ostream& out, const tendril& x) + { + namespace bp = boost::python; + bp::object o; + x >> o; + out << std::string( bp::extract(bp::str(o))); + } + } + struct PrintFunctions + { + + typedef boost::function function_t; + typedef std::map ProcMap; ProcMap processes; PrintFunctions() { - processes[ecto::name_of()] = function_t(&PrintFunctions::print); - processes[ecto::name_of()] = function_t(&PrintFunctions::print); - processes[ecto::name_of()] = function_t(&PrintFunctions::print); - processes[ecto::name_of()] = function_t(&PrintFunctions::print); - processes[ecto::name_of()] = function_t(&PrintFunctions::print); + processes[ecto::name_of().c_str()] = function_t(print); + processes[ecto::name_of().c_str()] = function_t(print); + processes[ecto::name_of().c_str()] = function_t(print); + processes[ecto::name_of().c_str()] = function_t(print); + processes[ecto::name_of().c_str()] = function_t(print); + processes[ecto::name_of().c_str()] = function_t(print); } 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); @@ -64,6 +78,12 @@ namespace ecto void operator()(const std::pair& 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