top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Forward varible template arguments to array of objects for in-place construction

0 votes
288 views

I am experimenting a bit with exactly how much is (user-wise, anyways)
conveniently implementable with C+11 variadic templates and a strict
construction/destruction pattern. In addition I use this project for a
playground for other techniques, such as designing everything through
composition and mixins, as well as the occasional functor, for maximum
flexibility. Repository: https://github.com/lycantrophe/Curses-- [1]

So far so good, but all currently implemented widgets have been very
basic and only wrapped around the Widget class. What I am having trouble
with is the "ButtonGroup" widget.

This widget is, as the name suggests, a group wrapper for the Button
class that handles things such as a common return value, behaving like
an external entity towards user code and, most importantly, being
responsible for handling input and focus switch between the buttons in
the group it governs.

What I am trying to accomplish is being able to forward a set of
arguments to the individual, underlying buttons. These parameters
include text value (the string to be displayed), individual return
values and individual anchors. Things like border style and behaviour in
regard to focus toggle will of course be shared between all buttons.
Some code to clarify:

 template< typename Return >
 class ButtonGroup {
 ButtonGroup();
 ButtonGroup( const Anchor

 template< typename Action, typename... Args >
 ButtonGroup(
 const std::initializer_list< std::string >& texts,
 const std::initializer_list< Action >& actions,
 const std::initializer_list< Anchor >& anchors,
 const Args

 Return trigger();
 void redraw();

 private:
 std::vector< Button< Return > > buttons;
 typename std::vector< Button< Return > >::iterator current;

 };

Return is the return value from the buttons (typically true/false, but
can be anything). Action is a template as it requires a functor, whereas
bool is treated as a special case (simply creates a functor for you).

This did not work; both gcc and clang/llvm were unable to properly
deduce my use of initializer lists (from the ButtonGroup( const Anchor
to provide me with a vector, array or whatever suitable structure
that holds:
[ Button( "OK", true, Anchor( 0, 0 ), Button( "Cancel", false, Anchor(
0, 3 ) ) ], from here on [ Button, Button... ].

Sending the arguments in a structured matter, such as grouping all
strings, all return-values and all anchors would be fine as well. Now,
it is NOT acceptable to take the actual Buttons (or pointers, for that
matter) as arguments during construction as this would require double
construction and, as you probably see from the other construction code,
defeats the purpose of this style.

Are there any ways I can try to accomplish this? Any other criticism
will be welcome as well.

posted May 14, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

An of course I forgot to add the rest of the code samples. This is a WIP sketch, but should give a good idea of what I am trying to do.

 template< typename T >
 ButtonGroup< T >::ButtonGroup() :
 ButtonGroup( Anchor{ 0, 0 } )
 {}

 template< typename T >
 ButtonGroup< T >::ButtonGroup( const Anchor& a ) :
 ButtonGroup( { "OK", "Cancel" },
 { true, false },
 { { a.y, a.x }, { 0, 5 } } )
 {}

 template< typename T >
 template< typename Action, typename... Args >
 ButtonGroup< T >::ButtonGroup(
 const std::initializer_list< std::string >& texts,
 const std::initializer_list< Action >& actions,
 const std::initializer_list< Anchor >& anchors,
 const Args
 auto action = actions.begin();
 auto anchor = anchors.begin();
 this->buttons.reserve( texts.size() );

 /* Do in-place construction of all buttons */
 for(; text != texts.end(); +text, +action, +anchor )
 this->buttons.emplace_back( *text, *action, *anchor, args... );

 this->current = this->buttons.begin();
 this->current->focus();
 }
answer May 14, 2013 by anonymous
...