RESTinio
Toggle main menu visibility
Loading...
Searching...
No Matches
restinio
value_or.hpp
Go to the documentation of this file.
1
/*
2
restinio
3
*/
4
5
/*!
6
Helper functions for parameter extraction with default values.
7
8
@since v.0.4.4
9
*/
10
11
#
pragma
once
12
13
#
include
<
type_traits
>
14
15
#
include
<
restinio
/
cast_to
.
hpp
>
16
#
include
<
restinio
/
uri_helpers
.
hpp
>
17
#
include
<
restinio
/
router
/
express
.
hpp
>
18
19
namespace
restinio
20
{
21
22
//! Get parameter value or a given default.
23
/*!
24
Get the value of a parameter specified by \a key if parameter exists.
25
If parameter exists in \a params it is obtained as \c string_view_t object and
26
casted to a necessary type and then returns.
27
If \a params has no such parameters then the \a default_value is returned.
28
29
@since v.0.4.4
30
*/
31
template
<
typename
Value_Type,
typename
Parameter_Container >
32
typename
std::enable_if<
33
std::is_same< Parameter_Container,
query_string_params_t
>::value ||
34
std::is_same< Parameter_Container,
router
::
route_params_t
>::value,
35
Value_Type >::
type
36
value_or
(
const
Parameter_Container & params, string_view_t key, Value_Type default_value )
37
{
38
const
auto
value = params.get_param( key );
39
if
( value )
40
{
41
return
cast_to< Value_Type >( *value );
42
}
43
44
return
default_value;
45
}
46
47
/*!
48
\brief Gets the value of a parameter specified by \a key wrapped in `std::optional<Value_Type>`
49
if parameter exists and empty `std::optional<Value_Type>`
50
if parameter with a given key value doesn't exist.
51
52
If parameter exists in \a params it is obtained as \c string_view_t object and
53
casted to a necessary type and then is wrapped in `std::optional<Value_Type>`.
54
If \a params has no such parameters then the empty `std::optional<Value_Type>`
55
is returned.
56
57
@since v.0.4.5.1
58
*/
59
template
<
typename
Value_Type,
typename
Parameter_Container >
60
typename
std::enable_if<
61
std::is_same< Parameter_Container,
query_string_params_t
>::value ||
62
std::is_same< Parameter_Container,
router
::
route_params_t
>::value,
63
std::optional< Value_Type > >::
type
64
opt_value
(
const
Parameter_Container & params, string_view_t key )
65
{
66
std::optional< Value_Type > result{};
67
68
const
auto
value = params.get_param( key );
69
if
( value )
70
{
71
result = cast_to< Value_Type >( *value );
72
}
73
74
return
result;
75
}
76
77
}
/* namespace restinio */
restinio::query_string_params_t::parameters_container_t
std::vector< std::pair< string_view_t, string_view_t > > parameters_container_t
Definition
uri_helpers.hpp:45
restinio::router::route_params_t::named_parameters_container_t
std::vector< std::pair< string_view_t, string_view_t > > named_parameters_container_t
Definition
express.hpp:59
restinio::router
Definition
boost_regex_engine.hpp:17
restinio
Definition
sendfile_operation_default.ipp:12
restinio::opt_value
std::enable_if< std::is_same< Parameter_Container, query_string_params_t >::value||std::is_same< Parameter_Container, router::route_params_t >::value, std::optional< Value_Type > >::type opt_value(const Parameter_Container ¶ms, string_view_t key)
Gets the value of a parameter specified by key wrapped in std::optional<Value_Type> if parameter exis...
Definition
value_or.hpp:64
restinio::value_or
std::enable_if< std::is_same< Parameter_Container, query_string_params_t >::value||std::is_same< Parameter_Container, router::route_params_t >::value, Value_Type >::type value_or(const Parameter_Container ¶ms, string_view_t key, Value_Type default_value)
Get parameter value or a given default.
Definition
value_or.hpp:36
Generated by
1.17.0