RESTinio
target_path_holder.hpp
Go to the documentation of this file.
1 /*
2  * RESTinio
3  */
4 
5 /**
6  * @file
7  * @brief Implementation of target_path_holder helper class.
8  *
9  * @since v.0.6.6
10  */
11 
12 #pragma once
13 
14 #include <restinio/utils/percent_encoding.hpp>
15 
16 #include <restinio/string_view.hpp>
17 
18 #include <memory>
19 
20 namespace restinio
21 {
22 
23 namespace router
24 {
25 
26 namespace impl
27 {
28 
29 //
30 // target_path_holder_t
31 //
32 /*!
33  * @brief Helper class for holding a unique instance of char array with
34  * target_path value.
35  *
36  * This class is a kind of std::unique_ptr<char[]> but it performs
37  * the normalization of target_path value in the constructor.
38  * All percent-encoded characters from unreserved set will be
39  * decoded into their normal representation. It means that
40  * target_path `/%7Etest` will be automatically transformed into
41  * `/~test`.
42  *
43  * @since v.0.6.2
44  */
46 {
47  public:
48  using data_t = std::unique_ptr<char[]>;
49 
50  //! Initializing constructor.
51  /*!
52  * Copies the value of @a original_path into a unique and
53  * dynamically allocated array of chars.
54  *
55  * Basic URI normalization procedure is automatically performed
56  * if necessary.
57  *
58  * @note
59  * Can throws if allocation of new data buffer fails or if
60  * @a original_path has an invalid format.
61  */
62  target_path_holder_t( string_view_t original_path )
65  {
66  m_data.reset( new char[ m_size ] );
67 
68  if( m_size != original_path.size() )
69  // Transformation is actually needed.
70  restinio::utils::uri_normalization::unreserved_chars::
71  normalize_to( original_path, m_data.get() );
72  else
73  // Just copy original value to the destination.
74  std::copy(
75  original_path.begin(), original_path.end(),
76  m_data.get() );
77  }
78 
79  //! Get access to the value of target_path.
80  /*!
81  * @attention
82  * This method should not be called after a call to giveout_data().
83  */
84  RESTINIO_NODISCARD
85  string_view_t
86  view() const noexcept
87  {
88  return { m_data.get(), m_size };
89  }
90 
91  //! Give out the value from holder.
92  /*!
93  * @attention
94  * The holder becomes empty after the return from that method and
95  * should not be used anymore.
96  */
98  data_t
99  giveout_data() noexcept
100  {
101  return std::move(m_data);
102  }
103 
104  private:
105  //! Actual data with target_path.
106  /*!
107  * @note
108  * It becomes empty after a call to giveout_data().
109  */
111  //! The length of target_path.
113 };
114 
115 } /* namespace impl */
116 
117 } /* namespace router */
118 
119 } /* namespace restinio */
target_path_holder_t(string_view_t original_path)
Initializing constructor.
std::size_t m_size
The length of target_path.
RESTINIO_NODISCARD data_t giveout_data() noexcept
Give out the value from holder.
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