funny use of static_cast

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

funny use of static_cast

Jim Newton
I found the following curious usage. Does anyone think it is wrong. The code is taking and Element and casting it to Chord* before checking its element type. I would think the static_cast<chord*> and the if ( ... chord-.tupe() != Element::Type::CHORD) continue should be reversed? Am I confused? void Score::createPlayEvents() { int etrack = nstaves() * VOICES; for (int track = 0; track < etrack; ++track) { int mn=0; for (Measure* m = firstMeasure(); m; mn++, m = m->nextMeasure()) { // skip linked staves, except primary if (!m->score()->staff(track / VOICES)->primaryStaff()) continue; const Segment::Type st = Segment::Type::ChordRest; for (Segment* seg = m->first(st); seg; seg = seg->next(st)) { Chord* chord = static_cast<Chord*>(seg->element(track)); // curious line order if (chord == 0 || chord->type() != Element::Type::CHORD) // curious line order continue; createPlayEvents(chord); } } } }
Reply | Threaded
Open this post in threaded view
|

Re: funny use of static_cast

Marc Sabatella
It's a little confusing with the formatting messed up, but I think yes, this is "funny".  Should still work fine though.

------------------------------------------------------------------------------
One dashboard for servers and applications across Physical-Virtual-Cloud
Widest out-of-the-box monitoring support with 50+ applications
Performance metrics, stats and reports that give you Actionable Insights
Deep dive visibility with transaction tracing using APM Insight.
http://ad.doubleclick.net/ddm/clk/290420510;117567292;y
_______________________________________________
Mscore-developer mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/mscore-developer
Reply | Threaded
Open this post in threaded view
|

Re: funny use of static_cast

Jim Newton
In reply to this post by Jim Newton
I found the following curious usage. Does anyone think it is wrong. The code is taking and Element and casting it to Chord* before checking its element type. I would think the static_cast<chord*> and the if ( ... chord-.tupe() != Element::Type::CHORD) continue should be reversed? Am I confused?

https://github.com/musescore/MuseScore/blob/8911cdd6c94faf8a0d569829fb885fa2ddcd0f7d/libmscore/rendermidi.cpp#L1013-L1016
Reply | Threaded
Open this post in threaded view
|

Re: funny use of static_cast

Marc Sabatella
Yes, it's definitely "funny".  But it saves a line of code, or a call to seg->element(track), I guess.  Technically, we should check that - which is of type Element - directly, and only if it turns out to be the correct type should we cast it.  But it's harmless and slightly more compact or more efficient the way it is.

On Fri, May 15, 2015 at 3:09 PM, Jim Newton <[hidden email]> wrote:
I found the following curious usage. Does anyone think it is wrong. The code
is taking and Element and casting it to Chord* before checking its element
type. I would think the static_cast<chord*> and the if ( ... chord-.tupe()
!= Element::Type::CHORD) continue should be reversed? Am I confused?

https://github.com/musescore/MuseScore/blob/8911cdd6c94faf8a0d569829fb885fa2ddcd0f7d/libmscore/rendermidi.cpp#L1013-L1016




--
View this message in context: http://dev-list.musescore.org/funny-use-of-static-cast-tp7579351p7579354.html
Sent from the MuseScore Developer mailing list archive at Nabble.com.

------------------------------------------------------------------------------
One dashboard for servers and applications across Physical-Virtual-Cloud
Widest out-of-the-box monitoring support with 50+ applications
Performance metrics, stats and reports that give you Actionable Insights
Deep dive visibility with transaction tracing using APM Insight.
http://ad.doubleclick.net/ddm/clk/290420510;117567292;y
_______________________________________________
Mscore-developer mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/mscore-developer



--
Marc Sabatella
[hidden email]

------------------------------------------------------------------------------
One dashboard for servers and applications across Physical-Virtual-Cloud
Widest out-of-the-box monitoring support with 50+ applications
Performance metrics, stats and reports that give you Actionable Insights
Deep dive visibility with transaction tracing using APM Insight.
http://ad.doubleclick.net/ddm/clk/290420510;117567292;y
_______________________________________________
Mscore-developer mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/mscore-developer