MeVisLab Toolbox Reference
mlListField.h
Go to the documentation of this file.
1/*************************************************************************************
2**
3** Copyright 2018, MeVis Medical Solutions AG
4**
5** The user may use this file in accordance with the license agreement provided with
6** the Software or, alternatively, in accordance with the terms contained in a
7** written agreement between the user and MeVis Medical Solutions AG.
8**
9** For further information use the contact form at https://www.mevislab.de/contact
10**
11**************************************************************************************/
12
13
15
16#pragma once
17
18#include "mlField.h"
19#include "mlValuePersistence.h"
20
21#include <vector>
22
24
25template<class T>
26class ListField : public Field
27{
28public:
30 ListField() = default;
31
33 ListField(const std::string& name) { setName(name); }
34
36 size_t size() const { return _values.size(); }
37
39 void clear() { _values.clear(); }
40
42 void setValues(const std::vector<T>& values) {
43 _values = values;
44 touch();
45 }
46
49 void updateValues(const std::vector<T>& values) {
50 if (values != _values) {
51 setValues(values);
52 }
53 }
54
56 std::vector<T> getValues() const { return _values; }
57
60 void setValueAt(size_t index, T value) {
61 if (index >= _values.size()) {
62 _values.resize(index+1);
63 }
64 _values[index] = value;
65 touch();
66 }
67
70 void updateValueAt(size_t index, T value) {
71 if ((index >= _values.size()) || (_values[index] != value)) {
72 setValueAt(index, value);
73 }
74 }
75
77 T getValueAt(size_t index) const {
78 return _values[index];
79 }
80
82 std::string getStringValue() const override
83 {
84 std::string result;
85 if (_values.size() != 1) {
86 result.append("[ ");
87 }
88 bool first = true;
89 for (const T& value : _values) {
90 if (!first) {
91 result.append(", ");
92 } else {
93 first = false;
94 }
95 result.append(ValuePersistence::valueToString(value));
96 }
97 if (_values.size() != 1) {
98 result.append(" ]");
99 }
100 return result;
101 }
102
104 void setStringValue(const std::string& value) override
105 {
106 size_t endPos = std::min(value.find("]"), value.length());
107 _values.clear();
108 if (endPos > 1 && value[0] == '[') {
109 size_t pos = 1;
110 if (!isWhitespace(value, pos, endPos)) {
111 while (pos < endPos) {
112 size_t commaPos = std::min(value.find(",", pos), endPos);
113 T comp;
114 ValuePersistence::stringToValue(value.substr(pos, commaPos-pos), comp);
115 _values.push_back(comp);
116 pos = commaPos+1;
117 }
118 }
119 }
120 else {
121 T comp;
122 ValuePersistence::stringToValue(value, comp);
123 _values.push_back(comp);
124 }
125 touch();
126 }
127
128private:
129 static bool isWhitespace(const std::string& s, size_t start, size_t end)
130 {
131 for (size_t i = start; i < end; i++) {
132 if (!::isspace(static_cast<int>(s[i]))) {
133 return false;
134 }
135 }
136 return true;
137 }
138
139 std::vector<T> _values;
140};
141
@ T
Base class for all fields used in the ML.
Definition mlField.h:73
std::vector< T > getValues() const
Returns the value of the field as a Boolean.
Definition mlListField.h:56
void setValueAt(size_t index, T value)
Sets the field value at index index to value.
Definition mlListField.h:60
void setStringValue(const std::string &value) override
Sets the value of the field to value.
std::string getStringValue() const override
Returns the value of the field as string value.
Definition mlListField.h:82
void clear()
Clear value of field.
Definition mlListField.h:39
size_t size() const
Return number of elements of this fields value.
Definition mlListField.h:36
void setValues(const std::vector< T > &values)
Sets the field value to values.
Definition mlListField.h:42
void updateValueAt(size_t index, T value)
Sets the field value at index index to value, but only touch field if the new value is different from...
Definition mlListField.h:70
ListField(const std::string &name)
Constructor, creates a list field with a name.
Definition mlListField.h:33
ListField()=default
Default constructor, do not use it.
T getValueAt(size_t index) const
Get component value at index index. index must be in range.
Definition mlListField.h:77
void updateValues(const std::vector< T > &values)
Sets the field value to values, but only touch field if the new values are different from the old val...
Definition mlListField.h:49
Target mlrange_cast(Source arg)
Generic version of checked ML casts.