MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
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> class ListField : public Field
26{
27public:
29 ListField() = default;
30
32 ListField(const std::string& name)
33 {
34 setName(name);
35 }
36
38 size_t size() const
39 {
40 return _values.size();
41 }
42
44 void clear()
45 {
46 _values.clear();
47 }
48
50 void setValues(const std::vector<T>& values)
51 {
52 _values = values;
53 touch();
54 }
55
58 void updateValues(const std::vector<T>& values)
59 {
60 if (values != _values) {
61 setValues(values);
62 }
63 }
64
66 std::vector<T> getValues() const
67 {
68 return _values;
69 }
70
73 void setValueAt(size_t index, T value)
74 {
75 if (index >= _values.size()) {
76 _values.resize(index + 1);
77 }
78 _values[index] = value;
79 touch();
80 }
81
84 void updateValueAt(size_t index, T value)
85 {
86 if ((index >= _values.size()) || (_values[index] != value)) {
87 setValueAt(index, value);
88 }
89 }
90
92 T getValueAt(size_t index) const
93 {
94 return _values[index];
95 }
96
98 std::string getStringValue() const override
99 {
100 std::string result;
101 if (_values.size() != 1) {
102 result.append("[ ");
103 }
104 bool first = true;
105 for (const T& value : _values) {
106 if (!first) {
107 result.append(", ");
108 }
109 else {
110 first = false;
111 }
112 result.append(ValuePersistence::valueToString(value));
113 }
114 if (_values.size() != 1) {
115 result.append(" ]");
116 }
117 return result;
118 }
119
121 void setStringValue(const std::string& value) override
122 {
123 size_t endPos = std::min(value.find("]"), value.length());
124 _values.clear();
125 if (endPos > 1 && value[0] == '[') {
126 size_t pos = 1;
127 if (!isWhitespace(value, pos, endPos)) {
128 while (pos < endPos) {
129 size_t commaPos = std::min(value.find(",", pos), endPos);
130 T comp;
131 ValuePersistence::stringToValue(value.substr(pos, commaPos - pos), comp);
132 _values.push_back(comp);
133 pos = commaPos + 1;
134 }
135 }
136 }
137 else {
138 T comp;
139 ValuePersistence::stringToValue(value, comp);
140 _values.push_back(comp);
141 }
142 touch();
143 }
144
145private:
146 static bool isWhitespace(const std::string& s, size_t start, size_t end)
147 {
148 for (size_t i = start; i < end; i++) {
149 if (!::isspace(static_cast<int>(s[i]))) {
150 return false;
151 }
152 }
153 return true;
154 }
155
156 std::vector<T> _values;
157};
158
@ 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:66
void setValueAt(size_t index, T value)
Sets the field value at index index to value.
Definition mlListField.h:73
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:98
void clear()
Clears the value of this field.
Definition mlListField.h:44
size_t size() const
Returns the number of elements of this fields value.
Definition mlListField.h:38
void setValues(const std::vector< T > &values)
Sets the field value to values.
Definition mlListField.h:50
void updateValueAt(size_t index, T value)
Sets the field value at index index to value, but only touches the field if the new value is differen...
Definition mlListField.h:84
ListField(const std::string &name)
Constructor; creates a list field with a name.
Definition mlListField.h:32
ListField()=default
Default constructor, do not use it.
T getValueAt(size_t index) const
Returns component value at index index. index must be in range.
Definition mlListField.h:92
void updateValues(const std::vector< T > &values)
Sets the field value to values, but only touches the field if the new values are different from the o...
Definition mlListField.h:58
Target mlrange_cast(Source arg)
Generic version of checked ML casts.