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: 2021-09-08 17:05:25 Functions: 7 8 87.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2006-2021  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             : 
      36             : 
      37             : // C++ lib
      38             : //
      39             : #include    <map>
      40             : #include    <memory>
      41             : 
      42             : 
      43             : 
      44             : namespace advgetopt
      45             : {
      46             : 
      47             : 
      48             : typedef char32_t            short_name_t;
      49             : 
      50             : constexpr short_name_t      NO_SHORT_NAME = U'\0';
      51             : 
      52             : 
      53             : short_name_t                string_to_short_name(std::string const & name);
      54             : std::string                 short_name_to_string(short_name_t short_name);
      55             : 
      56             : 
      57             : 
      58             : 
      59             : enum class option_source_t
      60             : {
      61             :     SOURCE_COMMAND_LINE,            // set on command line
      62             :     SOURCE_CONFIGURATION,           // read from config file
      63             :     SOURCE_DIRECT,                  // set by programmer directly
      64             :     SOURCE_DYNAMIC,                 // set dynamically
      65             :     SOURCE_ENVIRONMENT_VARIABLE,    // found in environment variable
      66             : 
      67             :     SOURCE_UNDEFINED,               // the option object exists, but the value is still undefined
      68             : };
      69             : 
      70             : 
      71             : // the `option_info` can be used instead or on top of the `struct option`
      72             : // it is especially used to read an external getopt declaration file
      73             : //
      74        1509 : class option_info
      75             : {
      76             : public:
      77             :     typedef std::shared_ptr<option_info>                    pointer_t;
      78             :     typedef std::vector<pointer_t>                          vector_t;
      79             :     typedef std::map<std::string, pointer_t>                map_by_name_t;
      80             :     typedef std::map<short_name_t, pointer_t>               map_by_short_name_t;
      81             :     typedef std::function<void (option_info const & opt)>   callback_t;
      82             :     typedef int                                             callback_id_t;
      83             : 
      84             :                                 option_info(std::string const & name, short_name_t short_name = NO_SHORT_NAME);
      85             : 
      86             :     std::string const &         get_name() const;
      87             :     void                        set_short_name(short_name_t short_name);
      88             :     short_name_t                get_short_name() const;
      89             :     std::string                 get_basename() const;
      90             :     std::string                 get_section_name() const;
      91             :     string_list_t               get_section_name_list() const;
      92             :     bool                        is_default_option() const;
      93             : 
      94             :     void                        set_flags(flag_t flags);
      95             :     void                        add_flag(flag_t flag);
      96             :     void                        remove_flag(flag_t flag);
      97             :     flag_t                      get_flags() const;
      98             :     bool                        has_flag(flag_t flag) const;
      99             : 
     100             :     bool                        has_default() const;
     101             :     void                        set_default(std::string const & default_value);
     102             :     void                        set_default(char const * default_value);
     103             :     void                        remove_default();
     104             :     std::string const &         get_default() const;
     105             : 
     106             :     void                        set_help(std::string const & help);
     107             :     void                        set_help(char const * help);
     108             :     std::string const &         get_help() const;
     109             : 
     110             :     bool                        set_validator(std::string const & name_and_params);
     111             :     bool                        set_validator(validator::pointer_t validator);
     112             :     bool                        set_validator(std::nullptr_t);
     113             :     validator::pointer_t        get_validator() const;
     114             : 
     115             :     void                        set_alias_destination(option_info::pointer_t destination);
     116             :     option_info::pointer_t      get_alias_destination() const;
     117             : 
     118             :     void                        set_multiple_separators(string_list_t const & separators);
     119             :     void                        set_multiple_separators(char const * const * separators);
     120             :     string_list_t const &       get_multiple_separators() const;
     121             : 
     122             :     bool                        has_value(std::string const & value) const;
     123             :     bool                        add_value(std::string const & value, option_source_t source = option_source_t::SOURCE_DIRECT);
     124             :     bool                        set_value(int idx, std::string const & value, option_source_t source = option_source_t::SOURCE_DIRECT);
     125             :     bool                        set_multiple_values(std::string const & value, option_source_t source = option_source_t::SOURCE_DIRECT);
     126             :     bool                        is_defined() const;
     127             :     option_source_t             source() const;
     128             :     static void                 set_trace_sources(bool trace);
     129             :     string_list_t const &       trace_sources() const;
     130             :     static void                 set_configuration_filename(std::string const & filename);
     131             :     size_t                      size() const;
     132             :     std::string const &         get_value(int idx = 0) const;
     133             :     long                        get_long(int idx = 0) const;
     134             :     void                        lock(bool always = true);
     135             :     void                        unlock();
     136             :     void                        reset();
     137             : 
     138             :     callback_id_t               add_callback(callback_t const & c);
     139             :     void                        remove_callback(callback_id_t id);
     140             : 
     141             : private:
     142          37 :     struct callback_entry_t
     143             :     {
     144           2 :         callback_entry_t(callback_id_t id, callback_t const & c)
     145           2 :             : f_id(id)
     146           2 :             , f_callback(c)
     147             :         {
     148           2 :         }
     149             : 
     150             :         callback_id_t           f_id = 0;
     151             :         callback_t              f_callback = callback_t();
     152             :     };
     153             :     typedef std::vector<callback_entry_t>
     154             :                                 callback_vector_t;
     155             : 
     156             :     bool                        validate_all_values();
     157             :     bool                        validates(int idx = 0);
     158             :     void                        value_changed(int idx);
     159             :     void                        trace_source(int idx);
     160             : 
     161             :     // definitions
     162             :     //
     163             :     std::string                 f_name = std::string();
     164             :     short_name_t                f_short_name = NO_SHORT_NAME;
     165             :     flag_t                      f_flags = GETOPT_FLAG_NONE;
     166             :     std::string                 f_default_value = std::string();
     167             :     std::string                 f_help = std::string();
     168             :     validator::pointer_t        f_validator = validator::pointer_t();
     169             :     pointer_t                   f_alias_destination = pointer_t();
     170             :     string_list_t               f_multiple_separators = string_list_t();
     171             :     callback_vector_t           f_callbacks = callback_vector_t();
     172             :     id_t                        f_next_callback_id = 0;
     173             :     string_list_t               f_trace_sources = string_list_t();
     174             : 
     175             :     // value read from command line, environment, .conf file
     176             :     //
     177             :     option_source_t             f_source = option_source_t::SOURCE_UNDEFINED;
     178             :     string_list_t               f_value = string_list_t();
     179             :     mutable std::vector<long>   f_integer = std::vector<long>();
     180             : };
     181             : 
     182             : 
     183         165 : class option_info_ref
     184             : {
     185             : public:
     186             :                                 option_info_ref(option_info::pointer_t opt);
     187             : 
     188             :     bool                        empty() const;
     189             :     size_t                      length() const;
     190             :     size_t                      size() const;
     191             :     long                        get_long() const;
     192             : 
     193             :                                 operator std::string () const;
     194             : 
     195             :     option_info_ref &           operator = (char value);
     196             :     option_info_ref &           operator = (char32_t value);
     197             :     option_info_ref &           operator = (char const * value);
     198             :     option_info_ref &           operator = (std::string const & value);
     199             :     option_info_ref &           operator = (option_info_ref const & value);
     200             : 
     201             :     option_info_ref &           operator += (char value);
     202             :     option_info_ref &           operator += (char32_t value);
     203             :     option_info_ref &           operator += (char const * value);
     204             :     option_info_ref &           operator += (std::string const & value);
     205             :     option_info_ref &           operator += (option_info_ref const & value);
     206             : 
     207             :     std::string                 operator + (char value) const;
     208             :     std::string                 operator + (char32_t value) const;
     209             :     std::string                 operator + (char const * value) const;
     210             :     std::string                 operator + (std::string const & value) const;
     211             :     std::string                 operator + (option_info_ref const & value) const;
     212             : 
     213             :     friend std::string          operator + (char value, option_info_ref const & rhs);
     214             :     friend std::string          operator + (char32_t value, option_info_ref const & rhs);
     215             :     friend std::string          operator + (char const * value, option_info_ref const & rhs);
     216             :     friend std::string          operator + (std::string const & value, option_info_ref const & rhs);
     217             : 
     218             :                                 operator bool () const;
     219             :     bool                        operator ! () const;
     220             : 
     221             :     bool                        operator == (char const * value) const;
     222             :     bool                        operator == (std::string const & value) const;
     223             :     bool                        operator == (option_info_ref const & value) const;
     224             : 
     225             :     bool                        operator != (char const * value) const;
     226             :     bool                        operator != (std::string const & value) const;
     227             :     bool                        operator != (option_info_ref const & value) const;
     228             : 
     229             :     bool                        operator < (char const * value) const;
     230             :     bool                        operator < (std::string const & value) const;
     231             :     bool                        operator < (option_info_ref const & value) const;
     232             : 
     233             :     bool                        operator <= (char const * value) const;
     234             :     bool                        operator <= (std::string const & value) const;
     235             :     bool                        operator <= (option_info_ref const & value) const;
     236             : 
     237             :     bool                        operator > (char const * value) const;
     238             :     bool                        operator > (std::string const & value) const;
     239             :     bool                        operator > (option_info_ref const & value) const;
     240             : 
     241             :     bool                        operator >= (char const * value) const;
     242             :     bool                        operator >= (std::string const & value) const;
     243             :     bool                        operator >= (option_info_ref const & value) const;
     244             : 
     245             :     friend bool                 operator == (char const * value, option_info_ref const & rhs);
     246             :     friend bool                 operator == (std::string const & value, option_info_ref const & rhs);
     247             : 
     248             :     friend bool                 operator != (char const * value, option_info_ref const & rhs);
     249             :     friend bool                 operator != (std::string const & value, option_info_ref const & rhs);
     250             : 
     251             :     friend bool                 operator < (char const * value, option_info_ref const & rhs);
     252             :     friend bool                 operator < (std::string const & value, option_info_ref const & rhs);
     253             : 
     254             :     friend bool                 operator <= (char const * value, option_info_ref const & rhs);
     255             :     friend bool                 operator <= (std::string const & value, option_info_ref const & rhs);
     256             : 
     257             :     friend bool                 operator > (char const * value, option_info_ref const & rhs);
     258             :     friend bool                 operator > (std::string const & value, option_info_ref const & rhs);
     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             : private:
     264             :     option_info::pointer_t      f_opt = option_info::pointer_t();
     265             : };
     266             : 
     267             : 
     268             : }   // namespace advgetopt
     269             : 
     270             : 
     271             : std::string operator + (char32_t value, std::string const & rhs);
     272             : std::string operator + (std::string const & lhs, char32_t value);
     273             : 
     274             : 
     275             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13