aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
author Spartan322 <Megacake1234@gmail.com>2023-08-18 22:37:30 +0200
committer Spartan322 <Megacake1234@gmail.com>2023-08-19 00:02:49 +0200
commit8ebcba11d103ebe8ab259b311e874e1bac05dfd3 (patch)
treeb5250c8712d57ec223333f02a1b31c69a0dd12d4 /include
parent9808d275dc7a98fb26c4d67b610a5f1030891b07 (diff)
Add BasicCallbackOStreamBuffer for callback streamingfunctioning-unlike-france
Add CallbackStreamBuffer for char Add CallbackWStreamBuffer for wchar_t Add BasicCallbackStream Add CallbackStream for char Add CallbackWStream for wchar_t
Diffstat (limited to 'include')
-rw-r--r--include/openvic-dataloader/detail/CallbackOStream.hpp100
1 files changed, 100 insertions, 0 deletions
diff --git a/include/openvic-dataloader/detail/CallbackOStream.hpp b/include/openvic-dataloader/detail/CallbackOStream.hpp
new file mode 100644
index 0000000..641d53f
--- /dev/null
+++ b/include/openvic-dataloader/detail/CallbackOStream.hpp
@@ -0,0 +1,100 @@
+#pragma once
+
+#include <cstring>
+#include <functional>
+#include <ostream>
+#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> {
+ public:
+ using base_type = std::basic_streambuf<CHAR_T, traits>;
+ using callback_type = Callback;
+ using char_type = typename base_type::char_type;
+ using int_type = typename base_type::int_type;
+
+ BasicCallbackStreamBuffer(Callback cb, void* user_data = nullptr)
+ : _callback(cb),
+ _user_data(user_data) {}
+
+ protected:
+ std::streamsize xsputn(const char_type* s, std::streamsize n) override {
+ if constexpr (std::is_same_v<void, typename decltype(std::function { _callback })::result_type>) {
+ _callback(s, n, _user_data);
+ return n;
+ } else {
+ return _callback(s, n, _user_data); // returns the number of characters successfully written.
+ }
+ };
+
+ int_type overflow(int_type ch) override {
+ if constexpr (std::is_same_v<void, typename decltype(std::function { _callback })::result_type>) {
+ _callback(&ch, 1, _user_data);
+ return 1;
+ } else {
+ return _callback(&ch, 1, _user_data); // returns the number of characters successfully written.
+ }
+ }
+
+ private:
+ Callback _callback;
+ void* _user_data;
+ };
+
+ template<typename Callback>
+ class CallbackStreamBuffer : public BasicCallbackStreamBuffer<Callback, char> {
+ public:
+ using base_type = BasicCallbackStreamBuffer<Callback, char>;
+ using callback_type = Callback;
+ using char_type = typename base_type::char_type;
+ using int_type = typename base_type::int_type;
+
+ CallbackStreamBuffer(Callback cb, void* user_data = nullptr) : base_type(cb, user_data) {}
+ };
+
+ template<typename Callback>
+ class CallbackWStreamBuffer : public BasicCallbackStreamBuffer<Callback, wchar_t> {
+ public:
+ using base_type = BasicCallbackStreamBuffer<Callback, wchar_t>;
+ using callback_type = Callback;
+ using char_type = typename base_type::char_type;
+ using int_type = typename base_type::int_type;
+
+ 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> {
+ public:
+ using base_type = std::basic_ostream<CHAR_T, 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);
+ }
+
+ private:
+ BasicCallbackStreamBuffer<Callback, CHAR_T, traits> m_sbuf;
+ };
+
+ template<typename Callback>
+ class CallbackStream : public BasicCallbackStream<Callback, char> {
+ public:
+ using base_type = BasicCallbackStream<Callback, char>;
+
+ CallbackStream(Callback cb, void* user_data = nullptr) : base_type(cb, user_data) {
+ }
+ };
+
+ template<typename Callback>
+ class CallbackWStream : public BasicCallbackStream<Callback, wchar_t> {
+ public:
+ using base_type = BasicCallbackStream<Callback, wchar_t>;
+
+ CallbackWStream(Callback cb, void* user_data = nullptr) : base_type(cb, user_data) {
+ }
+ };
+} \ No newline at end of file