annotate src/Support/Module.cc @ 611:0fbaf443ec82

Carfac C++ revision 3, indluding more style improvements. The output structs are now classes again, and have separate storage methods for each output structure along with flags in the Run and RunSegment methods to allow for only storing NAPs if desired.
author alexbrandmeyer
date Fri, 17 May 2013 19:52:45 +0000
parents af531fc3f280
children
rev   line source
tomwalters@0 1 // Copyright 2010, Thomas Walters
tomwalters@0 2 //
tomwalters@0 3 // AIM-C: A C++ implementation of the Auditory Image Model
tomwalters@0 4 // http://www.acousticscale.org/AIMC
tomwalters@0 5 //
tomwalters@45 6 // Licensed under the Apache License, Version 2.0 (the "License");
tomwalters@45 7 // you may not use this file except in compliance with the License.
tomwalters@45 8 // You may obtain a copy of the License at
tomwalters@0 9 //
tomwalters@45 10 // http://www.apache.org/licenses/LICENSE-2.0
tomwalters@0 11 //
tomwalters@45 12 // Unless required by applicable law or agreed to in writing, software
tomwalters@45 13 // distributed under the License is distributed on an "AS IS" BASIS,
tomwalters@45 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
tomwalters@45 15 // See the License for the specific language governing permissions and
tomwalters@45 16 // limitations under the License.
tomwalters@0 17
tomwalters@0 18 /*! \file
tomwalters@0 19 * \brief Base class for all AIM-C modules.
tomwalters@0 20 */
tomwalters@0 21
tomwalters@0 22 /*! \author: Thomas Walters <tom@acousticscale.org>
tomwalters@0 23 * \date 2010/01/23
tomwalters@0 24 * \version \$Id$
tomwalters@0 25 */
tomwalters@0 26
tomwalters@0 27 #include "Support/Module.h"
tomwalters@0 28
tomwalters@0 29 #include <utility>
tomwalters@0 30
tomwalters@0 31 namespace aimc {
tomwalters@0 32 using std::pair;
tomwalters@49 33 using std::ostream;
tomwalters@232 34 using std::endl;
tomwalters@0 35 Module::Module(Parameters *parameters) {
tomwalters@0 36 initialized_ = false;
tomwalters@0 37 targets_.clear();
tomwalters@0 38 parameters_ = parameters;
tomwalters@0 39 module_identifier_ = "MODULE IDENTIFIER NOT SET";
tomwalters@0 40 module_type_ = "MODULE TYPE NOT SET";
tomwalters@0 41 module_description_ = "MODULE DESCRIPTION NOT SET";
tomwalters@0 42 module_version_ = "MODULE VERSION NOT SET";
tomwalters@232 43 instance_name_ = "";
tomwalters@232 44 done_ = false;
tomwalters@0 45 };
tomwalters@0 46
tomwalters@0 47 Module::~Module() {
tomwalters@0 48 };
tomwalters@0 49
tomwalters@231 50 bool Module::Initialize(const SignalBank &input,
tomwalters@231 51 Parameters *global_parameters) {
tomwalters@232 52 if (global_parameters == NULL) {
tomwalters@232 53 return false;
tomwalters@232 54 }
tomwalters@231 55 global_parameters_ = global_parameters;
tomwalters@0 56 // Validate the input
tomwalters@0 57 if (!input.Validate()) {
tomwalters@6 58 LOG_ERROR(_T("Input SignalBank not valid"));
tomwalters@0 59 return false;
tomwalters@0 60 }
tomwalters@1 61 if (!InitializeInternal(input)) {
tomwalters@6 62 LOG_ERROR(_T("Initialization failed in module %s"),
tomwalters@6 63 module_identifier_.c_str());
tomwalters@0 64 return false;
tomwalters@0 65 }
tomwalters@0 66 // If the module has an output bank, then we can set up the targets
tomwalters@231 67 // of this module.
tomwalters@0 68 if (output_.initialized()) {
tomwalters@0 69 // Check that the output SignalBank has been set up correctly
tomwalters@0 70 if (!output_.Validate()) {
tomwalters@6 71 LOG_ERROR(_T("Output SignalBank not valid in module %s"),
tomwalters@6 72 module_identifier_.c_str());
tomwalters@0 73 return false;
tomwalters@0 74 }
tomwalters@0 75 // Iterate through all the targets of this module, initializing
tomwalters@0 76 // them if they have not already been initialized. If they have
tomwalters@0 77 // already been initialized, then they are assumed to have been
tomwalters@0 78 // set up to accept an input SignalBank of the correct form, but
tomwalters@0 79 // this is not checked.
tomwalters@0 80 set<Module*>::const_iterator it;
tomwalters@0 81 for (it = targets_.begin(); it != targets_.end(); ++it) {
tomwalters@232 82 //if (!(*it)->initialized())
tomwalters@231 83 if (!(*it)->Initialize(output_, global_parameters_))
tomwalters@0 84 return false;
tomwalters@0 85 }
tomwalters@0 86 }
tomwalters@0 87 initialized_ = true;
tomwalters@0 88 return true;
tomwalters@0 89 }
tomwalters@0 90
tomwalters@3 91 void Module::Reset() {
tomwalters@3 92 if (!initialized_)
tomwalters@3 93 return;
tomwalters@3 94
tomwalters@3 95 ResetInternal();
tomwalters@3 96
tomwalters@3 97 // Iterate through all the targets of this module, resetting
tomwalters@3 98 // them.
tomwalters@3 99 set<Module*>::const_iterator it;
tomwalters@3 100 for (it = targets_.begin(); it != targets_.end(); ++it)
tomwalters@3 101 (*it)->Reset();
tomwalters@3 102 }
tomwalters@3 103
tomwalters@0 104 bool Module::initialized() const {
tomwalters@0 105 return initialized_;
tomwalters@0 106 }
tomwalters@0 107
tomwalters@0 108 bool Module::AddTarget(Module* target_module) {
tomwalters@231 109 bool result = false;
tomwalters@0 110 if (target_module) {
tomwalters@0 111 pair<set<Module*>::iterator, bool> ret;
tomwalters@0 112 ret = targets_.insert(target_module);
tomwalters@231 113 result = ret.second;
tomwalters@231 114 if (result) {
tomwalters@232 115 if (initialized_) {
tomwalters@232 116 if (output_.initialized()) {
tomwalters@231 117 if (!target_module->initialized()) {
tomwalters@231 118 target_module->Initialize(output_, global_parameters_);
tomwalters@231 119 }
tomwalters@231 120 }
tomwalters@231 121 }
tomwalters@231 122 }
tomwalters@0 123 }
tomwalters@231 124 return result;
tomwalters@0 125 }
tomwalters@0 126
tomwalters@9 127 bool Module::RemoveTarget(Module* target_module) {
tomwalters@0 128 if (targets_.erase(target_module) != 0)
tomwalters@0 129 return true;
tomwalters@0 130 return false;
tomwalters@0 131 }
tomwalters@0 132
tomwalters@9 133 void Module::RemoveAllTargets() {
tomwalters@0 134 targets_.clear();
tomwalters@0 135 }
tomwalters@0 136
tomwalters@0 137 const SignalBank* Module::GetOutputBank() const {
tomwalters@0 138 return &output_;
tomwalters@0 139 }
tomwalters@0 140
tomwalters@0 141 void Module::PushOutput() {
tomwalters@0 142 if (output_.initialized()) {
tomwalters@0 143 set<Module*>::const_iterator it;
tomwalters@0 144 for (it = targets_.begin(); it != targets_.end(); ++it) {
tomwalters@0 145 (*it)->Process(output_);
tomwalters@0 146 }
tomwalters@0 147 }
tomwalters@0 148 }
tomwalters@49 149
tomwalters@232 150 void Module::PrintTargetsForDot(ostream &out) {
tomwalters@232 151 //string parameters_string = parameters_->WriteString();
tomwalters@232 152 out << " " << instance_name() << " [shape = none, margin = 0, label = <" << endl;
tomwalters@232 153 out << " <TABLE BORDER=\"0\" CELLBORDER=\"1\" CELLSPACING=\"0\" CELLPADDING=\"4\"> " << endl;
tomwalters@232 154 out << " <TR><TD>" << instance_name() << "</TD></TR><TR><TD>" << id();
tomwalters@232 155 out << "</TD></TR></TABLE>>]" << ";" << endl;
tomwalters@232 156 // <TD><TR>" << parameters_string << "</TD></TR>
tomwalters@232 157 set<Module*>::const_iterator it;
tomwalters@232 158 for (it = targets_.begin(); it != targets_.end(); ++it) {
tomwalters@232 159 out << " " << instance_name() << " -> " << (*it)->instance_name() << ";" << endl;
tomwalters@232 160 (*it)->PrintTargetsForDot(out);
tomwalters@49 161 }
tomwalters@49 162 }
tomwalters@49 163
tomwalters@232 164 void Module::PrintTargets(ostream &out) {
tomwalters@49 165 set<Module*>::const_iterator it;
tomwalters@49 166 for (it = targets_.begin(); it != targets_.end(); ++it) {
tomwalters@232 167 out << " " << instance_name() << " -> " << (*it)->instance_name() << ";" << endl;
tomwalters@232 168 (*it)->PrintTargets(out);
tomwalters@232 169 }
tomwalters@232 170 }
tomwalters@232 171
tomwalters@232 172 void Module::PrintConfiguration(ostream &out) {
tomwalters@232 173 out << "# " << id() << endl;
tomwalters@232 174 out << "# " << instance_name() << endl;
tomwalters@232 175 out << "# " << version() << endl;
tomwalters@232 176 string parameters_string = parameters_->WriteString();
tomwalters@232 177 out << parameters_string << endl;
tomwalters@232 178 set<Module*>::const_iterator it;
tomwalters@232 179 for (it = targets_.begin(); it != targets_.end(); ++it) {
tomwalters@232 180 (*it)->PrintConfiguration(out);
tomwalters@49 181 }
tomwalters@49 182 }
tomwalters@8 183 } // namespace aimc
tomwalters@0 184