View on GitHub

Vistle

Distributed Data-parallel Scientific Visualization in VR

parameter.h
Go to the documentation of this file.
1#ifndef PARAMETER_H
2#define PARAMETER_H
3
4#include <string>
5#include <sstream>
6#include <iostream>
7#include <algorithm>
8#include <limits>
9#include <boost/mpl/vector.hpp>
10#include <boost/lexical_cast.hpp>
11
12#include <vistle/util/enum.h>
13#include "paramvector.h"
14#include "export.h"
15
16class VistleInteractor;
17
18namespace gui {
19class Parameters;
20}
21
22namespace vistle {
23
24typedef boost::mpl::vector<Integer, Float, ParamVector, std::string> Parameters;
25
27public:
29 (Unknown) // keep first
30 (Float)(Integer)(Vector)(IntVector)(String)(Invalid) // keep last
31 )
32
34 (Generic) // default, keep first
35 (Filename) // String
36 (ExistingFilename) // String
37 (Directory) // String
38 (ExistingDirectory) // String
39 (NewPathname) // String
40 (Boolean) // Integer
41 (Choice) // Integer (fixed choice) and String (dynamic choice)
42 (Slider) // Integer, Float
43 (Color) // Vector
44 (InvalidPresentation) // keep last
45 )
46
48 // keep in that order
49 (Minimum) // also used for filebrowser filters
50 (Value)(Maximum))
51
52 Parameter(int moduleId, const std::string &name, Type = Invalid, Presentation = Generic);
53 Parameter(const Parameter &other);
54 virtual ~Parameter();
55
56 virtual Parameter *clone() const = 0;
57
58 void setPresentation(Presentation presentation);
59 void setDescription(const std::string &description);
60 void setChoices(const std::vector<std::string> &choices);
61
62 void setGroup(const std::string &group);
63 const std::string &group() const;
64
65 virtual operator std::string() const = 0;
66 virtual bool isDefault() const = 0;
67 virtual bool checkChoice(const std::vector<std::string> &choices) const { return true; }
68 int module() const;
69 const std::string &getName() const;
70 Type type() const;
71 Presentation presentation() const;
72 const std::string &description() const;
73 const std::vector<std::string> &choices() const;
74
75protected:
76 std::vector<std::string> m_choices;
77
78private:
79 int m_module;
80 std::string m_name;
81 std::string m_description;
82 std::string m_group;
83 enum Type m_type;
84 enum Presentation m_presentation;
85};
86
87template<typename T>
88struct ParameterType {};
89
90template<typename T>
92 static bool check(const std::vector<std::string> &, const T &) { return true; }
93};
94
95namespace message {
96class SetParameter;
97}
98
99template<typename T>
101 friend class ParameterManager;
102 friend class Module;
103 friend class Renderer;
105 friend class VistleConnection;
106 friend class gui::Parameters;
107 friend class ::VistleInteractor;
108
109 virtual bool setValue(T value, bool init = false, bool delayed = false)
110 {
111 if (init)
112 m_defaultValue = value;
113 else if (!delayed && !checkValue(value))
114 return false;
115 this->m_value = value;
116 return true;
117 }
118 virtual void setMinimum(const T &value) { m_minimum = value; }
119 virtual void setMaximum(const T &value) { m_maximum = value; }
120
121public:
122 typedef T ValueType;
123
124 ParameterBase(int moduleId, const std::string &name, T value = T())
125 : Parameter(moduleId, name, ParameterType<T>::type)
126 , m_value(value)
127 , m_defaultValue(value)
128 , m_minimum(ParameterType<T>::min())
129 , m_maximum(ParameterType<T>::max())
130 {}
132 : Parameter(other)
133 , m_value(other.m_value)
134 , m_defaultValue(other.m_defaultValue)
135 , m_minimum(other.m_minimum)
136 , m_maximum(other.m_maximum)
137 {}
138 virtual ~ParameterBase() {}
139
140 virtual ParameterBase<T> *clone() const { return new ParameterBase<T>(*this); }
141
142 bool isDefault() const { return m_value == m_defaultValue; }
143 const T getDefaultValue() const { return m_defaultValue; }
144 const T getValue() const { return m_value; }
145 const T getValue(RangeType rt) const
146 {
147 switch (rt) {
148 case Minimum:
149 return m_minimum;
150 case Maximum:
151 return m_maximum;
152 case Value:;
153 }
154 return m_value;
155 }
156 virtual bool checkValue(const T &value) const
157 {
159 if (value < m_minimum)
160 return false;
161 if (value > m_maximum)
162 return false;
163 }
164 if (presentation() != Choice)
165 return true;
166 return ParameterCheck<T>::check(m_choices, value);
167 }
168 virtual bool checkChoice(const std::vector<std::string> &ch) const
169 {
170 if (presentation() != Choice)
171 return false;
172 return ParameterCheck<T>::check(ch, m_value);
173 }
174 virtual T minimum() const { return m_minimum; }
175 virtual T maximum() const { return m_maximum; }
176 operator std::string() const { return boost::lexical_cast<std::string>(m_value); }
177
178private:
179 T m_value;
180 T m_defaultValue;
181 T m_minimum, m_maximum;
182};
183
184template<>
186 typedef Integer T;
188 static const bool isNumber = true;
189 static T min() { return std::numeric_limits<T>::lowest(); }
190 static T max() { return std::numeric_limits<T>::max(); }
191};
192
193template<>
195 typedef Float T;
197 static const bool isNumber = true;
198 static T min() { return std::numeric_limits<T>::lowest(); }
199 static T max() { return std::numeric_limits<T>::max(); }
200};
201
202template<>
204 typedef ParamVector T;
206 static const bool isNumber = true;
207 static T min() { return T::min(); }
208 static T max() { return T::max(); }
209};
210
211template<>
214 static const Parameter::Type type = Parameter::IntVector;
215 static const bool isNumber = true;
216 static T min() { return T::min(); }
217 static T max() { return T::max(); }
218};
219
220template<>
221struct V_COREEXPORT ParameterType<std::string> {
222 typedef std::string T;
223 static const Parameter::Type type = Parameter::String;
224 static const bool isNumber = false;
225 static T min() { return ""; }
226 static T max() { return ""; }
227};
228
229template<>
231 static bool check(const std::vector<std::string> &choices, const Integer &value)
232 {
233 if (choices.empty()) {
234 // choices not yet initialized
235 return true;
236 }
237 if (value < 0 || size_t(value) >= choices.size()) {
238 std::cerr << "IntParameter: choice out of range" << std::endl;
239 return false;
240 }
241 return true;
242 }
243};
244
245template<>
246struct ParameterCheck<std::string> {
247 static bool check(const std::vector<std::string> &choices, const std::string &value)
248 {
249 if (choices.empty()) {
250 // choices not yet initialized
251 return true;
252 }
253 if (std::find(choices.begin(), choices.end(), value) == choices.end()) {
254 if (!value.empty())
255 std::cerr << "StringParameter: choice \"" << value << "\" not valid" << std::endl;
256 return false;
257 }
258 return true;
259 }
260};
261
262#define V_PARAM_TYPE(ValueType, Name) \
263 extern template class V_COREEXPORT ParameterBase<ValueType>; \
264 typedef ParameterBase<ValueType> Name;
265V_PARAM_TYPE(ParamVector, VectorParameter)
266V_PARAM_TYPE(IntParamVector, IntVectorParameter)
267V_PARAM_TYPE(Float, FloatParameter)
268V_PARAM_TYPE(Integer, IntParameter)
269V_PARAM_TYPE(std::string, StringParameter)
270#undef V_PARAM_TYPE
271
272V_ENUM_OUTPUT_OP(Type, Parameter)
273V_ENUM_OUTPUT_OP(Presentation, Parameter)
274V_ENUM_OUTPUT_OP(RangeType, Parameter)
275
276} // namespace vistle
277#endif
Definition: module.h:116
Definition: parameter.h:100
const T getValue(RangeType rt) const
Definition: parameter.h:145
const T getValue() const
Definition: parameter.h:144
ParameterBase(int moduleId, const std::string &name, T value=T())
Definition: parameter.h:124
const T getDefaultValue() const
Definition: parameter.h:143
virtual T minimum() const
Definition: parameter.h:174
virtual ~ParameterBase()
Definition: parameter.h:138
T ValueType
Definition: parameter.h:122
friend class Renderer
Definition: parameter.h:103
virtual T maximum() const
Definition: parameter.h:175
virtual bool checkValue(const T &value) const
Definition: parameter.h:156
friend class VistleConnection
Definition: parameter.h:105
bool isDefault() const
Definition: parameter.h:142
virtual bool checkChoice(const std::vector< std::string > &ch) const
Definition: parameter.h:168
ParameterBase(const ParameterBase< T > &other)
Definition: parameter.h:131
virtual ParameterBase< T > * clone() const
Definition: parameter.h:140
Definition: parametermanager.h:16
Definition: paramvector.h:18
Definition: parameter.h:26
Minimum() const std::string Type
Definition: parameter.h:52
std::vector< std::string > m_choices
Definition: parameter.h:76
DEFINE_ENUM_WITH_STRING_CONVERSIONS(Type,(Unknown)(Float)(Integer)(Vector)(IntVector)(String)(Invalid)) DEFINE_ENUM_WITH_STRING_CONVERSIONS(Presentation
Minimum() const std::string & name
Definition: parameter.h:52
Minimum() Value(Maximum)) Parameter(int moduleId
Type type() const
Definition: parameter.cpp:54
Presentation presentation() const
Definition: parameter.cpp:64
request parameter value update or notify that a parameter value has been changed
Definition: messages.h:501
#define V_COREEXPORT
Definition: export.h:9
#define V_ENUM_OUTPUT_OP(name, scope)
Definition: enum.h:72
Definition: parameter.h:18
std::string module(const std::string &prefix)
Definition: directory.cpp:61
static T min(T a, T b)
Definition: messages.cpp:28
Definition: allobjects.cpp:30
ParameterVector< Integer > IntParamVector
Definition: paramvector.h:188
double Float
Definition: scalar.h:18
Vector3 Vector
Definition: vector.h:36
DEFINE_ENUM_WITH_STRING_CONVERSIONS(FieldCompressionMode,(Uncompressed)(ZfpFixedRate)(ZfpAccuracy)(ZfpPrecision)) namespace detail
Definition: archives_config.h:38
boost::mpl::vector< Integer, Float, ParamVector, std::string > Parameters
Definition: parameter.h:24
int64_t Integer
Definition: scalar.h:19
ParameterVector< Float > ParamVector
Definition: paramvector.h:187
Definition: parameter.h:88
#define V_PARAM_TYPE(ValueType, Name)
Definition: parameter.h:262
static bool check(const std::vector< std::string > &choices, const Integer &value)
Definition: parameter.h:231
static bool check(const std::vector< std::string > &choices, const std::string &value)
Definition: parameter.h:247
Definition: parameter.h:91
static bool check(const std::vector< std::string > &, const T &)
Definition: parameter.h:92
static T max()
Definition: parameter.h:199
static T min()
Definition: parameter.h:198
Float T
Definition: parameter.h:195
static T max()
Definition: parameter.h:217
IntParamVector T
Definition: parameter.h:213
static T min()
Definition: parameter.h:216
static T max()
Definition: parameter.h:190
Integer T
Definition: parameter.h:186
static T min()
Definition: parameter.h:189
ParamVector T
Definition: parameter.h:204
static T max()
Definition: parameter.h:208
static T min()
Definition: parameter.h:207
static T max()
Definition: parameter.h:226
std::string T
Definition: parameter.h:222
static T min()
Definition: parameter.h:225