Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Mixins

26 views
Skip to first unread message

Mr Flibble

unread,
Nov 27, 2022, 5:16:54 AM11/27/22
to
C++ templates are great for creating mixins. #cpp #gamedev #coding

template <typename LayoutItemType>
class size_policy_of_parent : public LayoutItemType
{
private:
typedef LayoutItemType base_type;
public:
using base_type::base_type;
public:
neogfx::size_policy size_policy() const override
{
if (base_type::has_size_policy())
{
return base_type::size_policy();
}
else if (base_type::has_parent_layout_item())
{
return base_type::parent_layout_item().size_policy();
}
return base_type::size_policy();
}
};

/Flibble

Bonita Montero

unread,
Nov 27, 2022, 9:40:21 AM11/27/22
to
You could also do a ...

if constexpr( requires( base_type base ) { base.xxx(); } )
...

Bonita Montero

unread,
Nov 27, 2022, 9:41:25 AM11/27/22
to
if constexpr( requires( base_type base ) { { base.xxx() } ->
convertible_to<base_type::size_policy>; } )

Mr Flibble

unread,
Nov 27, 2022, 10:17:08 AM11/27/22
to
If I could have used if constexpr then I would have used if constexpr; I
cannot use if constexpr as size policy is dynamic not static.

/Flibble

Bonita Montero

unread,
Nov 27, 2022, 10:38:02 AM11/27/22
to
I would dispatch that whole thing inside th LayoutItemType.

Mr Flibble

unread,
Nov 27, 2022, 10:56:44 AM11/27/22
to
You obviously don't understand how mixins work and are asserting things
based on ignorance of the architectural design of my project.

/Flibble

Alf P. Steinbach

unread,
Nov 29, 2022, 10:55:29 AM11/29/22
to
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

Alf P. Steinbach

unread,
Nov 29, 2022, 11:01:52 AM11/29/22
to
On 29 Nov 2022 16:55, Alf P. Steinbach wrote:
>         using Unit_iterator     = typename Unit_iterator_of_<tp_Derived>;

Oh, sorry about the extraneous `typename` there, something from the
previous version.

- Alf


0 new messages