00001 /* 00002 * Copyright (C) 2006-2011, SRI International (R) 00003 * 00004 * This program is free software: you can redistribute it and/or modify 00005 * it under the terms of the GNU Lesser General Public License as published by 00006 * the Free Software Foundation, either version 3 of the License, or 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU Lesser General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU Lesser General Public License 00015 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 */ 00017 00018 #include <map> 00019 #include <iostream> 00020 00021 #ifdef USE_TBB 00022 #include <tbb/mutex.h> 00023 #endif 00024 00025 #include <OpenKarto/SensorRegistry.h> 00026 #include <OpenKarto/Exception.h> 00027 #include <OpenKarto/Sensor.h> 00028 #include <OpenKarto/Logger.h> 00029 00030 namespace karto 00031 { 00032 00036 00037 // Note to not change the Sensor* to SensorPtr or SmartPointer<Sensor>. 00038 struct SensorRegistryPrivate 00039 { 00040 List<Sensor*> m_Sensors; 00041 00042 typedef std::map<Identifier, Sensor*> SensorManagerMap; 00043 SensorManagerMap m_SensorMap; 00044 }; 00045 00046 SensorRegistry::SensorRegistry() 00047 : m_pSensorRegistryPrivate(new SensorRegistryPrivate()) 00048 { 00049 } 00050 00051 SensorRegistry::~SensorRegistry() 00052 { 00053 m_pSensorRegistryPrivate->m_Sensors.Clear(); 00054 delete m_pSensorRegistryPrivate; 00055 } 00056 00057 SensorRegistry* SensorRegistry::GetInstance() 00058 { 00059 #ifdef USE_TBB 00060 static tbb::mutex myMutex; 00061 tbb::mutex::scoped_lock lock(myMutex); 00062 #endif 00063 00064 static SmartPointer<SensorRegistry> sInstance = new SensorRegistry(); 00065 return sInstance; 00066 } 00067 00068 void SensorRegistry::RegisterSensor(Sensor* pSensor) 00069 { 00070 if (pSensor != NULL) 00071 { 00072 if (pSensor->GetIdentifier().GetScope() != "Karto/System") 00073 { 00074 Validate(pSensor); 00075 00076 Log(LOG_DEBUG, String("Registering sensor: [") + pSensor->GetIdentifier().ToString() + "]"); 00077 } 00078 00079 if ((m_pSensorRegistryPrivate->m_SensorMap.find(karto::Identifier(pSensor->GetIdentifier())) != m_pSensorRegistryPrivate->m_SensorMap.end())) 00080 { 00081 String errorMessage; 00082 errorMessage.Append("Cannot register sensor: already registered: ["); 00083 errorMessage.Append(pSensor->GetIdentifier().ToString()); 00084 errorMessage.Append("]"); 00085 00086 throw Exception(errorMessage); 00087 } 00088 00089 m_pSensorRegistryPrivate->m_SensorMap[karto::Identifier(pSensor->GetIdentifier())] = pSensor; 00090 m_pSensorRegistryPrivate->m_Sensors.Add(pSensor); 00091 } 00092 } 00093 00094 void SensorRegistry::UnregisterSensor(Sensor* pSensor) 00095 { 00096 if (pSensor != NULL) 00097 { 00098 if (pSensor->GetIdentifier().GetScope() != "Karto/System") 00099 { 00100 Log(LOG_DEBUG, String("Unregistering sensor: [") + pSensor->GetIdentifier().ToString() + "]"); 00101 } 00102 00103 if (m_pSensorRegistryPrivate->m_SensorMap.find(pSensor->GetIdentifier()) != m_pSensorRegistryPrivate->m_SensorMap.end()) 00104 { 00105 m_pSensorRegistryPrivate->m_SensorMap.erase(pSensor->GetIdentifier()); 00106 00107 m_pSensorRegistryPrivate->m_Sensors.Remove(pSensor); 00108 } 00109 else 00110 { 00111 String errorMessage; 00112 errorMessage.Append("Cannot unregister sensor: not registered: ["); 00113 errorMessage.Append(pSensor->GetIdentifier().ToString()); 00114 errorMessage.Append("]"); 00115 00116 throw Exception(errorMessage); 00117 } 00118 } 00119 } 00120 00121 Sensor* SensorRegistry::GetSensorByName(const Identifier& rName) 00122 { 00123 if (m_pSensorRegistryPrivate->m_SensorMap.find(rName) != m_pSensorRegistryPrivate->m_SensorMap.end()) 00124 { 00125 Sensor* pSensor = m_pSensorRegistryPrivate->m_SensorMap[rName]; 00126 00127 assert(pSensor != NULL); 00128 00129 return pSensor; 00130 } 00131 00132 String errorMessage; 00133 errorMessage.Append("Sensor not registered: ["); 00134 errorMessage.Append(rName.ToString()); 00135 errorMessage.Append("]"); 00136 throw Exception(errorMessage); 00137 } 00138 00139 void SensorRegistry::Clear() 00140 { 00141 m_pSensorRegistryPrivate->m_Sensors.Clear(); 00142 m_pSensorRegistryPrivate->m_SensorMap.clear(); 00143 } 00144 00145 void SensorRegistry::Validate(Sensor* pSensor) 00146 { 00147 if (pSensor == NULL) 00148 { 00149 throw Exception("Invalid sensor: NULL"); 00150 } 00151 else if (pSensor->GetIdentifier().Size() == 0) 00152 { 00153 throw Exception("Invalid sensor: Nameless"); 00154 } 00155 } 00156 00157 } 00158