aboutsummaryrefslogtreecommitdiff
path: root/include/openvic-dataloader/detail/CallbackOStream.hpp
diff options
context:
space:
mode:
author Spartan322 <Megacake1234@gmail.com>2023-11-28 11:09:26 +0100
committer Spartan322 <Megacake1234@gmail.com>2024-05-09 22:11:26 +0200
commit757114a3c5b748567b42f273c7b78ca039ae983c (patch)
treee07390b682052129c91f4b157068bcdd84ceecb4 /include/openvic-dataloader/detail/CallbackOStream.hpp
parent7211a228e68c8a6b1ad1c1c5ec68c8d720b6d2ba (diff)
Add `deps/dryad` -> https://github.com/Spartan322/dryadadd/dryad
Add `deps/fmt` -> https://github.com/fmtlib/fmt Add `deps/range-v3` -> https://github.com/ericniebler/range-v3 Improve parser error and warning support Update .clang-format Update `deps/SCsub`
Diffstat (limited to 'include/openvic-dataloader/detail/CallbackOStream.hpp')
-rw-r--r--include/openvic-dataloader/detail/CallbackOStream.hpp31
1 files changed, 19 insertions, 12 deletions
diff --git a/include/openvic-dataloader/detail/CallbackOStream.hpp b/include/openvic-dataloader/detail/CallbackOStream.hpp
index 641d53f..f7cfc4a 100644
--- a/include/openvic-dataloader/detail/CallbackOStream.hpp
+++ b/include/openvic-dataloader/detail/CallbackOStream.hpp
@@ -6,10 +6,10 @@
#include <type_traits>
namespace ovdl::detail {
- template<typename Callback, class CHAR_T, class traits = std::char_traits<CHAR_T>>
- class BasicCallbackStreamBuffer : public std::basic_streambuf<CHAR_T, traits> {
+ template<typename Callback, class CharT, class traits = std::char_traits<CharT>>
+ class BasicCallbackStreamBuffer : public std::basic_streambuf<CharT, traits> {
public:
- using base_type = std::basic_streambuf<CHAR_T, traits>;
+ using base_type = std::basic_streambuf<CharT, traits>;
using callback_type = Callback;
using char_type = typename base_type::char_type;
using int_type = typename base_type::int_type;
@@ -29,11 +29,12 @@ namespace ovdl::detail {
};
int_type overflow(int_type ch) override {
+ auto c = static_cast<char_type>(ch);
if constexpr (std::is_same_v<void, typename decltype(std::function { _callback })::result_type>) {
- _callback(&ch, 1, _user_data);
+ _callback(&c, 1, _user_data);
return 1;
} else {
- return _callback(&ch, 1, _user_data); // returns the number of characters successfully written.
+ return _callback(&c, 1, _user_data); // returns the number of characters successfully written.
}
}
@@ -64,22 +65,28 @@ namespace ovdl::detail {
CallbackWStreamBuffer(Callback cb, void* user_data = nullptr) : base_type(cb, user_data) {}
};
- template<typename Callback, class CHAR_T, class traits = std::char_traits<CHAR_T>>
- class BasicCallbackStream : public std::basic_ostream<CHAR_T, traits> {
+ template<class CharT, typename Callback, class traits = std::char_traits<CharT>>
+ class BasicCallbackStream : public std::basic_ostream<CharT, traits> {
public:
- using base_type = std::basic_ostream<CHAR_T, traits>;
+ using base_type = std::basic_ostream<CharT, traits>;
BasicCallbackStream(Callback cb, void* user_data = nullptr)
: m_sbuf(cb, user_data),
- std::basic_ios<CHAR_T, traits>(&m_sbuf),
- std::basic_ostream<CHAR_T, traits>(&m_sbuf) {
- std::basic_ios<CHAR_T, traits>::init(&m_sbuf);
+ std::basic_ios<CharT, traits>(&m_sbuf),
+ std::basic_ostream<CharT, traits>(&m_sbuf) {
+ std::basic_ios<CharT, traits>::init(&m_sbuf);
}
private:
- BasicCallbackStreamBuffer<Callback, CHAR_T, traits> m_sbuf;
+ BasicCallbackStreamBuffer<Callback, CharT, traits> m_sbuf;
};
+ template<typename CharT>
+ auto make_callback_stream(auto&& cb, void* user_data = nullptr) {
+ using Callback = std::decay_t<decltype(cb)>;
+ return BasicCallbackStream<CharT, Callback> { std::forward<Callback>(cb), user_data };
+ }
+
template<typename Callback>
class CallbackStream : public BasicCallbackStream<Callback, char> {
public: