digiKam
configparam.h
Go to the documentation of this file.
1 /*
2  * H.265 video codec.
3  * Copyright (c) 2013-2014 struktur AG, Dirk Farin <farin@struktur.de>
4  *
5  * Authors: struktur AG, Dirk Farin <farin@struktur.de>
6  *
7  * This file is part of libde265.
8  *
9  * libde265 is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser General Public License as
11  * published by the Free Software Foundation, either version 3 of
12  * the License, or (at your option) any later version.
13  *
14  * libde265 is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with libde265. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #ifndef CONFIG_PARAM_H
24 #define CONFIG_PARAM_H
25 
26 #include "en265.h"
27 #include "util.h"
28 
29 #include <climits>
30 #include <vector>
31 #include <string>
32 #include <stddef.h>
33 #include <assert.h>
34 
35 
36 /* Notes: probably best to keep cmd-line-options here. So it will be:
37  - automatically consistent even when having different combinations of algorithms
38  - no other place to edit
39  - if needed, one can still override it at another place
40  */
41 
42 // TODO: set a stack of default prefixes in config_parameters, such that all options added
43 // will receive this namespace prefix.
44 
45 // TODO: add the possibility to remove long options again, i.e., not use the default id name
47 {
48  public:
49  option_base() : mShortOption(0), mLongOption(NULL) { }
50  option_base(const char* name) : mIDName(name), mShortOption(0), mLongOption(NULL) { }
51  virtual ~option_base() { }
52 
53 
54  // --- option identifier ---
55 
56  void set_ID(const char* name) { mIDName=name; }
57  void add_namespace_prefix(std::string prefix) { mPrefix = prefix + ":" + mPrefix; }
58 
59  std::string get_name() const { return mPrefix + mIDName; }
60 
61 
62  // --- description ---
63 
64  void set_description(std::string descr) { mDescription = descr; }
65  std::string get_description() const { return mDescription; }
66  bool has_description() const { return !mDescription.empty(); }
67 
68 
69  // --- value ---
70 
71  virtual bool is_defined() const = 0;
72  bool is_undefined() const { return !is_defined(); }
73 
74  virtual bool has_default() const = 0;
75 
76 
77  // --- command line options ----
78 
79  void set_cmd_line_options(const char* long_option, char short_option = 0)
80  {
81  mShortOption = short_option;
82  mLongOption = long_option;
83  }
84 
85  void set_short_option(char short_option) { mShortOption=short_option; }
86 
88  {
89  mShortOption = 0;
90  mLongOption = NULL;
91  }
92 
93  bool hasShortOption() const { return mShortOption!=0; }
94  char getShortOption() const { return mShortOption; }
95  bool hasLongOption() const { return true; } //mLongOption!=NULL; }
96  std::string getLongOption() const { return mLongOption ? std::string(mLongOption) : get_name(); }
97 
98  virtual LIBDE265_API bool processCmdLineArguments(char** argv, int* argc, int idx) { return false; }
99 
100 
101 
102  virtual std::string getTypeDescr() const = 0;
103 
104  virtual std::string get_default_string() const { return "N/A"; }
105 
106  private:
107  std::string mPrefix;
108  std::string mIDName;
109 
110  std::string mDescription;
111 
112  char mShortOption;
113  const char* mLongOption;
114 };
115 
116 
117 
118 class option_bool : public option_base
119 {
120 public:
121  option_bool() : value_set(false), default_set(false) { }
122 
123  operator bool() const {
124  assert(value_set || default_set);
125  return value_set ? value : default_value;
126  }
127 
128  virtual bool is_defined() const { return value_set || default_set; }
129  virtual bool has_default() const { return default_set; }
130 
131  void set_default(bool v) { default_value=v; default_set=true; }
132  virtual std::string get_default_string() const { return default_value ? "true":"false"; }
133 
134  virtual std::string getTypeDescr() const { return "(boolean)"; }
135  virtual LIBDE265_API bool processCmdLineArguments(char** argv, int* argc, int idx) { set(true); return true; }
136 
137  bool set(bool v) { value_set=true; value=v; return true; }
138 
139  private:
140  bool value_set;
141  bool value;
142 
143  bool default_set;
144  bool default_value;
145 };
146 
147 
149 {
150 public:
151  option_string() : value_set(false), default_set(false) { }
152 
153  const option_string& operator=(std::string v) { value=v; value_set=true; return *this; }
154 
155  operator std::string() const { return get(); }
156  std::string get() const {
157  assert(value_set || default_set);
158  return value_set ? value : default_value;
159  }
160 
161  virtual bool is_defined() const { return value_set || default_set; }
162  virtual bool has_default() const { return default_set; }
163 
164  void set_default(std::string v) { default_value=v; default_set=true; }
165  virtual LIBDE265_API std::string get_default_string() const { return default_value; }
166 
167  virtual LIBDE265_API std::string getTypeDescr() const { return "(string)"; }
168  virtual LIBDE265_API bool processCmdLineArguments(char** argv, int* argc, int idx);
169 
170  bool set(std::string v) { value_set=true; value=v; return true; }
171 
172  private:
173  bool value_set;
174  std::string value;
175 
176  bool default_set;
177  std::string default_value;
178 };
179 
180 
181 class option_int : public option_base
182 {
183 public:
184  option_int() : value_set(false), default_set(false),
185  have_low_limit(false), have_high_limit(false) { }
186 
187  void set_minimum(int mini) { have_low_limit =true; low_limit =mini; }
188  void set_maximum(int maxi) { have_high_limit=true; high_limit=maxi; }
189  void set_range(int mini,int maxi);
190  void set_valid_values(const std::vector<int>& v) { valid_values_set = v; }
191 
192  const option_int& operator=(int v) { value=v; value_set=true; return *this; }
193 
194  int operator() () const {
195  assert(value_set || default_set);
196  return value_set ? value : default_value;
197  }
198  operator int() const { return operator()(); }
199 
200  virtual bool is_defined() const { return value_set || default_set; }
201  virtual bool has_default() const { return default_set; }
202 
203  void set_default(int v) { default_value=v; default_set=true; }
204  virtual LIBDE265_API std::string get_default_string() const;
205 
206  virtual LIBDE265_API std::string getTypeDescr() const;
207  virtual LIBDE265_API bool processCmdLineArguments(char** argv, int* argc, int idx);
208 
209  bool set(int v) {
210  if (is_valid(v)) { value_set=true; value=v; return true; }
211  else { return false; }
212  }
213 
214  private:
215  bool value_set;
216  int value;
217 
218  bool default_set;
219  int default_value;
220 
221  bool have_low_limit, have_high_limit;
222  int low_limit, high_limit;
223 
224  std::vector<int> valid_values_set;
225 
226  bool is_valid(int v) const;
227 };
228 
229 
230 
232 {
233 public:
234  choice_option_base() : choice_string_table(NULL) { }
235  ~choice_option_base() { delete[] choice_string_table; }
236 
237  bool set(std::string v) { return set_value(v); }
238  virtual bool set_value(const std::string& val) = 0;
239  virtual std::vector<std::string> get_choice_names() const = 0;
240 
241  virtual std::string getTypeDescr() const;
242  virtual LIBDE265_API bool processCmdLineArguments(char** argv, int* argc, int idx);
243 
244  const char** get_choices_string_table() const;
245 
246  protected:
248  delete[] choice_string_table;
249  choice_string_table = NULL;
250  }
251 
252  private:
253  mutable char* choice_string_table;
254 };
255 
256 
257 template <class T> class choice_option : public choice_option_base
258 {
259  public:
260  choice_option() : default_set(false), value_set(false) { }
261 
262  // --- initialization ---
263 
264  void add_choice(const std::string& s, T id, bool default_value=false) {
265  choices.push_back( std::make_pair(s,id) );
266  if (default_value) {
267  defaultID = id;
268  defaultValue = s;
269  default_set = true;
270  }
271 
273  }
274 
275  void set_default(T val) {
276 #ifdef FOR_LOOP_AUTO_SUPPORT
277  FOR_LOOP(auto, c, choices) {
278 #else
279  for (typename std::vector< std::pair<std::string,T> >::const_iterator it=choices.begin(); it!=choices.end(); ++it) {
280  const std::pair<std::string,T> & c = *it;
281 #endif
282  if (c.second == val) {
283  defaultID = val;
284  defaultValue = c.first;
285  default_set = true;
286  return;
287  }
288  }
289 
290  assert(false); // value does not exist
291  }
292 
293 
294  // --- usage ---
295 
296  bool set_value(const std::string& val) // returns false if it is not a valid option
297  {
298  value_set = true;
299  selectedValue=val;
300 
301  validValue = false;
302 
303 #ifdef FOR_LOOP_AUTO_SUPPORT
304  FOR_LOOP(auto, c, choices) {
305 #else
306  for (typename std::vector< std::pair<std::string,T> >::const_iterator it=choices.begin(); it!=choices.end(); ++it) {
307  const std::pair<std::string,T> & c = *it;
308 #endif
309  if (val == c.first) {
310  selectedID = c.second;
311  validValue = true;
312  }
313  }
314 
315  return validValue;
316  }
317 
318  bool isValidValue() const { return validValue; }
319 
320  const std::string& getValue() const {
321  assert(value_set || default_set);
322  return value_set ? selectedValue : defaultValue;
323  }
324  void setID(T id) { selectedID=id; validValue=true; }
325  const T getID() const { return value_set ? selectedID : defaultID; }
326 
327  virtual bool is_defined() const { return value_set || default_set; }
328  virtual bool has_default() const { return default_set; }
329 
330  std::vector<std::string> get_choice_names() const
331  {
332  std::vector<std::string> names;
333 #ifdef FOR_LOOP_AUTO_SUPPORT
334  FOR_LOOP(auto, p, choices) {
335 #else
336  for (typename std::vector< std::pair<std::string,T> >::const_iterator it=choices.begin(); it!=choices.end(); ++it) {
337  const std::pair<std::string,T> & p = *it;
338 #endif
339  names.push_back(p.first);
340  }
341  return names;
342  }
343 
344  std::string get_default_string() const { return defaultValue; }
345 
346  T operator() () const { return (T)getID(); }
347 
348  private:
349  std::vector< std::pair<std::string,T> > choices;
350 
351  bool default_set;
352  std::string defaultValue;
353  T defaultID;
354 
355  bool value_set;
356  std::string selectedValue;
357  T selectedID;
358 
359  bool validValue;
360 };
361 
362 
363 
364 
366 {
367  public:
368  config_parameters() : param_string_table(NULL) { }
369  ~config_parameters() { delete[] param_string_table; }
370 
372 
374  bool LIBDE265_API parse_command_line_params(int* argc, char** argv, int* first_idx=NULL,
375  bool ignore_unknown_options=false);
376 
377 
378  // --- connection to C API ---
379 
380  std::vector<std::string> get_parameter_IDs() const;
381  enum en265_parameter_type get_parameter_type(const char* param) const;
382 
383  std::vector<std::string> get_parameter_choices(const char* param) const;
384 
385  bool set_bool(const char* param, bool value);
386  bool set_int(const char* param, int value);
387  bool set_string(const char* param, const char* value);
388  bool set_choice(const char* param, const char* value);
389 
390  const char** get_parameter_string_table() const;
391  const char** get_parameter_choices_table(const char* param) const;
392 
393  private:
394  std::vector<option_base*> mOptions;
395 
396  option_base* find_option(const char* param) const;
397 
398  mutable char* param_string_table;
399 };
400 
401 #endif
Definition: configparam.h:232
choice_option_base()
Definition: configparam.h:234
virtual std::string getTypeDescr() const
const char ** get_choices_string_table() const
bool set(std::string v)
Definition: configparam.h:237
void invalidate_choices_string_table()
Definition: configparam.h:247
~choice_option_base()
Definition: configparam.h:235
virtual std::vector< std::string > get_choice_names() const =0
virtual bool set_value(const std::string &val)=0
virtual LIBDE265_API bool processCmdLineArguments(char **argv, int *argc, int idx)
Definition: configparam.h:258
virtual bool has_default() const
Definition: configparam.h:328
virtual bool is_defined() const
Definition: configparam.h:327
bool isValidValue() const
Definition: configparam.h:318
std::vector< std::string > get_choice_names() const
Definition: configparam.h:330
std::string get_default_string() const
Definition: configparam.h:344
choice_option()
Definition: configparam.h:260
const std::string & getValue() const
Definition: configparam.h:320
void add_choice(const std::string &s, T id, bool default_value=false)
Definition: configparam.h:264
void setID(T id)
Definition: configparam.h:324
const T getID() const
Definition: configparam.h:325
void set_default(T val)
Definition: configparam.h:275
bool set_value(const std::string &val)
Definition: configparam.h:296
T operator()() const
Definition: configparam.h:346
Definition: configparam.h:366
const char ** get_parameter_string_table() const
const char ** get_parameter_choices_table(const char *param) const
void LIBDE265_API print_params() const
void LIBDE265_API add_option(option_base *o)
~config_parameters()
Definition: configparam.h:369
config_parameters()
Definition: configparam.h:368
std::vector< std::string > get_parameter_choices(const char *param) const
bool set_string(const char *param, const char *value)
bool set_bool(const char *param, bool value)
bool set_choice(const char *param, const char *value)
std::vector< std::string > get_parameter_IDs() const
bool LIBDE265_API parse_command_line_params(int *argc, char **argv, int *first_idx=NULL, bool ignore_unknown_options=false)
bool set_int(const char *param, int value)
Definition: configparam.h:47
void set_description(std::string descr)
Definition: configparam.h:64
std::string get_name() const
Definition: configparam.h:59
std::string get_description() const
Definition: configparam.h:65
void set_cmd_line_options(const char *long_option, char short_option=0)
Definition: configparam.h:79
virtual std::string get_default_string() const
Definition: configparam.h:104
bool hasLongOption() const
Definition: configparam.h:95
bool hasShortOption() const
Definition: configparam.h:93
void unsetCmdLineOption()
Definition: configparam.h:87
void set_short_option(char short_option)
Definition: configparam.h:85
std::string getLongOption() const
Definition: configparam.h:96
char getShortOption() const
Definition: configparam.h:94
virtual std::string getTypeDescr() const =0
virtual ~option_base()
Definition: configparam.h:51
option_base(const char *name)
Definition: configparam.h:50
virtual bool is_defined() const =0
void set_ID(const char *name)
Definition: configparam.h:56
virtual LIBDE265_API bool processCmdLineArguments(char **argv, int *argc, int idx)
Definition: configparam.h:98
void add_namespace_prefix(std::string prefix)
Definition: configparam.h:57
virtual bool has_default() const =0
bool is_undefined() const
Definition: configparam.h:72
option_base()
Definition: configparam.h:49
bool has_description() const
Definition: configparam.h:66
Definition: configparam.h:119
option_bool()
Definition: configparam.h:121
virtual bool is_defined() const
Definition: configparam.h:128
virtual LIBDE265_API bool processCmdLineArguments(char **argv, int *argc, int idx)
Definition: configparam.h:135
virtual bool has_default() const
Definition: configparam.h:129
bool set(bool v)
Definition: configparam.h:137
virtual std::string get_default_string() const
Definition: configparam.h:132
virtual std::string getTypeDescr() const
Definition: configparam.h:134
void set_default(bool v)
Definition: configparam.h:131
Definition: configparam.h:182
void set_range(int mini, int maxi)
void set_valid_values(const std::vector< int > &v)
Definition: configparam.h:190
virtual LIBDE265_API std::string get_default_string() const
bool set(int v)
Definition: configparam.h:209
virtual LIBDE265_API bool processCmdLineArguments(char **argv, int *argc, int idx)
option_int()
Definition: configparam.h:184
void set_maximum(int maxi)
Definition: configparam.h:188
virtual bool has_default() const
Definition: configparam.h:201
int operator()() const
Definition: configparam.h:194
virtual bool is_defined() const
Definition: configparam.h:200
void set_minimum(int mini)
Definition: configparam.h:187
const option_int & operator=(int v)
Definition: configparam.h:192
void set_default(int v)
Definition: configparam.h:203
virtual LIBDE265_API std::string getTypeDescr() const
Definition: configparam.h:149
std::string get() const
Definition: configparam.h:156
virtual bool has_default() const
Definition: configparam.h:162
void set_default(std::string v)
Definition: configparam.h:164
virtual bool is_defined() const
Definition: configparam.h:161
virtual LIBDE265_API std::string get_default_string() const
Definition: configparam.h:165
const option_string & operator=(std::string v)
Definition: configparam.h:153
virtual LIBDE265_API std::string getTypeDescr() const
Definition: configparam.h:167
option_string()
Definition: configparam.h:151
virtual LIBDE265_API bool processCmdLineArguments(char **argv, int *argc, int idx)
bool set(std::string v)
Definition: configparam.h:170
#define LIBDE265_API
Definition: de265.h:52
en265_parameter_type
Definition: en265.h:66
QStringView prefix
Definition: itemviewutilities.cpp:593
qulonglong value
Definition: itemviewutilities.cpp:592
#define T
#define FOR_LOOP(type, var, list)
Definition: util.h:72