LCOV - code coverage report
Current view: top level - advgetopt - validator_list.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 28 28 100.0 %
Date: 2022-07-15 17:40:56 Functions: 10 11 90.9 %
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             : 
      20             : /** \file
      21             :  * \brief Implementation of the list validator.
      22             :  *
      23             :  * The list validator allows the library to run multiple validators against
      24             :  * parameters to easily allow for multiple possible values.
      25             :  *
      26             :  * This is especially useful if a parameter supports a value such as an
      27             :  * integer and a few keywords (i.e. "off", "disabled", "maximum", etc.)
      28             :  */
      29             : 
      30             : // self
      31             : //
      32             : #include    "advgetopt/validator_list.h"
      33             : 
      34             : 
      35             : // cppthread
      36             : //
      37             : #include    <cppthread/log.h>
      38             : 
      39             : 
      40             : // last include
      41             : //
      42             : #include    <snapdev/poison.h>
      43             : 
      44             : 
      45             : 
      46             : 
      47             : namespace advgetopt
      48             : {
      49             : 
      50             : 
      51             : 
      52             : namespace
      53             : {
      54             : 
      55             : 
      56             : 
      57           2 : class validator_list_factory
      58             :     : public validator_factory
      59             : {
      60             : public:
      61           2 :     validator_list_factory()
      62           2 :     {
      63           2 :         validator::register_validator(*this);
      64           2 :     }
      65             : 
      66           4 :     virtual std::string get_name() const override
      67             :     {
      68           4 :         return std::string("list");
      69             :     }
      70             : 
      71           2 :     virtual std::shared_ptr<validator> create(string_list_t const & data) const override
      72             :     {
      73           2 :         return std::make_shared<validator_list>(data);
      74             :     }
      75             : };
      76             : 
      77           2 : validator_list_factory         g_validator_list_factory;
      78             : 
      79             : 
      80             : 
      81             : } // no name namespace
      82             : 
      83             : 
      84             : 
      85             : 
      86             : 
      87           2 : validator_list::validator_list(string_list_t const & param_list)
      88             : {
      89           2 :     if(!param_list.empty())
      90             :     {
      91           2 :         cppthread::log << cppthread::log_level_t::error
      92           1 :                        << "validator_list() does not support any parameter."
      93           2 :                        << cppthread::end;
      94           1 :         return;
      95             :     }
      96             : }
      97             : 
      98             : 
      99           2 : void validator_list::add_validator(validator::pointer_t v)
     100             : {
     101           2 :     if(v != nullptr)
     102             :     {
     103           2 :         f_validators.push_back(v);
     104             :     }
     105           2 : }
     106             : 
     107             : 
     108             : /** \brief Return the name of this validator.
     109             :  *
     110             :  * This function returns "list".
     111             :  *
     112             :  * \return "list".
     113             :  */
     114           1 : std::string validator_list::name() const
     115             : {
     116           1 :     return std::string("list");
     117             : }
     118             : 
     119             : 
     120             : /** \brief Check the value against all the validators for one valid one.
     121             :  *
     122             :  * This function goes through the validators registered with it and if
     123             :  * at least one of these validators return true, then the function
     124             :  * considered that input value as valid and it returns true.
     125             :  *
     126             :  * \param[in] value  The value to be validated.
     127             :  *
     128             :  * \return true on a match.
     129             :  */
     130         124 : bool validator_list::validate(std::string const & value) const
     131             : {
     132         266 :     for(auto const & v : f_validators)
     133             :     {
     134         245 :         if(v->validate(value))
     135             :         {
     136         103 :             return true;
     137             :         }
     138             :     }
     139             : 
     140          21 :     return false;
     141             : }
     142             : 
     143             : 
     144             : 
     145           6 : } // namespace advgetopt
     146             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13