advgetopt 2.0.49
Parse complex command line arguments and configuration files in C++.
Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Private Attributes | List of all members
advgetopt::validator_duration Class Reference

#include <validator_duration.h>

Inheritance diagram for advgetopt::validator_duration:
Inheritance graph
[legend]
Collaboration diagram for advgetopt::validator_duration:
Collaboration graph
[legend]

Public Types

typedef std::uint32_t flag_t
 
typedef std::shared_ptr< validatorpointer_t
 
typedef std::vector< pointer_tvector_t
 

Public Member Functions

 validator_duration (string_list_t const &data)
 Initialize the duration validator.
 
virtual std::string name () const override
 Return the name of this validator.
 
virtual bool validate (std::string const &value) const override
 Determine whether value is a valid duration.
 

Static Public Member Functions

static bool convert_string (std::string const &duration, flag_t flags, double &result)
 Convert a string to a double value representing a duration.
 
static pointer_t create (std::string const &name, string_list_t const &data)
 
static pointer_t create (std::string const &name_and_params)
 Set the validator for this option.
 
static void register_validator (validator_factory const &factory)
 

Static Public Attributes

static constexpr flag_t VALIDATOR_DURATION_DEFAULT_FLAGS = 0x00
 
static constexpr flag_t VALIDATOR_DURATION_LONG = 0x01
 

Private Attributes

flag_t f_flags = VALIDATOR_DURATION_DEFAULT_FLAGS
 

Detailed Description

Definition at line 48 of file validator_duration.h.

Member Typedef Documentation

◆ flag_t

Definition at line 52 of file validator_duration.h.

◆ pointer_t

Definition at line 64 of file validator.h.

◆ vector_t

Definition at line 65 of file validator.h.

Constructor & Destructor Documentation

◆ validator_duration()

advgetopt::validator_duration::validator_duration ( string_list_t const flag_list)

The constructor accepts a string defining the acceptable durations.

The string uses the following format:

flags: 'small'
| 'large'
constexpr flag_t option_flags_merge()
Definition flags.h:87

'small' stands for small values (down to 1 second).

'large' stands for large values (so the 'm' suffix represents month, not minutes).

The 'small' and 'large' flags are exclusive, the last one will be effective.

Parameters
[in]flag_listThe flags used to define the usage of the 'm' suffix.

Definition at line 112 of file validator_duration.cpp.

References f_flags, advgetopt::option_flags_merge(), and VALIDATOR_DURATION_LONG.

Member Function Documentation

◆ convert_string()

bool advgetopt::validator_duration::convert_string ( std::string const value,
flag_t  flags,
double result 
)
static

This function is used to convert a string to a double representing a duration. The duration can be specified with one of the following suffixes:

  • "s" or " second" or " seconds" – the double is returned as is
  • "m" or " minute" or " minutes" – the double is multiplied by 60
  • "h" or " hour" or " hours" – the double is multiplied by 3600
  • "d" or " day" or " days" – the double is multiplied by 86400
  • "w" or " week" or " weeks" – the double is multiplied by 604800
  • "m" or " month" or " months" – the double is multiplied by 2592000
  • "y" or " year" or " years" – the double is multiplied by 31536000

The "m" suffix is interpreted as "minute" by default. The flags passed to the contructor can change that interpretation into "month" instead.

One month uses 30 days.

One year uses 365 days.

Note that the input can be a double. So you can define a duration of "1.3 seconds" or "2.25 days".

Todo:
The last multiplication doesn't verify that no overflow or underflow happens.
Warning
There is no range checks in this function since it does not have access to the ranges (i.e. it is static).
Parameters
[in]valueThe value to be converted to a duration.
[in]flagsThe flags to determine how to interpret the suffix.
[out]resultThe resulting duration in seconds.
Returns
true if the conversion succeeded.

Definition at line 204 of file validator_duration.cpp.

References advgetopt::validator_double::convert_string(), advgetopt::option_flags_merge(), and VALIDATOR_DURATION_LONG.

Referenced by validate().

◆ create() [1/2]

validator::pointer_t advgetopt::validator::create ( std::string const name,
string_list_t const data 
)
staticinherited

◆ create() [2/2]

validator::pointer_t advgetopt::validator::create ( std::string const name_and_params)
staticinherited

This function parses the specified name and optional parameters and create a corresponding validator for this option.

The name_and_params string can be defined as:

virtual std::string name() const override
Return the name of this validator.

The list of parameters is optional. There may be an empty, just one, or any number of parameters. How the parameters are parsed is left to the validator to decide.

If the input string is empty, the current validator, if one is installed, gets removed.

Parameters
[in]name_and_paramsThe validator name and parameters.

Definition at line 626 of file validator.cpp.

References advgetopt::validator::create(), and advgetopt::option_flags_merge().

◆ name()

std::string advgetopt::validator_duration::name ( ) const
overridevirtual

This function returns "duration".

Returns
"duration".

Implements advgetopt::validator.

Definition at line 142 of file validator_duration.cpp.

◆ register_validator()

void advgetopt::validator::register_validator ( validator_factory const factory)
staticinherited

Definition at line 571 of file validator.cpp.

References advgetopt::option_flags_merge().

◆ validate()

bool advgetopt::validator_duration::validate ( std::string const value) const
overridevirtual

This function verifies that the specified value is a valid duration.

It makes sures that the value is a valid decimal number which optionally starts with a sign ([-+]?) and is optionally followed by a known measurement suffix.

Parameters
[in]valueThe value to validate.
Returns
true if the value validates.

Implements advgetopt::validator.

Definition at line 160 of file validator_duration.cpp.

References convert_string(), f_flags, and advgetopt::option_flags_merge().

Member Data Documentation

◆ f_flags

flag_t advgetopt::validator_duration::f_flags = VALIDATOR_DURATION_DEFAULT_FLAGS
private

Definition at line 69 of file validator_duration.h.

Referenced by validator_duration(), and validate().

◆ VALIDATOR_DURATION_DEFAULT_FLAGS

constexpr flag_t advgetopt::validator_duration::VALIDATOR_DURATION_DEFAULT_FLAGS = 0x00
staticconstexpr

Definition at line 54 of file validator_duration.h.

◆ VALIDATOR_DURATION_LONG

constexpr flag_t advgetopt::validator_duration::VALIDATOR_DURATION_LONG = 0x01
staticconstexpr

Definition at line 55 of file validator_duration.h.

Referenced by validator_duration(), and convert_string().


The documentation for this class was generated from the following files:

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.