Skip to content

Commit

Permalink
Remove shared pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
iurienistor committed Feb 10, 2024
1 parent 69ef52a commit e016b63
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 91 deletions.
34 changes: 19 additions & 15 deletions src/envelope_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,39 +42,43 @@ RK_DECLARE_IMAGE_RC(layer3_env_active);
EnvelopeWidget::EnvelopeWidget(GeonkickWidget *parent,
GeonkickApi *api,
const std::vector<std::unique_ptr<Oscillator>> &oscillators)
: GeonkickWidget(parent)
, drawArea{nullptr}
, layer1Button{nullptr}
, layer2Button{nullptr}
, layer3Button{nullptr}
, geonkickApi{api}
: GeonkickWidget(parent)
, drawArea{nullptr}
, layer1Button{nullptr}
, layer2Button{nullptr}
, layer3Button{nullptr}
, geonkickApi{api}
{
// Create drawing area.
drawArea = new EnvelopeWidgetDrawingArea(this, geonkickApi);
auto rect = drawArea->getDrawingArea();

// Oscillator1 envelope
auto oscillator = oscillators[static_cast<int>(Oscillator::Type::Oscillator1)].get();
auto envelope = std::dynamic_pointer_cast<Envelope>(std::make_shared<OscillatorEnvelope>(oscillator, rect));
envelopes.insert({static_cast<int>(Envelope::Category::Oscillator1), envelope});
auto envelope = std::make_unique<OscillatorEnvelope>(oscillator, rect);
envelope->setCategory(Envelope::Category::Oscillator1);
envelopes.insert({static_cast<int>(Envelope::Category::Oscillator1),
std::move(envelope)});

// Oscillator2 envelope
oscillator = oscillators[static_cast<int>(Oscillator::Type::Oscillator2)].get();
envelope = std::dynamic_pointer_cast<Envelope>(std::make_shared<OscillatorEnvelope>(oscillator, rect));
envelopes.insert({static_cast<int>(Envelope::Category::Oscillator2), envelope});
envelope = std::make_unique<OscillatorEnvelope>(oscillator, rect);
envelope->setCategory(Envelope::Category::Oscillator2);
envelopes.insert({static_cast<int>(Envelope::Category::Oscillator2),
std::move(envelope)});

// Noise envelope
oscillator = oscillators[static_cast<int>(Oscillator::Type::Noise)].get();
envelope = std::dynamic_pointer_cast<Envelope>(std::make_shared<OscillatorEnvelope>(oscillator, rect));
envelopes.insert({static_cast<int>(Envelope::Category::Noise), envelope});
envelope = std::make_unique<OscillatorEnvelope>(oscillator, rect);
envelope->setCategory(Envelope::Category::Noise);
envelopes.insert({static_cast<int>(Envelope::Category::Noise),
std::move(envelope)});

// General envelope
envelope = std::dynamic_pointer_cast<Envelope>(std::make_shared<GeneralEnvelope>(geonkickApi, rect));
envelopes.insert({static_cast<int>(Envelope::Category::General), envelope});
envelope->setCategory(Envelope::Category::General);
auto generalEnvelope = std::make_unique<GeneralEnvelope>(geonkickApi, rect);
generalEnvelope->setCategory(Envelope::Category::General);
envelopes.insert({static_cast<int>(Envelope::Category::General),
std::move(generalEnvelope)});
createButtomMenu();
showEnvelope(Envelope::Category::General, Envelope::Type::Amplitude);
RK_ACT_BIND(viewState(), envelopeChanged,
Expand Down
2 changes: 1 addition & 1 deletion src/envelope_widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class EnvelopeWidget : public GeonkickWidget
void createPointInfoLabel();

private:
std::unordered_map<int, std::shared_ptr<Envelope>> envelopes;
std::unordered_map<int, std::unique_ptr<Envelope>> envelopes;
EnvelopeWidgetDrawingArea *drawArea;
GeonkickButton *layer1Button;
GeonkickButton *layer2Button;
Expand Down
20 changes: 10 additions & 10 deletions src/geonkick_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ std::unique_ptr<KitState> GeonkickApi::getDefaultKitState()
return std::make_unique<KitState>();
}

std::shared_ptr<PercussionState> GeonkickApi::getDefaultPercussionState()
std::unique_ptr<PercussionState> GeonkickApi::getDefaultPercussionState()
{
std::shared_ptr<PercussionState> state = std::make_shared<PercussionState>();
auto state = std::make_unique<PercussionState>();
state->setName("Default");
state->setId(0);
state->setPlayingKey(-1);
Expand Down Expand Up @@ -240,7 +240,7 @@ std::shared_ptr<PercussionState> GeonkickApi::getDefaultPercussionState()
return state;
}

void GeonkickApi::setPercussionState(const std::shared_ptr<PercussionState> &state)
void GeonkickApi::setPercussionState(const std::unique_ptr<PercussionState> &state)
{
if (!state)
return;
Expand Down Expand Up @@ -307,7 +307,7 @@ void GeonkickApi::setPercussionState(const std::string &data)
setPercussionState(state);
}

std::shared_ptr<PercussionState> GeonkickApi::getPercussionState(size_t id) const
std::unique_ptr<PercussionState> GeonkickApi::getPercussionState(size_t id) const
{
if (id == currentPercussion()) {
return getPercussionState();
Expand All @@ -324,9 +324,9 @@ std::shared_ptr<PercussionState> GeonkickApi::getPercussionState(size_t id) cons
}
}

std::shared_ptr<PercussionState> GeonkickApi::getPercussionState() const
std::unique_ptr<PercussionState> GeonkickApi::getPercussionState() const
{
auto state = std::make_shared<PercussionState>();
auto state = std::make_unique<PercussionState>();
state->setId(currentPercussion());
state->setName(getPercussionName(state->getId()));
state->setLimiterValue(limiterValue());
Expand Down Expand Up @@ -381,7 +381,7 @@ std::shared_ptr<PercussionState> GeonkickApi::getPercussionState() const

void GeonkickApi::getOscillatorState(GeonkickApi::Layer layer,
OscillatorType osc,
const std::shared_ptr<PercussionState> &state) const
const std::unique_ptr<PercussionState> &state) const
{
auto temp = currentLayer;
currentLayer = layer;
Expand Down Expand Up @@ -423,7 +423,7 @@ void GeonkickApi::getOscillatorState(GeonkickApi::Layer layer,

void GeonkickApi::setOscillatorState(GeonkickApi::Layer layer,
OscillatorType oscillator,
const std::shared_ptr<PercussionState> &state)
const std::unique_ptr<PercussionState> &state)
{
auto temp = currentLayer;
currentLayer = layer;
Expand Down Expand Up @@ -479,7 +479,7 @@ std::unique_ptr<KitState> GeonkickApi::getKitState() const
for (const auto &id : ordredPercussionIds()) {
auto state = getPercussionState(id);
state->setId(i);
kit->addPercussion(state);
kit->addPercussion(std::move(state));
GEONKICK_LOG_DEBUG("PER: " << state->getName() << ": id = " << state->getId());
i++;
}
Expand Down Expand Up @@ -1631,7 +1631,7 @@ void GeonkickApi::copyToClipboard()
void GeonkickApi::pasteFromClipboard()
{
if (clipboardPercussion) {
auto state = std::make_shared<PercussionState>(*clipboardPercussion);
auto state = std::make_unique<PercussionState>(*clipboardPercussion);
auto currId = currentPercussion();
state->setId(currId);
state->setName(getPercussionName(currId));
Expand Down
14 changes: 7 additions & 7 deletions src/geonkick_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,15 @@ class GeonkickApi : public RkObject {
double limiterValue() const;
int getSampleRate() const;
static std::unique_ptr<KitState> getDefaultKitState();
static std::shared_ptr<PercussionState> getDefaultPercussionState();
static std::unique_ptr<PercussionState> getDefaultPercussionState();
// This function is called only from the audio thread.
void setKeyPressed(bool b, int note, int velocity);

// This function is called only from the audio thread.
void process(float** out, size_t channel, size_t size);

std::shared_ptr<PercussionState> getPercussionState(size_t id) const;
std::shared_ptr<PercussionState> getPercussionState() const;
std::unique_ptr<PercussionState> getPercussionState(size_t id) const;
std::unique_ptr<PercussionState> getPercussionState() const;
bool isCompressorEnabled() const;
double getCompressorAttack() const;
double getCompressorRelease() const;
Expand All @@ -216,7 +216,7 @@ class GeonkickApi : public RkObject {
void enableKickFilter(bool b);
void setKickFilterType(FilterType type);
void setPercussionState(const std::string &data);
void setPercussionState(const std::shared_ptr<PercussionState> &state);
void setPercussionState(const std::unique_ptr<PercussionState> &state);
std::unique_ptr<KitState> getKitState() const;
bool setKitState(const std::string &data);
bool setKitState(const std::unique_ptr<KitState> &state);
Expand Down Expand Up @@ -353,10 +353,10 @@ class GeonkickApi : public RkObject {
void updateKickBuffer(const std::vector<gkick_real> &&buffer, size_t id);
void setOscillatorState(Layer layer,
OscillatorType oscillator,
const std::shared_ptr<PercussionState> &state);
const std::unique_ptr<PercussionState> &state);
void getOscillatorState(Layer layer,
OscillatorType osc,
const std::shared_ptr<PercussionState> &state) const;
const std::unique_ptr<PercussionState> &state) const;
void setLimiterLevelerValue(size_t index, double val);
static std::vector<gkick_real> loadSample(const std::string &file,
double length = 4.0,
Expand All @@ -376,7 +376,7 @@ class GeonkickApi : public RkObject {
std::string kitName;
std::string kitAuthor;
std::string kitUrl;
std::shared_ptr<PercussionState> clipboardPercussion;
std::unique_ptr<PercussionState> clipboardPercussion;

/**
* Current working paths for entire application.
Expand Down
14 changes: 7 additions & 7 deletions src/kit_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ std::string KitState::getUrl() const
return kitUrl;
}

std::vector<std::shared_ptr<PercussionState>>& KitState::percussions()
const std::vector<std::unique_ptr<PercussionState>>& KitState::percussions() const
{
return percussionsList;
}
Expand Down Expand Up @@ -152,10 +152,10 @@ void KitState::parsePercussions(const rapidjson::Value &percussionsArray)
{
size_t i = 0;
for (const auto &per: percussionsArray.GetArray()) {
auto state = std::make_shared<PercussionState>();
auto state = std::make_unique<PercussionState>();
state->setId(i++);
state->loadObject(per);
addPercussion(state);
addPercussion(std::move(state));
}
}

Expand All @@ -182,14 +182,14 @@ std::string KitState::toJson() const
return jsonStream.str();
}

void KitState::addPercussion(const std::shared_ptr<PercussionState> &percussion)
void KitState::addPercussion(std::unique_ptr<PercussionState> percussion)
{
percussionsList.push_back(percussion);
percussionsList.push_back(std::move(percussion));
}

std::shared_ptr<PercussionState> KitState::getPercussion(size_t id)
const PercussionState* KitState::getPercussion(size_t id) const
{
if (id < percussionsList.size())
return percussionsList[id];
return percussionsList[id].get();
return nullptr;
}
8 changes: 4 additions & 4 deletions src/kit_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ class KitState {
void setUrl(const std::string &url);
std::string getUrl() const;
std::string toJson() const;
void addPercussion(const std::shared_ptr<PercussionState> &percussion);
std::shared_ptr<PercussionState> getPercussion(size_t id);
std::vector<std::shared_ptr<PercussionState>>& percussions();
void addPercussion(std::unique_ptr<PercussionState> percussion);
const PercussionState* getPercussion(size_t id) const;
const std::vector<std::unique_ptr<PercussionState>>& percussions() const;

protected:
void parsePercussions(const rapidjson::Value &percussionsArray);

private:
std::vector<std::shared_ptr<PercussionState>> percussionsList;
std::vector<std::unique_ptr<PercussionState>> percussionsList;
int kitAppVersion;
std::string kitName;
std::string kitAuthor;
Expand Down
Loading

0 comments on commit e016b63

Please sign in to comment.