Extending Features (C++ question)


#1

I want to add a new attribute to the slider. The attribute will cause sliders with that attribute to be rendered differently. What’s the right way to handle it?

I have a class inheriting from Slider, to which I’ve added the attribute, and a custom look and feel class.

The look and feel routine gets passed a reference: Slider &slider, which knows nothing about my fancy new slider class. Any suggestions? I’m sure the answer is obvious if you know enough C++ :slight_smile:

I was trying to avoid modifying all the Juce code, but maybe I should just get stuck in!

thank you! J.


#2

Well, Slider subclasses Component, and every component has a property map, which you could stick a property in to indicate the presence of whatever that attribute is. Check out the method, NamedValueSet & getProperties (), in Component.


#3

Sweet as - thank you.


#4

The other solution is a dynamic_cast. Since you know the type of your object, you dynamically cast (usually) a pointer to your class, something like:

MySliderClass* theSlider = <dynamic_cast>(aSliderPointer); if (theSlider) // it was your class! { do some shit with your new pointer }

Bruce


#5

Personally, I’d also go for a dynamic_cast, but remember to always write your null tests with the assignment inside the ‘if’ statement like this:

void MyLookAndFeel::drawSlider (Slider& slider, ...etc)
{
    if (MySlider* s = dynamic_cast <MySlider*> (&slider))
    {
        ... custom stuff...
    }
    else
    {
         LookAndFeel::drawSlider (..etc)
    }

(see the juce coding standards page for an explanation why this is good practice!)


#6

[quote=“jules”]Personally, I’d also go for a dynamic_cast, but remember to always write your null tests with the assignment inside the ‘if’ statement like this:

void MyLookAndFeel::drawSlider (Slider& slider, ...etc)
{
    if (MySlider* s = dynamic_cast <MySlider*> (&slider))
    {
        ... custom stuff...
    }
    else
    {
         LookAndFeel::drawSlider (..etc)
    }

(see the juce coding standards page for an explanation why this is good practice!)[/quote]

Ah ha, thanks. Nice updates on the coding styles, btw. Doesn’t that construct break: “When you’re testing a pointer to see if it’s null, never write “if (myPointer)”. Always avoid that implicit cast-to-bool”?

BTW - I’m with you on a space before a parenthesis, but xcode’s auto-complete really isn’t, and it makes life awkward.