Orocos Real-Time Toolkit  2.8.3
NameServer.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Thu Oct 10 16:16:57 CEST 2002 NameServer.hpp
3 
4  NameServer.hpp - description
5  -------------------
6  begin : Thu October 10 2002
7  copyright : (C) 2002 Peter Soetens
8  email : peter.soetens@mech.kuleuven.ac.be
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU General Public *
13  * License as published by the Free Software Foundation; *
14  * version 2 of the License. *
15  * *
16  * As a special exception, you may use this file as part of a free *
17  * software library without restriction. Specifically, if other files *
18  * instantiate templates or use macros or inline functions from this *
19  * file, or you compile this file and link it with other files to *
20  * produce an executable, this file does not by itself cause the *
21  * resulting executable to be covered by the GNU General Public *
22  * License. This exception does not however invalidate any other *
23  * reasons why the executable file might be covered by the GNU General *
24  * Public License. *
25  * *
26  * This library is distributed in the hope that it will be useful, *
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29  * Lesser General Public License for more details. *
30  * *
31  * You should have received a copy of the GNU General Public *
32  * License along with this library; if not, write to the Free Software *
33  * Foundation, Inc., 59 Temple Place, *
34  * Suite 330, Boston, MA 02111-1307 USA *
35  * *
36  ***************************************************************************/
37 
38 #ifndef ORO_NAMESERVER_HPP
39 #define ORO_NAMESERVER_HPP
40 
41 #include <iterator>
42 #include <string>
43 #include <map>
44 // #include "rtt-config.h"
45 // #ifdef OROPKG_CORELIB_REPORTING
46 // #include "../../Logger.hpp"
47 // #endif
48 
49 namespace RTT
50 { namespace dev {
51 
70  template < class _ValueType >
71  class NameServer
72  {
73  public:
74  typedef _ValueType ValueType;
75  typedef std::string NameType;
76  typedef std::map<NameType, ValueType> Rep;
80  typedef typename Rep::iterator iterator;
84  typedef typename Rep::const_iterator const_iterator;
85 
89  NameServer() : objects()
90  {}
91 
96  {}
97 
105  bool isNameRegistered( const NameType& s ) const
106  {
107  return ( objects.find( s ) != objects.end() );
108  }
109 
117  bool isObjectRegistered( const ValueType o ) const
118  {
119  for ( const_iterator i = objects.begin(); i != objects.end(); ++i )
120  {
121  if ( ( *i ).second == o )
122  return true;
123  }
124 
125  return false;
126  }
127 
136  ValueType getObject( const NameType& s ) const
137  {
138  const_iterator itc = objects.find( s );
139  if ( itc == objects.end() )
140  return ValueType();
141  return( *itc ).second;
142  }
143 
152  const NameType&
153  getName( const ValueType s ) const
154  {
155  for ( const_iterator i = objects.begin(); i != objects.end(); ++i )
156  {
157  if ( ( *i ).second == s )
158  return ( *i ).first;
159  }
160 
161  return NoName;
162  }
163 
172  bool registerObject( const ValueType obj, const NameType& name )
173  {
174  if ( isNameRegistered( name ) )
175  return false;
176 // #ifdef OROPKG_CORELIB_REPORTING
177 // Logger::log() << Logger::Debug << "NameServer : Adding " << name << Logger::endl;
178 // #endif
179  objects.insert(std::make_pair(name,obj));
180  return true;
181  }
182 
190  void unregisterObject( const ValueType obj )
191  {
192  iterator i(objects.begin());
193 
197  for(;;)
198  {
199  for ( i = objects.begin();
200  i != objects.end();
201  ++i )
202  {
203  if ( ( *i ).second == obj )
204  break;
205  }
206 
207  if ( i == objects.end() ) return;
208 // #ifdef OROPKG_CORELIB_REPORTING
209 // Logger::log() << Logger::Debug << "NameServer : Removing " << (*i).first << Logger::endl;
210 // #endif
211  objects.erase( i );
212  }
213  }
214 
221  void unregisterName( const NameType& name )
222  {
223  iterator i( objects.find( name ) );
224 
225  if ( i != objects.end() ) {
226  objects.erase( i );
227 // #ifdef OROPKG_CORELIB_REPORTING
228 // Logger::log() << Logger::Debug << "NameServer : Removing " << name << Logger::endl;
229 // #endif
230  }
231  }
232 
238 #if __GNUC__ == 2
239  class value_iterator : public bidirectional_iterator<ValueType, int>
240 #else
241  class value_iterator : public std::iterator<std::input_iterator_tag, ValueType>
242 #endif
243  {
244  protected:
246 
247  public:
248  value_iterator( iterator _i ) : i( _i )
249  {}
250 
251  ValueType operator*()
252  {
253  return ( ( *i ).second );
254  }
255 
257  {
258  ++i;
259  return *this;
260  }
261 
263  {
264  --i;
265  return *this;
266  }
267 
269  {
270  value_iterator ret;
271  operator++();
272  return ret;
273  }
274 
276  {
277  value_iterator ret;
278  operator--();
279  return ret;
280  }
281 
283  {
284  return ( i == other.i );
285  }
286 
288  {
289  return ( i != other.i );
290  }
291 
293  {
294  return ( i -other.i );
295  }
296  };
297 
303 #if __GNUC__ == 2
304  class name_iterator : public bidirectional_iterator<NameType, int>
305 #else
306  class name_iterator : public std::iterator< std::input_iterator_tag , NameType>
307 #endif
308  {
309 
310  protected:
312 
313  public:
314 
315  name_iterator( iterator _i ) : i( _i )
316  {}
317 
318  NameType operator*()
319  {
320  return ( ( *i ).first );
321  }
322 
324  {
325  name_iterator ret( i );
326  operator++();
327  return ret;
328  }
329 
331  {
332  ++i;
333  return *this;
334  }
335 
337  {
338  --i;
339  return *this;
340  }
341 
343  {
344  name_iterator ret( i );
345  operator--();
346  return ret;
347  }
348 
349  bool operator==( name_iterator other )
350  {
351  return ( i == other.i );
352  }
353 
354  bool operator!=( name_iterator other )
355  {
356  return ( i != other.i );
357  }
358 
360  {
361  return ( i -other.i );
362  }
363  };
364 
368  name_iterator getNameBegin() { return name_iterator( objects.begin() ); }
369 
373  name_iterator getNameEnd() { return name_iterator( objects.end() ); }
374 
378  value_iterator getValueBegin() { return value_iterator( objects.begin() ); }
379 
383  value_iterator getValueEnd() { return value_iterator( objects.end() ); }
384 
385  private:
386  Rep objects;
387  static NameType NoName;
388  };
389 
390  template<class T>
391  std::string NameServer<T>::NoName;
392 
393 }}
394 
395 #endif // NAMESERVER_HPP
bool operator!=(value_iterator other)
Definition: NameServer.hpp:287
~NameServer()
Destruct a nameserver.
Definition: NameServer.hpp:95
bool operator==(value_iterator other)
Definition: NameServer.hpp:282
A nameserver for Orocos Device classes.
Definition: NameServer.hpp:71
An Iterator to iterate over the registered objects.
Definition: NameServer.hpp:241
bool registerObject(const ValueType obj, const NameType &name)
Register an object with a name.
Definition: NameServer.hpp:172
bool isNameRegistered(const NameType &s) const
Determine if a given name is registered.
Definition: NameServer.hpp:105
An Iterator to iterate over the registered names.
Definition: NameServer.hpp:306
NameServer< _ValueType >::iterator i
Definition: NameServer.hpp:245
name_iterator getNameEnd()
Get an iterator to the end of the names list.
Definition: NameServer.hpp:373
bool operator==(name_iterator other)
Definition: NameServer.hpp:349
value_iterator getValueBegin()
Get an iterator to the beginning of the objects list.
Definition: NameServer.hpp:378
NameServer< _ValueType >::iterator i
Definition: NameServer.hpp:311
Rep::const_iterator const_iterator
The const_iterator for iterating over the internal representation.
Definition: NameServer.hpp:84
Rep::iterator iterator
The iterator for iterating over the internal representation.
Definition: NameServer.hpp:80
ValueType getObject(const NameType &s) const
Get the object registered for a name.
Definition: NameServer.hpp:136
std::map< NameType, ValueType > Rep
Definition: NameServer.hpp:76
const NameType & getName(const ValueType s) const
Get the name registered for a object.
Definition: NameServer.hpp:153
bool isObjectRegistered(const ValueType o) const
Determine if a given object is registered.
Definition: NameServer.hpp:117
int operator-(value_iterator other)
Definition: NameServer.hpp:292
void unregisterObject(const ValueType obj)
Remove an object from the nameserver registrations.
Definition: NameServer.hpp:190
bool operator!=(name_iterator other)
Definition: NameServer.hpp:354
name_iterator getNameBegin()
Get an iterator to the beginning of the names list.
Definition: NameServer.hpp:368
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:51
value_iterator getValueEnd()
Get an iterator to the end of the objects list.
Definition: NameServer.hpp:383
_ValueType ValueType
Definition: NameServer.hpp:74
NameServer()
Construct an empty NameServer.
Definition: NameServer.hpp:89
std::string NameType
Definition: NameServer.hpp:75
void unregisterName(const NameType &name)
Remove a name from the nameserver registrations.
Definition: NameServer.hpp:221