Image Component Library (ICL)
|
00001 /******************************************************************** 00002 ** Image Component Library (ICL) ** 00003 ** ** 00004 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld ** 00005 ** Neuroinformatics Group ** 00006 ** Website: www.iclcv.org and ** 00007 ** http://opensource.cit-ec.de/projects/icl ** 00008 ** ** 00009 ** File : ICLUtils/src/ICLUtils/ProgArg.h ** 00010 ** Module : ICLUtils ** 00011 ** Authors: Christof Elbrechter, Andre Justus ** 00012 ** ** 00013 ** ** 00014 ** GNU LESSER GENERAL PUBLIC LICENSE ** 00015 ** This file may be used under the terms of the GNU Lesser General ** 00016 ** Public License version 3.0 as published by the ** 00017 ** ** 00018 ** Free Software Foundation and appearing in the file LICENSE.GPL ** 00019 ** included in the packaging of this file. Please review the ** 00020 ** following information to ensure the license requirements will ** 00021 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt ** 00022 ** ** 00023 ** The development of this software was supported by the ** 00024 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. ** 00025 ** The Excellence Cluster EXC 277 is a grant of the Deutsche ** 00026 ** Forschungsgemeinschaft (DFG) in the context of the German ** 00027 ** Excellence Initiative. ** 00028 ** ** 00029 ********************************************************************/ 00030 00031 #pragma once 00032 00033 #include <ICLUtils/Any.h> 00034 #include <ICLUtils/Exception.h> 00035 #include <ICLUtils/Macros.h> 00036 00037 namespace icl{ 00038 namespace utils{ 00039 00041 struct ProgArgException : public ICLException{ 00042 ProgArgException(const std::string &func, const std::string &what): 00043 ICLException(func+":"+what){} 00044 }; 00045 #define THROW_ProgArgException(X) throw ProgArgException(__FUNCTION__,(X)) 00046 00048 // internally used programm argument data type 00049 class ProgArgData{ 00050 protected: 00051 friend const std::string &pa_subarg_internal(const ProgArgData &pa) throw (ProgArgException); 00052 friend bool pa_defined_internal(const ProgArgData &pa) throw (ProgArgException); 00053 std::string id; 00054 int subargidx; 00055 bool danglingOnly; 00056 inline ProgArgData(const std::string &id, int subargidx): 00057 id(id),subargidx((int)subargidx){ 00058 } 00059 inline ProgArgData(unsigned int idx, bool danglingOnly): 00060 subargidx(idx),danglingOnly(danglingOnly){ 00061 } 00062 00063 }; 00067 // internal function for program argument explanation 00068 void pa_explain_internal(const std::string &pa, const std::string &ex); 00069 00070 // internal sub-argument access function 00071 const std::string &pa_subarg_internal(const ProgArgData &pa) throw (ProgArgException); 00072 00073 // another internal helper function 00074 bool pa_defined_internal(const ProgArgData &pa) throw (ProgArgException); 00077 00078 00079 class ProgArg : public ProgArgData{ 00081 00084 inline ProgArg(const std::string &id, unsigned int subargidx): 00085 ProgArgData(id,subargidx){ 00086 } 00087 00088 inline ProgArg(unsigned int idx, bool danglingOnly): 00089 ProgArgData(idx,danglingOnly){ 00090 } 00091 00092 public: 00094 // undocumented friend 00095 friend const ProgArg pa(const std::string &,unsigned int) throw (ProgArgException); 00096 // undocumented friend 00097 friend const ProgArg pa(unsigned int,bool); 00098 // yet another one 00099 friend bool pa_defined_internal(const ProgArgData &pa) throw (ProgArgException); 00102 00103 00104 int n() const throw (ProgArgException); 00105 00107 Any operator[](int subArgIdx) const throw (ProgArgException); 00108 00110 00112 template<class T> 00113 inline operator T() const throw (ProgArgException){ 00114 return parse<T>(pa_subarg_internal(*this)); 00115 } 00116 00118 template<class T> 00119 inline T as() const throw (ProgArgException){ 00120 return parse<T>(pa_subarg_internal(*this)); 00121 } 00122 00124 00126 inline std::string operator*() const throw (ProgArgException){ 00127 return pa_subarg_internal(*this); 00128 } 00129 00131 inline const std::string &getID() const { 00132 return id; 00133 } 00134 }; 00135 00137 inline std::ostream &operator<<(std::ostream &s,const ProgArg &pa){ 00138 return s << pa.as<std::string>(); 00139 } 00140 00142 // explicit specialization for bool types (returns whether the arg was actually given) 00143 template<> 00144 inline ProgArg::operator bool() const throw(utils::ProgArgException){ 00145 return pa_defined_internal(*this); 00146 } 00147 00148 // explicit specialization for bool types (returns whether the arg was actually given) 00149 template<> 00150 inline bool ProgArg::as() const throw (ProgArgException){ 00151 return pa_defined_internal(*this); 00152 } 00153 00156 00157 00164 inline bool operator&&(const ProgArg &a, const ProgArg &b){ 00165 return a.as<bool>() && b.as<bool>(); 00166 } 00167 00169 00174 inline bool operator&&(const ProgArg &a, bool b){ 00175 return b && a.as<bool>(); 00176 } 00177 00179 00184 inline bool operator&&(bool &b, const ProgArg &a){ 00185 return b && a.as<bool>(); 00186 } 00187 00189 00196 inline bool operator||(const ProgArg &a, const ProgArg &b){ 00197 return a.as<bool>() || b.as<bool>(); 00198 } 00200 00205 inline bool operator||(const ProgArg &a, bool b){ 00206 return b || a.as<bool>(); 00207 } 00208 00210 00215 inline bool operator||(bool &b, const ProgArg &a){ 00216 return b || a.as<bool>(); 00217 } 00218 00219 00220 00221 00223 00293 inline const ProgArg pa(const std::string &id, unsigned int subargidx=0) throw (ProgArgException){ 00294 if(!id.length()) THROW_ProgArgException("invalid programm argument id ''"); 00295 return ProgArg(id,subargidx); 00296 } 00297 00299 00300 inline const ProgArg pa(unsigned int idx, bool danglingOnly=true){ 00301 return ProgArg(idx,danglingOnly); 00302 } 00303 00304 00306 template<class T> 00307 inline const T pa_def(const std::string &id, unsigned int subargidx, const T &def) throw (ProgArgException){ 00308 const ProgArg p = pa(id,subargidx); 00309 return p ? parse<T>(p) : def; 00310 } 00311 00313 template<class T> 00314 inline const T pa_def(const std::string &id, const T &def) throw (ProgArgException){ 00315 return pa_def(id,0,def); 00316 } 00317 00318 00319 00321 00322 unsigned int pa_get_count(bool danglingOnly=true); 00323 00325 00332 const std::string &pa_get_progname(bool fullpath=false); 00333 00335 void pa_show_usage(const std::string &msg=""); 00336 00338 // utility class which allows the user to call the paex-function in a 'stacked' manner 00339 struct PAEX{ 00340 PAEX operator()(const std::string &pa, const std::string &ex); 00341 }; 00344 00345 00354 inline PAEX pa_explain(const std::string &pa, const std::string &ex){ 00355 pa_explain_internal(pa,ex); 00356 return PAEX(); 00357 } 00358 00360 // deferred implementation of stacking operator 00361 inline PAEX PAEX::operator()(const std::string &pa, const std::string &ex){ 00362 return pa_explain(pa,ex); 00363 } 00367 00368 00455 void pa_init(int n,char **ppc,const std::string &init, bool allowDanglingArgs=false); 00456 00457 00459 void pa_show(); 00460 00462 00463 void pa_set_license(const std::string &newLicenseText); 00464 00466 00467 void pa_set_help_text(const std::string &newHelpText); 00468 00470 std::string pa_get_license(); 00471 00473 std::string pa_get_help_text(); 00474 } // namespace utils 00475 } 00476