Libbarrett  1.2.4
include/barrett/systems/detail/summer-inl.h
00001 /*
00002         Copyright 2009, 2010 Barrett Technology <support@barrett.com>
00003 
00004         This file is part of libbarrett.
00005 
00006         This version of libbarrett is free software: you can redistribute it
00007         and/or modify it under the terms of the GNU General Public License as
00008         published by the Free Software Foundation, either version 3 of the
00009         License, or (at your option) any later version.
00010 
00011         This version of libbarrett is distributed in the hope that it will be
00012         useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
00013         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014         GNU General Public License for more details.
00015 
00016         You should have received a copy of the GNU General Public License along
00017         with this version of libbarrett.  If not, see
00018         <http://www.gnu.org/licenses/>.
00019 
00020         Further, non-binding information about licensing is available at:
00021         <http://wiki.barrett.com/libbarrett/wiki/LicenseNotes>
00022 */
00023 
00024 /*
00025  * summer-inl.h
00026  *
00027  *  Created on: Sep 11, 2009
00028  *      Author: dc
00029  */
00030 
00031 #include <boost/array.hpp>
00032 #include <string>
00033 #include <bitset>
00034 
00035 #include <barrett/detail/stl_utils.h>
00036 #include <barrett/math/traits.h>
00037 #include <barrett/systems/abstract/system.h>
00038 
00039 
00040 namespace barrett {
00041 namespace systems {
00042 
00043 
00044 template<typename T, size_t numInputs, bool RequiresAlignment>
00045 Summer<T, numInputs, RequiresAlignment>::Summer(const Polarity& inputPolarity, bool undefinedIsZero, const std::string& sysName) :
00046         System(sysName), SingleOutput<T>(this), polarity(inputPolarity), strict(!undefinedIsZero), sum()
00047 {
00048         initInputs();
00049 }
00050 
00051 template<typename T, size_t numInputs, bool RequiresAlignment>
00052 Summer<T, numInputs, RequiresAlignment>::Summer(const std::string& inputPolarity, bool undefinedIsZero, const std::string& sysName) :
00053         System(sysName), SingleOutput<T>(this), polarity(inputPolarity), strict(!undefinedIsZero), sum()
00054 {
00055         initInputs();
00056 }
00057 
00058 template<typename T, size_t numInputs, bool RequiresAlignment>
00059 Summer<T, numInputs, RequiresAlignment>::Summer(const char* inputPolarity, bool undefinedIsZero, const std::string& sysName) :
00060         System(sysName), SingleOutput<T>(this), polarity(inputPolarity), strict(!undefinedIsZero), sum()
00061 {
00062         initInputs();
00063 }
00064 
00065 template<typename T, size_t numInputs, bool RequiresAlignment>
00066 Summer<T, numInputs, RequiresAlignment>::Summer(const std::bitset<numInputs>& inputPolarity, bool undefinedIsZero, const std::string& sysName) :
00067         System(sysName), SingleOutput<T>(this), polarity(inputPolarity), strict(!undefinedIsZero), sum()
00068 {
00069         initInputs();
00070 }
00071 
00072 template<typename T, size_t numInputs, bool RequiresAlignment>
00073 Summer<T, numInputs, RequiresAlignment>::Summer(bool undefinedIsZero, const std::string& sysName) :
00074         System(sysName), SingleOutput<T>(this), polarity(), strict(!undefinedIsZero), sum()
00075 {
00076         initInputs();
00077 }
00078 
00079 template<typename T, size_t numInputs, bool RequiresAlignment>
00080 Summer<T, numInputs, RequiresAlignment>::~Summer()
00081 {
00082         mandatoryCleanUp();
00083         barrett::detail::purge(inputs);
00084 }
00085 
00086 template<typename T, size_t numInputs, bool RequiresAlignment>
00087 inline System::Input<T>& Summer<T, numInputs, RequiresAlignment>::getInput(const size_t i) {
00088         return *( inputs.at(i) );
00089 }
00090 
00091 template<typename T, size_t numInputs, bool RequiresAlignment>
00092 void Summer<T, numInputs, RequiresAlignment>::operate()
00093 {
00094         typedef math::Traits<T> Traits;
00095 
00096         sum = Traits::zero();
00097         for (size_t i = 0; i < numInputs; ++i) {
00098                 if (inputs[i]->valueDefined()) {
00099                         sum = Traits::add(sum, Traits::mult(polarity[i], inputs[i]->getValue()));
00100                 } else if (strict) {
00101                         this->outputValue->setUndefined();
00102                         return;
00103                 }
00104         }
00105 
00106         this->outputValue->setData(&sum);
00107 }
00108 
00109 template<typename T, size_t numInputs, bool RequiresAlignment>
00110 void Summer<T, numInputs, RequiresAlignment>::initInputs()
00111 {
00112         for (size_t i = 0; i < numInputs; ++i) {
00113                 inputs[i] = new Input<T>(this);
00114         }
00115 }
00116 
00117 
00118 }
00119 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Defines