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

- Module tree
author tomwalters
date Sun, 17 Oct 2010 02:42:12 +0000
parents 47b009f2c936
children 3cdaa81c3aca
rev   line source
tomwalters@0 1 // Copyright 2010, Thomas Walters
tomwalters@0 2 //
tomwalters@0 3 // AIM-C: A C++ implementation of the Auditory Image Model
tomwalters@0 4 // http://www.acousticscale.org/AIMC
tomwalters@0 5 //
tomwalters@45 6 // Licensed under the Apache License, Version 2.0 (the "License");
tomwalters@45 7 // you may not use this file except in compliance with the License.
tomwalters@45 8 // You may obtain a copy of the License at
tomwalters@0 9 //
tomwalters@45 10 // http://www.apache.org/licenses/LICENSE-2.0
tomwalters@0 11 //
tomwalters@45 12 // Unless required by applicable law or agreed to in writing, software
tomwalters@45 13 // distributed under the License is distributed on an "AS IS" BASIS,
tomwalters@45 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
tomwalters@45 15 // See the License for the specific language governing permissions and
tomwalters@45 16 // limitations under the License.
tomwalters@0 17
tomwalters@0 18 /*! \file
tomwalters@0 19 * \brief Base class for all AIM-C modules.
tomwalters@0 20 */
tomwalters@0 21
tomwalters@0 22 /*! \author: Thomas Walters <tom@acousticscale.org>
tomwalters@0 23 * \date 2010/01/23
tomwalters@0 24 * \version \$Id$
tomwalters@0 25 */
tomwalters@0 26
tomwalters@0 27 #include "Support/Module.h"
tomwalters@0 28
tomwalters@0 29 #include <utility>
tomwalters@0 30
tomwalters@0 31 namespace aimc {
tomwalters@0 32 using std::pair;
tomwalters@49 33 using std::ostream;
tomwalters@0 34 Module::Module(Parameters *parameters) {
tomwalters@0 35 initialized_ = false;
tomwalters@0 36 targets_.clear();
tomwalters@0 37 parameters_ = parameters;
tomwalters@0 38 module_identifier_ = "MODULE IDENTIFIER NOT SET";
tomwalters@0 39 module_type_ = "MODULE TYPE NOT SET";
tomwalters@0 40 module_description_ = "MODULE DESCRIPTION NOT SET";
tomwalters@0 41 module_version_ = "MODULE VERSION NOT SET";
tomwalters@0 42 };
tomwalters@0 43
tomwalters@0 44 Module::~Module() {
tomwalters@0 45 };
tomwalters@0 46
tomwalters@120 47 bool Module::Initialize(const SignalBank &input,
tomwalters@120 48 Parameters *global_parameters) {
tomwalters@120 49 global_parameters_ = global_parameters;
tomwalters@0 50 // Validate the input
tomwalters@0 51 if (!input.Validate()) {
tomwalters@6 52 LOG_ERROR(_T("Input SignalBank not valid"));
tomwalters@0 53 return false;
tomwalters@0 54 }
tomwalters@1 55 if (!InitializeInternal(input)) {
tomwalters@6 56 LOG_ERROR(_T("Initialization failed in module %s"),
tomwalters@6 57 module_identifier_.c_str());
tomwalters@0 58 return false;
tomwalters@0 59 }
tomwalters@0 60 // If the module has an output bank, then we can set up the targets
tomwalters@120 61 // of this module.
tomwalters@0 62 if (output_.initialized()) {
tomwalters@0 63 // Check that the output SignalBank has been set up correctly
tomwalters@0 64 if (!output_.Validate()) {
tomwalters@6 65 LOG_ERROR(_T("Output SignalBank not valid in module %s"),
tomwalters@6 66 module_identifier_.c_str());
tomwalters@0 67 return false;
tomwalters@0 68 }
tomwalters@0 69 // Iterate through all the targets of this module, initializing
tomwalters@0 70 // them if they have not already been initialized. If they have
tomwalters@0 71 // already been initialized, then they are assumed to have been
tomwalters@0 72 // set up to accept an input SignalBank of the correct form, but
tomwalters@0 73 // this is not checked.
tomwalters@0 74 set<Module*>::const_iterator it;
tomwalters@0 75 for (it = targets_.begin(); it != targets_.end(); ++it) {
tomwalters@0 76 if (!(*it)->initialized())
tomwalters@120 77 if (!(*it)->Initialize(output_, global_parameters_))
tomwalters@0 78 return false;
tomwalters@0 79 }
tomwalters@0 80 }
tomwalters@0 81 initialized_ = true;
tomwalters@0 82 return true;
tomwalters@0 83 }
tomwalters@0 84
tomwalters@3 85 void Module::Reset() {
tomwalters@3 86 if (!initialized_)
tomwalters@3 87 return;
tomwalters@3 88
tomwalters@3 89 ResetInternal();
tomwalters@3 90
tomwalters@3 91 // Iterate through all the targets of this module, resetting
tomwalters@3 92 // them.
tomwalters@3 93 set<Module*>::const_iterator it;
tomwalters@3 94 for (it = targets_.begin(); it != targets_.end(); ++it)
tomwalters@3 95 (*it)->Reset();
tomwalters@3 96 }
tomwalters@3 97
tomwalters@0 98 bool Module::initialized() const {
tomwalters@0 99 return initialized_;
tomwalters@0 100 }
tomwalters@0 101
tomwalters@0 102 bool Module::AddTarget(Module* target_module) {
tomwalters@120 103 bool result = false;
tomwalters@0 104 if (target_module) {
tomwalters@0 105 pair<set<Module*>::iterator, bool> ret;
tomwalters@0 106 ret = targets_.insert(target_module);
tomwalters@120 107 result = ret.second;
tomwalters@120 108 if (result) {
tomwalters@120 109 if(initialized_) {
tomwalters@120 110 if(output_.initialized()) {
tomwalters@120 111 if (!target_module->initialized()) {
tomwalters@120 112 target_module->Initialize(output_, global_parameters_);
tomwalters@120 113 }
tomwalters@120 114 }
tomwalters@120 115 }
tomwalters@120 116 }
tomwalters@0 117 }
tomwalters@120 118 return result;
tomwalters@0 119 }
tomwalters@0 120
tomwalters@9 121 bool Module::RemoveTarget(Module* target_module) {
tomwalters@0 122 if (targets_.erase(target_module) != 0)
tomwalters@0 123 return true;
tomwalters@0 124 return false;
tomwalters@0 125 }
tomwalters@0 126
tomwalters@9 127 void Module::RemoveAllTargets() {
tomwalters@0 128 targets_.clear();
tomwalters@0 129 }
tomwalters@0 130
tomwalters@0 131 const SignalBank* Module::GetOutputBank() const {
tomwalters@0 132 return &output_;
tomwalters@0 133 }
tomwalters@0 134
tomwalters@0 135 void Module::PushOutput() {
tomwalters@0 136 if (output_.initialized()) {
tomwalters@0 137 set<Module*>::const_iterator it;
tomwalters@0 138 for (it = targets_.begin(); it != targets_.end(); ++it) {
tomwalters@0 139 (*it)->Process(output_);
tomwalters@0 140 }
tomwalters@0 141 }
tomwalters@0 142 }
tomwalters@49 143
tomwalters@49 144 void Module::PrintTargets(ostream &out) {
tomwalters@49 145 out << id();
tomwalters@49 146 if (targets_.size() > 0) {
tomwalters@49 147 out << "->(";
tomwalters@49 148 set<Module*>::const_iterator it;
tomwalters@49 149 for (it = targets_.begin(); it != targets_.end(); ++it) {
tomwalters@49 150 (*it)->PrintTargets(out);
tomwalters@49 151 if (targets_.size() > 1) {
tomwalters@49 152 out << ",";
tomwalters@49 153 }
tomwalters@49 154 }
tomwalters@49 155 out << ")";
tomwalters@49 156 }
tomwalters@49 157 }
tomwalters@49 158
tomwalters@49 159 void Module::PrintVersions(ostream &out) {
tomwalters@49 160 out << version() << "\n";
tomwalters@49 161 set<Module*>::const_iterator it;
tomwalters@49 162 for (it = targets_.begin(); it != targets_.end(); ++it) {
tomwalters@49 163 (*it)->PrintVersions(out);
tomwalters@49 164 }
tomwalters@49 165 }
tomwalters@8 166 } // namespace aimc
tomwalters@0 167