Example of a mixin I slightly revised today. All the functionality it
provides, for an UTF-8 sequence, depends only on an iterator/pointer to
the first byte of the sequence. So it is the same for a sequence
reference and a sequence value, which can just inherit in the functions:
template< class > struct Unit_iterator_of_t_;
template< class Type > using Unit_iterator_of_ = typename
Unit_iterator_of_t_<Type>::T;
template< class tp_Derived >
class Sequence_inspectors_mixin_
{
public:
using Unit_iterator = typename Unit_iterator_of_<tp_Derived>;
using Unit = typename
iterator_traits<Unit_iterator>::value_type;
private:
auto self() const
-> const tp_Derived&
{ return static_cast<const tp_Derived&>( *this ); }
auto it_start() const -> Unit_iterator { return
self().unit_iterator(); }
public:
auto first_unit() const -> Unit { return *it_start(); }
auto n_bytes() const -> int { return
lead_byte::sequence_length_of( first_unit() ); }
auto begin() const -> Unit_iterator { return it_start(); }
auto end() const -> Unit_iterator { return it_start()
+ n_bytes(); }
auto unit_pointer() const -> const Unit* { return
&*it_start(); }
auto char_pointer() const -> const char* { return
reinterpret_cast<const char*>( unit_pointer() ); }
auto codepoint() const -> char32_t { return
to_codepoint( it_start() ); }
auto sv() const -> string_view { return
string_view( char_pointer(), n_bytes() ); }
auto str() const -> string { return string(
char_pointer(), n_bytes() ); }
};
- Alf