Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-09 08:28:01

0001 //
0002 // Copyright (c) 2019-2024 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
0003 //
0004 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0005 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 
0008 #ifndef BOOST_MYSQL_DETAIL_OUTPUT_STRING_HPP
0009 #define BOOST_MYSQL_DETAIL_OUTPUT_STRING_HPP
0010 
0011 #include <boost/mysql/string_view.hpp>
0012 
0013 #include <boost/mysql/detail/config.hpp>
0014 
0015 #include <cstddef>
0016 
0017 #ifdef BOOST_MYSQL_HAS_CONCEPTS
0018 #include <concepts>
0019 #endif
0020 
0021 namespace boost {
0022 namespace mysql {
0023 namespace detail {
0024 
0025 #ifdef BOOST_MYSQL_HAS_CONCEPTS
0026 
0027 template <class T>
0028 concept output_string = std::movable<T> && requires(T& t, const char* data, std::size_t sz) {
0029     t.append(data, sz);
0030     t.clear();
0031 };
0032 
0033 #define BOOST_MYSQL_OUTPUT_STRING ::boost::mysql::detail::output_string
0034 
0035 #else
0036 
0037 #define BOOST_MYSQL_OUTPUT_STRING class
0038 
0039 #endif
0040 
0041 class output_string_ref
0042 {
0043     using append_fn_t = void (*)(void*, const char*, std::size_t);
0044 
0045     append_fn_t append_fn_;
0046     void* container_;
0047 
0048     template <class T>
0049     static void do_append(void* container, const char* data, std::size_t size)
0050     {
0051         static_cast<T*>(container)->append(data, size);
0052     }
0053 
0054 public:
0055     output_string_ref(append_fn_t append_fn, void* container) noexcept
0056         : append_fn_(append_fn), container_(container)
0057     {
0058     }
0059 
0060     template <class T>
0061     static output_string_ref create(T& obj) noexcept
0062     {
0063         return output_string_ref(&do_append<T>, &obj);
0064     }
0065 
0066     void append(string_view data)
0067     {
0068         if (data.size() > 0u)
0069             append_fn_(container_, data.data(), data.size());
0070     }
0071 };
0072 
0073 }  // namespace detail
0074 }  // namespace mysql
0075 }  // namespace boost
0076 
0077 #endif