20 #ifndef MIRTK_Object_H 21 #define MIRTK_Object_H 23 #include "mirtk/Array.h" 24 #include "mirtk/Exception.h" 25 #include "mirtk/Pair.h" 26 #include "mirtk/String.h" 27 #include "mirtk/Stream.h" 39 typedef ParameterList::iterator ParameterIterator;
40 typedef ParameterList::const_iterator ParameterConstIterator;
71 virtual bool Set(
const char *name,
const char *value);
97 template <
typename... Args>
113 template <
typename... Args>
129 return "mirtk::Object";
148 for (ParameterConstIterator it = param.begin(); it != param.end(); ++it) {
149 ok = this->
Set(it->first.c_str(), it->second.c_str()) && ok;
155 template <
typename... Args>
165 template <
typename... Args>
177 inline ParameterConstIterator
Find(
const ParameterList ¶ms,
string name)
179 ParameterConstIterator it = params.begin();
180 while (it != params.end() && it->first != name) ++it;
186 inline ParameterIterator
Find(ParameterList ¶ms,
string name)
188 ParameterIterator it = params.begin();
189 while (it != params.end() && it->first != name) ++it;
195 inline bool Contains(
const ParameterList ¶ms,
string name)
197 return Find(params, name) != params.end();
202 inline string Get(
const ParameterList ¶ms,
string name)
204 ParameterConstIterator pos =
Find(params, name);
205 if (pos == params.end())
return string(
"");
212 inline ParameterList &
Insert(ParameterList ¶ms,
string name, T value)
214 ParameterIterator pos =
Find(params, name);
215 if (pos == params.end()) {
216 params.push_back(make_pair(name,
ToString(value)));
226 inline ParameterList &
Insert(ParameterList ¶ms,
string name,
const char *value)
228 ParameterIterator pos =
Find(params, name);
229 if (pos == params.end()) {
230 params.push_back(make_pair(name,
string(value)));
240 inline ParameterList &
Insert(ParameterList ¶ms,
string name,
string value)
242 ParameterIterator pos =
Find(params, name);
243 if (pos == params.end()) {
244 params.push_back(make_pair(name, value));
253 inline ParameterList &
Insert(ParameterList ¶ms,
254 const ParameterList &other,
255 const char *prefix = NULL)
259 for (ParameterConstIterator it = other.begin(); it != other.end(); ++it) {
261 name[0] = ::tolower(name[0]);
262 Insert(params,
string(prefix) +
" " + name, it->second);
265 for (ParameterConstIterator it = other.begin(); it != other.end(); ++it) {
266 Insert(params, it->first, it->second);
274 inline ParameterList &
Remove(ParameterList ¶ms,
string name)
276 ParameterIterator pos =
Find(params, name);
277 if (pos != params.end()) params.erase(pos);
287 #define mirtkAbstractMacro(name) \ 290 inline static const char *NameOfType() { return #name; } \ 292 virtual const char *NameOfClass() const = 0; \ 297 #define mirtkObjectMacro(name) \ 300 inline static const char *NameOfType() { return #name; } \ 302 inline virtual const char *NameOfClass() const { return #name; } \ 309 #define mirtkMutableObjectMacro(name) \ 312 inline static const char *NameOfType() { return #name; } \ 314 virtual const char *NameOfClass() const; \ 320 #define mirtkSetMacro(name, type) \ 321 virtual void Set##name(type arg) { this->_##name = arg; } 326 #define mirtkGetMacro(name, type) \ 327 type Get##name() const { return this->_##name; } 332 #define mirtkOnOffMacro(name) \ 333 virtual void name##On() { this->_##name = true; } \ 334 virtual void name##Off() { this->_##name = false; } 338 #define mirtkDefineReadOnlyAttributeMacro(typedecl, type, name) \ 343 inline type &name() { return _##name; } \ 345 inline const type &name() const { return _##name; } \ 350 #define mirtkDefineAttributeMacro(access, typedecl, type, name) \ 355 inline virtual void name(type arg) { _##name = arg; } \ 357 inline type &name() { return _##name; } \ 359 inline const type &name() const { return _##name; } \ 364 #define mirtkPublicAttributeMacro(type, name) \ 365 mirtkDefineAttributeMacro(public, type, type, name) 367 #define mirtkReadOnlyAttributeMacro(type, name) \ 368 mirtkDefineReadOnlyAttributeMacro(type, type, name) 370 #define mirtkPublicMutableAttributeMacro(type, name) \ 371 mirtkDefineAttributeMacro(public, mutable type, type, name) 375 #define mirtkAttributeMacro(type, name) \ 376 mirtkDefineAttributeMacro(protected, type, type, name) 378 #define mirtkMutableAttributeMacro(type, name) \ 379 mirtkDefineAttributeMacro(protected, mutable type, type, name) 383 #define mirtkDefineAggregateMacro(access, type, name) \ 388 inline virtual void name(type *arg) { _##name = arg; } \ 390 inline type *name() const { return _##name; } \ 395 #define mirtkDefineReadOnlyAggregateMacro(access, type, name) \ 400 inline type *name() const { return _##name; } \ 405 #define mirtkPublicAggregateMacro(type, name) \ 406 mirtkDefineAggregateMacro(public, type, name) 408 #define mirtkReadOnlyAggregateMacro(type, name) \ 409 mirtkDefineReadOnlyAggregateMacro(public, type, name) 413 #define mirtkAggregateMacro(type, name) \ 414 mirtkDefineAggregateMacro(protected, type, name) 418 #define mirtkDefineComponentMacro(access, type, name) \ 423 inline virtual void name(type *arg) { delete _##name; _##name = arg; } \ 425 inline type *name() { return _##name; } \ 427 inline const type *name() const { return _##name; } \ 432 #define mirtkPublicComponentMacro(type, name) \ 433 mirtkDefineComponentMacro(public, type, name) 435 #define mirtkReadOnlyComponentMacro(type, name) \ 436 mirtkDefineReadOnlyAggregateMacro(public, type, name) 440 #define mirtkComponentMacro(type, name) \ 441 mirtkDefineComponentMacro(protected, type, name) 458 #define mirtkLooseComponentMacro(type, name) \ 461 bool _##name##Owner; \ 464 inline virtual void name(type *arg) { \ 465 if (_##name##Owner) delete _##name; \ 468 _##name##Owner = false; \ 470 _##name = new type(*_##name); \ 471 _##name##Owner = true; \ 475 inline type *name() { return _##name; } \ 477 inline const type *name() const { return _##name; } \ 483 #endif // MIRTK_Object_H static void ThrowStatic(ErrorType err, const char *cls, const char *func, Args... args)
static const char * NameOfType()
Get name of this class type.
Array< T >::iterator Find(Array< T > &values, const T &value)
Find value in unsorted Array.
string Get(const ParameterList ¶ms, string name)
Get parameter value from parameters list.
bool Contains(const ParameterList ¶ms, string name)
Whether parameter is in parameters list.
Array< Pair< string, string > > ParameterList
Ordered list of parameter name/value pairs.
virtual bool Set(const char *name, const char *value)
virtual ~Object()
Destructor.
string ToString(const EnergyMeasure &value, int w, char c, bool left)
Convert energy measure enumeration value to string.
ParameterList & Remove(ParameterList ¶ms, string name)
Remove parameter from parameters list.
void Throw(ErrorType err, const char *func, Args... args) const
void Throw(ErrorType err, const char *func, Args... args)
virtual const char * NameOfClass() const =0
Get name of class, which this object is an instance of.
virtual ParameterList Parameter() const
Get parameter name/value pairs.
ParameterList & Insert(ParameterList ¶ms, string name, T value)
Insert/replace value into/in parameters list.
ErrorType
Enumeration of error types / exit codes.