Skip to content

Commit

Permalink
Add context_printable concept
Browse files Browse the repository at this point in the history
  • Loading branch information
trcrsired committed Jan 20, 2024
1 parent 98dd5c3 commit deaeafb
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 7 deletions.
14 changes: 14 additions & 0 deletions include/fast_io_core_impl/concepts/decorator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

namespace fast_io
{

template<typename T>
concept decorator = requires(T t,::std::byte const* fromfirst, ::std::byte const* fromlast,
::std::byte const* tofirst, ::std::byte const* tolast)
{
{t.deco_define(fromfirst,fromlast,tofirst,tolast)}->::std::same_as<context_print_result<::std::byte*>>;
{t.deco_unshift_define(tofirst,tolast)}->::std::same_as<context_print_result<::std::byte*>>;
};

}
1 change: 1 addition & 0 deletions include/fast_io_core_impl/concepts/impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
#include"operation_details.h"
#include"operation.h"
#include"strlike.h"
#include"decorator.h"
11 changes: 5 additions & 6 deletions include/fast_io_core_impl/concepts/operation.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,16 @@ concept dynamic_reserve_printable=::std::integral<char_type>&&requires(T t,char_
{print_reserve_size(io_reserve_type<char_type,::std::remove_cvref_t<T>>,t)}->::std::convertible_to<::std::size_t>;
{print_reserve_define(io_reserve_type<char_type,::std::remove_cvref_t<T>>,ptr,t)}->::std::convertible_to<char_type*>;
};
#if 0

template<typename char_type,typename T>
concept context_reserve_printable=::std::integral<char_type>&&requires(T t,char_type* ptr)
concept context_printable=::std::integral<char_type>&&requires(T t,char_type* ptr)
{
{print_context_reserve_size(io_reserve_type<char_type,::std::remove_cvref_t<T>>)}->::std::convertible_to<::std::size_t>;
requires requires(typename ::std::remove_cvref_t<decltype(print_context_type(io_reserve_type<char_type,T>))>::type st)
{
{print_context_define(io_reserve_type<char_type,::std::remove_cvref_t<T>>,ptr,st)};
}
{st.print_context_define(t,ptr,ptr)}->::std::same_as<context_print_result<char_type*>>;
};
};
#endif

template<typename char_type,typename T>
concept printable_internal_shift=requires(T t)
{
Expand Down
121 changes: 120 additions & 1 deletion include/fast_io_core_impl/operations/print_freestanding.h
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,124 @@ inline constexpr void print_control_single(output outstm,T t)
}
::fast_io::operations::decay::scatter_write_all_decay(outstm,scattersbuffer,ptr);
}
else if constexpr(context_printable<char_type,value_type>)
{
typename ::std::remove_cvref_t<decltype(print_context_type(io_reserve_type<char_type,value_type>))>::type st;
constexpr
::std::size_t reserved_size{32u};
constexpr
::std::ptrdiff_t reserved_size_no_line{static_cast<::std::ptrdiff_t>(reserved_size-static_cast<::std::size_t>(line))};
if constexpr(::fast_io::operations::decay::defines::has_obuffer_basic_operations<output>)
{
for(;;)
{
auto bcurr{obuffer_curr(outstm)};
auto bed{obuffer_end(outstm)};
if(bed<=bcurr)
#if __has_cpp_attribute(unlikely)
[[unlikely]]
#endif
{
if constexpr(minimum_buffer_output_stream_require_size_impl<output,reserved_size>)
{
obuffer_minimum_size_flush_prepare_define(outstm);
bcurr=obuffer_curr(outstm);
bed=obuffer_end(outstm);
}
else
{
::fast_io::operations::decay::output_stream_buffer_flush_decay(outstm);
bcurr = obuffer_curr(outstm);
bed = obuffer_end(outstm);
if(bed-bcurr<reserved_size_no_line)
#if __has_cpp_attribute(unlikely)
[[unlikely]]
#endif
{
char_type buffer[reserved_size];
char_type *buffered{buffer+reserved_size_no_line};
for (;;)
{
auto [resit, reqsize] = st.print_context_define(t, buffer,buffered);
bool done{!reqsize};
if constexpr(line)
{
if(done)
{
*resit = lfch;
++resit;
}
::fast_io::operations::decay::write_all_decay(outstm,buffer,resit);
if(done)
{
return;
}
}
else
{
::fast_io::operations::decay::write_all_decay(outstm,buffer,resit);
if(done)
{
return;
}
}
}
return;
}
}
}
else
{
auto [resit, reqsize] = st.print_context_define(t,bcurr,bed);
obuffer_set_curr(resit);
if(!reqsize)
#if __has_cpp_attribute(likely)
[[likely]]
#endif
{
if constexpr(line)
{
::fast_io::operations::decay::char_put_decay(outstm,lfch);
}
return;
}

}
}
}
else
{
char_type buffer[reserved_size];
char_type *buffered{buffer+reserved_size_no_line};
for (;;)
{
auto [resit, reqsize] = st.print_context_define(t, buffer,buffered);
bool done{!reqsize};
if constexpr(line)
{
if(done)
{
*resit = lfch;
++resit;
}
::fast_io::operations::decay::write_all_decay(outstm,buffer,resit);
if(done)
{
return;
}
}
else
{
::fast_io::operations::decay::write_all_decay(outstm,buffer,resit);
if(done)
{
return;
}
}
}
return;
}
}
else if constexpr(printable<char_type,value_type>)
{
print_define(::fast_io::io_reserve_type<char_type,value_type>,outstm,t);
Expand Down Expand Up @@ -1187,7 +1305,8 @@ ::std::integral<char_type>&&((::fast_io::printable<char_type,Args>||
::fast_io::reserve_printable<char_type,Args>||
::fast_io::dynamic_reserve_printable<char_type,Args>||
::fast_io::scatter_printable<char_type,Args>||
::fast_io::reserve_scatters_printable<char_type,Args>)&&...);
::fast_io::reserve_scatters_printable<char_type,Args>||
::fast_io::context_printable<char_type,Args>)&&...);


template<typename output,typename ...Args>
Expand Down

0 comments on commit deaeafb

Please sign in to comment.