Skip to content

Commit

Permalink
Use composition in m::model::Channels instead of inheriting from u::C…
Browse files Browse the repository at this point in the history
…ontainer
  • Loading branch information
gvnnz committed Apr 6, 2024
1 parent 1c4148e commit d951b37
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/core/channels/channelManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ void ChannelManager::deleteChannel(ID channelId)
const Channel& ch = m_model.get().channels.get(channelId);
const Wave* wave = ch.sampleChannel ? ch.sampleChannel->getWave() : nullptr;

m_model.get().channels.removeById(channelId);
m_model.get().channels.remove(channelId);
m_model.swap(model::SwapType::HARD);

if (wave != nullptr)
Expand Down
45 changes: 25 additions & 20 deletions src/core/model/channels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,18 @@ namespace giada::m::model
{
const Channel& Channels::get(ID id) const
{
const Channel* out = find_(id);
const Channel* out = nullptr;
for (const Channel& ch : m_channels.getAll())
{
if (ch.id == id)
out = &ch;
if (ch.type == ChannelType::GROUP)
{
const Channel* child = ch.groupChannel->channels->find(id);
if (child != nullptr)
out = child;
}
}
assert(out != nullptr);
return *out;
}
Expand All @@ -51,13 +62,25 @@ Channel& Channels::get(ID id)

/* -------------------------------------------------------------------------- */

std::vector<Channel>& Channels::getAll() { return m_channels.getAll(); }
const std::vector<Channel>& Channels::getAll() const { return m_channels.getAll(); }
Channel* Channels::find(ID id) { return m_channels.findById(id); }
const Channel* Channels::find(ID id) const { return m_channels.findById(id); }
Channel& Channels::add(Channel&& c) { return m_channels.add(std::move(c)); };
Channel& Channels::getLast() { return m_channels.getLast(); }
void Channels::remove(ID id) { return m_channels.removeById(id); };
bool Channels::anyOf(std::function<bool(const Channel&)> f) const { return m_channels.anyOf(f); }
std::vector<Channel*> Channels::getIf(std::function<bool(const Channel&)> f) { return m_channels.getIf(f); }

/* -------------------------------------------------------------------------- */

#ifdef G_DEBUG_MODE

void Channels::debug() const
{
puts("model::channels");

for (int i = 0; const Channel& c : getAll())
for (int i = 0; const Channel& c : m_channels.getAll())
{
fmt::print("\t{} - {}\n", i++, c.debug());
if (c.type == ChannelType::GROUP)
Expand All @@ -74,22 +97,4 @@ void Channels::debug() const
}

#endif

/* -------------------------------------------------------------------------- */

const Channel* Channels::find_(ID id) const
{
for (const Channel& ch : getAll())
{
if (ch.id == id)
return &ch;
if (ch.type == ChannelType::GROUP)
{
const Channel* child = ch.groupChannel->channels->findById(id);
if (child != nullptr)
return child;
}
}
return nullptr;
}
} // namespace giada::m::model
14 changes: 12 additions & 2 deletions src/core/model/channels.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

namespace giada::m::model
{
class Channels : public u::Container<Channel, /*Identifiable=*/true, /*Sortable=*/true>
class Channels
{
public:
/* get
Expand All @@ -42,12 +42,22 @@ class Channels : public u::Container<Channel, /*Identifiable=*/true, /*Sortable=
const Channel& get(ID) const;
Channel& get(ID);

std::vector<Channel>& getAll();
const std::vector<Channel>& getAll() const;
Channel* find(ID);
const Channel* find(ID) const;
Channel& add(Channel&&);
Channel& getLast();
void remove(ID);
bool anyOf(std::function<bool(const Channel&)>) const;
std::vector<Channel*> getIf(std::function<bool(const Channel&)>);

#ifdef G_DEBUG_MODE
void debug() const;
#endif

private:
const Channel* find_(ID) const;
u::Container<Channel, /*Identifiable=*/true, /*Sortable=*/true> m_channels;
};
} // namespace giada::m::model

Expand Down

0 comments on commit d951b37

Please sign in to comment.