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