1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
#include <iostream>
#include <string>
#include <thread>
#include <chrono>
template < typename C, typename T = std::char_traits<C>, typename A = std::allocator<C> >
struct slow_text
{
std::basic_string<C,T,A> str ;
unsigned int delay_msecs ;
};
template < typename C, typename T, typename A >
std::basic_ostream<C>& operator<< ( std::basic_ostream<C>& stm,
const slow_text<C,T,A>& text )
{
const auto period = std::chrono::milliseconds( text.delay_msecs ) ;
for( auto c : text.str )
{
std::this_thread::sleep_for(period) ;
stm << c << std::flush ;
}
return stm ;
}
template < typename C, typename T, typename A >
slow_text<C,T,A> slowly( std::basic_string<C,T,A>& str, unsigned int msecs = 200 )
{ return slow_text<C,T,A>{ str, msecs }; }
template < typename C > slow_text<C> slowly( const C* cstr, unsigned int msecs = 250 )
{ return slow_text<C>{ cstr, msecs }; }
template < typename C > slow_text<C> slowly( C ch, unsigned int msecs = 250 )
{ return slow_text<C>{ std::basic_string<C>( 1, ch ), msecs }; }
int main()
{
std::cout << slowly( "hello" ) << slowly( " narrow", 300 )
<< slowly( " world!", 400 ) << slowly('\n') ;
std::wcout << slowly( L"hello" ) << slowly( L" wide", 300 )
<< slowly( L" world!", 400 ) << slowly( L'\n' ) ;
}
|