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-07-15 03:11:49 Functions: 4 6 66.7 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.12