Mercurial > hg > aimc
changeset 401:b71ec2cbe55b
- Module tree
author | tomwalters |
---|---|
date | Sun, 17 Oct 2010 02:42:12 +0000 |
parents | dd13c9834ceb |
children | 69466da9745e |
files | trunk/src/Support/Module.cc trunk/src/Support/Module.h trunk/src/Support/ModuleTree.cc trunk/src/Support/ModuleTree.h |
diffstat | 4 files changed, 90 insertions(+), 34 deletions(-) [+] |
line wrap: on
line diff
--- a/trunk/src/Support/Module.cc Sat Oct 16 23:05:26 2010 +0000 +++ b/trunk/src/Support/Module.cc Sun Oct 17 02:42:12 2010 +0000 @@ -44,8 +44,9 @@ Module::~Module() { }; -bool Module::Initialize(const SignalBank &input) { - // LOG_INFO_NN(_T("-> %s "), module_identifier_.c_str()); +bool Module::Initialize(const SignalBank &input, + Parameters *global_parameters) { + global_parameters_ = global_parameters; // Validate the input if (!input.Validate()) { LOG_ERROR(_T("Input SignalBank not valid")); @@ -57,7 +58,7 @@ return false; } // If the module has an output bank, then we can set up the targets - // of this module.. + // of this module. if (output_.initialized()) { // Check that the output SignalBank has been set up correctly if (!output_.Validate()) { @@ -73,11 +74,9 @@ set<Module*>::const_iterator it; for (it = targets_.begin(); it != targets_.end(); ++it) { if (!(*it)->initialized()) - if (!(*it)->Initialize(output_)) + if (!(*it)->Initialize(output_, global_parameters_)) return false; } - } else { - // LOG_INFO(_T("|")); } initialized_ = true; return true; @@ -101,12 +100,22 @@ } bool Module::AddTarget(Module* target_module) { + bool result = false; if (target_module) { pair<set<Module*>::iterator, bool> ret; ret = targets_.insert(target_module); - return ret.second; + result = ret.second; + if (result) { + if(initialized_) { + if(output_.initialized()) { + if (!target_module->initialized()) { + target_module->Initialize(output_, global_parameters_); + } + } + } + } } - return false; + return result; } bool Module::RemoveTarget(Module* target_module) {
--- a/trunk/src/Support/Module.h Sat Oct 16 23:05:26 2010 +0000 +++ b/trunk/src/Support/Module.h Sun Oct 17 02:42:12 2010 +0000 @@ -108,8 +108,14 @@ * override the default Initialize function. When creating a new module, do * not create a new version of Initialize uness you're sure you know what * you're doing! + * + * global_parameters stores things like the input filename, output filenames + * and any other metadata that modules might want to share. If present, it + * is propagated to all children. These parameters are mutable and may + * change. */ - virtual bool Initialize(const SignalBank &input); + virtual bool Initialize(const SignalBank &input, + Parameters *global_parameters); /*! \brief Returns true if the module has been correctly initialized * \return true if module has been initialized, false otherwise @@ -162,6 +168,12 @@ */ void Reset(); + /*! \brief Provide a pointer to the global parameters store. + * + + */ + void SetGlobalParameters(); + /*! \brief Return a pointer to the output SignalBank_ for this class. * \return pointer to the SignalBank that this module uses to store its * output. @@ -199,6 +211,7 @@ set<Module*> targets_; SignalBank output_; Parameters* parameters_; + Parameters* global_parameters_; string module_identifier_; string module_type_;
--- a/trunk/src/Support/ModuleTree.cc Sat Oct 16 23:05:26 2010 +0000 +++ b/trunk/src/Support/ModuleTree.cc Sun Oct 17 02:42:12 2010 +0000 @@ -27,51 +27,83 @@ #include "Support/ModuleTree.h" #include "Support/ModuleFactory.h" +#include "Support/Module.h" +#include "Support/Parameters.h" namespace aimc { -LoadConfigFile(const string &filename) { - parameters_.Load(filename.c_str()); +bool ModuleTree::LoadConfigFile(const string &filename) { + config_.Load(filename.c_str()); return ConstructTree(); } -LoadConfigText(const string &config) { - parameters_.Parse(config.c_str()); +bool ModuleTree::LoadConfigText(const string &config) { + config_.Parse(config.c_str()); return ConstructTree(); } -ConstructTree() { +bool ModuleTree::ConstructTree() { // Make two passes over the configuration file. // The first pass creates all the named modules with their parameters. bool done = false; bool error = false; - string module; + char module_name_var[AimParameters::MaxParamNameLength]; + char module_id_var[AimParameters::MaxParamNameLength]; + char module_parameters_var[AimParameters::MaxParamNameLength]; int module_number = 1; while (!done) { - snprintf("module%d", module_number); - if (parameters_.IsSet(module + ".name") { - string module_name = - string module_id = - string module_params = - modules_[] + sprintf(module_name_var, "module%d.name", module_number); + sprintf(module_id_var, "module%d.id", module_number); + sprintf(module_parameters_var, "module%d.parameters", module_number); + if (config_.IsSet(module_name_var) { + char* name = config_.GetString(module_name_var)); + if (module_number == 1) { + root_name_ = name; + } + if (config_.IsSet(module_id_var)) { + char* id = config_.GetString(module_id_var)); + char* parameters = config_.DefaultString(module_parameters_var), ""); + parameters_[name] = new Parameters(); + parameters_[name]->Parse(parameters); + modules_[name] = factory_.Create(id, parameters_[name].get()); + } else { + LOG_ERROR("id field missing for module named %s", name); + error = true; + done = true; + } } else { done = true; } ++module_number; } // The second pass connects up all the modules into a tree. - module_number = 1; - while (!done) { - module = sprintf("module%d", module_number); - if (parameters_.IsSet(module + ".name") { - string module_name = - string module_id = - string module_params = - modules_[] + char module_child_var[AimParameters::MaxParamNameLength]; + for (int i = 0; i < modules_.size(); ++i) { + int child_number = 1; + done = false; + sprintf(module_name_var, "module%d.name", module_number); + if (config_.IsSet(module_name_var) { + char* name = config_.GetString(module_name_var)); + while (!done) { + sprintf(module_child_var, "module%d.child%d", i, child_number); + if (config_.IsSet(module_child_var) { + char* child = config_.GetString(module_child_var); + modules_[name]->AddTarget(modules_[child].get()); + } else { + done = true; + } + } + ++child_number; } else { - done = true; + error = true; + break; } - ++module_number; } return error; } + +bool ModuleTree::Initialize(Parameters *global_parameters) { + SignalBank s(1,1,1); + modules_[root_name_]->Initialize(s, global_parameters); +} + } // namespace aimc
--- a/trunk/src/Support/ModuleTree.h Sat Oct 16 23:05:26 2010 +0000 +++ b/trunk/src/Support/ModuleTree.h Sun Oct 17 02:42:12 2010 +0000 @@ -28,10 +28,11 @@ #include <hash_map> #include <string> -#include "Support/Parameters.h" - namespace aimc { using std::string; +class Module; +class Parameters; + class ModuleTree { public: bool ParseConfigFile(const string &filename); @@ -44,9 +45,10 @@ return output_filename_prefix_; } private: - Paramters parameters_; + Paramters config_; string output_filename_prefix_; hash_map<string, scoped_ptr<Module> > modules_; + hash_map<string, scoped_ptr<Parameters> > parameters_; string root_name_; DISALLOW_COPY_AND_ASSIGN(ModuleTree); };