MeVisLab Toolbox Reference
mlBase.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_BASE_H
14 #define ML_BASE_H
15 
20 
21 // Project-includes
22 #include "mlUtilsSystem.h"
23 #include "mlRuntimeSubClass.h"
24 #include "mlRuntime.h"
25 
26 #include <ThirdPartyWarningsDisable.h>
27 #include <string>
28 #include <vector>
29 #if ML_DEPRECATED_SINCE(3,5,0)
30  #include <memory>
31 #endif
32 #include <ThirdPartyWarningsRestore.h>
33 
35 #define ML_SET_ADDSTATE_VERSION(v) static int getAddStateVersion(void) { return v; };
37 #define ML_PERSISTENCE_VERSION(v) ML_SET_ADDSTATE_VERSION(v)
38 
39 ML_UTILS_START_NAMESPACE
40 
42 class TreeNode;
43 class AbstractPersistenceOutputStream;
44 class AbstractPersistenceInputStream;
46 
47 //----------------------------------------------------------------------
52 
60 //----------------------------------------------------------------------
62 {
63 public:
64 
66  Base();
67 
69  virtual ~Base();
70 
71  struct StateDeleter
72  {
73  const Base* _this{};
74 #if ML_DEPRECATED_SINCE(3,5,0)
75  void operator()(char* ) const {}
76 #else
77  void operator()(char* p) const
78  {
79  _this->clearPersistentState(p);
80  }
81 #endif
82  };
83 
85 
87 
92  PersistenceByStream
93  };
94 
97  virtual bool implementsPersistence(PersistenceInterface) const { return false; }
98 
100  virtual std::string persistentState() const;
101 
104  virtual void setPersistentState(const std::string& state);
105 
106 #if !ML_DEPRECATED_SINCE(3,5,0)
107 protected:
108 #endif
109 
114  [[nodiscard]]
115  [[deprecated]] virtual char* getPersistentState() const { return nullptr; }
116 
120  [[deprecated]] virtual void setPersistentState(const char* /*state*/){ }
121 
123  [[deprecated]] virtual void clearPersistentState(char* /*state*/) const { }
124 #if !ML_DEPRECATED_SINCE(3,5,0)
125 public:
126 #endif
127 
128 
130  virtual void addStateToTree(TreeNode* /*parent*/) const { ; }
131 
133  virtual void readStateFromTree(TreeNode* /*parent*/) { ; }
134 
142  virtual void writeTo(AbstractPersistenceOutputStream* /*stream*/) const {}
143 
153  virtual void readFrom(AbstractPersistenceInputStream* /*stream*/, int /*version*/) {}
154 
157 
158 
159 
160 
163  virtual Base* deepCopy() const { return nullptr; };
164 
168  bool isOfAllowedType(const std::vector<const RuntimeType*>& types) const;
169 
171  virtual bool isRefCountedBase() const { return false; }
172 
175  virtual std::string detailString() const { return std::string(); }
176 
180 
181 };
182 
183 ML_UTILS_END_NAMESPACE
184 
185 #undef ML_BASE_VERSION
186 
188 
216 template<typename ToTypePtr>
217 inline ToTypePtr mlbase_cast(ML_UTILS_NAMESPACE::Base* from) {
218  if (from) {
219  if (from->getTypeId()->isDerivedFrom((static_cast<ToTypePtr>(0))->getClassTypeId())) {
220  return static_cast<ToTypePtr>(from);
221  } else {
222  return NULL;
223  }
224  } else {
225  return NULL;
226  }
227 }
228 
230 template<typename ToTypePtr>
231 inline ToTypePtr mlbase_cast(const ML_UTILS_NAMESPACE::Base* from) {
232  if (from) {
233  if (from->getTypeId()->isDerivedFrom((static_cast<ToTypePtr>(0))->getClassTypeId())) {
234  return static_cast<ToTypePtr>(from);
235  } else {
236  return NULL;
237  }
238  } else {
239  return NULL;
240  }
241 }
242 
243 #endif // __mlBase_H
244 
245 
Class for reading object data from a stream.
Class for writing object data to a stream.
Class representing general ML objects that support import/export via strings (setPersistentState() an...
Definition: mlBase.h:62
virtual void setPersistentState(const std::string &state)
Restores the object's internal state from a string that had been previously generated using persisten...
virtual void clearPersistentState(char *) const
Disposes a string previously allocated by getPersistentState().
Definition: mlBase.h:123
virtual std::string detailString() const
Return a string describing this object.
Definition: mlBase.h:175
virtual bool implementsPersistence(PersistenceInterface) const
Override this method to declare which persistence interfaces are implemented by your derived class.
Definition: mlBase.h:97
Base()
Constructor.
virtual std::string persistentState() const
Returns a string describing the object's internal state.
virtual void addStateToTree(TreeNode *) const
Attaches the object state as children of the given parent node.
Definition: mlBase.h:130
virtual ~Base()
Destructor.
virtual void readFrom(AbstractPersistenceInputStream *, int)
Read the objects state from the data stream object.
Definition: mlBase.h:153
virtual bool isRefCountedBase() const
Returns if the instance is derived from RefCountedBase.
Definition: mlBase.h:171
virtual char * getPersistentState() const
Returns a C string describing the object's internal state.
Definition: mlBase.h:115
virtual void writeTo(AbstractPersistenceOutputStream *) const
Write the objects state to the data stream object.
Definition: mlBase.h:142
bool isOfAllowedType(const std::vector< const RuntimeType * > &types) const
Check if this object's type is equal to or derived from one of the types given in the argument.
PersistenceInterface
This enum describes the different persistence interfaces available.
Definition: mlBase.h:89
@ PersistenceByString
Definition: mlBase.h:90
@ PersistenceByTreeNode
Definition: mlBase.h:91
virtual void setPersistentState(const char *)
Restores the object's internal state from a string that had been previously generated using getPersis...
Definition: mlBase.h:120
virtual void readStateFromTree(TreeNode *)
Reads the object state from the children of the given parent node.
Definition: mlBase.h:133
The class TreeNode is the abstract base class for the import/export of ML objects.
Definition: mlTreeNode.h:170
ToTypePtr mlbase_cast(ml::Base *from)
Template function to allow type-safe casting of a base object to a derived object.
Definition: mlBase.h:217
#define ML_PERSISTENCE_VERSION(v)
Nicer name for ML_SET_ADDSTATE_VERSION.
Definition: mlBase.h:37
#define ML_ABSTRACT_ROOT_CLASS_HEADER(className)
#define ML_UTILS_EXPORT
Defines platform dependent DLL export macro for mlUtils.
Definition: mlUtilities.h:20
void operator()(char *p) const
Definition: mlBase.h:77