Ticket #3103 (assigned)

Opened 9 years ago

Last modified 5 years ago

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:1 Changed 9 years ago by Nick Draper

  • Milestone changed from Iteration 29 to Iteration 30

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

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:6 Changed 8 years ago by Nick Draper

  • Milestone changed from Release 2.2 to Release 2.3

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:9 Changed 7 years ago by Nick Draper

  • Milestone changed from Release 2.5 to Release 2.6

Moved to r2.6 at the end of r2.5

comment:10 Changed 7 years ago by Nick Draper

  • Status changed from assigned to new

comment:11 Changed 7 years ago by Nick Draper

  • Component changed from Mantid to Framework

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:14 Changed 5 years ago by Stuart Campbell

  • Description modified (diff)

comment:15 Changed 5 years ago by Stuart Campbell

This ticket has been transferred to github issue 3950

Note: See TracTickets for help on using tickets.