Ticket #3103 (assigned)
Can casting be avoided when setting properties?
| Reported by: | Anders Markvardsen | Owned by: | Anyone |
|---|---|---|---|
| Priority: | minor | Milestone: | Backlog |
| Component: | Framework | Keywords: | |
| Cc: | Blocked By: | ||
| Blocking: | Tester: |
Description (last modified by Stuart Campbell) (diff)
This ticket is mainly to help us developers.
For example, say, we have the property:
declareProperty(new WorkspaceProperty<Workspace>("InputWorkspace","",Direction::Input),
"Name of the workspace to be saved");
And you have some code along the lines of:
EventWorkspace_sptr WS = WorkspaceCreationHelper::CreateGroupedEventWorkspace(groups, 100, 1.0);
WS->getEventList(0).switchTo(TOF);
alg.setProperty("InputWorkspace", boost::dynamic_pointer_cast<Workspace>(WS));
The slight nuisance here is that a casting is needed to set the property "InputWorkspace", in this case from EventWorkspace to Workspace, which is a bit irritating since an EventWorkspace is a Workspace!
So basically this ticket is if anyone have a good idea for how to avoid this. Likely this will involve a change to the IPropertyManager method:
/** Templated method to set the value of a PropertyWithValue
* @param name :: The name of the property (case insensitive)
* @param value :: The value to assign to the property
* @throw Exception::NotFoundError If the named property is unknown
* @throw std::invalid_argument If an attempt is made to assign to a property of different type
*/
template <typename T>
IPropertyManager* setProperty(const std::string &name, const T & value)
{
PropertyWithValue<T> *prop = dynamic_cast<PropertyWithValue<T>*>(getPointerToProperty(name));
if (prop)
{
*prop = value;
}
else
{
throw std::invalid_argument("Attempt to assign to property (" + name + ") of incorrect type");
}
return this;
}
/** Templated method to set the value of a PropertyWithValue
* @param name :: The name of the property (case insensitive)
* @param value :: The value to assign to the property
* @throw Exception::NotFoundError If the named property is unknown
* @throw std::invalid_argument If an attempt is made to assign to a property of different type
*/
template <typename T>
IPropertyManager* setProperty(const std::string &name, const T & value)
{
PropertyWithValue<T> *prop = dynamic_cast<PropertyWithValue<T>*>(getPointerToProperty(name));
if (prop)
{
*prop = value;
}
else
{
throw std::invalid_argument("Attempt to assign to property (" + name + ") of incorrect type");
}
return this;
}
Change History
comment:2 Changed 9 years ago by Nick Draper
- Status changed from new to assigned
- Owner set to Anyone
comment:3 Changed 9 years ago by Nick Draper
- Milestone changed from Iteration 30 to Iteration 31
Bulk move of tickets to iteration 31 at the iteration 30 code freeze
comment:4 Changed 9 years ago by Nick Draper
- Milestone changed from Iteration 32 to Iteration 33
Moved to iteration 33 at iteration 32 code freeze
comment:5 Changed 8 years ago by Nick Draper
- Milestone changed from Release 2.1 to Release 2.2
Moved at end of release 2.1
comment:7 Changed 8 years ago by Nick Draper
- Milestone changed from Release 2.3 to Release 2.4
Moved to milestone 2.4
comment:8 Changed 8 years ago by Nick Draper
- Milestone changed from Release 2.4 to Release 2.5
Moved at the code freeze for release 2.4
comment:12 Changed 7 years ago by Nick Draper
- Milestone changed from Release 2.6 to Backlog
Moved to backlog at the code freeze for R2.6
comment:13 Changed 7 years ago by Nick Draper
- Status changed from new to assigned
bulk move to assigned at the into of the triage step
comment:15 Changed 5 years ago by Stuart Campbell
This ticket has been transferred to github issue 3950

"New" tickets moved at the code freeze of iteration 29