NameServer.hpp

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 {
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             while ( i != objects.end() )
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() ) {
00208                         objects.erase( i );
00209 // #ifdef OROPKG_CORELIB_REPORTING
00210 //                         Logger::log() << Logger::Debug << "NameServer : Removing " << (*i).first << Logger::endl;
00211 // #endif
00212                     }
00213                 }
00214         }
00215 
00222         void unregisterName( const NameType& name )
00223         {
00224             iterator i( objects.find( name ) );
00225 
00226             if ( i != objects.end() ) {
00227                 objects.erase( i );
00228 // #ifdef OROPKG_CORELIB_REPORTING
00229 //                 Logger::log() << Logger::Debug << "NameServer : Removing " << name << Logger::endl;
00230 // #endif
00231                     }
00232         }
00233 
00239 #if __GNUC__ == 2
00240         class value_iterator : public bidirectional_iterator<ValueType, int>
00241 #else
00242         class value_iterator : public std::iterator<std::input_iterator_tag, ValueType>
00243 #endif
00244         {
00245         protected:
00246             iterator i;
00247 
00248         public:
00249             value_iterator( iterator _i ) : i( _i )
00250             {}
00251 
00252             ValueType operator*()
00253             {
00254                 return ( ( *i ).second );
00255             }
00256 
00257             value_iterator& operator++()
00258             {
00259                 ++i;
00260                 return *this;
00261             }
00262 
00263             value_iterator& operator--()
00264             {
00265                 --i;
00266                 return *this;
00267             }
00268 
00269             value_iterator operator++( int )
00270             {
00271                 value_iterator ret;
00272                 operator++();
00273                 return ret;
00274             }
00275 
00276             value_iterator operator--( int )
00277             {
00278                 value_iterator ret;
00279                 operator--();
00280                 return ret;
00281             }
00282 
00283             bool operator==( value_iterator other )
00284             {
00285                 return ( i == other.i );
00286             }
00287 
00288             bool operator!=( value_iterator other )
00289             {
00290                 return ( i != other.i );
00291             }
00292 
00293             int operator- ( value_iterator other )
00294             {
00295                 return ( i -other.i );
00296             }
00297         };
00298 
00304 #if __GNUC__ == 2
00305         class name_iterator : public bidirectional_iterator<NameType, int>
00306 #else
00307         class name_iterator : public std::iterator< std::input_iterator_tag , NameType>
00308 #endif
00309         {
00310 
00311         protected:
00312             iterator i;
00313 
00314         public:
00315 
00316             name_iterator( iterator _i ) : i( _i )
00317             {}
00318 
00319             NameType operator*()
00320             {
00321                 return ( ( *i ).first );
00322             }
00323 
00324             name_iterator operator++( int )
00325             {
00326                 name_iterator ret( i );
00327                 operator++();
00328                 return ret;
00329             }
00330 
00331             name_iterator& operator++()
00332             {
00333                 ++i;
00334                 return *this;
00335             }
00336 
00337             name_iterator& operator--()
00338             {
00339                 --i;
00340                 return *this;
00341             }
00342 
00343             name_iterator operator--( int )
00344             {
00345                 name_iterator ret( i );
00346                 operator--();
00347                 return ret;
00348             }
00349 
00350             bool operator==( name_iterator other )
00351             {
00352                 return ( i == other.i );
00353             }
00354 
00355             bool operator!=( name_iterator other )
00356             {
00357                 return ( i != other.i );
00358             }
00359 
00360             int operator- ( name_iterator other )
00361             {
00362                 return ( i -other.i );
00363             }
00364         };
00365 
00369         name_iterator getNameBegin() { return name_iterator( objects.begin() ); }
00370 
00374         name_iterator getNameEnd() { return name_iterator( objects.end() ); }
00375 
00379         value_iterator getValueBegin() { return value_iterator( objects.begin() ); }
00380 
00384         value_iterator getValueEnd() { return value_iterator( objects.end() ); }
00385 
00386     private:
00387         Rep objects;
00388         static NameType NoName;
00389     };
00390 
00391     template<class T>
00392     std::string NameServer<T>::NoName;
00393 
00394 }
00395 
00396 #endif // NAMESERVER_HPP

Generated on Tue Aug 25 14:17:22 2009 for Orocos Real-Time Toolkit by  doxygen 1.5.8