Changeset 1313 in 3DVCSoftware for trunk/source/Lib/TAppCommon/program_options_lite.h
- Timestamp:
- 13 Aug 2015, 17:38:13 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/source/Lib/TAppCommon/program_options_lite.h
r1179 r1313 2 2 * License, included below. This software may be subject to other third party 3 3 * and contributor rights, including patent rights, and no such rights are 4 * granted under this license. 4 * granted under this license. 5 5 * 6 * Copyright (c) 2010-2015, ITU/ISO/IEC6 * Copyright (c) 2010-2015, ITU/ISO/IEC 7 7 * All rights reserved. 8 8 * … … 36 36 #include <list> 37 37 #include <map> 38 #include "../TLibCommon/ TypeDef.h"39 40 #if H_MV38 #include "../TLibCommon/CommonDef.h" 39 40 #if NH_MV 41 41 #include <vector> 42 42 #include <errno.h> … … 47 47 #endif 48 48 #endif 49 50 51 #ifndef __PROGRAM_OPTIONS_LITE__ 52 #define __PROGRAM_OPTIONS_LITE__ 53 49 54 //! \ingroup TAppCommon 50 55 //! \{ … … 56 61 { 57 62 struct Options; 58 63 59 64 struct ParseFailure : public std::exception 60 65 { … … 71 76 }; 72 77 78 struct ErrorReporter 79 { 80 ErrorReporter() : is_errored(0) {} 81 virtual ~ErrorReporter() {} 82 virtual std::ostream& error(const std::string& where); 83 virtual std::ostream& warn(const std::string& where); 84 bool is_errored; 85 }; 86 87 extern ErrorReporter default_error_reporter; 88 73 89 void doHelp(std::ostream& out, Options& opts, unsigned columns = 80); 74 unsigned parseGNU(Options& opts, unsigned argc, const char* argv[]); 75 unsigned parseSHORT(Options& opts, unsigned argc, const char* argv[]); 76 std::list<const char*> scanArgv(Options& opts, unsigned argc, const char* argv[]); 77 void scanLine(Options& opts, std::string& line); 78 void scanFile(Options& opts, std::istream& in); 90 std::list<const char*> scanArgv(Options& opts, unsigned argc, const char* argv[], ErrorReporter& error_reporter = default_error_reporter); 79 91 void setDefaults(Options& opts); 80 void parseConfigFile(Options& opts, const std::string& filename); 81 bool storePair(Options& opts, const std::string& name, const std::string& value); 82 92 void parseConfigFile(Options& opts, const std::string& filename, ErrorReporter& error_reporter = default_error_reporter); 93 83 94 /** OptionBase: Virtual base class for storing information relating to a 84 95 * specific option This base class describes common elements. Type specific … … 86 97 struct OptionBase 87 98 { 88 #if H_MV99 #if NH_MV 89 100 OptionBase(const std::string& name, const std::string& desc, bool duplicate = false) 90 101 : opt_string(name), opt_desc(desc), opt_duplicate(duplicate) … … 94 105 #endif 95 106 {}; 96 107 97 108 virtual ~OptionBase() {} 98 109 99 110 /* parse argument arg, to obtain a value for the option */ 100 virtual void parse(const std::string& arg ) = 0;111 virtual void parse(const std::string& arg, ErrorReporter&) = 0; 101 112 /* set the argument to the default value */ 102 113 virtual void setDefault() = 0; 103 114 104 115 std::string opt_string; 105 116 std::string opt_desc; 106 #if H_MV117 #if NH_MV 107 118 bool opt_duplicate; 108 119 #endif 109 120 }; 110 121 111 122 /** Type specific option storage */ 112 123 template<typename T> 113 124 struct Option : public OptionBase 114 125 { 115 #if H_MV126 #if NH_MV 116 127 Option(const std::string& name, T& storage, T default_val, const std::string& desc, bool duplicate = false) 117 128 : OptionBase(name, desc, duplicate), opt_storage(storage), opt_default_val(default_val) … … 121 132 #endif 122 133 {} 123 124 void parse(const std::string& arg );125 134 135 void parse(const std::string& arg, ErrorReporter&); 136 126 137 void setDefault() 127 138 { 128 139 opt_storage = opt_default_val; 129 140 } 130 141 131 142 T& opt_storage; 132 143 T opt_default_val; 133 144 }; 134 145 135 146 /* Generic parsing */ 136 147 template<typename T> 137 148 inline void 138 Option<T>::parse(const std::string& arg )149 Option<T>::parse(const std::string& arg, ErrorReporter&) 139 150 { 140 151 std::istringstream arg_ss (arg,std::istringstream::in); … … 149 160 } 150 161 } 151 162 152 163 /* string parsing is specialized -- copy the whole string, not just the 153 164 * first word */ 154 165 template<> 155 166 inline void 156 Option<std::string>::parse(const std::string& arg )167 Option<std::string>::parse(const std::string& arg, ErrorReporter&) 157 168 { 158 169 opt_storage = arg; 159 170 } 160 171 161 #if H_MV172 #if NH_MV 162 173 template<> 163 174 inline void 164 Option<char*>::parse(const std::string& arg )175 Option<char*>::parse(const std::string& arg, ErrorReporter&) 165 176 { 166 177 opt_storage = arg.empty() ? NULL : strdup(arg.c_str()) ; … … 169 180 template<> 170 181 inline void 171 Option< std::vector<char*> >::parse(const std::string& arg )182 Option< std::vector<char*> >::parse(const std::string& arg, ErrorReporter&) 172 183 { 173 184 opt_storage.clear(); … … 191 202 template<> 192 203 inline void 193 Option< std::vector<double> >::parse(const std::string& arg )204 Option< std::vector<double> >::parse(const std::string& arg, ErrorReporter&) 194 205 { 195 206 char* pcNextStart = (char*) arg.data(); … … 227 238 template<> 228 239 inline void 229 Option< std::vector<int> >::parse(const std::string& arg )240 Option< std::vector<int> >::parse(const std::string& arg, ErrorReporter&) 230 241 { 231 242 opt_storage.clear(); … … 265 276 template<> 266 277 inline void 267 Option< std::vector<bool> >::parse(const std::string& arg )278 Option< std::vector<bool> >::parse(const std::string& arg, ErrorReporter&) 268 279 { 269 280 char* pcNextStart = (char*) arg.data(); … … 299 310 struct OptionFunc : public OptionBase 300 311 { 301 typedef void (Func)(Options&, const std::string& );302 312 typedef void (Func)(Options&, const std::string&, ErrorReporter&); 313 303 314 OptionFunc(const std::string& name, Options& parent_, Func *func_, const std::string& desc) 304 315 : OptionBase(name, desc), parent(parent_), func(func_) 305 316 {} 306 307 void parse(const std::string& arg )308 { 309 func(parent, arg );310 } 311 317 318 void parse(const std::string& arg, ErrorReporter& error_reporter) 319 { 320 func(parent, arg, error_reporter); 321 } 322 312 323 void setDefault() 313 324 { 314 325 return; 315 326 } 316 327 317 328 private: 318 329 Options& parent; 319 void (*func)(Options&, const std::string&);320 }; 321 330 Func* func; 331 }; 332 322 333 class OptionSpecific; 323 334 struct Options 324 335 { 325 336 ~Options(); 326 337 327 338 OptionSpecific addOptions(); 328 339 329 340 struct Names 330 341 { … … 333 344 { 334 345 if (opt) 346 { 335 347 delete opt; 348 } 336 349 } 337 350 std::list<std::string> opt_long; … … 341 354 342 355 void addOption(OptionBase *opt); 343 356 344 357 typedef std::list<Names*> NamesPtrList; 345 358 NamesPtrList opt_list; 346 359 347 360 typedef std::map<std::string, NamesPtrList> NamesMap; 348 361 NamesMap opt_long_map; 349 362 NamesMap opt_short_map; 350 363 }; 351 364 352 365 /* Class with templated overloaded operator(), for use by Options::addOptions() */ 353 366 class OptionSpecific … … 355 368 public: 356 369 OptionSpecific(Options& parent_) : parent(parent_) {} 357 370 358 371 /** 359 372 * Add option described by name to the parent Options list, … … 369 382 return *this; 370 383 } 371 372 #if H_MV384 385 #if NH_MV 373 386 template<typename T> 374 387 OptionSpecific& … … 419 432 Options& parent; 420 433 }; 421 422 } ;/* namespace: program_options_lite */423 } ;/* namespace: df */434 435 } /* namespace: program_options_lite */ 436 } /* namespace: df */ 424 437 425 438 //! \} 439 440 #endif
Note: See TracChangeset for help on using the changeset viewer.