On 10/15/23 05:41, Gavin Smith wrote:
texi2any is full of code like this:
if ($target_element->{'extra'}
and $target_element->{'extra'}->{'unit_command'}) {
if ($target_element->{'extra'}->{'unit_command'}->{'cmdname'} eq
'node') {
$command = $target_element->{'extra'}->{'unit_command'};
} elsif ($target_element->{'extra'}->{'unit_command'}->{'extra'}
and $target_element->{'extra'}->{'unit_command'}
->{'extra'}->{'associated_node'}) {
$command = $target_element->{'extra'}->{'unit_command'}
->{'extra'}->{'associated_node'};
}
}
I'm far from a C++ expert these days, but some ideas:
* First of course you can define some helper methods:
class TargetElement {
Extra *extra;
Command *unit_command() { return extra ? extra->init_command() : nullptr; }
}
* Declarations in 'if' statement:
if (auto unit_command = target_elememt->unit_command()) {
if (unit_command->cmdname() == "node") ...
}
* Some kind of smart pointer may be useful.
However, I don't really have a good handle on smart pointer,
and I don't know if this further than you want to go in terms of C++-isms.
I can do some research, though.
* Perhaps use a subclass for the "extra" fields:
class TargetElementWithExtra :: TargetElement {
Command *unit_command;
}
if (auto te = dynamic_cast<TargetElementWithExtra*>(target_element)) {
// te is target_element safely cast to TargetElementWithExtra*.
}
--
--Per Bothner
p...@bothner.com http://per.bothner.com/