Image Component Library (ICL)
ProgArg.h
Go to the documentation of this file.
1 /********************************************************************
2 ** Image Component Library (ICL) **
3 ** **
4 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld **
5 ** Neuroinformatics Group **
6 ** Website: www.iclcv.org and **
7 ** http://opensource.cit-ec.de/projects/icl **
8 ** **
9 ** File : ICLUtils/src/ICLUtils/ProgArg.h **
10 ** Module : ICLUtils **
11 ** Authors: Christof Elbrechter, Andre Justus **
12 ** **
13 ** **
14 ** GNU LESSER GENERAL PUBLIC LICENSE **
15 ** This file may be used under the terms of the GNU Lesser General **
16 ** Public License version 3.0 as published by the **
17 ** **
18 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
19 ** included in the packaging of this file. Please review the **
20 ** following information to ensure the license requirements will **
21 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt **
22 ** **
23 ** The development of this software was supported by the **
24 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. **
25 ** The Excellence Cluster EXC 277 is a grant of the Deutsche **
26 ** Forschungsgemeinschaft (DFG) in the context of the German **
27 ** Excellence Initiative. **
28 ** **
29 ********************************************************************/
30 
31 #pragma once
32 
33 #include <ICLUtils/CompatMacros.h>
34 #include <ICLUtils/Any.h>
35 #include <ICLUtils/Exception.h>
36 #include <ICLUtils/Macros.h>
37 
38 namespace icl{
39  namespace utils{
40 
42  struct ProgArgException : public ICLException{
43  ProgArgException(const std::string &func, const std::string &what):
44  ICLException(func+":"+what){}
45  };
46  #define THROW_ProgArgException(X) throw ProgArgException(__FUNCTION__,(X))
47 
49  // internally used programm argument data type
50  class ProgArgData{
51  protected:
52  friend ICLUtils_API const std::string &pa_subarg_internal(const ProgArgData &pa);
53  friend ICLUtils_API bool pa_defined_internal(const ProgArgData &pa);
54  std::string id;
55  int subargidx;
56  bool danglingOnly;
57  inline ProgArgData(const std::string &id, int subargidx):
58  id(id),subargidx((int)subargidx){
59  }
60  inline ProgArgData(unsigned int idx, bool danglingOnly):
61  subargidx(idx),danglingOnly(danglingOnly){
62  }
63 
64  };
68  // internal function for program argument explanation
69  ICLUtils_API void pa_explain_internal(const std::string &pa, const std::string &ex);
70 
71  // internal sub-argument access function
72  ICLUtils_API const std::string &pa_subarg_internal(const ProgArgData &pa);
73 
74  // another internal helper function
75  ICLUtils_API bool pa_defined_internal(const ProgArgData &pa);
78 
80  class ProgArg : public ProgArgData{
82 
85  inline ProgArg(const std::string &id, unsigned int subargidx):
86  ProgArgData(id,subargidx){
87  }
88 
89  inline ProgArg(unsigned int idx, bool danglingOnly):
90  ProgArgData(idx,danglingOnly){
91  }
92 
93  public:
95  // undocumented friend
96  friend ICLUtils_API const ProgArg pa(const std::string &,unsigned int);
97  // undocumented friend
98  friend ICLUtils_API const ProgArg pa(unsigned int, bool);
99  // yet another one
100  friend ICLUtils_API bool pa_defined_internal(const ProgArgData &pa);
103 
105  ICLUtils_API int n() const;
106 
108  ICLUtils_API Any operator[](int subArgIdx) const;
109 
111 
113  template<class T>
114  inline operator T() const{
115  return parse<T>(pa_subarg_internal(*this));
116  }
117 
119  inline bool operator!() const{
120  return !pa_defined_internal(*this);
121  }
122 
124  template<class T>
125  inline T as() const{
126  return parse<T>(pa_subarg_internal(*this));
127  }
128 
130 
132  inline std::string operator*() const{
133  return pa_subarg_internal(*this);
134  }
135 
137  inline const std::string &getID() const {
138  return id;
139  }
140  };
141 
143  inline std::ostream &operator<<(std::ostream &s, const ProgArg &pa){
144  return s << pa.as<std::string>();
145  }
146 
148  // explicit specialization for bool types (returns whether the arg was actually given)
149  template<>
150  inline ProgArg::operator bool() const {
151  return pa_defined_internal(*this);
152  }
153 
154  // explicit specialization for bool types (returns whether the arg was actually given)
155  template<>
156  inline bool ProgArg::as() const{
157  return pa_defined_internal(*this);
158  }
159 
162 
170  inline bool operator&&(const ProgArg &a, const ProgArg &b){
171  return a.as<bool>() && b.as<bool>();
172  }
173 
175 
180  inline bool operator&&(const ProgArg &a, bool b){
181  return b && a.as<bool>();
182  }
183 
185 
190  inline bool operator&&(bool &b, const ProgArg &a){
191  return b && a.as<bool>();
192  }
193 
195 
202  inline bool operator||(const ProgArg &a, const ProgArg &b){
203  return a.as<bool>() || b.as<bool>();
204  }
206 
211  inline bool operator||(const ProgArg &a, bool b){
212  return b || a.as<bool>();
213  }
214 
216 
221  inline bool operator||(bool &b, const ProgArg &a){
222  return b || a.as<bool>();
223  }
224 
225 
226 
227 
229 
304  inline const ProgArg pa(const std::string &id, unsigned int subargidx = 0){
305  if(!id.length()) THROW_ProgArgException("invalid programm argument id ''");
306  return ProgArg(id,subargidx);
307  }
308 
310 
311  inline const ProgArg pa(unsigned int idx, bool danglingOnly = true){
312  return ProgArg(idx,danglingOnly);
313  }
314 
315 
317  template<class T>
318  inline const T pa_def(const std::string &id, unsigned int subargidx, const T &def){
319  const ProgArg p = pa(id,subargidx);
320  return p ? parse<T>(p) : def;
321  }
322 
324  template<class T>
325  inline const T pa_def(const std::string &id, const T &def){
326  return pa_def(id,0,def);
327  }
328 
329 
330 
332 
333  ICLUtils_API unsigned int pa_get_count(bool danglingOnly = true);
334 
336 
343  ICLUtils_API const std::string &pa_get_progname(bool fullpath = false);
344 
346  ICLUtils_API void pa_show_usage(const std::string &msg = "");
347 
349  // utility class which allows the user to call the paex-function in a 'stacked' manner
350  struct ICLUtils_API PAEX{
351  PAEX operator()(const std::string &pa, const std::string &ex);
352  };
355 
365  inline PAEX pa_explain(const std::string &pa, const std::string &ex){
366  pa_explain_internal(pa,ex);
367  return PAEX();
368  }
369 
371  // deferred implementation of stacking operator
372  inline PAEX PAEX::operator()(const std::string &pa, const std::string &ex){
373  return pa_explain(pa,ex);
374  }
378 
466  ICLUtils_API void pa_init(int n, char **ppc, const std::string &init, bool allowDanglingArgs = false);
467 
468 
470  ICLUtils_API void pa_show();
471 
473 
474  ICLUtils_API void pa_set_license(const std::string &newLicenseText);
475 
477 
478  ICLUtils_API void pa_set_help_text(const std::string &newHelpText);
479 
481  ICLUtils_API std::string pa_get_license();
482 
484  ICLUtils_API std::string pa_get_help_text();
485  } // namespace utils
486 }
487 
bool operator||(const ProgArg &a, const ProgArg &b)
this allows to check if either of two progargs are defined
Definition: ProgArg.h:202
undocument this line if you encounter any issues!
Definition: Any.h:37
ICLUtils_API void pa_show_usage(const std::string &msg="")
shows current available programm arguments
ICLUtils_API Any operator[](int subArgIdx) const
returns the given sub-argument in shape of an utils::Any
ICLUtils_API void pa_show()
shows all given program arguments
bool operator!() const
negation operator
Definition: ProgArg.h:119
ICLUtils_API std::string pa_get_license()
returns the current license text
#define ICLUtils_API
this macros are important for creating dll's
Definition: CompatMacros.h:171
#define THROW_ProgArgException(X)
Definition: ProgArg.h:46
ProgArg(const std::string &id, unsigned int subargidx)
private constructor
Definition: ProgArg.h:85
ProgArgException(const std::string &func, const std::string &what)
Definition: ProgArg.h:43
ICLUtils_API std::string pa_get_help_text()
returns the current help text (which is empty, if it was not set)
ICLUtils_API void pa_set_license(const std::string &newLicenseText)
Sets a license text, that is used when applications are run with –version or -v.
ICLUtils_API void pa_set_help_text(const std::string &newHelpText)
Sets a applications help text that is used when applications are run with –help or with unknown argum...
ICLUtils_API unsigned int pa_get_count(bool danglingOnly=true)
returns number of actually given args given
const ProgArg pa(const std::string &id, unsigned int subargidx=0)
returns given program argument
Definition: ProgArg.h:304
ICLUtils_API std::ostream & operator<<(std::ostream &s, const ConfigFile &cf)
Default ostream operator to put a ConfigFile into a stream.
Programm argument utility class.
Definition: ProgArg.h:80
const T pa_def(const std::string &id, unsigned int subargidx, const T &def)
utility function that allows to use a default value, if given argument was not defined
Definition: ProgArg.h:318
const std::string & getID() const
returns the prog-arg id
Definition: ProgArg.h:137
ICLUtils_API const std::string & pa_get_progname(bool fullpath=false)
returns application name (full command line)
PAEX pa_explain(const std::string &pa, const std::string &ex)
This function can be used to provide additional information for certain program arguments.
Definition: ProgArg.h:365
ProgArg(unsigned int idx, bool danglingOnly)
Definition: ProgArg.h:89
Base class for Exception handling in the ICL.
Definition: Exception.h:42
bool operator &&(const ProgArg &a, const ProgArg &b)
this allows to check if two progargs are defined
Definition: ProgArg.h:170
ICLUtils_API int n() const
returns the count of actually given sub arguments
Simple generic data type implementation that uses a string based data representation.
Definition: Any.h:109
std::string operator *() const
important convenience operator for using a ProgArg instance as string
Definition: ProgArg.h:132
ICLUtils_API void pa_init(int n, char **ppc, const std::string &init, bool allowDanglingArgs=false)
initialization function for ICL's program argument evaluation framework
T as() const
this template function can be used to explicitly cast a program argument into a given type
Definition: ProgArg.h:125
Programm argument environment exception type .
Definition: ProgArg.h:42