Skip to content
Snippets Groups Projects
Commit 7f8a7f0c authored by Matthias Puchner's avatar Matthias Puchner
Browse files

improve function ptr initialization

parent 3e48ebe6
No related branches found
No related tags found
1 merge request!393Selection descriptor
......@@ -40,14 +40,25 @@ using std::function;
//! member, a std::variant or any other construction to define a selection.
template <typename T> class SelectionDescriptor {
public:
SelectionDescriptor() = default;
SelectionDescriptor()
{
// initialize function pointers to a lambda which throws an error. This helps later on if
// accidentally the real initialization has been forgotten.
setCurrentIndex = [](int) { throw std::runtime_error("Function pointer not initialized"); };
currentIndex = []() -> int {
throw std::runtime_error("Function pointer not initialized");
};
currentItem = []() -> T { throw std::runtime_error("Function pointer not initialized"); };
}
//! Initialize the members by means of a GroupItem.
//!
//! currentItem can only be initialized if the template parameter is a pointer (like
//! RotationItem*). If it is e.g. a std::variant<>, the currentItem has to be initialized by the
//! caller. Only for easier migration. Should be removed after SessionItem refactoring.
explicit SelectionDescriptor(GroupItem* item)
explicit SelectionDescriptor(GroupItem* item) : SelectionDescriptor()
{
label = item->displayName();
options = item->value().value<ComboProperty>().getValues();
......@@ -65,12 +76,12 @@ public:
currentItem = [=] { return dynamic_cast<T>(item->currentItem()); };
}
QString label; //!< A label text (short, no trailing colon)
QString tooltip; //!< Tooltip text
QStringList options; //!< List of options, usually presented as combo entries
function<void(int)> setCurrentIndex = nullptr; //!< Function to set currently selected option
function<int()> currentIndex = nullptr; //!< Function to get currently selected option
function<T()> currentItem = nullptr; //!< Function to get currently selected item
QString label; //!< A label text (short, no trailing colon)
QString tooltip; //!< Tooltip text
QStringList options; //!< List of options, usually presented as combo entries
function<void(int)> setCurrentIndex; //!< Function to set currently selected option
function<int()> currentIndex; //!< Function to get currently selected option
function<T()> currentItem; //!< Function to get currently selected item
};
#endif // BORNAGAIN_GUI_MODELS_SELECTIONDESCRIPTOR_H
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment