Libbarrett  1.2.4
include/barrett/systems/detail/summer-polarity-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-polarity-inl.h
00026  *
00027  *  Created on: Sep 12, 2009
00028  *      Author: dc
00029  */
00030 
00031 #include <string>
00032 #include <stdexcept>
00033 
00034 
00035 namespace barrett {
00036 namespace systems {
00037 
00038 
00039 template<typename T, size_t numInputs, bool RequiresAlignment>
00040 Summer<T, numInputs, RequiresAlignment>::Polarity::Polarity() :  // default: all positive
00041         polarity()
00042 {
00043         polarity.set();
00044 }
00045 
00046 template<typename T, size_t numInputs, bool RequiresAlignment>
00047 Summer<T, numInputs, RequiresAlignment>::Polarity::Polarity(std::string polarityStr)
00048 throw(std::invalid_argument) :
00049         polarity()
00050 {
00051         if (polarityStr.size() != numInputs) {
00052                 throw std::invalid_argument("(systems::Summer::Polarity::Polarity): "
00053                                             "polarityStr must be of the same length "
00054                                             "as the number of inputs to the Summer.");
00055         }
00056 
00057         for (size_t i = 0; i < numInputs; ++i) {
00058                 switch (polarityStr[i]) {
00059                 case '+':
00060                         polarity.set(i);
00061                         break;
00062                 case '-':
00063                         polarity.reset(i);
00064                         break;
00065                 default:
00066                         throw std::invalid_argument(
00067                                         "(systems::Summer::Polarity::Polarity): polarityStr must "
00068                                         "contain only '+' and '-' characters.");
00069                         break;
00070                 }
00071         }
00072 }
00073 
00074 template<typename T, size_t numInputs, bool RequiresAlignment>
00075 inline int Summer<T, numInputs, RequiresAlignment>::Polarity::operator[] (const size_t i) const
00076 {
00077         return polarity[i] ? 1 : -1;
00078 }
00079 
00080 
00081 }
00082 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Defines