RESTinio
tagged_scalar.hpp
Go to the documentation of this file.
1 /*
2  * RESTinio
3  */
4 
5 /*!
6  * @file
7  * @brief Helper template for defining tagged scalar types.
8  *
9  * @since v.0.6.12
10  */
11 
12 #pragma once
13 
14 #include <restinio/compiler_features.hpp>
15 
16 #include <type_traits>
17 
18 namespace restinio
19 {
20 
21 namespace utils
22 {
23 
24 //
25 // tagged_scalar_t
26 //
27 /*!
28  * @brief Helper template for defining tagged scalar types.
29  *
30  * Usage example:
31  * @code
32  * struct max_parallel_connections_tag {};
33  * using max_parallel_connections_t = tagged_scalar_t<
34  * std::size_t, max_parallel_connections_tag >;
35  *
36  * struct max_active_accepts_tag {};
37  * using max_active_accepts_t = tagged_scalar_t<
38  * std::size_t, max_active_accepts_tag >;
39  *
40  * class limiter_t
41  * {
42  * public:
43  * limiter_t(
44  * max_parallel_connections_t parallel_connections,
45  * max_active_accepts_t active_accepts);
46  * ...
47  * };
48  * @endcode
49  *
50  * @since v.0.6.12
51  */
52 template< typename Scalar, typename Tag >
54 {
55  static_assert( std::is_scalar<Scalar>::value,
56  "Scalar is expected to be scalar type" );
57 
58  Scalar m_value;
59 
60 public:
61  constexpr explicit tagged_scalar_t( Scalar value ) noexcept
62  : m_value{ value }
63  {}
64 
65  [[nodiscard]]
66  constexpr Scalar
67  value() const noexcept { return m_value; }
68 };
69 
70 } /* namespace utils */
71 
72 } /* namespace restinio */
string_view_t from_string< string_view_t >(string_view_t s)
Get a value from string_view.
constexpr Scalar value() const noexcept
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 &params, 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
constexpr tagged_scalar_t(Scalar value) noexcept
Helper template for defining tagged scalar types.