annotate trunk/src/Support/Module.cc @ 401:b71ec2cbe55b

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