LCOV - code coverage report
Current view: top level - advgetopt - option_info.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 7 7 100.0 %
Date: 2022-05-26 21:41:34 Functions: 7 8 87.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2006-2022  Made to Order Software Corp.  All Rights Reserved
       2             : //
       3             : // https://snapwebsites.org/project/advgetopt
       4             : // contact@m2osw.com
       5             : //
       6             : // This program is free software; you can redistribute it and/or modify
       7             : // it under the terms of the GNU General Public License as published by
       8             : // the Free Software Foundation; either version 2 of the License, or
       9             : // (at your option) any later version.
      10             : //
      11             : // This program is distributed in the hope that it will be useful,
      12             : // but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             : // GNU General Public License for more details.
      15             : //
      16             : // You should have received a copy of the GNU General Public License along
      17             : // with this program; if not, write to the Free Software Foundation, Inc.,
      18             : // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      19             : #pragma once
      20             : 
      21             : /** \file
      22             :  * \brief Declaration of the option_info class used to record available options.
      23             :  *
      24             :  * The library offers a way to verify your command line and other options
      25             :  * with features such as validators and reading of various types of
      26             :  * configuration files.
      27             :  *
      28             :  * The class defined in this file is used to describe an option.
      29             :  */
      30             : 
      31             : // advgetopt lib
      32             : //
      33             : #include    "advgetopt/flags.h"
      34             : #include    "advgetopt/validator.h"
      35             : #include    "advgetopt/variables.h"
      36             : 
      37             : 
      38             : // C++ lib
      39             : //
      40             : #include    <functional>
      41             : #include    <map>
      42             : 
      43             : 
      44             : 
      45             : namespace advgetopt
      46             : {
      47             : 
      48             : 
      49             : typedef char32_t            short_name_t;
      50             : 
      51             : constexpr short_name_t      NO_SHORT_NAME = U'\0';
      52             : 
      53             : 
      54             : short_name_t                string_to_short_name(std::string const & name);
      55             : std::string                 short_name_to_string(short_name_t short_name);
      56             : 
      57             : 
      58             : 
      59             : 
      60             : enum class option_source_t
      61             : {
      62             :     SOURCE_COMMAND_LINE,            // set on command line
      63             :     SOURCE_CONFIGURATION,           // read from config file
      64             :     SOURCE_DIRECT,                  // set by programmer directly
      65             :     SOURCE_DYNAMIC,                 // set dynamically (a.k.a. fluid-settings)
      66             :     SOURCE_ENVIRONMENT_VARIABLE,    // found in environment variable
      67             : 
      68             :     SOURCE_UNDEFINED,               // the option object exists, but the value is still undefined
      69             : };
      70             : 
      71             : 
      72             : // the `option_info` can be used instead or on top of the `struct option`
      73             : // it is especially used to read an external getopt declaration file
      74             : //
      75        1719 : class option_info
      76             : {
      77             : public:
      78             :     typedef std::shared_ptr<option_info>                    pointer_t;
      79             :     typedef std::vector<pointer_t>                          vector_t;
      80             :     typedef std::map<std::string, pointer_t>                map_by_name_t;
      81             :     typedef std::map<short_name_t, pointer_t>               map_by_short_name_t;
      82             :     typedef std::function<void (option_info const & opt)>   callback_t;
      83             :     typedef int                                             callback_id_t;
      84             : 
      85             :                                 option_info(std::string const & name, short_name_t short_name = NO_SHORT_NAME);
      86             : 
      87             :     std::string const &         get_name() const;
      88             :     void                        set_short_name(short_name_t short_name);
      89             :     short_name_t                get_short_name() const;
      90             :     std::string                 get_basename() const;
      91             :     std::string                 get_section_name() const;
      92             :     string_list_t               get_section_name_list() const;
      93             :     bool                        is_default_option() const;
      94             : 
      95             :     void                        set_environment_variable_name(std::string const & name);
      96             :     void                        set_environment_variable_name(char const * name);
      97             :     std::string                 get_environment_variable_name() const;
      98             :     bool                        get_environment_variable_value(
      99             :                                       std::string & value
     100             :                                     , char const * intro = nullptr) const;
     101             : 
     102             :     void                        set_flags(flag_t flags);
     103             :     void                        add_flag(flag_t flag);
     104             :     void                        remove_flag(flag_t flag);
     105             :     flag_t                      get_flags() const;
     106             :     bool                        has_flag(flag_t flag) const;
     107             : 
     108             :     bool                        has_default() const;
     109             :     void                        set_default(std::string const & default_value);
     110             :     void                        set_default(char const * default_value);
     111             :     void                        remove_default();
     112             :     std::string const &         get_default() const;
     113             : 
     114             :     void                        set_help(std::string const & help);
     115             :     void                        set_help(char const * help);
     116             :     std::string const &         get_help() const;
     117             : 
     118             :     bool                        set_validator(std::string const & name_and_params);
     119             :     bool                        set_validator(validator::pointer_t validator);
     120             :     bool                        set_validator(std::nullptr_t);
     121             :     validator::pointer_t        get_validator() const;
     122             : 
     123             :     void                        set_alias_destination(option_info::pointer_t destination);
     124             :     option_info::pointer_t      get_alias_destination() const;
     125             : 
     126             :     void                        set_multiple_separators(string_list_t const & separators);
     127             :     void                        set_multiple_separators(char const * const * separators);
     128             :     string_list_t const &       get_multiple_separators() const;
     129             : 
     130             :     void                        set_variables(variables::pointer_t vars);
     131             :     variables::pointer_t        get_variables() const;
     132             :     bool                        has_value(std::string const & value) const;
     133             :     bool                        add_value(std::string const & value, option_source_t source = option_source_t::SOURCE_DIRECT);
     134             :     bool                        set_value(int idx, std::string const & value, option_source_t source = option_source_t::SOURCE_DIRECT);
     135             :     bool                        set_multiple_values(std::string const & value, option_source_t source = option_source_t::SOURCE_DIRECT);
     136             :     bool                        is_defined() const;
     137             :     option_source_t             source() const;
     138             :     static void                 set_trace_sources(bool trace);
     139             :     string_list_t const &       trace_sources() const;
     140             :     static void                 set_configuration_filename(std::string const & filename);
     141             :     size_t                      size() const;
     142             :     std::string                 get_value(int idx = 0, bool raw = false) const;
     143             :     long                        get_long(int idx = 0) const;
     144             :     double                      get_double(int idx = 0) const;
     145             :     void                        lock(bool always = true);
     146             :     void                        unlock();
     147             :     void                        reset();
     148             : 
     149             :     callback_id_t               add_callback(callback_t const & c);
     150             :     void                        remove_callback(callback_id_t id);
     151             : 
     152             : private:
     153          37 :     struct callback_entry_t
     154             :     {
     155           2 :         callback_entry_t(callback_id_t id, callback_t const & c)
     156           2 :             : f_id(id)
     157           2 :             , f_callback(c)
     158             :         {
     159           2 :         }
     160             : 
     161             :         callback_id_t           f_id = 0;
     162             :         callback_t              f_callback = callback_t();
     163             :     };
     164             :     typedef std::vector<callback_entry_t>
     165             :                                 callback_vector_t;
     166             : 
     167             :     bool                        validate_all_values();
     168             :     bool                        validates(int idx = 0);
     169             :     void                        value_changed(int idx);
     170             :     void                        trace_source(int idx);
     171             : 
     172             :     // definitions
     173             :     //
     174             :     std::string                 f_name = std::string();
     175             :     short_name_t                f_short_name = NO_SHORT_NAME;
     176             :     std::string                 f_environment_variable_name = std::string();
     177             :     flag_t                      f_flags = GETOPT_FLAG_NONE;
     178             :     std::string                 f_default_value = std::string();
     179             :     std::string                 f_help = std::string();
     180             :     validator::pointer_t        f_validator = validator::pointer_t();
     181             :     pointer_t                   f_alias_destination = pointer_t();
     182             :     string_list_t               f_multiple_separators = string_list_t();
     183             :     callback_vector_t           f_callbacks = callback_vector_t();
     184             :     id_t                        f_next_callback_id = 0;
     185             :     string_list_t               f_trace_sources = string_list_t();
     186             :     variables::pointer_t        f_variables = variables::pointer_t();
     187             : 
     188             :     // value read from command line, environment, .conf file
     189             :     //
     190             :     option_source_t             f_source = option_source_t::SOURCE_UNDEFINED;
     191             :     string_list_t               f_value = string_list_t();
     192             :     mutable std::vector<long>   f_integer = std::vector<long>();
     193             :     mutable std::vector<double> f_double = std::vector<double>();
     194             : };
     195             : 
     196             : 
     197         165 : class option_info_ref
     198             : {
     199             : public:
     200             :                                 option_info_ref(option_info::pointer_t opt);
     201             : 
     202             :     bool                        empty() const;
     203             :     size_t                      length() const;
     204             :     size_t                      size() const;
     205             :     long                        get_long() const;
     206             :     double                      get_double() const;
     207             : 
     208             :                                 operator std::string () const;
     209             : 
     210             :     option_info_ref &           operator = (char value);
     211             :     option_info_ref &           operator = (char32_t value);
     212             :     option_info_ref &           operator = (char const * value);
     213             :     option_info_ref &           operator = (std::string const & value);
     214             :     option_info_ref &           operator = (option_info_ref const & value);
     215             : 
     216             :     option_info_ref &           operator += (char value);
     217             :     option_info_ref &           operator += (char32_t value);
     218             :     option_info_ref &           operator += (char const * value);
     219             :     option_info_ref &           operator += (std::string const & value);
     220             :     option_info_ref &           operator += (option_info_ref const & value);
     221             : 
     222             :     std::string                 operator + (char value) const;
     223             :     std::string                 operator + (char32_t value) const;
     224             :     std::string                 operator + (char const * value) const;
     225             :     std::string                 operator + (std::string const & value) const;
     226             :     std::string                 operator + (option_info_ref const & value) const;
     227             : 
     228             :     friend std::string          operator + (char value, option_info_ref const & rhs);
     229             :     friend std::string          operator + (char32_t value, option_info_ref const & rhs);
     230             :     friend std::string          operator + (char const * value, option_info_ref const & rhs);
     231             :     friend std::string          operator + (std::string const & value, option_info_ref const & rhs);
     232             : 
     233             :                                 operator bool () const;
     234             :     bool                        operator ! () const;
     235             : 
     236             :     bool                        operator == (char const * value) const;
     237             :     bool                        operator == (std::string const & value) const;
     238             :     bool                        operator == (option_info_ref const & value) const;
     239             : 
     240             :     bool                        operator != (char const * value) const;
     241             :     bool                        operator != (std::string const & value) const;
     242             :     bool                        operator != (option_info_ref const & value) const;
     243             : 
     244             :     bool                        operator < (char const * value) const;
     245             :     bool                        operator < (std::string const & value) const;
     246             :     bool                        operator < (option_info_ref const & value) const;
     247             : 
     248             :     bool                        operator <= (char const * value) const;
     249             :     bool                        operator <= (std::string const & value) const;
     250             :     bool                        operator <= (option_info_ref const & value) const;
     251             : 
     252             :     bool                        operator > (char const * value) const;
     253             :     bool                        operator > (std::string const & value) const;
     254             :     bool                        operator > (option_info_ref const & value) const;
     255             : 
     256             :     bool                        operator >= (char const * value) const;
     257             :     bool                        operator >= (std::string const & value) const;
     258             :     bool                        operator >= (option_info_ref const & value) const;
     259             : 
     260             :     friend bool                 operator == (char const * value, option_info_ref const & rhs);
     261             :     friend bool                 operator == (std::string const & value, option_info_ref const & rhs);
     262             : 
     263             :     friend bool                 operator != (char const * value, option_info_ref const & rhs);
     264             :     friend bool                 operator != (std::string const & value, option_info_ref const & rhs);
     265             : 
     266             :     friend bool                 operator < (char const * value, option_info_ref const & rhs);
     267             :     friend bool                 operator < (std::string const & value, option_info_ref const & rhs);
     268             : 
     269             :     friend bool                 operator <= (char const * value, option_info_ref const & rhs);
     270             :     friend bool                 operator <= (std::string const & value, option_info_ref const & rhs);
     271             : 
     272             :     friend bool                 operator > (char const * value, option_info_ref const & rhs);
     273             :     friend bool                 operator > (std::string const & value, option_info_ref const & rhs);
     274             : 
     275             :     friend bool                 operator >= (char const * value, option_info_ref const & rhs);
     276             :     friend bool                 operator >= (std::string const & value, option_info_ref const & rhs);
     277             : 
     278             : private:
     279             :     option_info::pointer_t      f_opt = option_info::pointer_t();
     280             : };
     281             : 
     282             : 
     283             : }   // namespace advgetopt
     284             : 
     285             : 
     286             : std::string operator + (char32_t value, std::string const & rhs);
     287             : std::string operator + (std::string const & lhs, char32_t value);
     288             : 
     289             : 
     290             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13