tomwalters@0: // Copyright 2010, Thomas Walters tomwalters@0: // tomwalters@0: // AIM-C: A C++ implementation of the Auditory Image Model tomwalters@0: // http://www.acousticscale.org/AIMC tomwalters@0: // tomwalters@45: // Licensed under the Apache License, Version 2.0 (the "License"); tomwalters@45: // you may not use this file except in compliance with the License. tomwalters@45: // You may obtain a copy of the License at tomwalters@0: // tomwalters@45: // http://www.apache.org/licenses/LICENSE-2.0 tomwalters@0: // tomwalters@45: // Unless required by applicable law or agreed to in writing, software tomwalters@45: // distributed under the License is distributed on an "AS IS" BASIS, tomwalters@45: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. tomwalters@45: // See the License for the specific language governing permissions and tomwalters@45: // limitations under the License. tomwalters@0: tomwalters@0: /*! \file tomwalters@0: * \brief Base class for all AIM-C modules. tomwalters@0: */ tomwalters@0: tomwalters@0: /*! \author: Thomas Walters tomwalters@0: * \date 2010/01/23 tomwalters@0: * \version \$Id$ tomwalters@0: */ tomwalters@0: tomwalters@0: #include "Support/Module.h" tomwalters@0: tomwalters@0: #include tomwalters@0: tomwalters@0: namespace aimc { tomwalters@0: using std::pair; tomwalters@49: using std::ostream; tomwalters@232: using std::endl; tomwalters@0: Module::Module(Parameters *parameters) { tomwalters@0: initialized_ = false; tomwalters@0: targets_.clear(); tomwalters@0: parameters_ = parameters; tomwalters@0: module_identifier_ = "MODULE IDENTIFIER NOT SET"; tomwalters@0: module_type_ = "MODULE TYPE NOT SET"; tomwalters@0: module_description_ = "MODULE DESCRIPTION NOT SET"; tomwalters@0: module_version_ = "MODULE VERSION NOT SET"; tomwalters@232: instance_name_ = ""; tomwalters@232: done_ = false; tomwalters@0: }; tomwalters@0: tomwalters@0: Module::~Module() { tomwalters@0: }; tomwalters@0: tomwalters@231: bool Module::Initialize(const SignalBank &input, tomwalters@231: Parameters *global_parameters) { tomwalters@232: if (global_parameters == NULL) { tomwalters@232: return false; tomwalters@232: } tomwalters@231: global_parameters_ = global_parameters; tomwalters@0: // Validate the input tomwalters@0: if (!input.Validate()) { tomwalters@6: LOG_ERROR(_T("Input SignalBank not valid")); tomwalters@0: return false; tomwalters@0: } tomwalters@1: if (!InitializeInternal(input)) { tomwalters@6: LOG_ERROR(_T("Initialization failed in module %s"), tomwalters@6: module_identifier_.c_str()); tomwalters@0: return false; tomwalters@0: } tomwalters@0: // If the module has an output bank, then we can set up the targets tomwalters@231: // of this module. tomwalters@0: if (output_.initialized()) { tomwalters@0: // Check that the output SignalBank has been set up correctly tomwalters@0: if (!output_.Validate()) { tomwalters@6: LOG_ERROR(_T("Output SignalBank not valid in module %s"), tomwalters@6: module_identifier_.c_str()); tomwalters@0: return false; tomwalters@0: } tomwalters@0: // Iterate through all the targets of this module, initializing tomwalters@0: // them if they have not already been initialized. If they have tomwalters@0: // already been initialized, then they are assumed to have been tomwalters@0: // set up to accept an input SignalBank of the correct form, but tomwalters@0: // this is not checked. tomwalters@0: set::const_iterator it; tomwalters@0: for (it = targets_.begin(); it != targets_.end(); ++it) { tomwalters@232: //if (!(*it)->initialized()) tomwalters@231: if (!(*it)->Initialize(output_, global_parameters_)) tomwalters@0: return false; tomwalters@0: } tomwalters@0: } tomwalters@0: initialized_ = true; tomwalters@0: return true; tomwalters@0: } tomwalters@0: tomwalters@3: void Module::Reset() { tomwalters@3: if (!initialized_) tomwalters@3: return; tomwalters@3: tomwalters@3: ResetInternal(); tomwalters@3: tomwalters@3: // Iterate through all the targets of this module, resetting tomwalters@3: // them. tomwalters@3: set::const_iterator it; tomwalters@3: for (it = targets_.begin(); it != targets_.end(); ++it) tomwalters@3: (*it)->Reset(); tomwalters@3: } tomwalters@3: tomwalters@0: bool Module::initialized() const { tomwalters@0: return initialized_; tomwalters@0: } tomwalters@0: tomwalters@0: bool Module::AddTarget(Module* target_module) { tomwalters@231: bool result = false; tomwalters@0: if (target_module) { tomwalters@0: pair::iterator, bool> ret; tomwalters@0: ret = targets_.insert(target_module); tomwalters@231: result = ret.second; tomwalters@231: if (result) { tomwalters@232: if (initialized_) { tomwalters@232: if (output_.initialized()) { tomwalters@231: if (!target_module->initialized()) { tomwalters@231: target_module->Initialize(output_, global_parameters_); tomwalters@231: } tomwalters@231: } tomwalters@231: } tomwalters@231: } tomwalters@0: } tomwalters@231: return result; tomwalters@0: } tomwalters@0: tomwalters@9: bool Module::RemoveTarget(Module* target_module) { tomwalters@0: if (targets_.erase(target_module) != 0) tomwalters@0: return true; tomwalters@0: return false; tomwalters@0: } tomwalters@0: tomwalters@9: void Module::RemoveAllTargets() { tomwalters@0: targets_.clear(); tomwalters@0: } tomwalters@0: tomwalters@0: const SignalBank* Module::GetOutputBank() const { tomwalters@0: return &output_; tomwalters@0: } tomwalters@0: tomwalters@0: void Module::PushOutput() { tomwalters@0: if (output_.initialized()) { tomwalters@0: set::const_iterator it; tomwalters@0: for (it = targets_.begin(); it != targets_.end(); ++it) { tomwalters@0: (*it)->Process(output_); tomwalters@0: } tomwalters@0: } tomwalters@0: } tomwalters@49: tomwalters@232: void Module::PrintTargetsForDot(ostream &out) { tomwalters@232: //string parameters_string = parameters_->WriteString(); tomwalters@232: out << " " << instance_name() << " [shape = none, margin = 0, label = <" << endl; tomwalters@232: out << " " << endl; tomwalters@232: out << "
" << instance_name() << "
" << id(); tomwalters@232: out << "
>]" << ";" << endl; tomwalters@232: // " << parameters_string << " tomwalters@232: set::const_iterator it; tomwalters@232: for (it = targets_.begin(); it != targets_.end(); ++it) { tomwalters@232: out << " " << instance_name() << " -> " << (*it)->instance_name() << ";" << endl; tomwalters@232: (*it)->PrintTargetsForDot(out); tomwalters@49: } tomwalters@49: } tomwalters@49: tomwalters@232: void Module::PrintTargets(ostream &out) { tomwalters@49: set::const_iterator it; tomwalters@49: for (it = targets_.begin(); it != targets_.end(); ++it) { tomwalters@232: out << " " << instance_name() << " -> " << (*it)->instance_name() << ";" << endl; tomwalters@232: (*it)->PrintTargets(out); tomwalters@232: } tomwalters@232: } tomwalters@232: tomwalters@232: void Module::PrintConfiguration(ostream &out) { tomwalters@232: out << "# " << id() << endl; tomwalters@232: out << "# " << instance_name() << endl; tomwalters@232: out << "# " << version() << endl; tomwalters@232: string parameters_string = parameters_->WriteString(); tomwalters@232: out << parameters_string << endl; tomwalters@232: set::const_iterator it; tomwalters@232: for (it = targets_.begin(); it != targets_.end(); ++it) { tomwalters@232: (*it)->PrintConfiguration(out); tomwalters@49: } tomwalters@49: } tomwalters@8: } // namespace aimc tomwalters@0: