Skip to content

Commit

Permalink
Controller: create templated downcastAndTakeOwnership method
Browse files Browse the repository at this point in the history
  • Loading branch information
Be-ing committed Feb 17, 2021
1 parent fd670fe commit 4efdeb5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 24 deletions.
9 changes: 1 addition & 8 deletions src/controllers/bulk/bulkcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,7 @@ QString BulkController::mappingExtension() {
}

void BulkController::setMapping(std::shared_ptr<LegacyControllerMapping> pMapping) {
VERIFY_OR_DEBUG_ASSERT(pMapping.use_count() == 1) {
return;
}
auto pDowncastedMapping = std::dynamic_pointer_cast<LegacyHidControllerMapping>(pMapping);
VERIFY_OR_DEBUG_ASSERT(pDowncastedMapping) {
return;
}
m_pMapping = pDowncastedMapping;
m_pMapping = downcastAndTakeOwnership<LegacyHidControllerMapping>(std::move(pMapping));
}

std::shared_ptr<LegacyControllerMapping> BulkController::cloneMapping() {
Expand Down
17 changes: 17 additions & 0 deletions src/controllers/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@ class Controller : public QObject {
void stopLearning();

protected:
template<typename SpecificMappingType>
std::shared_ptr<SpecificMappingType> downcastAndTakeOwnership(
std::shared_ptr<LegacyControllerMapping>&& pMapping) {
// Controller cannot take ownership if pMapping is referenced elsewhere because
// the controller polling thread needs exclusive accesses to the non-thread safe
// LegacyControllerMapping.
// Trying to cast a std::shared_ptr to a std::unique_ptr is not worth the trouble.
VERIFY_OR_DEBUG_ASSERT(pMapping.use_count() == 1) {
return nullptr;
}
auto pDowncastedMapping = std::dynamic_pointer_cast<SpecificMappingType>(pMapping);
VERIFY_OR_DEBUG_ASSERT(pDowncastedMapping) {
return nullptr;
}
return pDowncastedMapping;
}

// The length parameter is here for backwards compatibility for when scripts
// were required to specify it.
virtual void send(const QList<int>& data, unsigned int length = 0);
Expand Down
9 changes: 1 addition & 8 deletions src/controllers/hid/hidcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,7 @@ QString HidController::mappingExtension() {
}

void HidController::setMapping(std::shared_ptr<LegacyControllerMapping> pMapping) {
VERIFY_OR_DEBUG_ASSERT(pMapping.use_count() == 1) {
return;
}
auto pDowncastedMapping = std::dynamic_pointer_cast<LegacyHidControllerMapping>(pMapping);
VERIFY_OR_DEBUG_ASSERT(pDowncastedMapping) {
return;
}
m_pMapping = pDowncastedMapping;
m_pMapping = downcastAndTakeOwnership<LegacyHidControllerMapping>(std::move(pMapping));
}

std::shared_ptr<LegacyControllerMapping> HidController::cloneMapping() {
Expand Down
9 changes: 1 addition & 8 deletions src/controllers/midi/midicontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,7 @@ QString MidiController::mappingExtension() {
}

void MidiController::setMapping(std::shared_ptr<LegacyControllerMapping> pMapping) {
VERIFY_OR_DEBUG_ASSERT(pMapping.use_count() == 1) {
return;
}
auto pDowncastedMapping = std::dynamic_pointer_cast<LegacyMidiControllerMapping>(pMapping);
VERIFY_OR_DEBUG_ASSERT(pDowncastedMapping) {
return;
}
m_pMapping = pDowncastedMapping;
m_pMapping = downcastAndTakeOwnership<LegacyMidiControllerMapping>(std::move(pMapping));
}

std::shared_ptr<LegacyControllerMapping> MidiController::cloneMapping() {
Expand Down

0 comments on commit 4efdeb5

Please sign in to comment.