annotate trunk/src/Support/Module.cc @ 268:e14c70d1b171

- Initial add of support code and modules. Not everything is working yet.
author tomwalters
date Fri, 12 Feb 2010 12:31:23 +0000
parents
children c26222c51fb7
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@268 6 // This program is free software: you can redistribute it and/or modify
tomwalters@268 7 // it under the terms of the GNU General Public License as published by
tomwalters@268 8 // the Free Software Foundation, either version 3 of the License, or
tomwalters@268 9 // (at your option) any later version.
tomwalters@268 10 //
tomwalters@268 11 // This program is distributed in the hope that it will be useful,
tomwalters@268 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
tomwalters@268 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
tomwalters@268 14 // GNU General Public License for more details.
tomwalters@268 15 //
tomwalters@268 16 // You should have received a copy of the GNU General Public License
tomwalters@268 17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
tomwalters@268 18
tomwalters@268 19 /*! \file
tomwalters@268 20 * \brief Base class for all AIM-C modules.
tomwalters@268 21 */
tomwalters@268 22
tomwalters@268 23 /*! \author: Thomas Walters <tom@acousticscale.org>
tomwalters@268 24 * \date 2010/01/23
tomwalters@268 25 * \version \$Id$
tomwalters@268 26 */
tomwalters@268 27
tomwalters@268 28 #include "Support/Module.h"
tomwalters@268 29
tomwalters@268 30 #include <utility>
tomwalters@268 31
tomwalters@268 32 namespace aimc {
tomwalters@268 33 using std::pair;
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@268 47 bool Module::Initialize(const SignalBank &input) {
tomwalters@268 48 // Validate the input
tomwalters@268 49 if (!input.Validate()) {
tomwalters@268 50 LOG_ERROR("Input SignalBank not valid");
tomwalters@268 51 return false;
tomwalters@268 52 }
tomwalters@268 53 if (!InitializeInternal(input)) {
tomwalters@268 54 LOG_ERROR("Module initialization failed");
tomwalters@268 55 return false;
tomwalters@268 56 }
tomwalters@268 57 // If the module has an output bank, then we can set up the targets
tomwalters@268 58 // of this module..
tomwalters@268 59 if (output_.initialized()) {
tomwalters@268 60 // Check that the output SignalBank has been set up correctly
tomwalters@268 61 if (!output_.Validate()) {
tomwalters@268 62 LOG_ERROR("Output SignalBank not valid");
tomwalters@268 63 return false;
tomwalters@268 64 }
tomwalters@268 65 // Iterate through all the targets of this module, initializing
tomwalters@268 66 // them if they have not already been initialized. If they have
tomwalters@268 67 // already been initialized, then they are assumed to have been
tomwalters@268 68 // set up to accept an input SignalBank of the correct form, but
tomwalters@268 69 // this is not checked.
tomwalters@268 70 set<Module*>::const_iterator it;
tomwalters@268 71 for (it = targets_.begin(); it != targets_.end(); ++it) {
tomwalters@268 72 if (!(*it)->initialized())
tomwalters@268 73 if (!(*it)->Initialize(output_))
tomwalters@268 74 return false;
tomwalters@268 75 }
tomwalters@268 76 }
tomwalters@268 77 initialized_ = true;
tomwalters@268 78 return true;
tomwalters@268 79 }
tomwalters@268 80
tomwalters@268 81 bool Module::initialized() const {
tomwalters@268 82 return initialized_;
tomwalters@268 83 }
tomwalters@268 84
tomwalters@268 85 bool Module::AddTarget(Module* target_module) {
tomwalters@268 86 if (target_module) {
tomwalters@268 87 pair<set<Module*>::iterator, bool> ret;
tomwalters@268 88 ret = targets_.insert(target_module);
tomwalters@268 89 return ret.second;
tomwalters@268 90 }
tomwalters@268 91 return false;
tomwalters@268 92 }
tomwalters@268 93
tomwalters@268 94 bool Module::DeleteTarget(Module* target_module) {
tomwalters@268 95 if (targets_.erase(target_module) != 0)
tomwalters@268 96 return true;
tomwalters@268 97 return false;
tomwalters@268 98 }
tomwalters@268 99
tomwalters@268 100 void Module::DeleteAllTargets() {
tomwalters@268 101 targets_.clear();
tomwalters@268 102 }
tomwalters@268 103
tomwalters@268 104 const SignalBank* Module::GetOutputBank() const {
tomwalters@268 105 return &output_;
tomwalters@268 106 }
tomwalters@268 107
tomwalters@268 108 void Module::PushOutput() {
tomwalters@268 109 if (output_.initialized()) {
tomwalters@268 110 set<Module*>::const_iterator it;
tomwalters@268 111 for (it = targets_.begin(); it != targets_.end(); ++it) {
tomwalters@268 112 (*it)->Process(output_);
tomwalters@268 113 }
tomwalters@268 114 }
tomwalters@268 115 }
tomwalters@268 116 } // namespace aimc
tomwalters@268 117