RESTinio

value_or.hpp
Go to the documentation of this file.
1/*
2 restinio
3*/
4
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
23
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
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 optional_t< Value_Type > >::type
64 opt_value( const Parameter_Container & params, string_view_t key )
65{
66 optional_t< 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 */
cast_to.hpp
nonstd::optional_lite::optional
class optional
Definition: optional.hpp:919
express.hpp
restinio
Definition: asio_include.hpp:21
restinio::string_view_t
nonstd::string_view string_view_t
Definition: string_view.hpp:19
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 &params, string_view_t key, Value_Type default_value)
Get parameter value or a given default.
Definition: value_or.hpp:36
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, optional_t< Value_Type > >::type opt_value(const Parameter_Container &params, string_view_t key)
Gets the value of a parameter specified by key wrapped in optional_t<Value_Type> if parameter exists ...
Definition: value_or.hpp:64
uri_helpers.hpp