LCOV - code coverage report
Current view: top level - advgetopt - validator.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 4 4 100.0 %
Date: 2019-08-10 16:09:07 Functions: 4 6 66.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * License:
       3             :  *    Copyright (c) 2006-2019  Made to Order Software Corp.  All Rights Reserved
       4             :  *
       5             :  *    https://snapwebsites.org/
       6             :  *    contact@m2osw.com
       7             :  *
       8             :  *    This program is free software; you can redistribute it and/or modify
       9             :  *    it under the terms of the GNU General Public License as published by
      10             :  *    the Free Software Foundation; either version 2 of the License, or
      11             :  *    (at your option) any later version.
      12             :  *
      13             :  *    This program is distributed in the hope that it will be useful,
      14             :  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16             :  *    GNU General Public License for more details.
      17             :  *
      18             :  *    You should have received a copy of the GNU General Public License along
      19             :  *    with this program; if not, write to the Free Software Foundation, Inc.,
      20             :  *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      21             :  *
      22             :  * Authors:
      23             :  *    Alexis Wilke   alexis@m2osw.com
      24             :  *    Doug Barbieri  doug@m2osw.com
      25             :  */
      26             : #pragma once
      27             : 
      28             : /** \file
      29             :  * \brief Declaration of validators which can be used to verify the parameters.
      30             :  *
      31             :  * The library offers parameter validations using validator objects. You
      32             :  * can even make your own validator objects available before parsing your
      33             :  * data so that way it can be verified as expected.
      34             :  *
      35             :  * Validators are recognized by name. A value can be assigned a validator
      36             :  * by specify the \em type of data it supports.
      37             :  */
      38             : 
      39             : // advgetopt lib
      40             : //
      41             : #include    "advgetopt/utils.h"
      42             : 
      43             : // C++ lib
      44             : //
      45             : #include    <memory>
      46             : #include    <regex>
      47             : 
      48             : 
      49             : namespace advgetopt
      50             : {
      51             : 
      52             : 
      53             : 
      54             : class validator;
      55             : 
      56           5 : class validator_factory
      57             : {
      58             : public:
      59             :     virtual                     ~validator_factory();
      60             : 
      61             :     virtual std::string         get_name() const = 0;
      62             :     virtual std::shared_ptr<validator>
      63             :                                 create(string_list_t const & data) const = 0;
      64             : };
      65             : 
      66             : 
      67          66 : class validator
      68             : {
      69             : public:
      70             :     typedef std::shared_ptr<validator>      pointer_t;
      71             : 
      72             :     virtual                     ~validator();
      73             : 
      74             :     // virtuals
      75             :     //
      76             :     virtual std::string const   name() const = 0;
      77             :     virtual bool                validate(std::string const & value) const = 0;
      78             : 
      79             :     static void                 register_validator(validator_factory const & factory);
      80             :     static pointer_t            create(std::string const & name, string_list_t const & data);
      81             :     static pointer_t            create(std::string const & name_and_params);
      82             : };
      83             : 
      84             : 
      85          48 : class validator_integer
      86             :     : public validator
      87             : {
      88             : public:
      89             :     typedef bool (*to_integer_t)(std::string const & number
      90             :                                , std::int64_t & result);
      91             : 
      92             :                                 validator_integer(string_list_t const & data);
      93             : 
      94             :     // validator implementation
      95             :     //
      96             :     virtual std::string const   name() const;
      97             :     virtual bool                validate(std::string const & value) const;
      98             : 
      99             :     static bool                 convert_string(std::string const & number
     100             :                                              , std::int64_t & result);
     101             : 
     102             : private:
     103             :     struct range_t
     104             :     {
     105             :         typedef std::vector<range_t>    vector_t;
     106             : 
     107             :         std::int64_t            f_minimum = std::numeric_limits<std::int64_t>::min();
     108             :         std::int64_t            f_maximum = std::numeric_limits<std::int64_t>::max();
     109             :     };
     110             : 
     111             :     range_t::vector_t           f_allowed_values = range_t::vector_t();
     112             : };
     113             : 
     114             : 
     115          18 : class validator_regex
     116             :     : public validator
     117             : {
     118             : public:
     119             :                                 validator_regex(string_list_t const & data);
     120             : 
     121             :     // validator implementation
     122             :     //
     123             :     virtual std::string const   name() const;
     124             :     virtual bool                validate(std::string const & value) const;
     125             : 
     126             : private:
     127             :     std::regex                  f_regex = std::regex();
     128             : };
     129             : 
     130             : 
     131             : 
     132             : 
     133             : 
     134             : }   // namespace advgetopt
     135             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.12