annotate src/Support/ModuleTree.cc @ 120:89e4facffd93

- Module tree
author tomwalters
date Sun, 17 Oct 2010 02:42:12 +0000
parents 3801517c4e8f
children 3cdaa81c3aca
rev   line source
tomwalters@84 1 // Copyright 2010, Thomas Walters
tomwalters@84 2 //
tomwalters@84 3 // AIM-C: A C++ implementation of the Auditory Image Model
tomwalters@84 4 // http://www.acousticscale.org/AIMC
tomwalters@84 5 //
tomwalters@84 6 // Licensed under the Apache License, Version 2.0 (the "License");
tomwalters@84 7 // you may not use this file except in compliance with the License.
tomwalters@84 8 // You may obtain a copy of the License at
tomwalters@84 9 //
tomwalters@84 10 // http://www.apache.org/licenses/LICENSE-2.0
tomwalters@84 11 //
tomwalters@84 12 // Unless required by applicable law or agreed to in writing, software
tomwalters@84 13 // distributed under the License is distributed on an "AS IS" BASIS,
tomwalters@84 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
tomwalters@84 15 // See the License for the specific language governing permissions and
tomwalters@84 16 // limitations under the License.
tomwalters@84 17
tomwalters@84 18 /*! \file
tomwalters@84 19 * \brief Parse a configuration file to generate a tree of modules.
tomwalters@84 20 */
tomwalters@84 21
tomwalters@84 22 /*! \author: Thomas Walters <tom@acousticscale.org>
tomwalters@84 23 * \date 2010/08/08
tomwalters@84 24 * \version \$Id: $
tomwalters@84 25 */
tomwalters@84 26
tomwalters@114 27 #include "Support/ModuleTree.h"
tomwalters@114 28
tomwalters@114 29 #include "Support/ModuleFactory.h"
tomwalters@120 30 #include "Support/Module.h"
tomwalters@120 31 #include "Support/Parameters.h"
tomwalters@114 32
tomwalters@84 33 namespace aimc {
tomwalters@120 34 bool ModuleTree::LoadConfigFile(const string &filename) {
tomwalters@120 35 config_.Load(filename.c_str());
tomwalters@114 36 return ConstructTree();
tomwalters@114 37 }
tomwalters@114 38
tomwalters@120 39 bool ModuleTree::LoadConfigText(const string &config) {
tomwalters@120 40 config_.Parse(config.c_str());
tomwalters@114 41 return ConstructTree();
tomwalters@114 42 }
tomwalters@114 43
tomwalters@120 44 bool ModuleTree::ConstructTree() {
tomwalters@114 45 // Make two passes over the configuration file.
tomwalters@114 46 // The first pass creates all the named modules with their parameters.
tomwalters@114 47 bool done = false;
tomwalters@114 48 bool error = false;
tomwalters@120 49 char module_name_var[AimParameters::MaxParamNameLength];
tomwalters@120 50 char module_id_var[AimParameters::MaxParamNameLength];
tomwalters@120 51 char module_parameters_var[AimParameters::MaxParamNameLength];
tomwalters@114 52 int module_number = 1;
tomwalters@114 53 while (!done) {
tomwalters@120 54 sprintf(module_name_var, "module%d.name", module_number);
tomwalters@120 55 sprintf(module_id_var, "module%d.id", module_number);
tomwalters@120 56 sprintf(module_parameters_var, "module%d.parameters", module_number);
tomwalters@120 57 if (config_.IsSet(module_name_var) {
tomwalters@120 58 char* name = config_.GetString(module_name_var));
tomwalters@120 59 if (module_number == 1) {
tomwalters@120 60 root_name_ = name;
tomwalters@120 61 }
tomwalters@120 62 if (config_.IsSet(module_id_var)) {
tomwalters@120 63 char* id = config_.GetString(module_id_var));
tomwalters@120 64 char* parameters = config_.DefaultString(module_parameters_var), "");
tomwalters@120 65 parameters_[name] = new Parameters();
tomwalters@120 66 parameters_[name]->Parse(parameters);
tomwalters@120 67 modules_[name] = factory_.Create(id, parameters_[name].get());
tomwalters@120 68 } else {
tomwalters@120 69 LOG_ERROR("id field missing for module named %s", name);
tomwalters@120 70 error = true;
tomwalters@120 71 done = true;
tomwalters@120 72 }
tomwalters@115 73 } else {
tomwalters@115 74 done = true;
tomwalters@115 75 }
tomwalters@115 76 ++module_number;
tomwalters@115 77 }
tomwalters@115 78 // The second pass connects up all the modules into a tree.
tomwalters@120 79 char module_child_var[AimParameters::MaxParamNameLength];
tomwalters@120 80 for (int i = 0; i < modules_.size(); ++i) {
tomwalters@120 81 int child_number = 1;
tomwalters@120 82 done = false;
tomwalters@120 83 sprintf(module_name_var, "module%d.name", module_number);
tomwalters@120 84 if (config_.IsSet(module_name_var) {
tomwalters@120 85 char* name = config_.GetString(module_name_var));
tomwalters@120 86 while (!done) {
tomwalters@120 87 sprintf(module_child_var, "module%d.child%d", i, child_number);
tomwalters@120 88 if (config_.IsSet(module_child_var) {
tomwalters@120 89 char* child = config_.GetString(module_child_var);
tomwalters@120 90 modules_[name]->AddTarget(modules_[child].get());
tomwalters@120 91 } else {
tomwalters@120 92 done = true;
tomwalters@120 93 }
tomwalters@120 94 }
tomwalters@120 95 ++child_number;
tomwalters@114 96 } else {
tomwalters@120 97 error = true;
tomwalters@120 98 break;
tomwalters@114 99 }
tomwalters@114 100 }
tomwalters@114 101 return error;
tomwalters@114 102 }
tomwalters@120 103
tomwalters@120 104 bool ModuleTree::Initialize(Parameters *global_parameters) {
tomwalters@120 105 SignalBank s(1,1,1);
tomwalters@120 106 modules_[root_name_]->Initialize(s, global_parameters);
tomwalters@120 107 }
tomwalters@120 108
tomwalters@115 109 } // namespace aimc