std::unique_ptr<Slider>sliderInit(String name, Slider::SliderStyle style, float min, float max, float step) {
auto sliderPtr = std::unique_ptr<Slider>{new Slider(name)};
sliderPtr->setSliderStyle(style);
sliderPtr->setRange(min, max, step);
return sliderPtr;
}
ClipperGUI::ClipperGUI (ClipperAudio& p): AudioProcessorEditor (&p), processor (p)
{
setSize(400, 300);//Set the size of my plugin to be 400 by 300.
GainSlider = sliderInit("Gain", Slider::SliderStyle::LinearBarVertical, 0.0f, 1.0f, 0.0001f);
addAndMakeVisible(*GainSlider);
GainSlider->addListener(this);
}
void ClipperGUI::paint(Graphics& g)
{
g.fillAll(Colours::orange);
g.setColour(Colours::black);
g.setFont(15.0f);
g.drawFittedText("Drive", 0, 0, getWidth(), 30, Justification::centred, 1);
}
void ClipperGUI::resized()
{
GainSlider->setBounds(40, 30, 20, getHeight() - 60);
}
void ClipperGUI::sliderValueChanged(Slider* slider)
{
processor.mGain = GainSlider->getValue();
}
ClipperGUI::~ClipperGUI()
{
GainSlider = NULL;
}
A bit more of context description beneath the code and the topic would be really nice, like: What goes wrong? A compiler error (what does it say) a runtime error…?
What looks suspicious:
std::unique_ptr<Slider>sliderInit(String name, Slider::SliderStyle style, float min, float max, float step)
So sliderInit
is a free function declared outside the class in your compile unit? I wouldn’t consider this as best practice. Better make it a private static member function of the ClipperGUI
class. Furthermore, is the missing space in std::unique_ptr<Slider>sliderInit
a copy & paste error? Otherwise I’m not sure if this is valid, I guess not.
Then: Looking at the documentations for the operator=
of std::unique_ptr
here https://en.cppreference.com/w/cpp/memory/unique_ptr/operator%3D shows us that all the methods there look like this:
unique_ptr& operator=( unique_ptr&& r)
What’s important here is the &&
before the argument. It tells you, that a unique_ptr
can only be move-assigned. So assumed GainSlider
is a std::unique_ptr
, the line GainSlider = sliderInit (...)
won’t work. Instead you need to write GainSlider = std::move (sliderInit (...))
.
Two code-style related things: It’s good practise to begin member variables with a lowercase letter, and types with an upper case one, so you should consider naming your slider member gainSlider
. Second: As you use auto
for sliderPtr
(which is good!), it’s considered clean and modern C++ to write auto sliderPtr = std::make_unique<Slider> (name);
.