ISIS Core Library 0.7.2 (api 3.0.0)

/scr/tee1/isis/lib/Core/CoreUtils/selection.cpp

Go to the documentation of this file.
00001 /*
00002  *  selection.cpp
00003  *  isis
00004  *
00005  *  Created by Enrico Reimer on 03.04.10.
00006  *  Copyright 2010 cbs.mpg.de. All rights reserved.
00007  *
00008  */
00009 
00010 #include "selection.hpp"
00011 #include <boost/foreach.hpp>
00012 
00013 namespace isis
00014 {
00015 namespace util
00016 {
00017 
00018 Selection::Selection( const char *entries, const char *init_val ): m_set( 0 )
00019 {
00020     int ID = 1;
00021     BOOST_FOREACH( const util::istring & ref, stringToList<util::istring>( util::istring( entries ), ',' ) ) {
00022         const MapType::value_type pair( ref, ID++ );
00023 
00024         if( ! ent_map.insert( pair ).second ) {
00025             LOG( Debug, error ) << "Entry " << util::MSubject( pair ) << " could not be inserted";
00026         }
00027     }
00028 
00029     if( init_val[0] )
00030         set( init_val );
00031 }
00032 Selection::Selection(): m_set( 0 ) {}
00033 
00034 Selection::operator int()const {return m_set;}
00035 Selection::operator util::istring()const
00036 {
00037     BOOST_FOREACH( MapType::const_reference ref, ent_map ) {
00038         if ( ref.second == m_set )
00039             return ref.first;
00040     }
00041     return util::istring( "<<NOT_SET>>" );
00042 }
00043 Selection::operator std::string()const
00044 {
00045     util::istring buff = *this;
00046     return std::string( buff.begin(), buff.end() );
00047 }
00048 
00049 bool Selection::set( unsigned short entry )
00050 {
00051     if( getEntries().size() > entry ) {
00052         m_set = entry;
00053         return true;
00054     } else {
00055         return false;
00056     }
00057 
00058 }
00059 bool Selection::set( const char *entry )
00060 {
00061     MapType::const_iterator found = ent_map.find( entry );
00062 
00063     if ( found != ent_map.end() ) {
00064         m_set = found->second;
00065         return true;
00066     } else {
00067         LOG( Runtime, error ) << "Failed to set " << MSubject( entry ) << ", valid values are " << getEntries();
00068         return false;
00069     }
00070 }
00071 
00072 bool Selection::operator==( const Selection &ref )const
00073 {
00074     return m_set == ref.m_set && ent_map == ref.ent_map;
00075 }
00076 bool Selection::operator==( const char ref[] ) const
00077 {
00078     return ( ( const util::istring & ) * this ) == ref ;
00079 }
00080 bool Selection::operator==( const int ref ) const
00081 {
00082     return ( ( int ) * this ) == ref;
00083 }
00084 
00085 
00086 std::list<util::istring> Selection::getEntries()const
00087 {
00088     std::list<util::istring> ret;
00089     BOOST_FOREACH( MapType::const_reference ref, ent_map ) {
00090         ret.push_back( ref.first );
00091     }
00092     return ret;
00093 }
00094 }
00095 }