ControlMode.cpp
Go to the documentation of this file.00001 /* ============================================================ 00002 * 00003 * This file is a part of RCI project 00004 * 00005 * Copyright (C) 2011 by Arne Nordmann <anordman at cor-lab dot uni-bielefeld dot de> 00006 * 00007 * This file may be licensed under the terms of the 00008 * GNU Lesser General Public License Version 3 (the ``LGPL''), 00009 * or (at your option) any later version. 00010 * 00011 * Software distributed under the License is distributed 00012 * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either 00013 * express or implied. See the LGPL for the specific language 00014 * governing rights and limitations. 00015 * 00016 * You should have received a copy of the LGPL along with this 00017 * program. If not, go to http://www.gnu.org/licenses/lgpl.html 00018 * or write to the Free Software Foundation, Inc., 00019 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00020 * 00021 * The development of this software was supported by: 00022 * CoR-Lab, Research Institute for Cognition and Robotics 00023 * Bielefeld University 00024 * 00025 * ============================================================ */ 00026 00027 #include "ControlMode.h" 00028 00029 #include <sstream> 00030 #include <algorithm> 00031 00032 using namespace std; 00033 00034 namespace rci { 00035 00036 ControlMode::ControlMode(string cname) : 00037 name(cname), contained() { 00038 } 00039 00040 ControlMode::~ControlMode() { 00041 } 00042 00043 std::string ControlMode::getName() const { 00044 return this->name; 00045 } 00046 00047 bool ControlMode::equals(const ControlModePtr &mode) const { 00048 if (this->numAspects() != mode->numAspects()) { 00049 return false; 00050 } 00051 // Since aspects are sorted, we can just compare aspect by aspect 00052 for (unsigned int asp = 0; asp < this->numAspects(); ++asp) { 00053 if (this->contained[asp] != mode->contained[asp]) { 00054 return false; 00055 } 00056 } 00057 return true; 00058 } 00059 00060 bool ControlMode::supports(const ControlAspectPtr &aspect) const { 00061 unsigned int num = this->numAspects(); 00062 if ((num == 0) && (!aspect->equals(noControl))) { 00063 return false; 00064 } 00065 for (unsigned int asp = 0; asp < num; ++asp) { 00066 if (this->contained[asp]->equals(aspect)) { 00067 return true; 00068 } 00069 } 00070 return false; 00071 } 00072 00073 void ControlMode::addControlAspect(ControlAspectPtr mode) { 00074 this->contained.push_back(mode); 00075 std::sort(this->contained.begin(), this->contained.end()); 00076 } 00077 00078 ControlModePtr ControlMode::combineWith(const ControlModePtr &mode, 00079 const std::string &nme) const { 00080 ControlMode *cm = new ControlMode(nme); 00081 00082 // Add control aspects of passed control mode 00083 for (unsigned int asp = 0; asp < this->contained.size(); ++asp) { 00084 cm->addControlAspect(this->contained[asp]); 00085 } 00086 00087 // Add my own control aspects 00088 for (unsigned int asp = 0; asp < mode->contained.size(); ++asp) { 00089 cm->addControlAspect(mode->contained[asp]); 00090 } 00091 00092 return ControlModePtr(cm); 00093 } 00094 00095 unsigned int ControlMode::numAspects() const { 00096 return this->contained.size(); 00097 } 00098 00099 std::string ControlMode::print() const { 00100 ostringstream outstream(ostringstream::out); 00101 outstream << "ControlMode<" << name << ">:" << endl; 00102 00103 // Add control aspects of passed control mode 00104 for (unsigned int asp = 0; asp < this->contained.size(); ++asp) { 00105 outstream << " " << this->contained[asp]->print() << endl; 00106 } 00107 00108 return outstream.str(); 00109 } 00110 00111 ControlModePtr ControlMode::create(string name) { 00112 ControlModePtr mode(new ControlMode(name)); 00113 return mode; 00114 } 00115 00116 ControlModePtr ControlMode::create(string name, ControlAspectPtr aspect) { 00117 ControlModePtr mode(new ControlMode(name)); 00118 mode->addControlAspect(aspect); 00119 return mode; 00120 } 00121 00122 ControlModePtr ControlMode::create(string name, ControlAspectPtr aspect1, 00123 ControlAspectPtr aspect2) { 00124 ControlModePtr mode(new ControlMode(name)); 00125 mode->addControlAspect(aspect1); 00126 mode->addControlAspect(aspect2); 00127 return mode; 00128 } 00129 00130 }
Generated on Thu Aug 2 14:02:47 2012 for RCI by 1.6.3