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

Optional constructor arguments (article + small header library)

40 views
Skip to first unread message

Alf P. Steinbach

unread,
Mar 28, 2010, 4:39:37 PM3/28/10
to
This is a very small header-only library that supports optional constructor
arguments (with strict static typechecking), also for constructors in a class
hierarchy.

I.e., class Derived can have a set of optional constructor arguments that is an
extension of the optional arguments supported by Base.

Or for that matter you can have multiple different sets of optional arguments
per class.

Or, just use it for optional arguments for ordinary functions.

I've used this code for small hobbyist projects. It works, it's simple, and in
spite of appearances it's also pretty darned efficient. Generally, with just the
lowest level of optimization, accessing e.g. an 'int' type optional arg defined
in some base-base-base class results in at most one machine code instruction (if
the compiler notices that it already has the value in a register the generated
code for an access can be nothing, -- the power of C++ inlining).

The simplest usage is when you're already using the Boost, or don't mind
starting using Boost (even though Boost is large); then you can use the
[options_boosted.h] header and write e.g. ...


<code>
#include <iostream>
#include <progrock/cppx/collections/options_boosted.h>

struct AbstractButton
{
int hTextAlign;

CPPX_DEFINE_OPTIONCLASS( Params, CPPX_OPTIONS_NO_BASE,
( hTextAlign, int, 0 )
( vTextAlign, int, 0 )
( buttonPlacement, int, 0 )
)

explicit AbstractButton( Params const& params = Params() )
: hTextAlign( params.hTextAlign() )
{}
};

struct CheckBox: AbstractButton
{
bool isAuto;

CPPX_DEFINE_OPTIONCLASS( Params, AbstractButton::Params,
( isAuto , bool, true )
)

explicit CheckBox( Params const& params = Params() )
: AbstractButton( params )
, isAuto( params.isAuto() )
{}
};

void show( CheckBox const& cb )
{
using namespace std;
cout
<< boolalpha
<< "align = " << cb.hTextAlign << ", "
<< "isAuto = " << cb.isAuto << ".\n";
}

int main()
{
CheckBox widget1;
show( widget1 ); // 0, true (the default values)

CheckBox widget2( CheckBox::Params()
.hTextAlign( 1 )
);
show( widget2 ); // 1, true

CheckBox widget3( CheckBox::Params()
.hTextAlign( 1 )
.isAuto( false )
);
show( widget3 ); // 1, false
}
</code>


If you don't want the Boost dependency then you can use the basic [options.h]
header, and instead of just CPPX_DEFINE_OPTIONCLASS, in the example above you
would write respectively CPPX_DEFINE_3OPTIONCLASS and CPPX_DEFINE_1OPTIONCLASS.

It may not be apparent why the macros are / may be preferable to just defining
the nested Params classes directly, but if you try to do that you'll discover
why. Essentially, with a derived options class there is a problem with defining
setters that can be chained like in the widget3 declaration above. The code + an
article discussing this and various other solutions (including five reasons why
the Boost Parameter Library can be undesirable for the above) and how this magic
works, is available at Google Docs via

<url: http://preview.tinyurl.com/cppx-optional-arguments>

I've tested the code with g++ 3.4.5, MSVC 7.1 and MSVC 9.0, for the
[options_boosted.h] header with Boost lib versions 1.35 and 1.42, in Windows XP.


Enjoy.

- Alf

PS: Why an article instead of e.g. technical documentation? The article came
about as just an idea when I was cleaning up this code, it's old code. I
submitted the article to Dr. Dobbs Journal some weeks ago. But as always, I
think in total I've submitted three articles to DDJ since 1996, fifteen years or
so, I just got an initial aknowledgement of receipt with "I'll get back in touch
shortly", with no further response: they don't respond to mails at all.

I think it's rather rude. A rejection would have been OK, a reasoned rejection
great (e.g. I can imagine that the article is way too long & wordy), but this
silence, it's just rude. It's been the same with letters to the editor (letters
column); BYTE magazine, now defunct but under the DDJ umbrella, was happy to
publish e.g. a response letter explaining the Ackermann function, and Software
Development (I think it was called), which I believe also merged into DDJ, was
happy to publish a letter about generating permutations using the factorial
number system, but DDJ has never published a letter of mine, and has never
responded to letter submissions. Well, although it means that I can boast of
never having had an article rejected by DDJ, 15 years of Deafening Silence, of
being actively ignored, is enough, so this time I'm self-publishing the article
on the web so that people may benefit. Perhaps also to vent some steam.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

0 new messages