Ignore:
Timestamp:
13 Aug 2015, 17:38:13 (9 years ago)
Author:
tech
Message:

Merged 14.1-update-dev1@1312.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/source/Lib/TAppCommon/program_options_lite.h

    r1179 r1313  
    22 * License, included below. This software may be subject to other third party
    33 * and contributor rights, including patent rights, and no such rights are
    4  * granted under this license. 
     4 * granted under this license.
    55 *
    6 * Copyright (c) 2010-2015, ITU/ISO/IEC
     6 * Copyright (c) 2010-2015, ITU/ISO/IEC
    77 * All rights reserved.
    88 *
     
    3636#include <list>
    3737#include <map>
    38 #include  "../TLibCommon/TypeDef.h"
    39 
    40 #if H_MV
     38#include  "../TLibCommon/CommonDef.h"
     39
     40#if NH_MV
    4141#include <vector>
    4242#include <errno.h>
     
    4747#endif
    4848#endif
     49
     50
     51#ifndef __PROGRAM_OPTIONS_LITE__
     52#define __PROGRAM_OPTIONS_LITE__
     53
    4954//! \ingroup TAppCommon
    5055//! \{
     
    5661  {
    5762    struct Options;
    58    
     63
    5964    struct ParseFailure : public std::exception
    6065    {
     
    7176    };
    7277
     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
    7389    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);
    7991    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
    8394    /** OptionBase: Virtual base class for storing information relating to a
    8495     * specific option This base class describes common elements.  Type specific
     
    8697    struct OptionBase
    8798    {
    88 #if H_MV     
     99#if NH_MV     
    89100      OptionBase(const std::string& name, const std::string& desc, bool duplicate = false)
    90101        : opt_string(name), opt_desc(desc), opt_duplicate(duplicate)
     
    94105#endif
    95106      {};
    96      
     107
    97108      virtual ~OptionBase() {}
    98      
     109
    99110      /* 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;
    101112      /* set the argument to the default value */
    102113      virtual void setDefault() = 0;
    103      
     114
    104115      std::string opt_string;
    105116      std::string opt_desc;
    106 #if H_MV
     117#if NH_MV
    107118      bool        opt_duplicate;
    108119#endif
    109120    };
    110    
     121
    111122    /** Type specific option storage */
    112123    template<typename T>
    113124    struct Option : public OptionBase
    114125    {
    115 #if H_MV
     126#if NH_MV
    116127      Option(const std::string& name, T& storage, T default_val, const std::string& desc, bool duplicate = false)
    117128        : OptionBase(name, desc, duplicate), opt_storage(storage), opt_default_val(default_val)
     
    121132#endif
    122133      {}
    123      
    124       void parse(const std::string& arg);
    125      
     134
     135      void parse(const std::string& arg, ErrorReporter&);
     136
    126137      void setDefault()
    127138      {
    128139        opt_storage = opt_default_val;
    129140      }
    130      
     141
    131142      T& opt_storage;
    132143      T opt_default_val;
    133144    };
    134    
     145
    135146    /* Generic parsing */
    136147    template<typename T>
    137148    inline void
    138     Option<T>::parse(const std::string& arg)
     149    Option<T>::parse(const std::string& arg, ErrorReporter&)
    139150    {
    140151      std::istringstream arg_ss (arg,std::istringstream::in);
     
    149160      }
    150161    }
    151    
     162
    152163    /* string parsing is specialized -- copy the whole string, not just the
    153164     * first word */
    154165    template<>
    155166    inline void
    156     Option<std::string>::parse(const std::string& arg)
     167    Option<std::string>::parse(const std::string& arg, ErrorReporter&)
    157168    {
    158169      opt_storage = arg;
    159170    }
    160171
    161 #if H_MV   
     172#if NH_MV   
    162173    template<>
    163174    inline void
    164       Option<char*>::parse(const std::string& arg)
     175      Option<char*>::parse(const std::string& arg, ErrorReporter&)
    165176    {
    166177      opt_storage = arg.empty() ? NULL : strdup(arg.c_str()) ;
     
    169180    template<>
    170181    inline void
    171       Option< std::vector<char*> >::parse(const std::string& arg)
     182      Option< std::vector<char*> >::parse(const std::string& arg, ErrorReporter&)
    172183    {
    173184      opt_storage.clear();
     
    191202    template<>   
    192203    inline void
    193       Option< std::vector<double> >::parse(const std::string& arg)
     204      Option< std::vector<double> >::parse(const std::string& arg, ErrorReporter&)
    194205    {
    195206      char* pcNextStart = (char*) arg.data();
     
    227238    template<>
    228239    inline void
    229       Option< std::vector<int> >::parse(const std::string& arg)
     240      Option< std::vector<int> >::parse(const std::string& arg, ErrorReporter&)
    230241    {
    231242      opt_storage.clear();
     
    265276    template<>
    266277    inline void
    267       Option< std::vector<bool> >::parse(const std::string& arg)
     278      Option< std::vector<bool> >::parse(const std::string& arg, ErrorReporter&)
    268279    {
    269280      char* pcNextStart = (char*) arg.data();
     
    299310    struct OptionFunc : public OptionBase
    300311    {
    301       typedef void (Func)(Options&, const std::string&);
    302      
     312      typedef void (Func)(Options&, const std::string&, ErrorReporter&);
     313
    303314      OptionFunc(const std::string& name, Options& parent_, Func *func_, const std::string& desc)
    304315      : OptionBase(name, desc), parent(parent_), func(func_)
    305316      {}
    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
    312323      void setDefault()
    313324      {
    314325        return;
    315326      }
    316      
     327
    317328    private:
    318329      Options& parent;
    319       void (*func)(Options&, const std::string&);
    320     };
    321    
     330      Func* func;
     331    };
     332
    322333    class OptionSpecific;
    323334    struct Options
    324335    {
    325336      ~Options();
    326      
     337
    327338      OptionSpecific addOptions();
    328      
     339
    329340      struct Names
    330341      {
     
    333344        {
    334345          if (opt)
     346          {
    335347            delete opt;
     348          }
    336349        }
    337350        std::list<std::string> opt_long;
     
    341354
    342355      void addOption(OptionBase *opt);
    343      
     356
    344357      typedef std::list<Names*> NamesPtrList;
    345358      NamesPtrList opt_list;
    346      
     359
    347360      typedef std::map<std::string, NamesPtrList> NamesMap;
    348361      NamesMap opt_long_map;
    349362      NamesMap opt_short_map;
    350363    };
    351    
     364
    352365    /* Class with templated overloaded operator(), for use by Options::addOptions() */
    353366    class OptionSpecific
     
    355368    public:
    356369      OptionSpecific(Options& parent_) : parent(parent_) {}
    357      
     370
    358371      /**
    359372       * Add option described by name to the parent Options list,
     
    369382        return *this;
    370383      }
    371      
    372 #if H_MV
     384
     385#if NH_MV
    373386      template<typename T>
    374387      OptionSpecific&
     
    419432      Options& parent;
    420433    };
    421    
    422   }; /* namespace: program_options_lite */
    423 }; /* namespace: df */
     434
     435  } /* namespace: program_options_lite */
     436} /* namespace: df */
    424437
    425438//! \}
     439
     440#endif
Note: See TracChangeset for help on using the changeset viewer.