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<
35  Value_Type >::type
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 `optional_t<Value_Type>`
49  if parameter exists and empty `optional_t<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 `optional_t<Value_Type>`.
54  If \a params has no such parameters then the empty `optional_t<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 ||
65 {
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 */
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