00001 /************************************************************************* 00002 * * 00003 * Open Physics Abstraction Layer * 00004 * Copyright (C) 2004-2005 * 00005 * Alan Fischer alan.fischer@gmail.com * 00006 * Andres Reinot andres@reinot.com * 00007 * Tyler Streeter tylerstreeter@gmail.com * 00008 * All rights reserved. * 00009 * Web: opal.sourceforge.net * 00010 * * 00011 * This library is free software; you can redistribute it and/or * 00012 * modify it under the terms of EITHER: * 00013 * (1) The GNU Lesser General Public License as published by the Free * 00014 * Software Foundation; either version 2.1 of the License, or (at * 00015 * your option) any later version. The text of the GNU Lesser * 00016 * General Public License is included with this library in the * 00017 * file license-LGPL.txt. * 00018 * (2) The BSD-style license that is included with this library in * 00019 * the file license-BSD.txt. * 00020 * * 00021 * This library is distributed in the hope that it will be useful, * 00022 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files * 00024 * license-LGPL.txt and license-BSD.txt for more details. * 00025 * * 00026 *************************************************************************/ 00027 00028 // class headers 00029 #include "BlueprintInstance.h" 00030 00031 // project headers 00032 #include "Solid.h" 00033 #include "Joint.h" 00034 #include "Motor.h" 00035 #include "Sensor.h" 00036 00037 // system headers 00038 #include <map> 00039 #include <vector> 00040 00041 using namespace std; 00042 00043 namespace opal 00044 { 00045 BlueprintInstance::BlueprintInstance() 00046 { 00047 // nothing to do 00048 } 00049 00050 BlueprintInstance::~BlueprintInstance() 00051 { 00052 mSolidMap.clear(); 00053 mJointMap.clear(); 00054 mMotorMap.clear(); 00055 mSensorMap.clear(); 00056 00057 mSolidList.clear(); 00058 mJointList.clear(); 00059 mMotorList.clear(); 00060 mSensorList.clear(); 00061 } 00062 00063 Solid * BlueprintInstance::detachSolid(unsigned int i) 00064 { 00065 assert( i < mSolidList.size() ); 00066 Solid * detached = mSolidList[i]; 00067 00068 // detach from list 00069 mSolidList.erase( mSolidList.begin() + i ); 00070 00071 // detach from map 00072 map<string, Solid*>::iterator iter = 00073 mSolidMap.find( detached->getName() ); 00074 mSolidMap.erase( iter ); 00075 00076 return detached; 00077 } 00078 00079 Joint * BlueprintInstance::detachJoint(unsigned int i) 00080 { 00081 assert( i < mJointList.size() ); 00082 Joint * detached = mJointList[i]; 00083 00084 // detach from list 00085 mJointList.erase( mJointList.begin() + i ); 00086 00087 // detach from map 00088 map<string, Joint*>::iterator iter = 00089 mJointMap.find( detached->getName() ); 00090 mJointMap.erase( iter ); 00091 00092 return detached; 00093 } 00094 00095 Sensor * BlueprintInstance::detachSensor(unsigned int i) 00096 { 00097 assert( i < mSensorList.size() ); 00098 Sensor * detached = mSensorList[i]; 00099 00100 // detach from list 00101 mSensorList.erase( mSensorList.begin() + i ); 00102 00103 // detach from map 00104 map<string, Sensor*>::iterator iter = 00105 mSensorMap.find( detached->getName() ); 00106 mSensorMap.erase( iter ); 00107 00108 return detached; 00109 } 00110 00111 Motor * BlueprintInstance::detachMotor(unsigned int i) 00112 { 00113 assert( i < mMotorList.size() ); 00114 Motor * detached = mMotorList[i]; 00115 00116 // detach from list 00117 mMotorList.erase( mMotorList.begin() + i ); 00118 00119 // detach from map 00120 map<string, Motor*>::iterator iter = 00121 mMotorMap.find( detached->getName() ); 00122 mMotorMap.erase( iter ); 00123 00124 return detached; 00125 } 00126 00127 Solid * BlueprintInstance::detachSolid( const string & name ) 00128 { 00129 map<string, Solid*>::iterator iter = 00130 mSolidMap.find( name ); 00131 Solid * detached = iter->second; 00132 00133 // detach from list 00134 vector<Solid *>::iterator listIter; 00135 for ( listIter = mSolidList.begin(); 00136 listIter != mSolidList.end(); 00137 ++listIter ) 00138 { 00139 if ( ( *listIter )->getName() == name ) 00140 { 00141 mSolidList.erase( listIter ); 00142 break; 00143 } 00144 } 00145 00146 // detach from map 00147 mSolidMap.erase( iter ); 00148 00149 return detached; 00150 } 00151 00152 Solid* BlueprintInstance::getSolid(const string& name)const 00153 { 00154 map<string, Solid*>::const_iterator iter = 00155 mSolidMap.find(name); 00156 if (mSolidMap.end() == iter) 00157 { 00158 return NULL; 00159 } 00160 else 00161 { 00162 return (*iter).second; 00163 } 00164 } 00165 00166 Joint* BlueprintInstance::getJoint(const string& name)const 00167 { 00168 map<string, Joint*>::const_iterator iter = 00169 mJointMap.find(name); 00170 if (mJointMap.end() == iter) 00171 { 00172 return NULL; 00173 } 00174 else 00175 { 00176 return (*iter).second; 00177 } 00178 } 00179 00180 Motor* BlueprintInstance::getMotor(const string& name)const 00181 { 00182 map<string, Motor*>::const_iterator iter = 00183 mMotorMap.find(name); 00184 if (mMotorMap.end() == iter) 00185 { 00186 return NULL; 00187 } 00188 else 00189 { 00190 return (*iter).second; 00191 } 00192 } 00193 00194 Sensor* BlueprintInstance::getSensor(const string& name)const 00195 { 00196 map<string, Sensor*>::const_iterator iter = 00197 mSensorMap.find(name); 00198 if (mSensorMap.end() == iter) 00199 { 00200 return NULL; 00201 } 00202 else 00203 { 00204 return (*iter).second; 00205 } 00206 } 00207 00208 unsigned int BlueprintInstance::getNumSolids()const 00209 { 00210 return (int)(mSolidList.size()); 00211 } 00212 00213 unsigned int BlueprintInstance::getNumJoints()const 00214 { 00215 return (int)(mJointList.size()); 00216 } 00217 00218 unsigned int BlueprintInstance::getNumMotors()const 00219 { 00220 return (int)(mMotorList.size()); 00221 } 00222 00223 unsigned int BlueprintInstance::getNumSensors()const 00224 { 00225 return (int)(mSensorList.size()); 00226 } 00227 00228 Solid* BlueprintInstance::getSolid(unsigned int i)const 00229 { 00230 return mSolidList.at(i); 00231 } 00232 00233 Joint* BlueprintInstance::getJoint(unsigned int i)const 00234 { 00235 return mJointList.at(i); 00236 } 00237 00238 Motor* BlueprintInstance::getMotor(unsigned int i)const 00239 { 00240 return mMotorList.at(i); 00241 } 00242 00243 Sensor* BlueprintInstance::getSensor(unsigned int i)const 00244 { 00245 return mSensorList.at(i); 00246 } 00247 00248 void BlueprintInstance::internal_addSolid(Solid* s) 00249 { 00250 mSolidList.push_back(s); 00251 00252 if (NULL == s) 00253 { 00254 return; 00255 } 00256 00257 string name = s->getName(); 00258 00259 if (name.empty()) 00260 { 00261 return; 00262 } 00263 00264 mSolidMap[name] = s; 00265 } 00266 00267 void BlueprintInstance::internal_addJoint(Joint* j) 00268 { 00269 mJointList.push_back(j); 00270 00271 if (NULL == j) 00272 { 00273 return; 00274 } 00275 00276 string name = j->getName(); 00277 00278 if (name.empty()) 00279 { 00280 return; 00281 } 00282 00283 mJointMap[name] = j; 00284 } 00285 00286 void BlueprintInstance::internal_addMotor(Motor* m) 00287 { 00288 mMotorList.push_back(m); 00289 00290 if (NULL == m) 00291 { 00292 return; 00293 } 00294 00295 string name = m->getName(); 00296 00297 if (name.empty()) 00298 { 00299 return; 00300 } 00301 00302 mMotorMap[name] = m; 00303 } 00304 00305 void BlueprintInstance::internal_addSensor(Sensor* s) 00306 { 00307 mSensorList.push_back(s); 00308 00309 if (NULL == s) 00310 { 00311 return; 00312 } 00313 00314 string name = s->getName(); 00315 00316 if (name.empty()) 00317 { 00318 return; 00319 } 00320 00321 mSensorMap[name] = s; 00322 } 00323 }