MeVisLab Toolbox Reference
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 //-------------------------------------------------------------------------
36 template <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 //-------------------------------------------------------------------------
48 template <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 //----------------------------------------------------------------------------------
62 template <class DATATYPE> class MultiField : public Field {
63
64 public:
65 //----------------------------------------------------------------------------
69 //----------------------------------------------------------------------------
70 inline MultiField(const std::string &name="") : Field(name)
71 {
72 }
73
74 //----------------------------------------------------------------------------
79 //----------------------------------------------------------------------------
80 inline void setStringValue(const std::string &value) override
81 {
82 std::istringstream ist(value);
83 ist >> _fields;
84 Field::touch();
85 }
86
87 //----------------------------------------------------------------------------
89 //----------------------------------------------------------------------------
90 inline void setMultiField(const std::vector<DATATYPE> &value)
91 {
92 _fields = value;
93 Field::touch();
94 }
95
96 //----------------------------------------------------------------------------
100 //----------------------------------------------------------------------------
101 inline void setMultiField(const DATATYPE values[], size_t numValues)
102 {
103 if (nullptr == values){
104 printTemplateFatalError("MultiField::setMultiField",
106 "Ignoring function call");
107 }
108 else{
109 // Be sure that vector has enough entries.
110 _fields.resize(numValues);
111
112 // Copy values into field vector.
113 for (size_t c=0; c < numValues; ++c){ _fields[c] = values[c]; }
114
115 // Sent change signal to observers.
116 Field::touch();
117 }
118 }
119
120 //----------------------------------------------------------------------------
128 //----------------------------------------------------------------------------
129 inline void getMultiField(DATATYPE values[], size_t numValues)
130 {
131 if (nullptr == values){
132 printTemplateFatalError("MultiField::getMultiField",
134 "Ignoring function call");
135 }
136 else{
137 // Read at most the number of values available in the fields.
138 if (numValues > _fields.size()){ numValues = _fields.size(); }
139
140 // Copy values to return array.
141 for (size_t c=0; c < numValues; ++c){ values[c] = _fields[c]; }
142 }
143 }
144
145 //----------------------------------------------------------------------------
147 //----------------------------------------------------------------------------
148 inline std::string getStringValue() const override
149 {
150 std::ostringstream ost;
151 ost << _fields;
152 return ost.str();
153 }
154
155 //----------------------------------------------------------------------------
157 //----------------------------------------------------------------------------
158 inline const std::vector<DATATYPE>& getMultiField() const
159 {
160 return _fields;
161 }
162
163 //----------------------------------------------------------------------------
165 //----------------------------------------------------------------------------
166 inline std::vector<DATATYPE>& getNonConstMultiField()
167 {
168 return _fields;
169 }
170
171 //----------------------------------------------------------------------------
173 //----------------------------------------------------------------------------
174 inline size_t getSize() const
175 {
176 return _fields.size();
177 }
178
179 private:
181 std::vector<DATATYPE> _fields;
182 };
183
184 //-------------------------------------------------------------------------
185 // Instantiations of different multi fields
186 //-------------------------------------------------------------------------
187
188#if !defined(_WIN32) && !defined(WIN32)
189
190 // Needed to force instantiation of these types, because under linux
191 // otherwise link problems may occur, because symbols of these specializations
192 // otherwise are not created. See Stroustrup, 13, 2.7.
193 template class MultiField<MLdouble>;
194 template class MultiField<MLfloat>;
195 template class MultiField<MLint>;
196 template class MultiField<MLint32>;
197 template class MultiField<MLuint32>;
198
199#endif
200
201
202#ifdef WIN32
204#pragma warning( push )
205
209#pragma warning(disable : 4275 )
210#endif
211
212 //-------------------------------------------------------------------------
213 //-------------------------------------------------------------------------
214 // Instantiations of different multi fields
215 //-------------------------------------------------------------------------
218 {
219 public:
221 DoubleMultiField(const std::string &name="");
222
226 };
227
228 //-------------------------------------------------------------------------
230 //-------------------------------------------------------------------------
232 {
233 public:
235 FloatMultiField(const std::string &name="");
236
240 };
241
242 //-------------------------------------------------------------------------
244 //-------------------------------------------------------------------------
246 {
247 public:
249 IntMultiField(const std::string &name="");
250
254 };
255
256 //-------------------------------------------------------------------------
258 //-------------------------------------------------------------------------
260 {
261 public:
263 Int32MultiField(const std::string &name="");
264
268 };
269
270 //-------------------------------------------------------------------------
272 //-------------------------------------------------------------------------
274 {
275 public:
277 UInt32MultiField(const std::string &name="");
278
282 };
283
284#ifdef WIN32
286#pragma warning( pop )
287#endif
288
290
291#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; this sometimes indicates a memory a...
Definition mlTypeDefs.h:933
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
std::ostream & operator<<(std::ostream &out, const ml::Variant &variant)
Definition mlVariant.h:210
std::istream & operator>>(std::istream &in, ml::Variant &variant)
Definition mlVariant.h:215
void ML_UTILS_EXPORT printTemplateFatalError(const char *location, MLErrorCode reason, const std::string_view &handling)