NameServer.hpp
Go to the documentation of this file.
00001 /***************************************************************************
00002   tag: Peter Soetens  Thu Oct 10 16:16:57 CEST 2002  NameServer.hpp
00003 
00004                         NameServer.hpp -  description
00005                            -------------------
00006     begin                : Thu October 10 2002
00007     copyright            : (C) 2002 Peter Soetens
00008     email                : peter.soetens@mech.kuleuven.ac.be
00009 
00010  ***************************************************************************
00011  *   This library is free software; you can redistribute it and/or         *
00012  *   modify it under the terms of the GNU General Public                   *
00013  *   License as published by the Free Software Foundation;                 *
00014  *   version 2 of the License.                                             *
00015  *                                                                         *
00016  *   As a special exception, you may use this file as part of a free       *
00017  *   software library without restriction.  Specifically, if other files   *
00018  *   instantiate templates or use macros or inline functions from this     *
00019  *   file, or you compile this file and link it with other files to        *
00020  *   produce an executable, this file does not by itself cause the         *
00021  *   resulting executable to be covered by the GNU General Public          *
00022  *   License.  This exception does not however invalidate any other        *
00023  *   reasons why the executable file might be covered by the GNU General   *
00024  *   Public License.                                                       *
00025  *                                                                         *
00026  *   This library is distributed in the hope that it will be useful,       *
00027  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00028  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00029  *   Lesser General Public License for more details.                       *
00030  *                                                                         *
00031  *   You should have received a copy of the GNU General Public             *
00032  *   License along with this library; if not, write to the Free Software   *
00033  *   Foundation, Inc., 59 Temple Place,                                    *
00034  *   Suite 330, Boston, MA  02111-1307  USA                                *
00035  *                                                                         *
00036  ***************************************************************************/
00037 
00038 #ifndef ORO_NAMESERVER_HPP
00039 #define ORO_NAMESERVER_HPP
00040 
00041 #include <iterator>
00042 #include <string>
00043 #include <map>
00044 // #include "rtt-config.h"
00045 // #ifdef OROPKG_CORELIB_REPORTING
00046 // #include "../../Logger.hpp"
00047 // #endif
00048 
00049 namespace RTT
00050 { namespace dev {
00051 
00070     template < class _ValueType >
00071     class NameServer
00072     {
00073     public:
00074         typedef _ValueType ValueType;
00075         typedef std::string NameType;
00076         typedef std::map<NameType, ValueType> Rep;
00080         typedef typename Rep::iterator iterator;
00084         typedef typename Rep::const_iterator const_iterator;
00085 
00089         NameServer() : objects()
00090         {}
00091 
00095         ~NameServer()
00096         {}
00097 
00105         bool isNameRegistered( const NameType& s ) const
00106         {
00107             return ( objects.find( s ) != objects.end() );
00108         }
00109 
00117         bool isObjectRegistered( const ValueType o ) const
00118         {
00119             for ( const_iterator i = objects.begin(); i != objects.end(); ++i )
00120                 {
00121                     if ( ( *i ).second == o )
00122                         return true;
00123                 }
00124 
00125             return false;
00126         }
00127 
00136         ValueType getObject( const NameType& s ) const
00137         {
00138             const_iterator itc = objects.find( s );
00139             if ( itc == objects.end() )
00140                 return ValueType();
00141             return( *itc ).second;
00142         }
00143 
00152         const NameType&
00153         getName( const ValueType s ) const
00154         {
00155             for ( const_iterator i = objects.begin(); i != objects.end(); ++i )
00156                 {
00157                     if ( ( *i ).second == s )
00158                         return ( *i ).first;
00159                 }
00160 
00161             return NoName;
00162         }
00163 
00172         bool registerObject( const ValueType obj, const NameType& name )
00173         {
00174             if ( isNameRegistered( name ) )
00175                 return false;
00176 // #ifdef OROPKG_CORELIB_REPORTING
00177 //             Logger::log() << Logger::Debug << "NameServer : Adding " << name << Logger::endl;
00178 // #endif
00179             objects.insert(std::make_pair(name,obj));
00180             return true;
00181         }
00182 
00190         void unregisterObject( const ValueType obj )
00191         {
00192             iterator i(objects.begin());
00193 
00197             for(;;)
00198                 {
00199                     for ( i = objects.begin();
00200                           i != objects.end();
00201                           ++i )
00202                         {
00203                             if ( ( *i ).second == obj )
00204                                 break;
00205                         }
00206 
00207                     if ( i == objects.end() ) return;
00208 // #ifdef OROPKG_CORELIB_REPORTING
00209 //                         Logger::log() << Logger::Debug << "NameServer : Removing " << (*i).first << Logger::endl;
00210 // #endif
00211                        objects.erase( i );
00212                 }
00213         }
00214 
00221         void unregisterName( const NameType& name )
00222         {
00223             iterator i( objects.find( name ) );
00224 
00225             if ( i != objects.end() ) {
00226                 objects.erase( i );
00227 // #ifdef OROPKG_CORELIB_REPORTING
00228 //                 Logger::log() << Logger::Debug << "NameServer : Removing " << name << Logger::endl;
00229 // #endif
00230                     }
00231         }
00232 
00238 #if __GNUC__ == 2
00239         class value_iterator : public bidirectional_iterator<ValueType, int>
00240 #else
00241         class value_iterator : public std::iterator<std::input_iterator_tag, ValueType>
00242 #endif
00243         {
00244         protected:
00245                 typename NameServer<_ValueType>::iterator i;
00246 
00247         public:
00248             value_iterator( iterator _i ) : i( _i )
00249             {}
00250 
00251             ValueType operator*()
00252             {
00253                 return ( ( *i ).second );
00254             }
00255 
00256             value_iterator& operator++()
00257             {
00258                 ++i;
00259                 return *this;
00260             }
00261 
00262             value_iterator& operator--()
00263             {
00264                 --i;
00265                 return *this;
00266             }
00267 
00268             value_iterator operator++( int )
00269             {
00270                 value_iterator ret;
00271                 operator++();
00272                 return ret;
00273             }
00274 
00275             value_iterator operator--( int )
00276             {
00277                 value_iterator ret;
00278                 operator--();
00279                 return ret;
00280             }
00281 
00282             bool operator==( value_iterator other )
00283             {
00284                 return ( i == other.i );
00285             }
00286 
00287             bool operator!=( value_iterator other )
00288             {
00289                 return ( i != other.i );
00290             }
00291 
00292             int operator- ( value_iterator other )
00293             {
00294                 return ( i -other.i );
00295             }
00296         };
00297 
00303 #if __GNUC__ == 2
00304         class name_iterator : public bidirectional_iterator<NameType, int>
00305 #else
00306         class name_iterator : public std::iterator< std::input_iterator_tag , NameType>
00307 #endif
00308         {
00309 
00310         protected:
00311                 typename NameServer<_ValueType>::iterator i;
00312 
00313         public:
00314 
00315             name_iterator( iterator _i ) : i( _i )
00316             {}
00317 
00318             NameType operator*()
00319             {
00320                 return ( ( *i ).first );
00321             }
00322 
00323             name_iterator operator++( int )
00324             {
00325                 name_iterator ret( i );
00326                 operator++();
00327                 return ret;
00328             }
00329 
00330             name_iterator& operator++()
00331             {
00332                 ++i;
00333                 return *this;
00334             }
00335 
00336             name_iterator& operator--()
00337             {
00338                 --i;
00339                 return *this;
00340             }
00341 
00342             name_iterator operator--( int )
00343             {
00344                 name_iterator ret( i );
00345                 operator--();
00346                 return ret;
00347             }
00348 
00349             bool operator==( name_iterator other )
00350             {
00351                 return ( i == other.i );
00352             }
00353 
00354             bool operator!=( name_iterator other )
00355             {
00356                 return ( i != other.i );
00357             }
00358 
00359             int operator- ( name_iterator other )
00360             {
00361                 return ( i -other.i );
00362             }
00363         };
00364 
00368         name_iterator getNameBegin() { return name_iterator( objects.begin() ); }
00369 
00373         name_iterator getNameEnd() { return name_iterator( objects.end() ); }
00374 
00378         value_iterator getValueBegin() { return value_iterator( objects.begin() ); }
00379 
00383         value_iterator getValueEnd() { return value_iterator( objects.end() ); }
00384 
00385     private:
00386         Rep objects;
00387         static NameType NoName;
00388     };
00389 
00390     template<class T>
00391     std::string NameServer<T>::NoName;
00392 
00393 }}
00394 
00395 #endif // NAMESERVER_HPP


rtt
Author(s): RTT Developers
autogenerated on Wed Aug 26 2015 16:15:50