Return default initialized parameter when requested instead of assert

This commit is contained in:
Ian McInerney 2023-06-21 20:39:50 +01:00
parent a5411218a2
commit 9f705f5e81
1 changed files with 30 additions and 6 deletions

View File

@ -36,6 +36,7 @@
#include <optional> #include <optional>
#include <tool/tool_action.h> #include <tool/tool_action.h>
#include <wx/debug.h>
class TOOL_ACTION; class TOOL_ACTION;
class TOOL_MANAGER; class TOOL_MANAGER;
@ -432,23 +433,46 @@ public:
bool IsSimulator() const; bool IsSimulator() const;
/** /**
* Return a non-standard parameter assigned to the event. Its meaning depends on the * Return a parameter assigned to the event. Its meaning depends on the target tool.
* target tool.
*/ */
template<typename T> template<typename T>
T Parameter() const T Parameter( typename std::enable_if<!std::is_pointer<T>::value>::type* = 0 ) const
{ {
wxASSERT_MSG( m_param.has_value(), "Attempted to get a parameter from an event with no parameter." );
T param; T param;
wxCHECK_MSG( m_param.has_value(), T(), "Attempted to get a parameter from an event with no parameter." );
try try
{ {
param = std::any_cast<T>( m_param ); param = std::any_cast<T>( m_param );
} }
catch( const std::bad_any_cast& e ) catch( const std::bad_any_cast& e )
{ {
wxASSERT_MSG( false, wxCHECK_MSG( false, T(),
wxString::Format( "Requested parameter type %s from event with parameter type %s.",
typeid(T).name(), m_param.type().name() ) );
}
return param;
}
/**
* Return pointer parameter assigned to the event. Its meaning depends on the target tool.
*/
template<typename T>
T Parameter( typename std::enable_if<std::is_pointer<T>::value>::type* = 0 ) const
{
T param = nullptr;
wxCHECK_MSG( m_param.has_value(), param, "Attempted to get a parameter from an event with no parameter." );
try
{
param = std::any_cast<T>( m_param );
}
catch( const std::bad_any_cast& e )
{
wxCHECK_MSG( false, param,
wxString::Format( "Requested parameter type %s from event with parameter type %s.", wxString::Format( "Requested parameter type %s from event with parameter type %s.",
typeid(T).name(), m_param.type().name() ) ); typeid(T).name(), m_param.type().name() ) );
} }