commit c7cf8b8d9d061e8ea74fc6ae334f85e6139be855
parent 141f2eb6a0bb6aee5028cd2ed0622624988564a9
Author: dsp56300 <dsp56300@users.noreply.github.com>
Date: Wed, 16 Mar 2022 17:04:28 +0100
error handling if a requested component is not found
Diffstat:
2 files changed, 25 insertions(+), 8 deletions(-)
diff --git a/source/jucePlugin/genericUI/editor.cpp b/source/jucePlugin/genericUI/editor.cpp
@@ -61,21 +61,38 @@ namespace genericUI
}
}
- const std::vector<juce::Component*>& Editor::findComponents(const std::string& _name) const
+ const std::vector<juce::Component*>& Editor::findComponents(const std::string& _name, uint32_t _expectedCount/* = 0*/) const
{
const auto it = m_componentsByName.find(_name);
if(it != m_componentsByName.end())
+ {
+ if(_expectedCount && it->second.size() != _expectedCount)
+ {
+ std::stringstream ss;
+ ss << "Expected to find " << _expectedCount << " components named " << _name << " but found " << it->second.size();
+ throw std::runtime_error(ss.str());
+ }
return it->second;
+ }
+
+ if(_expectedCount)
+ {
+ std::stringstream ss;
+ ss << "Unable to find component named " << _name << ", expected to find " << _expectedCount << " components";
+ throw std::runtime_error(ss.str());
+ }
static std::vector<juce::Component*> empty;
return empty;
}
- juce::Component* Editor::findComponent(const std::string& _name) const
+ juce::Component* Editor::findComponent(const std::string& _name, bool _mustExist/* = true*/) const
{
const auto comps = findComponents(_name);
if(comps.size() > 1)
throw std::runtime_error("Failed to find unique component named " + _name + ", found more than one object with that name");
+ if(_mustExist && comps.empty())
+ throw std::runtime_error("Failed to find component named " + _name);
return comps.empty() ? nullptr : comps.front();
}
diff --git a/source/jucePlugin/genericUI/editor.h b/source/jucePlugin/genericUI/editor.h
@@ -28,12 +28,12 @@ namespace genericUI
void registerComponent(const std::string& _name, juce::Component* _component);
- const std::vector<juce::Component*>& findComponents(const std::string& _name) const;
+ const std::vector<juce::Component*>& findComponents(const std::string& _name, uint32_t _expectedCount = 0) const;
template<typename T>
- void findComponents(std::vector<T*>& _dst, const std::string& _name) const
+ void findComponents(std::vector<T*>& _dst, const std::string& _name, uint32_t _expectedCount = 0) const
{
- const auto& res = findComponents(_name);
+ const auto& res = findComponents(_name, _expectedCount);
for (auto* s : res)
{
auto* t = dynamic_cast<T*>(s);
@@ -42,12 +42,12 @@ namespace genericUI
}
}
- juce::Component* findComponent(const std::string& _name) const;
+ juce::Component* findComponent(const std::string& _name, bool _mustExist = true) const;
template<typename T>
- T* findComponentT(const std::string& _name) const
+ T* findComponentT(const std::string& _name, bool _mustExist = true) const
{
- juce::Component* c = findComponent(_name);
+ juce::Component* c = findComponent(_name, _mustExist);
return dynamic_cast<T*>(c);
}