SensorRegistry.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2011, SRI International (R)
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <map>
19 #include <iostream>
20 
21 #ifdef USE_TBB
22 #include <tbb/mutex.h>
23 #endif
24 
26 #include <OpenKarto/Exception.h>
27 #include <OpenKarto/Sensor.h>
28 #include <OpenKarto/Logger.h>
29 
30 namespace karto
31 {
32 
36 
37  // Note to not change the Sensor* to SensorPtr or SmartPointer<Sensor>.
39  {
41 
42  typedef std::map<Identifier, Sensor*> SensorManagerMap;
43  SensorManagerMap m_SensorMap;
44  };
45 
47  : m_pSensorRegistryPrivate(new SensorRegistryPrivate())
48  {
49  }
50 
52  {
55  }
56 
58  {
59 #ifdef USE_TBB
60  static tbb::mutex myMutex;
61  tbb::mutex::scoped_lock lock(myMutex);
62 #endif
63 
64  static SmartPointer<SensorRegistry> sInstance = new SensorRegistry();
65  return sInstance;
66  }
67 
69  {
70  if (pSensor != NULL)
71  {
72  if (pSensor->GetIdentifier().GetScope() != "Karto/System")
73  {
74  Validate(pSensor);
75 
76  Log(LOG_DEBUG, String("Registering sensor: [") + pSensor->GetIdentifier().ToString() + "]");
77  }
78 
80  {
81  String errorMessage;
82  errorMessage.Append("Cannot register sensor: already registered: [");
83  errorMessage.Append(pSensor->GetIdentifier().ToString());
84  errorMessage.Append("]");
85 
86  throw Exception(errorMessage);
87  }
88 
90  m_pSensorRegistryPrivate->m_Sensors.Add(pSensor);
91  }
92  }
93 
95  {
96  if (pSensor != NULL)
97  {
98  if (pSensor->GetIdentifier().GetScope() != "Karto/System")
99  {
100  Log(LOG_DEBUG, String("Unregistering sensor: [") + pSensor->GetIdentifier().ToString() + "]");
101  }
102 
104  {
106 
107  m_pSensorRegistryPrivate->m_Sensors.Remove(pSensor);
108  }
109  else
110  {
111  String errorMessage;
112  errorMessage.Append("Cannot unregister sensor: not registered: [");
113  errorMessage.Append(pSensor->GetIdentifier().ToString());
114  errorMessage.Append("]");
115 
116  throw Exception(errorMessage);
117  }
118  }
119  }
120 
122  {
124  {
125  Sensor* pSensor = m_pSensorRegistryPrivate->m_SensorMap[rName];
126 
127  assert(pSensor != NULL);
128 
129  return pSensor;
130  }
131 
132  String errorMessage;
133  errorMessage.Append("Sensor not registered: [");
134  errorMessage.Append(rName.ToString());
135  errorMessage.Append("]");
136  throw Exception(errorMessage);
137  }
138 
140  {
143  }
144 
146  {
147  if (pSensor == NULL)
148  {
149  throw Exception("Invalid sensor: NULL");
150  }
151  else if (pSensor->GetIdentifier().Size() == 0)
152  {
153  throw Exception("Invalid sensor: Nameless");
154  }
155  }
156 
157 }
158 
void Append(const String &rString)
Definition: String.cpp:82
const Identifier & GetIdentifier() const
Definition: Object.h:83
void RegisterSensor(Sensor *pSensor)
void UnregisterSensor(Sensor *pSensor)
void Log(LogLevel level, const karto::String &rMessage)
Definition: Logger.cpp:183
kt_size_t Size() const
Definition: Identifier.cpp:198
const String & ToString() const
Definition: Identifier.cpp:93
std::map< Identifier, Sensor * > SensorManagerMap
static void Validate(Sensor *pSensor)
Sensor * GetSensorByName(const Identifier &rName)
static SensorRegistry * GetInstance()
const String & GetScope() const
Definition: Identifier.cpp:74
Definition: Any.cpp:20
SensorRegistryPrivate * m_pSensorRegistryPrivate


nav2d_karto
Author(s): Sebastian Kasperski
autogenerated on Tue Nov 7 2017 06:02:36