3737namespace boost ::redis::adapter::detail
3838{
3939
40- // Serialization.
40+ template < class > struct is_integral : std::false_type {};
4141
42- template <class T >
43- auto boost_redis_from_bulk (T& i, std::string_view sv, system::error_code& ec) -> typename std::enable_if<std::is_integral<T>::value, void>::type
44- {
45- auto const res = std::from_chars (sv.data (), sv.data () + std::size (sv), i);
46- if (res.ec != std::errc ())
47- ec = redis::error::not_a_number;
48- }
42+ template <> struct is_integral <long long int > : std::true_type {};
43+ template <> struct is_integral <unsigned long long int > : std::true_type {};
44+ template <> struct is_integral <int > : std::true_type {};
4945
50- inline
51- void boost_redis_from_bulk (bool & t, std::string_view sv, system::error_code&)
52- {
53- t = *sv.data () == ' t' ;
54- }
46+ template <class T , bool = is_integral<T>::value>
47+ struct converter_impl ;
5548
56- inline
57- void boost_redis_from_bulk (double & d, std::string_view sv, system::error_code& ec)
58- {
49+ template <class T >
50+ struct converter_impl <T, true > {
51+ template <class String >
52+ static void
53+ apply (
54+ T& i,
55+ resp3::basic_node<String> const & node,
56+ system::error_code& ec)
57+ {
58+ auto const res =
59+ std::from_chars (node.value .data (), node.value .data () + node.value .size (), i);
60+ if (res.ec != std::errc ())
61+ ec = redis::error::not_a_number;
62+ }
63+ };
64+
65+ template <>
66+ struct converter_impl <bool , false > {
67+ template <class String >
68+ static void
69+ apply (
70+ bool & t,
71+ resp3::basic_node<String> const & node,
72+ system::error_code& ec)
73+ {
74+ t = *node.value .data () == ' t' ;
75+ }
76+ };
77+
78+ template <>
79+ struct converter_impl <double , false > {
80+ template <class String >
81+ static void
82+ apply (
83+ double & d,
84+ resp3::basic_node<String> const & node,
85+ system::error_code& ec)
86+ {
5987#ifdef _LIBCPP_VERSION
60- // The string in sv is not null terminated and we also don't know
61- // if there is enough space at the end for a null char. The easiest
62- // thing to do is to create a temporary.
63- std::string const tmp{sv. data (), sv. data () + std:: size (sv )};
64- char * end{};
65- d = std::strtod (tmp.data (), &end);
66- if (d == HUGE_VAL || d == 0 )
67- ec = redis::error::not_a_double;
88+ // The string in node.value is not null terminated and we also
89+ // don't know if there is enough space at the end for a null
90+ // char. The easiest thing to do is to create a temporary.
91+ std::string const tmp{node. value . data (), node. value . data () + node. value . size ()};
92+ char * end{};
93+ d = std::strtod (tmp.data (), &end);
94+ if (d == HUGE_VAL || d == 0 )
95+ ec = redis::error::not_a_double;
6896#else
69- auto const res = std::from_chars (sv. data (), sv. data () + std:: size (sv ), d);
70- if (res.ec != std::errc ())
71- ec = redis::error::not_a_double;
97+ auto const res = std::from_chars (node. value . data (), node. value . data () + node. value . size (), d);
98+ if (res.ec != std::errc ())
99+ ec = redis::error::not_a_double;
72100#endif // _LIBCPP_VERSION
73- }
101+ }
102+ };
74103
75104template <class CharT , class Traits , class Allocator >
105+ struct converter_impl <std::basic_string<CharT, Traits, Allocator>, false > {
106+ template <class String >
107+ static void
108+ apply (
109+ std::basic_string<CharT, Traits, Allocator>& s,
110+ resp3::basic_node<String> const & node,
111+ system::error_code&)
112+ {
113+ s.append (node.value .data (), node.value .size ());
114+ }
115+ };
116+
117+ template <class T , class String >
76118void
77119boost_redis_from_bulk (
78- std::basic_string<CharT, Traits, Allocator>& s ,
79- std::string_view sv ,
80- system::error_code&)
120+ T& t ,
121+ resp3::basic_node<String> const & node ,
122+ system::error_code& ec )
81123{
82- s. append (sv. data (), sv. size () );
124+ converter_impl<T>:: apply (t, node, ec );
83125}
84126
85127// ================================================
@@ -138,14 +180,14 @@ class simple_impl {
138180 void on_value_available (Result&) {}
139181
140182 template <class String >
141- void operator ()(Result& result, resp3::basic_node<String> const & n , system::error_code& ec)
183+ void operator ()(Result& result, resp3::basic_node<String> const & node , system::error_code& ec)
142184 {
143- if (is_aggregate (n .data_type )) {
185+ if (is_aggregate (node .data_type )) {
144186 ec = redis::error::expects_resp3_simple_type;
145187 return ;
146188 }
147189
148- boost_redis_from_bulk (result, n. value , ec);
190+ boost_redis_from_bulk (result, node , ec);
149191 }
150192};
151193
@@ -175,7 +217,7 @@ class set_impl {
175217 }
176218
177219 typename Result::key_type obj;
178- boost_redis_from_bulk (obj, nd. value , ec);
220+ boost_redis_from_bulk (obj, nd, ec);
179221 hint_ = result.insert (hint_, std::move (obj));
180222 }
181223};
@@ -208,11 +250,11 @@ class map_impl {
208250
209251 if (on_key_) {
210252 typename Result::key_type obj;
211- boost_redis_from_bulk (obj, nd. value , ec);
253+ boost_redis_from_bulk (obj, nd, ec);
212254 current_ = result.insert (current_, {std::move (obj), {}});
213255 } else {
214256 typename Result::mapped_type obj;
215- boost_redis_from_bulk (obj, nd. value , ec);
257+ boost_redis_from_bulk (obj, nd, ec);
216258 current_->second = std::move (obj);
217259 }
218260
@@ -233,7 +275,7 @@ class vector_impl {
233275 result.reserve (result.size () + m * nd.aggregate_size );
234276 } else {
235277 result.push_back ({});
236- boost_redis_from_bulk (result.back (), nd. value , ec);
278+ boost_redis_from_bulk (result.back (), nd, ec);
237279 }
238280 }
239281};
@@ -266,7 +308,7 @@ class array_impl {
266308 }
267309
268310 BOOST_ASSERT (nd.aggregate_size == 1 );
269- boost_redis_from_bulk (result.at (i_), nd. value , ec);
311+ boost_redis_from_bulk (result.at (i_), nd, ec);
270312 }
271313
272314 ++i_;
@@ -289,7 +331,7 @@ struct list_impl {
289331 }
290332
291333 result.push_back ({});
292- boost_redis_from_bulk (result.back (), nd. value , ec);
334+ boost_redis_from_bulk (result.back (), nd, ec);
293335 }
294336 }
295337};
0 commit comments