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@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@268: }; tomwalters@268: tomwalters@268: Module::~Module() { tomwalters@268: }; tomwalters@268: tomwalters@268: bool Module::Initialize(const SignalBank &input) { tomwalters@296: // LOG_INFO_NN(_T("-> %s "), module_identifier_.c_str()); 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@268: // 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@268: if (!(*it)->initialized()) tomwalters@268: if (!(*it)->Initialize(output_)) tomwalters@268: return false; tomwalters@268: } tomwalters@278: } else { tomwalters@296: // LOG_INFO(_T("|")); 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@296: // LOG_INFO("Resetting module %s", module_identifier_.c_str()); 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@268: if (target_module) { tomwalters@268: pair::iterator, bool> ret; tomwalters@268: ret = targets_.insert(target_module); tomwalters@268: return ret.second; tomwalters@268: } tomwalters@268: return false; 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@323: void Module::PrintTargets(ostream &out) { tomwalters@323: out << id(); tomwalters@323: if (targets_.size() > 0) { tomwalters@323: out << "->("; tomwalters@323: set::const_iterator it; tomwalters@323: for (it = targets_.begin(); it != targets_.end(); ++it) { tomwalters@323: (*it)->PrintTargets(out); tomwalters@323: if (targets_.size() > 1) { tomwalters@323: out << ","; tomwalters@323: } tomwalters@323: } tomwalters@323: out << ")"; tomwalters@323: } tomwalters@323: } tomwalters@323: tomwalters@323: void Module::PrintVersions(ostream &out) { tomwalters@323: out << version() << "\n"; tomwalters@323: set::const_iterator it; tomwalters@323: for (it = targets_.begin(); it != targets_.end(); ++it) { tomwalters@323: (*it)->PrintVersions(out); tomwalters@323: } tomwalters@323: } tomwalters@280: } // namespace aimc tomwalters@268: