MeVisLab Toolbox Reference
mlBaseItem.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_ITEM_H
14 #define ML_BASE_ITEM_H
15 
16 
18 
19 // Defines the following class:
20 // - BaseItem: A general base class (derived from Base) for list items that have
21 // an id and a name. Should be used for list objects that are editable via a
22 // container and/or editor module.
23 
24 // ML-includes
25 #include "mlBaseInit.h"
26 #include "mlModuleIncludes.h"
27 
28 ML_START_NAMESPACE
29 
30 class TreeNode;
31 
32 // ------------------------------------------------------------------
33 // Base object class BaseItem
34 // ------------------------------------------------------------------
35 
37 class MLBASEEXPORT BaseItem : public Base
38 {
39 public:
40 
43 
45  BaseItem () : Base(), _id(0), _name(nullptr) {}
46 
48  BaseItem (MLssize_t id, const char *nameStr = nullptr) : Base(), _id(id), _name(nullptr) { setName(nameStr); }
49 
51  BaseItem (const BaseItem &item) : Base(), _name(nullptr) { *this = item; }
52 
54 
56  void setId(MLssize_t id){ _id = id; }
57 
59  MLssize_t getId() const { return _id; }
60 
62  ~BaseItem () override { setName(nullptr); }
63 
64 
66  BaseItem &operator = (const BaseItem &item);
67 
68 
70  const char *name () const { return _name; }
71 
73  void setName (const char *newName);
74 
75 
78 
80  std::string persistentState() const override;
81 
83  void setPersistentState(const std::string& state) override;
84 
85 #if !ML_DEPRECATED_SINCE(3,5,0)
86 protected:
87 #endif
91  [[nodiscard]]
92  [[deprecated]] char *getPersistentState () const override;
93 
96  [[deprecated]] void setPersistentState (const char *state) override;
97 
98 #if !ML_DEPRECATED_SINCE(3,5,0)
99 public:
100 #endif
101 
104  [[deprecated]] void clearPersistentState(char *state) const override;
105 
106 
108  void addStateToTree(TreeNode* parent) const override;
109 
111 
116 
118  void readStateFromTree(TreeNode* parent) override;
119 
121  void writeTo(AbstractPersistenceOutputStream* stream) const override;
122 
124  void readFrom(AbstractPersistenceInputStream* stream, int version) override;
125 
127 
129  BaseItem* deepCopy() const override { return new BaseItem(*this); };
130 
131 protected:
132 
136  char *newString (const std::string &str) const;
137 
139  void deleteString (char *str) const;
140 
141 
142 private:
143 
145  MLssize_t _id;
146 
148  char *_name;
149 
151 
152 };
153 
154 
155 ML_END_NAMESPACE
156 
157 
158 #endif // MLBASEITEM_H
Class for reading object data from a stream.
Class for writing object data to a stream.
General Base object class for list items that have an id and a name.
Definition: mlBaseItem.h:38
std::string persistentState() const override
Return a string representation of the item object.
char * newString(const std::string &str) const
Convenience method to create a copy of the string str allocated on the heap.
BaseItem(const BaseItem &item)
Copy constructor.
Definition: mlBaseItem.h:51
void addStateToTree(TreeNode *parent) const override
Attaches the object state as children of the given parent node.
void writeTo(AbstractPersistenceOutputStream *stream) const override
write state to stream
const char * name() const
Get name.
Definition: mlBaseItem.h:70
void setPersistentState(const std::string &state) override
Initialize the item object from the string state.
void setPersistentState(const char *state) override
Initialize the item object from the string state.
BaseItem()
Default constructor.
Definition: mlBaseItem.h:45
BaseItem(MLssize_t id, const char *nameStr=nullptr)
ID and name.
Definition: mlBaseItem.h:48
void readFrom(AbstractPersistenceInputStream *stream, int version) override
read state from stream
~BaseItem() override
Destructor.
Definition: mlBaseItem.h:62
char * getPersistentState() const override
Return a string representation of the item object.
void readStateFromTree(TreeNode *parent) override
Reads the object state from the children of the given parent node.
void setId(MLssize_t id)
Sets the id of the item.
Definition: mlBaseItem.h:56
BaseItem * deepCopy() const override
Create a deep copy of the BaseItem.
Definition: mlBaseItem.h:129
ML_SET_ADDSTATE_VERSION(0)
Set addState version number.
MLssize_t getId() const
Sets the id of the item.
Definition: mlBaseItem.h:59
void setName(const char *newName)
Set name, object stores a copy.
void clearPersistentState(char *state) const override
Dispose the string state, which has been previously obtained by getPersistentState()
void deleteString(char *str) const
Dispose a string allocated with newString()
Class representing general ML objects that support import/export via strings (setPersistentState() an...
Definition: mlBase.h:62
The class TreeNode is the abstract base class for the import/export of ML objects.
Definition: mlTreeNode.h:170
#define MLBASEEXPORT
defined Header file mlBaseInit.h
Definition: mlBaseInit.h:22
#define ML_CLASS_HEADER(className)
Same like ML_CLASS_HEADER_EXPORTED with a non existing export symbol.
SSIZE_T MLssize_t
The signed ML size type which is a signed 32 bit size_t on 32 bit platforms and 64 bit one on 64 bit ...
Definition: mlTypeDefs.h:654