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.LGPL ** 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/CompatMacros.h> 00034 #include <ICLUtils/Any.h> 00035 #include <ICLUtils/Exception.h> 00036 #include <ICLUtils/Macros.h> 00037 00038 namespace icl{ 00039 namespace utils{ 00040 00042 struct ProgArgException : public ICLException{ 00043 ProgArgException(const std::string &func, const std::string &what): 00044 ICLException(func+":"+what){} 00045 }; 00046 #define THROW_ProgArgException(X) throw ProgArgException(__FUNCTION__,(X)) 00047 00049 // internally used programm argument data type 00050 class ProgArgData{ 00051 protected: 00052 friend ICLUtils_API const std::string &pa_subarg_internal(const ProgArgData &pa) throw (ProgArgException); 00053 friend ICLUtils_API bool pa_defined_internal(const ProgArgData &pa) throw (ProgArgException); 00054 std::string id; 00055 int subargidx; 00056 bool danglingOnly; 00057 inline ProgArgData(const std::string &id, int subargidx): 00058 id(id),subargidx((int)subargidx){ 00059 } 00060 inline ProgArgData(unsigned int idx, bool danglingOnly): 00061 subargidx(idx),danglingOnly(danglingOnly){ 00062 } 00063 00064 }; 00068 // internal function for program argument explanation 00069 ICLUtils_API void pa_explain_internal(const std::string &pa, const std::string &ex); 00070 00071 // internal sub-argument access function 00072 ICLUtils_API const std::string &pa_subarg_internal(const ProgArgData &pa) throw (ProgArgException); 00073 00074 // another internal helper function 00075 ICLUtils_API bool pa_defined_internal(const ProgArgData &pa) throw (ProgArgException); 00078 00079 00080 class ProgArg : public ProgArgData{ 00082 00085 inline ProgArg(const std::string &id, unsigned int subargidx): 00086 ProgArgData(id,subargidx){ 00087 } 00088 00089 inline ProgArg(unsigned int idx, bool danglingOnly): 00090 ProgArgData(idx,danglingOnly){ 00091 } 00092 00093 public: 00095 // undocumented friend 00096 friend ICLUtils_API const ProgArg pa(const std::string &,unsigned int) throw (ProgArgException); 00097 // undocumented friend 00098 friend ICLUtils_API const ProgArg pa(unsigned int, bool); 00099 // yet another one 00100 friend ICLUtils_API bool pa_defined_internal(const ProgArgData &pa) throw (ProgArgException); 00103 00104 00105 ICLUtils_API int n() const throw (ProgArgException); 00106 00108 ICLUtils_API Any operator[](int subArgIdx) const throw (ProgArgException); 00109 00111 00113 template<class T> 00114 inline operator T() const throw (ProgArgException){ 00115 return parse<T>(pa_subarg_internal(*this)); 00116 } 00117 00119 inline bool operator!() const throw (ProgArgException){ 00120 return !pa_defined_internal(*this); 00121 } 00122 00124 template<class T> 00125 inline T as() const throw (ProgArgException){ 00126 return parse<T>(pa_subarg_internal(*this)); 00127 } 00128 00130 00132 inline std::string operator*() const throw (ProgArgException){ 00133 return pa_subarg_internal(*this); 00134 } 00135 00137 inline const std::string &getID() const { 00138 return id; 00139 } 00140 }; 00141 00143 inline std::ostream &operator<<(std::ostream &s, const ProgArg &pa){ 00144 return s << pa.as<std::string>(); 00145 } 00146 00148 // explicit specialization for bool types (returns whether the arg was actually given) 00149 template<> 00150 inline ProgArg::operator bool() const throw(utils::ProgArgException){ 00151 return pa_defined_internal(*this); 00152 } 00153 00154 // explicit specialization for bool types (returns whether the arg was actually given) 00155 template<> 00156 inline bool ProgArg::as() const throw (ProgArgException){ 00157 return pa_defined_internal(*this); 00158 } 00159 00162 00163 00170 inline bool operator&&(const ProgArg &a, const ProgArg &b){ 00171 return a.as<bool>() && b.as<bool>(); 00172 } 00173 00175 00180 inline bool operator&&(const ProgArg &a, bool b){ 00181 return b && a.as<bool>(); 00182 } 00183 00185 00190 inline bool operator&&(bool &b, const ProgArg &a){ 00191 return b && a.as<bool>(); 00192 } 00193 00195 00202 inline bool operator||(const ProgArg &a, const ProgArg &b){ 00203 return a.as<bool>() || b.as<bool>(); 00204 } 00206 00211 inline bool operator||(const ProgArg &a, bool b){ 00212 return b || a.as<bool>(); 00213 } 00214 00216 00221 inline bool operator||(bool &b, const ProgArg &a){ 00222 return b || a.as<bool>(); 00223 } 00224 00225 00226 00227 00229 00304 inline const ProgArg pa(const std::string &id, unsigned int subargidx = 0) throw (ProgArgException){ 00305 if(!id.length()) THROW_ProgArgException("invalid programm argument id ''"); 00306 return ProgArg(id,subargidx); 00307 } 00308 00310 00311 inline const ProgArg pa(unsigned int idx, bool danglingOnly = true){ 00312 return ProgArg(idx,danglingOnly); 00313 } 00314 00315 00317 template<class T> 00318 inline const T pa_def(const std::string &id, unsigned int subargidx, const T &def) throw (ProgArgException){ 00319 const ProgArg p = pa(id,subargidx); 00320 return p ? parse<T>(p) : def; 00321 } 00322 00324 template<class T> 00325 inline const T pa_def(const std::string &id, const T &def) throw (ProgArgException){ 00326 return pa_def(id,0,def); 00327 } 00328 00329 00330 00332 00333 ICLUtils_API unsigned int pa_get_count(bool danglingOnly = true); 00334 00336 00343 ICLUtils_API const std::string &pa_get_progname(bool fullpath = false); 00344 00346 ICLUtils_API void pa_show_usage(const std::string &msg = ""); 00347 00349 // utility class which allows the user to call the paex-function in a 'stacked' manner 00350 struct ICLUtils_API PAEX{ 00351 PAEX operator()(const std::string &pa, const std::string &ex); 00352 }; 00355 00356 00365 inline PAEX pa_explain(const std::string &pa, const std::string &ex){ 00366 pa_explain_internal(pa,ex); 00367 return PAEX(); 00368 } 00369 00371 // deferred implementation of stacking operator 00372 inline PAEX PAEX::operator()(const std::string &pa, const std::string &ex){ 00373 return pa_explain(pa,ex); 00374 } 00378 00379 00466 ICLUtils_API void pa_init(int n, char **ppc, const std::string &init, bool allowDanglingArgs = false); 00467 00468 00470 ICLUtils_API void pa_show(); 00471 00473 00474 ICLUtils_API void pa_set_license(const std::string &newLicenseText); 00475 00477 00478 ICLUtils_API void pa_set_help_text(const std::string &newHelpText); 00479 00481 ICLUtils_API std::string pa_get_license(); 00482 00484 ICLUtils_API std::string pa_get_help_text(); 00485 } // namespace utils 00486 } 00487