MeVisLab Toolbox Reference
mlBaseList.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_LIST_H
14 #define ML_BASE_LIST_H
15 
16 
21 
22 // Defines the classes:
23 //
24 // - BaseContainerItem: BaseItem-derived container class for Base-derived objects.
25 // - BaseList : BaseListTemplate<BaseContainer*>-derived container class for
26 // BaseContainer objects.
27 
28 
29 // ML includes
30 #include "mlModuleIncludes.h"
31 
32 
33 #include "mlBaseInit.h"
34 #include "mlListBase.h"
35 
36 
37 ML_START_NAMESPACE
38 
39 
40 
41 // ------------------------------------------------------------------
42 // Base object class BaseContainerItem
43 // ------------------------------------------------------------------
44 
46 
53 {
54 public:
55 
58 
60 
62  BaseContainerItem(MLssize_t idParam, const char* nameParam = nullptr, Base* baseP = nullptr);
63 
64  BaseContainerItem(const BaseContainerItem &other, bool useDeepCopy = false);
65 
67 
68  ~BaseContainerItem() override;
69 
71 
77  virtual void setObject(Base& object);
78 
82  virtual Base* getObjectPointer() const {
83  return _baseObjectP;
84  }
85 
87  virtual const Base* getConstObjectPointer() const {
88  return const_cast<const Base*>(_baseObjectP);
89  }
90 
95 
99  static BaseContainerItem* toBaseContainerItem(Base& baseObj, MLssize_t id = 0, const char* name = nullptr);
100 
105 
111  virtual BaseContainerItem& copyFrom(const BaseContainerItem& other, bool performDeepCopy = true);
112 
116  std::string persistentState() const override;
117 
119  void setPersistentState(const std::string& state) override;
120 
121 #if !ML_DEPRECATED_SINCE(3,5,0)
122 private:
123 #endif
127  [[nodiscard]]
128  [[deprecated]] char *getPersistentState () const override;
129 
131  [[deprecated]] void setPersistentState (const char *state) override;
132 
133 #if !ML_DEPRECATED_SINCE(3,5,0)
134 public:
135 #endif
138  [[deprecated]] void clearPersistentState(char *state) const override;
139 
140 
142  void addStateToTree(TreeNode* parent) const override;
143 
146 
148  void readStateFromTree(TreeNode* parent) override;
149 
151 
152 protected:
153 
160  void _setObjectPointer(Base* objectP, bool isOwner = true);
161 
164 
169 
176 
177 private:
178 
181 
182 };
183 
184 
185 // ------------------------------------------------------------------
187 // ------------------------------------------------------------------
189 public:
191  virtual bool isLessThan(const Base& /*x*/, const Base& /*y*/) = 0;
193  virtual ~BaseListSortParameters(){ } /* Make destructor virtual to avoid warnings on GCC */
194 };
195 
196 
197 
198 
199 // ------------------------------------------------------------------
200 // Class BaseList
201 // ------------------------------------------------------------------
202 
204 class MLBASEEXPORT BaseList : public BaseListTemplate<BaseContainerItem>
205 {
206 
207 public:
208 
211 
213 
215  BaseList* deepCopy() const override;
216 
217 private:
218 
221 
222 };
223 
224 ML_END_NAMESPACE
225 
226 namespace std {
227 
228 
230  template<>
231  struct less<ML_NAMESPACE::BaseContainerItem>
232  {
233  using value_type = bool;
234 
235  less(ML_NAMESPACE::BaseListSortParameters* sortParams) {
236  _sortParams = sortParams;
237  }
238 
240  bool operator()(const ML_NAMESPACE::BaseContainerItem &x, const ML_NAMESPACE::BaseContainerItem &y) const
241  {
242  // An item containing an object defined to be less than an item without one:
243  if (!x.getConstObjectPointer()) { return false; }
244  if (!y.getConstObjectPointer()) { return true; }
245 
246  if (!_sortParams) { return false; }
247 
249  return _sortParams->isLessThan(*x.getConstObjectPointer(), *y.getConstObjectPointer());
250  };
251 
253  ML_NAMESPACE::BaseListSortParameters* _sortParams;
254 
255  };
256 
257 }
258 
259 
260 
261 #endif // MLBASELIST_H
Base object class BaseContainerItem encapsulates a pointer to a Base object as a list item.
Definition: mlBaseList.h:53
void _setObjectPointer(Base *objectP, bool isOwner=true)
Sets a new object by pointer.
virtual Base * removeObjectPointer()
Removes the contained object (deletes if owned and not ref-counted).
bool _ownsBaseObject
Flag remembering if the contained base object is owned.
Definition: mlBaseList.h:168
virtual BaseContainerItem & copyFrom(const BaseContainerItem &other, bool performDeepCopy=true)
Copies from other BaseContainerItem.
~BaseContainerItem() override
void setPersistentState(const std::string &state) override
Initialize the item object from the string state.
bool _baseObjectIsRefCounted
Flag remembering if the contained base object is ref-counted This flag is not strictly necessary,...
Definition: mlBaseList.h:175
static BaseContainerItem * toBaseContainerItem(Base &baseObj, MLssize_t id=0, const char *name=nullptr)
Returns baseObj, if baseObj already is a BaseContainerItem.
void readStateFromTree(TreeNode *parent) override
Reads the object state from the children of the given parent node.
void addStateToTree(TreeNode *parent) const override
Attaches the object state as children of the given parent node.
virtual const Base * getConstObjectPointer() const
Returns a const pointer to the contained base object.
Definition: mlBaseList.h:87
Base * _baseObjectP
Pointer to the actual object:
Definition: mlBaseList.h:163
virtual Base * getObjectPointer() const
Returns a pointer to the contained base object.
Definition: mlBaseList.h:82
void clearPersistentState(char *state) const override
Dispose the string state, which has been previously obtained by getPersistentState()
ML_SET_ADDSTATE_VERSION(0)
Set addState version number.
BaseContainerItem & operator=(const BaseContainerItem &other)
Assignment operator, performing a shallow copy as far as the contained base object is concerned.
BaseContainerItem(MLssize_t idParam, const char *nameParam=nullptr, Base *baseP=nullptr)
When assigning a base object, you should also provide a name.
BaseContainerItem(const BaseContainerItem &other, bool useDeepCopy=false)
BaseContainerItem(BaseContainerItem &&other) noexcept
virtual void setObject(Base &object)
Sets a new object.
std::string persistentState() const override
Return a string representation of the item object.
General Base object class for list items that have an id and a name.
Definition: mlBaseItem.h:38
void setPersistentState(const std::string &state) override
Initialize the item object from the string state.
char * getPersistentState() const override
Return a string representation of the item object.
Class that is designed to hold custom list sorting parameters (such as sort mode)
Definition: mlBaseList.h:188
virtual bool isLessThan(const Base &, const Base &)=0
Pure virtual comparison function.
virtual ~BaseListSortParameters()
virtual destructor
Definition: mlBaseList.h:193
Base object template list class for list item classes derived from BaseItem.
Definition: mlListBase.h:722
Base object class BaseList which stores a list of BaseContainerItem entries.
Definition: mlBaseList.h:205
BaseList(const BaseList &other)
Definition: mlBaseList.h:212
BaseList()
Constructor, enables persistence.
Definition: mlBaseList.h:210
BaseList * deepCopy() const override
Create a deep copy of the list.
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
bool operator()(const ml::BaseContainerItem &x, const ml::BaseContainerItem &y) const
comparison operator
Definition: mlBaseList.h:240
ml::BaseListSortParameters * _sortParams
sort mode
Definition: mlBaseList.h:250
less(ml::BaseListSortParameters *sortParams)
Definition: mlBaseList.h:235