Skip to content

Commit

Permalink
Fix add/replace of values for generic types
Browse files Browse the repository at this point in the history
A brand new parameter object is always created and it either overwrites
an existing value or adds to the map.
The addPositionCoordinate/addRotationParam are still broken.
Refs #9802
  • Loading branch information
martyngigg committed Jul 2, 2014
1 parent e4616a8 commit 395dbcc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 29 deletions.
Expand Up @@ -137,7 +137,9 @@ namespace Geometry
const std::string& value);

/**
* Method for adding a parameter providing its value of a particular type
* Method for adding a parameter providing its value of a particular type.
* If a parameter already exists then it is replaced with a new one of the
* given type and value
* @tparam T The concrete type
* @param type :: A string denoting the type, e.g. double, string, fitting
* @param comp :: A pointer to the component that this parameter is attached to
Expand All @@ -148,24 +150,15 @@ namespace Geometry
void add(const std::string& type,const IComponent* comp,const std::string& name,
const T& value)
{
PARALLEL_CRITICAL(parameter_add)
{
bool created(false);
boost::shared_ptr<Parameter> param = retrieveParameter(created, type, comp, name);
ParameterType<T> *paramT = dynamic_cast<ParameterType<T> *>(param.get());
if (!paramT)
{
throw std::runtime_error("Error in adding parameter: incompatible types");
}
paramT->setValue(value);
if( created )
{
m_map.insert(std::make_pair(comp->getComponentID(),param));
}
}
auto param = ParameterFactory::create(type,name);
auto typedParam = boost::dynamic_pointer_cast<ParameterType<T>>(param);
assert(typedParam); // If not true the factory has created the wrong type
typedParam->setValue(value);
this->add(comp, param);
}
/// Method for adding a parameter providing shared pointer to it. The class stores share pointer and increment ref count to it
void add(const IComponent* comp,const boost::shared_ptr<Parameter> &param);
void add(const IComponent* comp, const boost::shared_ptr<Parameter> &param);

/** @name Helper methods for adding and updating parameter types */
/// Create or adjust "pos" parameter for a component
void addPositionCoordinate(const IComponent* comp,const std::string& name, const double value);
Expand Down
20 changes: 8 additions & 12 deletions Code/Mantid/Framework/Geometry/src/Instrument/ParameterMap.cpp
Expand Up @@ -235,30 +235,26 @@ namespace Mantid
void ParameterMap::add(const std::string& type,const IComponent* comp,const std::string& name,
const std::string& value)
{
PARALLEL_CRITICAL(parameter_add)
{
bool created(false);
boost::shared_ptr<Parameter> param = retrieveParameter(created, type, comp, name);
param->fromString(value);
if( created )
{
m_map.insert(std::make_pair(comp->getComponentID(),param));
}
}
auto param = ParameterFactory::create(type,name);
param->fromString(value);
this->add(comp, param);
}

/** Method for adding a parameter providing shared pointer to it.
/** Method for adding/replacing a parameter providing shared pointer to it.
* @param comp :: A pointer to the component that this parameter is attached to
* @param par :: a shared pointer to existing parameter. The ParameterMap stores share pointer and increment ref count to it
*/
void ParameterMap::add(const IComponent* comp,const boost::shared_ptr<Parameter> &par)
void ParameterMap::add(const IComponent* comp, const boost::shared_ptr<Parameter> & par)
{
// can not add null pointer
if(!par)return;

PARALLEL_CRITICAL(parameter_add)
{
auto existing_par = positionOf(comp,par->name().c_str(),"");
// As this is only an add method it should really throw if it already exists.
// However, this is old behaviour and many things rely on this actually be an
// add/replace-style function
if (existing_par != m_map.end())
{
existing_par->second = par;
Expand Down

0 comments on commit 395dbcc

Please sign in to comment.