ISIS Core Library 0.7.2 (api 3.0.0)

/scr/tee1/isis/lib/Core/CoreUtils/progparameter.hpp

Go to the documentation of this file.
00001 /*
00002     Copyright (C) 2010  reimer@cbs.mpg.de
00003 
00004     This program is free software: you can redistribute it and/or modify
00005     it under the terms of the GNU General Public License as published by
00006     the Free Software Foundation, either version 3 of the License, or
00007     (at your option) any later version.
00008 
00009     This program is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012     GNU General Public License for more details.
00013 
00014     You should have received a copy of the GNU General Public License
00015     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 */
00018 
00019 #ifndef PROGPARAMETER_HPP
00020 #define PROGPARAMETER_HPP
00021 
00022 #include "property.hpp"
00023 #include <string>
00024 #include <map>
00025 
00026 namespace isis
00027 {
00028 namespace util
00029 {
00036 class ProgParameter: public PropertyValue
00037 {
00038     std::string m_description;
00039     bool m_hidden, m_set;
00040 public:
00047     ProgParameter();
00054     template<typename T> ProgParameter( const T &ref, bool is_needed = true ): PropertyValue( ref, is_needed ), m_hidden( false ), m_set( false ) {}
00063     bool parse( const isis::util::Value< std::string >& props );
00064     bool parse_list( const isis::util::Value< util::slist >& props_list );
00066     const std::string &description()const;
00067     /* set the description string
00068     *  \param desc description to be set */
00069     void setDescription( const std::string &desc );
00074     template<typename T> operator const T()const {
00075         LOG_IF( isEmpty(), isis::CoreDebug, isis::error ) << "Program parameters must not be empty. Please set it to any value.";
00076         return get()->castTo<T>();
00077     }
00078 
00079 
00080     operator boost::scoped_ptr<ValueBase>::unspecified_bool_type()const;// implicit conversion to "bool" stolen from boost
00081 
00083     bool isSet()const;
00084 
00086     bool isHidden()const;
00087 
00089     bool &hidden();
00090 };
00091 
00096 class ParameterMap: public std::map<std::string, ProgParameter>
00097 {
00098     struct neededP {
00099         bool operator()( const_reference ref )const {return ref.second.isNeeded();}
00100     };
00101     struct notneededP {
00102         bool operator()( const_reference ref )const {return !ref.second.isNeeded();}
00103     };
00104     struct hiddenP {
00105         bool operator()( const_reference ref )const {return ref.second.isHidden();}
00106     };
00107     template<class T> void printWithout() {
00108         std::map<key_type, mapped_type, key_compare> result( *this );
00109 
00110         for (
00111             iterator found = std::find_if( result.begin(), result.end(), T() );
00112             found != result.end();
00113             found = std::find_if( found, result.end(), hiddenP() )
00114         )
00115             result.erase( found++ );
00116 
00117         std::cout << result << std::endl;
00118     }
00119     bool parsed;
00120 public:
00121     const ProgParameter operator[]( const std::string key )const;
00122     ProgParameter &operator[]( const std::string key );
00123     /*
00124      * Default constructor to create an empty parameter map
00125      */
00126     ParameterMap();
00134     bool parse( int argc, char **argv );
00135 
00137     bool isComplete()const;
00138 };
00139 
00140 }
00141 }
00142 
00143 namespace std
00144 {
00146 template<typename charT, typename traits> basic_ostream<charT, traits>&
00147 operator<<( basic_ostream<charT, traits> &out, const isis::util::ProgParameter &s )
00148 {
00149     const std::string &desc = s.description();
00150 
00151     if ( ! desc.empty() ) {
00152         out << desc << ", ";
00153     }
00154 
00155     LOG_IF( s.isEmpty(), isis::CoreDebug, isis::error ) << "Program parameters must not be empty. Please set it to any value.";
00156     assert( !s.isEmpty() );
00157     out << "default=\"" << s.toString( false ) << "\", type=" << s.getTypeName();
00158 
00159     if ( s.isNeeded() )out << " (needed)";
00160 
00161     return out;
00162 }
00163 }
00164 #endif // PROGPARAMETER_HPP