Medical Imaging Interaction Toolkit  2026.06.00
Medical Imaging Interaction Toolkit
mitkGenericProperty.h
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifndef mitkGenericProperty_h
14 #define mitkGenericProperty_h
15 
16 #include <sstream>
17 #include <cstdlib>
18 #include <string>
19 
20 #include <mitkBaseProperty.h>
21 #include <mitkNumericTypes.h>
22 #include <MitkCoreExports.h>
23 
24 namespace mitk
25 {
45  template <typename T>
47  {
48  public:
51  itkCloneMacro(Self);
52 
54  typedef T ValueType;
55 
58  itkSetMacro(Value, T);
59 
62  itkGetConstMacro(Value, T);
63 
71  std::string GetValueAsString() const override
72  {
73  std::stringstream myStr;
74  myStr << GetValue();
75  return myStr.str();
76  }
77 
87  bool ToJSON(nlohmann::json&) const override
88  {
89  return false;
90  }
91 
101  bool FromJSON(const nlohmann::json&) override
102  {
103  return false;
104  }
105 
106  using BaseProperty::operator=;
107 
108  protected:
110  GenericProperty(T x) : m_Value(x) {}
111  GenericProperty(const GenericProperty &other) : BaseProperty(other), m_Value(other.m_Value) {}
113 
115 
116  private:
117  // purposely not implemented
118  GenericProperty &operator=(const GenericProperty &);
119 
120  bool IsEqual(const BaseProperty &other) const override
121  {
122  return (this->m_Value == static_cast<const Self &>(other).m_Value);
123  }
124 
125  bool Assign(const BaseProperty &other) override
126  {
127  this->m_Value = static_cast<const Self &>(other).m_Value;
128  return true;
129  }
130  };
131 
132 
133 } // namespace mitk
134 
143 #define mitkDeclareGenericProperty(PropertyName, Type, Export) \
144  \
145  class Export PropertyName : public GenericProperty<Type> \
146  \
147  { \
148  public: \
149  mitkClassMacro(PropertyName, GenericProperty<Type>); \
150  itkFactorylessNewMacro(Self); \
151  itkCloneMacro(Self); \
152  mitkNewMacro1Param(PropertyName, Type); \
153  \
154  bool ToJSON(nlohmann::json& j) const override; \
155  bool FromJSON(const nlohmann::json& j) override; \
156  \
157  using BaseProperty::operator=; \
158  \
159  protected: \
160  PropertyName(); \
161  PropertyName(const PropertyName &); \
162  PropertyName(Type x); \
163  \
164  mitkCloneMacro(Self); \
165  };
166 
167 #define mitkDefineGenericProperty(PropertyName, Type, DefaultValue) \
168  mitk::PropertyName::PropertyName() : Superclass(DefaultValue) {} \
169  mitk::PropertyName::PropertyName(const PropertyName &other) : GenericProperty<Type>(other) {} \
170  mitk::PropertyName::PropertyName(Type x) : Superclass(x) {} \
171  bool mitk::PropertyName::ToJSON(nlohmann::json& j) const \
172  { \
173  j = this->GetValue(); \
174  return true; \
175  } \
176  \
177  bool mitk::PropertyName::FromJSON(const nlohmann::json& j) \
178  { \
179  this->SetValue(j.get<Type>()); \
180  return true; \
181  }
182 
183 #endif
Abstract base class for properties.
Template class for generating properties for simple value types (int, float, bool,...
T ValueType
The type of the value stored by this property.
mitkClassMacro(GenericProperty, BaseProperty)
bool ToJSON(nlohmann::json &) const override
Serialize property value to JSON.
bool FromJSON(const nlohmann::json &) override
Deserialize property value from JSON.
std::string GetValueAsString() const override
Return the property value as a string.
GenericProperty(const GenericProperty &other)
mitkNewMacro1Param(GenericProperty< T >, T)
#define MITK_EXPORT
Definition: mitkCommon.h:222
nlohmann::json json
Convenience header that includes all MITK numeric types.
Find image slices visible on a given plane.