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 #include <ThirdPartyWarningsRestore.h>
30 
32 #define ML_SET_ADDSTATE_VERSION(v) static int getAddStateVersion(void) { return v; };
34 #define ML_PERSISTENCE_VERSION(v) ML_SET_ADDSTATE_VERSION(v)
35 
36 ML_UTILS_START_NAMESPACE
37 
39 class TreeNode;
40 class AbstractPersistenceOutputStream;
41 class AbstractPersistenceInputStream;
43 
44 //----------------------------------------------------------------------
49 
57 //----------------------------------------------------------------------
59 {
60 public:
61 
63  Base();
64 
66  virtual ~Base();
67 
69 
71 
76  PersistenceByStream
77  };
78 
81  virtual bool implementsPersistence(PersistenceInterface) const { return false; }
82 
84  virtual std::string persistentState() const;
85 
88  virtual void setPersistentState(const std::string& state);
89 
91  virtual void addStateToTree(TreeNode* /*parent*/) const { ; }
92 
94  virtual void readStateFromTree(TreeNode* /*parent*/) { ; }
95 
103  virtual void writeTo(AbstractPersistenceOutputStream* /*stream*/) const {}
104 
114  virtual void readFrom(AbstractPersistenceInputStream* /*stream*/, int /*version*/) {}
115 
116  // Sets addStateToTree version number that can be accessed via getAddStateVersion()
118 
119 
120 
121 
124  virtual Base* deepCopy() const { return nullptr; };
125 
129  bool isOfAllowedType(const std::vector<const RuntimeType*>& types) const;
130 
132  virtual bool isRefCountedBase() const { return false; }
133 
136  virtual std::string detailString() const { return std::string(); }
137 
141 
142 };
143 
144 ML_UTILS_END_NAMESPACE
145 
146 #undef ML_BASE_VERSION
147 
149 
175 template<typename ToTypePtr>
176 inline ToTypePtr mlbase_cast(ML_UTILS_NAMESPACE::Base* from) {
177  if (from) {
178  if (from->getTypeId()->isDerivedFrom((static_cast<ToTypePtr>(0))->getClassTypeId())) {
179  return static_cast<ToTypePtr>(from);
180  } else {
181  return NULL;
182  }
183  } else {
184  return NULL;
185  }
186 }
187 
189 template<typename ToTypePtr>
190 inline ToTypePtr mlbase_cast(const ML_UTILS_NAMESPACE::Base* from) {
191  if (from) {
192  if (from->getTypeId()->isDerivedFrom((static_cast<ToTypePtr>(0))->getClassTypeId())) {
193  return static_cast<ToTypePtr>(from);
194  } else {
195  return NULL;
196  }
197  } else {
198  return NULL;
199  }
200 }
201 
202 #endif // __mlBase_H
203 
204 
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:59
virtual void setPersistentState(const std::string &state)
Restores the object's internal state from a string that had been previously generated using persisten...
virtual std::string detailString() const
Returns a string describing this object.
Definition: mlBase.h:136
virtual bool implementsPersistence(PersistenceInterface) const
Override this method to declare which persistence interfaces are implemented by your derived class.
Definition: mlBase.h:81
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:91
virtual ~Base()
Destructor.
virtual void readFrom(AbstractPersistenceInputStream *, int)
Reads the objects state from the data stream object.
Definition: mlBase.h:114
virtual bool isRefCountedBase() const
Returns whether the instance is derived from RefCountedBase.
Definition: mlBase.h:132
virtual void writeTo(AbstractPersistenceOutputStream *) const
Writes the objects state to the data stream object.
Definition: mlBase.h:103
bool isOfAllowedType(const std::vector< const RuntimeType * > &types) const
Checks whether 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:73
@ PersistenceByString
Definition: mlBase.h:74
@ PersistenceByTreeNode
Definition: mlBase.h:75
virtual void readStateFromTree(TreeNode *)
Reads the object state from the children of the given parent node.
Definition: mlBase.h:94
The class TreeNode is the abstract base class for the import/export of ML objects.
Definition: mlTreeNode.h:154
ToTypePtr mlbase_cast(ml::Base *from)
Template function to allow type-safe casting of a base object to a derived object.
Definition: mlBase.h:176
#define ML_PERSISTENCE_VERSION(v)
Nicer name for ML_SET_ADDSTATE_VERSION.
Definition: mlBase.h:34
#define ML_ABSTRACT_ROOT_CLASS_HEADER(className)
#define ML_UTILS_EXPORT
Definition: mlUtilities.h:18