annotate trunk/src/Support/Module.cc @ 319:566a8543a6f1

Created wiki page through web user interface.
author tomwalters@google.com
date Wed, 19 May 2010 15:28:10 +0000
parents 30dde71d0230
children 0f54006e91ea
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@268 33 Module::Module(Parameters *parameters) {
tomwalters@268 34 initialized_ = false;
tomwalters@268 35 targets_.clear();
tomwalters@268 36 parameters_ = parameters;
tomwalters@268 37 module_identifier_ = "MODULE IDENTIFIER NOT SET";
tomwalters@268 38 module_type_ = "MODULE TYPE NOT SET";
tomwalters@268 39 module_description_ = "MODULE DESCRIPTION NOT SET";
tomwalters@268 40 module_version_ = "MODULE VERSION NOT SET";
tomwalters@268 41 };
tomwalters@268 42
tomwalters@268 43 Module::~Module() {
tomwalters@268 44 };
tomwalters@268 45
tomwalters@268 46 bool Module::Initialize(const SignalBank &input) {
tomwalters@296 47 // LOG_INFO_NN(_T("-> %s "), module_identifier_.c_str());
tomwalters@268 48 // Validate the input
tomwalters@268 49 if (!input.Validate()) {
tomwalters@278 50 LOG_ERROR(_T("Input SignalBank not valid"));
tomwalters@268 51 return false;
tomwalters@268 52 }
tomwalters@273 53 if (!InitializeInternal(input)) {
tomwalters@278 54 LOG_ERROR(_T("Initialization failed in module %s"),
tomwalters@278 55 module_identifier_.c_str());
tomwalters@268 56 return false;
tomwalters@268 57 }
tomwalters@268 58 // If the module has an output bank, then we can set up the targets
tomwalters@268 59 // of this module..
tomwalters@268 60 if (output_.initialized()) {
tomwalters@268 61 // Check that the output SignalBank has been set up correctly
tomwalters@268 62 if (!output_.Validate()) {
tomwalters@278 63 LOG_ERROR(_T("Output SignalBank not valid in module %s"),
tomwalters@278 64 module_identifier_.c_str());
tomwalters@268 65 return false;
tomwalters@268 66 }
tomwalters@268 67 // Iterate through all the targets of this module, initializing
tomwalters@268 68 // them if they have not already been initialized. If they have
tomwalters@268 69 // already been initialized, then they are assumed to have been
tomwalters@268 70 // set up to accept an input SignalBank of the correct form, but
tomwalters@268 71 // this is not checked.
tomwalters@268 72 set<Module*>::const_iterator it;
tomwalters@268 73 for (it = targets_.begin(); it != targets_.end(); ++it) {
tomwalters@268 74 if (!(*it)->initialized())
tomwalters@268 75 if (!(*it)->Initialize(output_))
tomwalters@268 76 return false;
tomwalters@268 77 }
tomwalters@278 78 } else {
tomwalters@296 79 // LOG_INFO(_T("|"));
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@296 89 // LOG_INFO("Resetting module %s", module_identifier_.c_str());
tomwalters@275 90 ResetInternal();
tomwalters@275 91
tomwalters@275 92 // Iterate through all the targets of this module, resetting
tomwalters@275 93 // them.
tomwalters@275 94 set<Module*>::const_iterator it;
tomwalters@275 95 for (it = targets_.begin(); it != targets_.end(); ++it)
tomwalters@275 96 (*it)->Reset();
tomwalters@275 97 }
tomwalters@275 98
tomwalters@268 99 bool Module::initialized() const {
tomwalters@268 100 return initialized_;
tomwalters@268 101 }
tomwalters@268 102
tomwalters@268 103 bool Module::AddTarget(Module* target_module) {
tomwalters@268 104 if (target_module) {
tomwalters@268 105 pair<set<Module*>::iterator, bool> ret;
tomwalters@268 106 ret = targets_.insert(target_module);
tomwalters@268 107 return ret.second;
tomwalters@268 108 }
tomwalters@268 109 return false;
tomwalters@268 110 }
tomwalters@268 111
tomwalters@281 112 bool Module::RemoveTarget(Module* target_module) {
tomwalters@268 113 if (targets_.erase(target_module) != 0)
tomwalters@268 114 return true;
tomwalters@268 115 return false;
tomwalters@268 116 }
tomwalters@268 117
tomwalters@281 118 void Module::RemoveAllTargets() {
tomwalters@268 119 targets_.clear();
tomwalters@268 120 }
tomwalters@268 121
tomwalters@268 122 const SignalBank* Module::GetOutputBank() const {
tomwalters@268 123 return &output_;
tomwalters@268 124 }
tomwalters@268 125
tomwalters@268 126 void Module::PushOutput() {
tomwalters@268 127 if (output_.initialized()) {
tomwalters@268 128 set<Module*>::const_iterator it;
tomwalters@268 129 for (it = targets_.begin(); it != targets_.end(); ++it) {
tomwalters@268 130 (*it)->Process(output_);
tomwalters@268 131 }
tomwalters@268 132 }
tomwalters@268 133 }
tomwalters@280 134 } // namespace aimc
tomwalters@268 135