File: ArgumentList.hh

package info (click to toggle)
exactimage 1.2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,048 kB
  • sloc: cpp: 35,940; ansic: 1,952; xml: 1,447; makefile: 338; perl: 138; sh: 110; python: 45; php: 37; ruby: 12
file content (236 lines) | stat: -rw-r--r-- 6,282 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
 * --- GSMP-COPYRIGHT-NOTE-BEGIN ---
 * 
 * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
 * Please add additional copyright information _after_ the line containing
 * the GSMP-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
 * the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
 * 
 * GSMP: utility/include/ArgumentList.hh
 *
 * Copyright (C) 2005 - 2011
 *   Ren\xe9\xa0\x92ebe, ExactCODE GmbH Germany
 *
 * General Sound Manipulation Program is Copyright (C) 2000 - 2004
 *   Valentin Ziegler and René ’ebe
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2. A copy of the GNU General
 * Public License can be found in the file LICENSE.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANT-
 * ABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 * 
 * --- GSMP-COPYRIGHT-NOTE-END ---
 */

/* Short Description:
 *   Object oriented argument list parsing.
 */

#ifndef UTILITY__ARGUMENTLIST_HH__
#define UTILITY__ARGUMENTLIST_HH__

#include <iostream>

#include <map>
#include <vector>
#include <string>

#include <sstream>

namespace Utility
{
  class BasicArgument {
  public:
  
    BasicArgument (const std::string& i_sname, const std::string& i_lname,
		   const std::string& i_desc,
		   int i_min_count, int i_max_count,
		   bool i_fragmented, bool i_reset);
  
    virtual ~BasicArgument ();
    
    void SetReset (bool i_reset) { reset=i_reset; }
    void SetList (bool i_list) { list=i_list; }
  
    virtual bool Probe ();
    virtual void Start () = 0;
    
    virtual bool Read () = 0;
    virtual bool Read (const std::string& arg) = 0;
    
    virtual bool Interrupt ();
    virtual bool Finalize ();
    
    std::string sname;
    std::string lname;
    std::string desc;
  
    int min_count, max_count;
    bool no_arg;
    bool fragmented; // fragment across several arguments
    bool reset; // reset (clear) for each new argument
    bool list; // list in the Usage
    
    int count, pass_count;
    
  protected:
    virtual bool InterruptImpl () {
      return true;
    }
  };

  template <typename T>
  class Argument : public BasicArgument {
 
  public:
  
    Argument (const std::string& i_sname, const std::string& i_lname,
	      const std::string& i_desc,
	      int i_min_count = 0, int i_max_count = 0,
	      bool i_fragmented = false, bool i_reset = false)
      : BasicArgument (i_sname, i_lname, i_desc, i_min_count, i_max_count,
		       i_fragmented, i_reset),
	callback (0)
    {
      // if bool (or other special option) always use exaclty one value
      if (no_arg)
	values.push_back (T () );
    }

    Argument (const std::string& i_sname, const std::string& i_lname,
	      const std::string& i_desc, 
	      const T& i_value, int i_min_count = 0, int i_max_count = 0,
	      bool i_fragmented = false, bool i_reset = false)
      : BasicArgument (i_sname, i_lname, i_desc, i_min_count, i_max_count,
		       i_fragmented, i_reset),
	callback (0)
    {
      values.push_back (i_value);
      // if bool (or other special option) always containing one value
      if (no_arg)
	values.push_back (T () );
    }
  
    
    void Start () {
      // reset requested?
      if (!no_arg && reset) {
	values.clear ();
	count = 0;
      }
    }
    
    bool Read () {
      std::cerr << "Error: Argument " << lname << " needs an parameter!" << std::endl;
      return false;
    }
  
    bool Read (const std::string& arg) {
      
      if (count >= max_count) {
	std::cerr << "Error: Too many parameter for argument " << lname
		  << ", only " << max_count << " allowed!" << std::endl;
	return false;
      }
      
      T value = ReadImpl (arg);
      
      // special case if default was supplied -> overwrite the first ...
      if (count == 0 && values.size () > 0)
	values[0] = value;
      else
	values.push_back (value);
    
      ++count; ++pass_count;
    
      return true;
    }
  
    T Get (unsigned int i = 0) const {
      if (values.size () > i) {
	return values [i];
      }
      else
	std::cerr << "Error: There is no parameter: " << i
		  << " present for argument " << this->lname << std::endl;
      return T ();
    }
    
    int Size () const { return values.size(); }
    int Count () const { return count; }
    
    void Bind (bool (*function)(const Argument<T>&)) {
      callback = function;
    }
    
    const std::vector<T>& Values () const { return values; }
    
  private:
    
    T ReadImpl (const std::string& arg)
    {
      std::stringstream stream (arg);
      // TODO: error handling
      T value;
      stream >> value;
      return value;
    }
    
    virtual bool InterruptImpl () {
      if (callback)
	return callback(*this);
      return true;
    }
    
    std::vector<T> values;
    bool (*callback)(const Argument<T>&);
  };

  // some bool specialisations ...
  template <> bool Argument<bool>::Read ();
  template <> bool Argument<bool>::Read (const std::string& arg);
  
  template <> std::string Argument<std::string>::ReadImpl (const std::string& arg);

  class ArgumentList {

  public:
  
    // inresidual mode the last stray parameters are gathered
    ArgumentList (bool i_residual = false);
    ~ArgumentList ();

    // register a to be parsed argument
    void Add (BasicArgument* arg);
    // parse options specified
    bool Read (int argc, char** argv);
  
    // return the vector of gathered residuals (if residual parsing enabled)
    const std::vector<std::string>& Residuals () const;

    // printout the usual usage list, generated from the registered arguments
    void Usage (std::ostream& os) const;
    
  private:
    typedef std::map<std::string, BasicArgument*> container;
    typedef container::iterator iterator;
    typedef container::const_iterator const_iterator;
  
    container short_content;
    container long_content;
  
    std::vector<std::string> residuals;
  
    bool residual;
  };

} // namespace Utility

#include "template/ArgumentList.tcc"

#endif // UTILITY__ARGUMENTLIST_HH__