MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
mlMultiFields.h
Go to the documentation of this file.
1/*************************************************************************************
2**
3** Copyright 2007, 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#ifndef ML_MULTI_FIELDS_H
14#define ML_MULTI_FIELDS_H
15
16
19
20#include "MLToolsSystem.h"
21
23
24#include <mlField.h>
25#include <sstream>
26
28
29//-------------------------------------------------------------------------
31//-------------------------------------------------------------------------
32
33//-------------------------------------------------------------------------
35//-------------------------------------------------------------------------
36template <class DATATYPE> inline std::ostream& operator<<(std::ostream& s, const std::vector<DATATYPE>& vec)
37{
38 s.precision(8);
39 for (size_t i = 0; i < vec.size(); i++) {
40 s << vec[i] << " ";
41 }
42 return s;
43}
44
45//-------------------------------------------------------------------------
47//-------------------------------------------------------------------------
48template <class DATATYPE> inline std::istream& operator>>(std::istream& s, std::vector<DATATYPE>& vec)
49{
50 vec.clear();
51 DATATYPE i = {};
52 while (s >> i) {
53 vec.push_back(i);
54 }
55 return s;
56}
57
58
59//----------------------------------------------------------------------------------
61//----------------------------------------------------------------------------------
62template <class DATATYPE> class MultiField : public Field
63{
64
65public:
66 //----------------------------------------------------------------------------
70 //----------------------------------------------------------------------------
71 inline MultiField(const std::string& name = "")
72 : Field(name)
73 {}
74
75 //----------------------------------------------------------------------------
80 //----------------------------------------------------------------------------
81 inline void setStringValue(const std::string& value) override
82 {
83 std::istringstream ist(value);
84 ist >> _fields;
85 Field::touch();
86 }
87
88 //----------------------------------------------------------------------------
90 //----------------------------------------------------------------------------
91 inline void setMultiField(const std::vector<DATATYPE>& value)
92 {
93 _fields = value;
94 Field::touch();
95 }
96
97 //----------------------------------------------------------------------------
101 //----------------------------------------------------------------------------
102 inline void setMultiField(const DATATYPE values[], size_t numValues)
103 {
104 if (nullptr == values) {
105 printTemplateFatalError("MultiField::setMultiField", ML_BAD_POINTER_OR_0, "Ignoring function call");
106 }
107 else {
108 // Be sure that vector has enough entries.
109 _fields.resize(numValues);
110
111 // Copy values into field vector.
112 for (size_t c = 0; c < numValues; ++c) {
113 _fields[c] = values[c];
114 }
115
116 // Sent change signal to observers.
117 Field::touch();
118 }
119 }
120
121 //----------------------------------------------------------------------------
129 //----------------------------------------------------------------------------
130 inline void getMultiField(DATATYPE values[], size_t numValues)
131 {
132 if (nullptr == values) {
133 printTemplateFatalError("MultiField::getMultiField", ML_BAD_POINTER_OR_0, "Ignoring function call");
134 }
135 else {
136 // Read at most the number of values available in the fields.
137 if (numValues > _fields.size()) {
138 numValues = _fields.size();
139 }
140
141 // Copy values to return array.
142 for (size_t c = 0; c < numValues; ++c) {
143 values[c] = _fields[c];
144 }
145 }
146 }
147
148 //----------------------------------------------------------------------------
150 //----------------------------------------------------------------------------
151 inline std::string getStringValue() const override
152 {
153 std::ostringstream ost;
154 ost << _fields;
155 return ost.str();
156 }
157
158 //----------------------------------------------------------------------------
160 //----------------------------------------------------------------------------
161 inline const std::vector<DATATYPE>& getMultiField() const
162 {
163 return _fields;
164 }
165
166 //----------------------------------------------------------------------------
168 //----------------------------------------------------------------------------
169 inline std::vector<DATATYPE>& getNonConstMultiField()
170 {
171 return _fields;
172 }
173
174 //----------------------------------------------------------------------------
176 //----------------------------------------------------------------------------
177 inline size_t getSize() const
178 {
179 return _fields.size();
180 }
181
182private:
184 std::vector<DATATYPE> _fields;
185};
186
187//-------------------------------------------------------------------------
188// Instantiations of different multi fields
189//-------------------------------------------------------------------------
190
191#if !defined(_WIN32) && !defined(WIN32)
192
193// Needed to force instantiation of these types, because under linux
194// otherwise link problems may occur, because symbols of these specializations
195// otherwise are not created. See Stroustrup, 13, 2.7.
196template class MultiField<MLdouble>;
197template class MultiField<MLfloat>;
198template class MultiField<MLint>;
199template class MultiField<MLint32>;
200template class MultiField<MLuint32>;
201
202#endif
203
204
205#ifdef WIN32
207#pragma warning(push)
208
212#pragma warning(disable : 4275)
213#endif
214
215//-------------------------------------------------------------------------
216//-------------------------------------------------------------------------
217// Instantiations of different multi fields
218//-------------------------------------------------------------------------
221{
222public:
224 DoubleMultiField(const std::string& name = "");
225
229};
230
231//-------------------------------------------------------------------------
233//-------------------------------------------------------------------------
235{
236public:
238 FloatMultiField(const std::string& name = "");
239
243};
244
245//-------------------------------------------------------------------------
247//-------------------------------------------------------------------------
249{
250public:
252 IntMultiField(const std::string& name = "");
253
257};
258
259//-------------------------------------------------------------------------
261//-------------------------------------------------------------------------
263{
264public:
266 Int32MultiField(const std::string& name = "");
267
271};
272
273//-------------------------------------------------------------------------
275//-------------------------------------------------------------------------
277{
278public:
280 UInt32MultiField(const std::string& name = "");
281
285};
286
287#ifdef WIN32
289#pragma warning(pop)
290#endif
291
293
294#endif //__mlMultiFields_H
#define MLTOOLS_EXPORT
Resolves system dependencies for this project.
Save warning state.
ML_CLASS_HEADER(DoubleMultiField)
Macro to implement the interface for the Runtime Type System and for the initialization of this class...
DoubleMultiField(const std::string &name="")
Constructor. See constructor of MultiField for documentation.
Base class for all fields used in the ML.
Definition mlField.h:73
Class FloatMultiField. See MultiField for documentation.
FloatMultiField(const std::string &name="")
Constructor. See constructor of MultiField for documentation.
ML_CLASS_HEADER(FloatMultiField)
Macro to implement the interface for the Runtime Type System and for the initialization of this class...
Class Int32MultiField. See MultiField for documentation.
ML_CLASS_HEADER(Int32MultiField)
Macro to implement the interface for the Runtime Type System and for the initialization of this class...
Int32MultiField(const std::string &name="")
Constructor. See constructor of MultiField for documentation.
Class IntMultiField. See MultiField for documentation.
ML_CLASS_HEADER(IntMultiField)
Macro to implement the interface for the Runtime Type System and for the initialization of this class...
IntMultiField(const std::string &name="")
Constructor. See constructor of MultiField for documentation.
Field to represent a dynamic vector of DATATYPE values.
void setMultiField(const std::vector< DATATYPE > &value)
Set values of the field to value which is a std::vector.
std::vector< DATATYPE > & getNonConstMultiField()
Returns the vector of the field as modifiable reference.
void setMultiField(const DATATYPE values[], size_t numValues)
Set values of the field to values which is an array of values.
const std::vector< DATATYPE > & getMultiField() const
Returns the vector of the field.
void setStringValue(const std::string &value) override
Set value to value.
std::string getStringValue() const override
Return the value as string value.
size_t getSize() const
Returns number of entries in vector.
void getMultiField(DATATYPE values[], size_t numValues)
Get values from the field into values which is an array of values.
MultiField(const std::string &name="")
Empty Constructor: Create a field with name name.
Class UInt32MultiField. See MultiField for documentation.
UInt32MultiField(const std::string &name="")
Constructor. See constructor of MultiField for documentation.
ML_CLASS_HEADER(UInt32MultiField)
Macro to implement the interface for the Runtime Type System and for the initialization of this class...
#define ML_BAD_POINTER_OR_0
A pointer is NULL or a value is NULL or 0 where it should not be.
Definition mlTypeDefs.h:989
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
std::ostream & operator<<(std::ostream &out, const ml::Variant &variant)
Definition mlVariant.h:231
std::istream & operator>>(std::istream &in, ml::Variant &variant)
Definition mlVariant.h:236
void ML_UTILS_EXPORT printTemplateFatalError(const char *location, MLErrorCode reason, const std::string_view &handling)