source: SHVCSoftware/branches/SHM-dev/source/Lib/TAppCommon/program_options_lite.h @ 1335

Last change on this file since 1335 was 1301, checked in by seregin, 9 years ago

port rev 4339 and rev 4340

  • Property svn:eol-style set to native
File size: 9.2 KB
Line 
1/* The copyright in this software is being made available under the BSD
2 * License, included below. This software may be subject to other third party
3 * and contributor rights, including patent rights, and no such rights are
4 * granted under this license.
5 *
6 * Copyright (c) 2010-2015, ITU/ISO/IEC
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 *  * Redistributions of source code must retain the above copyright notice,
13 *    this list of conditions and the following disclaimer.
14 *  * Redistributions in binary form must reproduce the above copyright notice,
15 *    this list of conditions and the following disclaimer in the documentation
16 *    and/or other materials provided with the distribution.
17 *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
18 *    be used to endorse or promote products derived from this software without
19 *    specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 */
33#include <iostream>
34#include <sstream>
35#include <string>
36#include <list>
37#include <map>
38
39#ifndef __PROGRAM_OPTIONS_LITE__
40#define __PROGRAM_OPTIONS_LITE__
41
42//! \ingroup TAppCommon
43//! \{
44
45
46namespace df
47{
48  namespace program_options_lite
49  {
50    struct Options;
51
52    struct ParseFailure : public std::exception
53    {
54      ParseFailure(std::string arg0, std::string val0) throw()
55      : arg(arg0), val(val0)
56      {}
57
58      ~ParseFailure() throw() {};
59
60      std::string arg;
61      std::string val;
62
63      const char* what() const throw() { return "Option Parse Failure"; }
64    };
65
66    struct ErrorReporter
67    {
68      ErrorReporter() : is_errored(0) {}
69      virtual ~ErrorReporter() {}
70      virtual std::ostream& error(const std::string& where);
71      virtual std::ostream& warn(const std::string& where);
72      bool is_errored;
73    };
74
75    extern ErrorReporter default_error_reporter;
76
77    void doHelp(std::ostream& out, Options& opts, unsigned columns = 80);
78    std::list<const char*> scanArgv(Options& opts, unsigned argc, const char* argv[], ErrorReporter& error_reporter = default_error_reporter);
79    void setDefaults(Options& opts);
80    void parseConfigFile(Options& opts, const std::string& filename, ErrorReporter& error_reporter = default_error_reporter);
81
82    /** OptionBase: Virtual base class for storing information relating to a
83     * specific option This base class describes common elements.  Type specific
84     * information should be stored in a derived class. */
85    struct OptionBase
86    {
87      OptionBase(const std::string& name, const std::string& desc)
88      : opt_string(name), opt_desc(desc)
89      {};
90
91      virtual ~OptionBase() {}
92
93      /* parse argument arg, to obtain a value for the option */
94      virtual void parse(const std::string& arg, ErrorReporter&) = 0;
95      /* set the argument to the default value */
96      virtual void setDefault() = 0;
97
98      std::string opt_string;
99      std::string opt_desc;
100    };
101
102    /** Type specific option storage */
103    template<typename T>
104    struct Option : public OptionBase
105    {
106      Option(const std::string& name, T& storage, T default_val, const std::string& desc)
107      : OptionBase(name, desc), opt_storage(storage), opt_default_val(default_val)
108      {}
109
110      void parse(const std::string& arg, ErrorReporter&);
111
112      void setDefault()
113      {
114        opt_storage = opt_default_val;
115      }
116
117      T& opt_storage;
118      T opt_default_val;
119    };
120
121    /* Generic parsing */
122    template<typename T>
123    inline void
124    Option<T>::parse(const std::string& arg, ErrorReporter&)
125    {
126      std::istringstream arg_ss (arg,std::istringstream::in);
127      arg_ss.exceptions(std::ios::failbit);
128      try
129      {
130        arg_ss >> opt_storage;
131      }
132      catch (...)
133      {
134        throw ParseFailure(opt_string, arg);
135      }
136    }
137
138    /* string parsing is specialized -- copy the whole string, not just the
139     * first word */
140    template<>
141    inline void
142    Option<std::string>::parse(const std::string& arg, ErrorReporter&)
143    {
144      opt_storage = arg;
145    }
146
147    /** Option class for argument handling using a user provided function */
148    struct OptionFunc : public OptionBase
149    {
150      typedef void (Func)(Options&, const std::string&, ErrorReporter&);
151
152      OptionFunc(const std::string& name, Options& parent_, Func *func_, const std::string& desc)
153      : OptionBase(name, desc), parent(parent_), func(func_)
154      {}
155
156      void parse(const std::string& arg, ErrorReporter& error_reporter)
157      {
158        func(parent, arg, error_reporter);
159      }
160
161      void setDefault()
162      {
163        return;
164      }
165
166    private:
167      Options& parent;
168      Func* func;
169    };
170
171    class OptionSpecific;
172    struct Options
173    {
174      ~Options();
175
176      OptionSpecific addOptions();
177
178      struct Names
179      {
180        Names() : opt(0) {};
181        ~Names()
182        {
183          if (opt)
184          {
185            delete opt;
186          }
187        }
188        std::list<std::string> opt_long;
189        std::list<std::string> opt_short;
190        OptionBase* opt;
191      };
192
193      void addOption(OptionBase *opt);
194
195      typedef std::list<Names*> NamesPtrList;
196      NamesPtrList opt_list;
197
198      typedef std::map<std::string, NamesPtrList> NamesMap;
199      NamesMap opt_long_map;
200      NamesMap opt_short_map;
201    };
202
203    /* Class with templated overloaded operator(), for use by Options::addOptions() */
204    class OptionSpecific
205    {
206    public:
207      OptionSpecific(Options& parent_) : parent(parent_) {}
208
209      /**
210       * Add option described by name to the parent Options list,
211       *   with storage for the option's value
212       *   with default_val as the default value
213       *   with desc as an optional help description
214       */
215      template<typename T>
216      OptionSpecific&
217      operator()(const std::string& name, T& storage, T default_val, const std::string& desc = "")
218      {
219        parent.addOption(new Option<T>(name, storage, default_val, desc));
220        return *this;
221      }
222#if 1 //SVC_EXTENSION
223      template<typename T>
224      OptionSpecific&
225        operator()(const std::string& name, T* storage, T default_val, unsigned uiMaxNum, const std::string& desc = "" )
226      {
227        std::string cNameBuffer;
228        std::string cDescriptionBuffer;
229
230        cNameBuffer       .resize( name.size() + 10 );
231        cDescriptionBuffer.resize( desc.size() + 10 );
232
233        for ( unsigned int uiK = 0; uiK < uiMaxNum; uiK++ )
234        {
235          // isn't there are sprintf function for string??
236          sprintf((char*) cNameBuffer.c_str()       ,name.c_str(),uiK,uiK);
237          sprintf((char*) cDescriptionBuffer.c_str(),desc.c_str(),uiK,uiK);
238
239          size_t pos = cNameBuffer.find_first_of('\0');
240          if(pos != std::string::npos)
241            cNameBuffer.resize(pos);
242     
243          parent.addOption(new Option<T>( cNameBuffer, (storage[uiK]), default_val, cDescriptionBuffer ));
244        }
245
246        return *this;
247      }
248
249      template<typename T>
250      OptionSpecific&
251        operator()(const std::string& name, T** storage, T default_val, unsigned uiMaxNum, const std::string& desc = "" )
252      {
253        std::string cNameBuffer;
254        std::string cDescriptionBuffer;
255
256        cNameBuffer       .resize( name.size() + 10 );
257        cDescriptionBuffer.resize( desc.size() + 10 );
258
259        for ( unsigned int uiK = 0; uiK < uiMaxNum; uiK++ )
260        {
261          // isn't there are sprintf function for string??
262          sprintf((char*) cNameBuffer.c_str()       ,name.c_str(),uiK,uiK);
263          sprintf((char*) cDescriptionBuffer.c_str(),desc.c_str(),uiK,uiK);
264
265          size_t pos = cNameBuffer.find_first_of('\0');
266          if(pos != std::string::npos)
267            cNameBuffer.resize(pos);
268
269          parent.addOption(new Option<T>( cNameBuffer, *(storage[uiK]), default_val, cDescriptionBuffer ));
270        }
271
272        return *this;
273      }
274#endif
275      /**
276       * Add option described by name to the parent Options list,
277       *   with desc as an optional help description
278       * instead of storing the value somewhere, a function of type
279       * OptionFunc::Func is called.  It is upto this function to correctly
280       * handle evaluating the option's value.
281       */
282      OptionSpecific&
283      operator()(const std::string& name, OptionFunc::Func *func, const std::string& desc = "")
284      {
285        parent.addOption(new OptionFunc(name, parent, func, desc));
286        return *this;
287      }
288    private:
289      Options& parent;
290    };
291
292  } /* namespace: program_options_lite */
293} /* namespace: df */
294
295//! \}
296
297#endif
Note: See TracBrowser for help on using the repository browser.