IPropertyManager.cpp 5.33 KB
Newer Older
1
2
3
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4
5
//   NScD Oak Ridge National Laboratory, European Spallation Source,
//   Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6
// SPDX - License - Identifier: GPL - 3.0 +
7
#include "MantidKernel/IPropertyManager.h"
Simon Heybrock's avatar
Simon Heybrock committed
8
#include "MantidKernel/IPropertySettings.h"
Hahn, Steven's avatar
Hahn, Steven committed
9
#include "MantidKernel/OptionalBool.h"
10

11
///@cond
12
13
14
15
16
17
18
19
DEFINE_IPROPERTYMANAGER_GETVALUE(int16_t)
DEFINE_IPROPERTYMANAGER_GETVALUE(uint16_t)
DEFINE_IPROPERTYMANAGER_GETVALUE(int32_t)
DEFINE_IPROPERTYMANAGER_GETVALUE(uint32_t)
DEFINE_IPROPERTYMANAGER_GETVALUE(int64_t)
DEFINE_IPROPERTYMANAGER_GETVALUE(uint64_t)
DEFINE_IPROPERTYMANAGER_GETVALUE(bool)
DEFINE_IPROPERTYMANAGER_GETVALUE(double)
20
DEFINE_IPROPERTYMANAGER_GETVALUE(OptionalBool)
21
22
23
24
25
26
27
28
29
DEFINE_IPROPERTYMANAGER_GETVALUE(std::vector<int16_t>)
DEFINE_IPROPERTYMANAGER_GETVALUE(std::vector<uint16_t>)
DEFINE_IPROPERTYMANAGER_GETVALUE(std::vector<int32_t>)
DEFINE_IPROPERTYMANAGER_GETVALUE(std::vector<uint32_t>)
DEFINE_IPROPERTYMANAGER_GETVALUE(std::vector<int64_t>)
DEFINE_IPROPERTYMANAGER_GETVALUE(std::vector<uint64_t>)
DEFINE_IPROPERTYMANAGER_GETVALUE(std::vector<double>)
DEFINE_IPROPERTYMANAGER_GETVALUE(std::vector<std::string>)
DEFINE_IPROPERTYMANAGER_GETVALUE(std::vector<std::vector<std::string>>)
30

31
namespace Mantid::Kernel {
32
33
34
35
// This template implementation has been left in because although you can't
// assign to an existing string
// via the getProperty() method, you can construct a local variable by saying,
// e.g.: std::string s = getProperty("myProperty")
Samuel Jones's avatar
Samuel Jones committed
36
template <> DLLExport std::string IPropertyManager::getValue<std::string>(const std::string &name) const {
37
38
  return getPropertyValue(name);
}
39

Samuel Jones's avatar
Samuel Jones committed
40
template <> DLLExport Property *IPropertyManager::getValue<Property *>(const std::string &name) const {
41
42
  return getPointerToProperty(name);
}
43

44
45
// If a string is given in the argument, we can be more flexible
template <>
Samuel Jones's avatar
Samuel Jones committed
46
IPropertyManager *IPropertyManager::setProperty<std::string>(const std::string &name, const std::string &value) {
47
48
49
  this->setPropertyValue(name, value);
  return this;
}
50

51
52
53
54
55
56
/**
 * @param name The name of the property being looked for.
 * @return True if the property is managed by this.
 */
bool IPropertyManager::existsProperty(const std::string &name) const {
  auto props = this->getProperties();
Samuel Jones's avatar
Samuel Jones committed
57
  for (std::vector<Property *>::const_iterator prop = props.begin(); prop != props.end(); ++prop) {
58
59
60
61
62
    if (name == (*prop)->name())
      return true;
  }
  return false;
}
63

64
65
66
67
68
69
70
/**
 * Set values of the properties existing in this manager to the values of
 * properties with the same name in another manger.
 * @param other A property manager to copy property values from.
 */
void IPropertyManager::updatePropertyValues(const IPropertyManager &other) {
  auto props = this->getProperties();
71
72
  for (auto &prop : props) {
    const std::string propName = (*prop).name();
73
    if (other.existsProperty(propName)) {
74
      (*prop).setValueFromProperty(*other.getPointerToProperty(propName));
75
    }
76
77
  }
}
78

Simon Heybrock's avatar
Simon Heybrock committed
79
80
81
82
83
/** Give settings to a property to determine when it gets enabled/hidden.
 * Passes ownership of the given IPropertySettings object to the named
 * property
 * @param name :: property name
 * @param settings :: IPropertySettings     */
Samuel Jones's avatar
Samuel Jones committed
84
void IPropertyManager::setPropertySettings(const std::string &name, std::unique_ptr<IPropertySettings> settings) {
Simon Heybrock's avatar
Simon Heybrock committed
85
86
87
88
89
  Property *prop = getPointerToProperty(name);
  if (prop)
    prop->setSettings(std::move(settings));
}

90
91
92
93
/**
 * Get all properties in a group.
 * @param group Name of a group.
 */
Samuel Jones's avatar
Samuel Jones committed
94
std::vector<Property *> IPropertyManager::getPropertiesInGroup(const std::string &group) const {
95
96
97
98
99
100
  auto props = getProperties();
  for (auto prop = props.begin(); prop != props.end();) {
    if ((**prop).getGroup() == group) {
      ++prop;
    } else {
      prop = props.erase(prop);
101
    }
102
103
104
  }
  return props;
}
105

106
107
// Definitions for TypedValue cast operators
// Have to come after getValue definitions above to keep MSVS2010 happy
Samuel Jones's avatar
Samuel Jones committed
108
109
110
111
112
113
114
115
116
117
118
IPropertyManager::TypedValue::operator int16_t() { return pm.getValue<int16_t>(prop); }
IPropertyManager::TypedValue::operator uint16_t() { return pm.getValue<uint16_t>(prop); }
IPropertyManager::TypedValue::operator int32_t() { return pm.getValue<int32_t>(prop); }
IPropertyManager::TypedValue::operator uint32_t() { return pm.getValue<uint32_t>(prop); }
IPropertyManager::TypedValue::operator int64_t() { return pm.getValue<int64_t>(prop); }
IPropertyManager::TypedValue::operator uint64_t() { return pm.getValue<uint64_t>(prop); }
IPropertyManager::TypedValue::operator bool() { return pm.getValue<bool>(prop); }
IPropertyManager::TypedValue::operator double() { return pm.getValue<double>(prop); }
IPropertyManager::TypedValue::operator std::string() { return pm.getPropertyValue(prop); }
IPropertyManager::TypedValue::operator OptionalBool() { return pm.getValue<OptionalBool>(prop); }
IPropertyManager::TypedValue::operator Property *() { return pm.getPointerToProperty(prop); }
119

120
#ifdef __APPLE__
121
122
// These must precede the operator() declaration, temporarily switch batch to global namespace
}
123
124
DEFINE_IPROPERTYMANAGER_GETVALUE(unsigned long);
DEFINE_IPROPERTYMANAGER_GETVALUE(std::vector<unsigned long>);
125
// Intel 64-bit size_t
126
namespace Mantid::Kernel {
Samuel Jones's avatar
Samuel Jones committed
127
IPropertyManager::TypedValue::operator unsigned long() { return pm.getValue<unsigned long>(prop); }
128
#endif
129
} // namespace Mantid::Kernel
130
/// @endcond