13 #ifndef ML_LIST_BASE_H
14 #define ML_LIST_BASE_H
68 : _hasPersistance(persistance),
76 : _hasPersistance(other.hasPersistance()),
77 _actionClass(other.getActionClass()),
78 _actionId(other.getActionId()),
79 _actionIndex(other.getActionIndex()),
80 _currentIndex(other.getCurrentIndex())
126 { setAction(ActNew); }
155 virtual void setPersistance (
bool persistance) { _hasPersistance = persistance; }
206 static const char *
const ActionClassNames[ActNumActions];
213 { setAction(actionClass, -1, -1); }
220 {
return _actionClass; }
224 {
return _actionId; }
230 {
return _actionIndex; }
234 {
return _currentIndex; }
238 {
return (ActSelect != _actionClass && ActNone != _actionClass); }
262 bool _hasPersistance;
339 {
using namespace std;
return vector<T>::size(); }
343 { ListBase::clearList();
using namespace std; vector<T>::clear(); }
351 ListBase::operator =(list);
352 vector<T>::operator =(list);
377 #if !ML_DEPRECATED_SINCE(3,5,0)
384 [[deprecated]]
char *getPersistentState()
const override;
388 [[deprecated]]
void setPersistentState (
const char *state)
override;
393 [[deprecated]]
void clearPersistentState (
char *state)
const override
394 { deleteString(state); }
396 #if !ML_DEPRECATED_SINCE(3,5,0)
411 struct ItemStateDeleter
416 void operator()(
char* p)
const
418 _this->clearItemState(it, p);
439 #if ML_DEPRECATED_SINCE(3,5,0)
440 const auto tmp = std::unique_ptr<char[], ItemStateDeleter>{ getItemState(it), ItemStateDeleter{
this, it} };
441 return std::string{ tmp.get()? tmp.get() :
"" };
450 #if !ML_DEPRECATED_SINCE(3,5,0)
455 [[deprecated]]
virtual void setItemState(
typename ListTemplate<T>::iterator ,
const char* ) {}
459 [[deprecated]]
virtual void clearItemState(
typename ListTemplate<T>::const_iterator ,
char *state)
const
460 { deleteString(state); }
474 char *ListTemplate<T>::getPersistentState ()
const
476 return newString(persistentState());
487 if (hasPersistance())
493 for (
auto it = vector<T>::begin(); it != vector<T>::end(); ++it)
496 if (it != vector<T>::begin()) {
501 auto itemStr = itemState(it);
502 if (!itemStr.empty())
504 if (ListParser::needsQuote(itemStr))
506 listStr += ListParser::quoteString(itemStr);
529 setPersistentState(std::string(state));
537 if (hasPersistance())
545 auto parserResult = parser.
init(state.c_str());
547 while (!parserResult)
550 std::string itemString;
551 std::tie(parserResult, itemString) = parser.
nextItem();
552 if (!itemString.empty() && parserResult != ListParser::kEndOfSource)
555 vector<T>::insert(vector<T>::end(),
T());
558 setItemState(vector<T>::end() - 1, itemString);
563 setAction(ListBase::ActNew);
566 if (parserResult > 0 && parserResult != ParserBase::kEmptyString) {
596 for (
auto it = vector<T>::cbegin(); it != vector<T>::cend(); ++it)
598 auto state = itemState(it);
610 if (!hasPersistance()) {
return; }
612 int version = parent->getVersion(
"ListTemplate");
625 bool endLoop =
false;
635 vector<T>::reserve( loadedSize );
637 TreeNode* items = parent->readContainerChild(
"ListItems");
642 if (items->hasChild()) {
643 items->readChild(currStr);
650 vector<T>::insert(vector<T>::end(),
T());
653 setItemState(vector<T>::end()-1, currStr.c_str());
660 setAction(ListBase::ActNew);
687 (*newList) = (*this);
747 return &(*this)[mlrange_cast<size_t>(index)];
752 return &(*this)[mlrange_cast<size_t>(index)];
760 if (item && (getItemTypeId() == item->getTypeId())) {
761 doInsertItem(index, *
static_cast<const T*
>(item) );
771 if (item && (getItemTypeId() == item->getTypeId())) {
772 doModifyItem(index, *
static_cast<const T*
>(item) );
796 return T::getClassTypeId();
901 #if !ML_DEPRECATED_SINCE(3,5,0)
923 { it->clearPersistentState(state); }
964 (*this)[
static_cast<size_t>(index)].setId(
id);
985 const size_t idx_size_t =
static_cast<size_t>(index);
986 const MLssize_t id = (*this)[idx_size_t].getId();
987 (*this)[idx_size_t] = item;
988 (*this)[idx_size_t].setId(
id);
1003 else if (index == -1)
1033 items->
addChild(&(*it),
"Item",
false);
1042 int version = parent->getVersion(
"BaseListTemplate");
1054 bool endLoop =
false;
1066 TreeNode* items = parent->readContainerChild(
"ListItems");
1070 if (items->hasChild()) {
General Base object class for list items that have an id and a name.
Base object template list class for list item classes derived from BaseItem.
void deleteItemAt(MLssize_t index) override
This virtual function is reimplemented from ListBase, where it does nothing at all.
virtual MLssize_t newId()
Get new unused id.
void setItemState(typename ListTemplate< T >::iterator it, const std::string &state) override
Initialize the item object from the string state.
BaseListTemplate()
Standard constructor, disables persistence.
void addStateToTree(TreeNode *parent) const override
Attaches the object state as children of the given parent node.
void modifyItemAt(MLssize_t index, const BaseItem *item) override
This virtual function is reimplemented from ListBase, where it does nothing at all.
ML_SET_ADDSTATE_VERSION(1)
Set addState version number.
virtual void doDeleteItem(MLssize_t index)
Delete single item at position index and set the corresponding ActDelete action.
std::string itemState(typename ListTemplate< T >::const_iterator it) const override
Return a string representation of the item object.
BaseItem * getItemAt(MLssize_t index) override
This virtual function is reimplemented from ListBase, where it returns 0 in any case (also in ListTem...
void clearItemState(typename ListTemplate< T >::const_iterator it, char *state) const override
Dispose the string state, which has been previously obtained by getItemState()
virtual void doInsertItem(MLssize_t index, const T &item)
Insert item at position index, assign a new id value to the inserted item and set the corresponding A...
void readStateFromTree(TreeNode *parent) override
Reads the object state from the children of the given parent node.
virtual void appendItem(const T &item)
Appends an item to the list.
char * getItemState(typename ListTemplate< T >::const_iterator it) const override
Return a string representation of the item object.
const BaseItem * getConstItemAt(MLssize_t index) const override
Same as getItemAt(MLssize_t index) for constant access.
BaseListTemplate(const BaseListTemplate &other)
BaseListTemplate< T > & operator=(const BaseListTemplate &other)=default
explicitly create default assignment operator
void selectItemAt(MLssize_t index) override
This virtual function is reimplemented from ListBase, where it does nothing at all.
void insertItemAt(MLssize_t index, const BaseItem *item) override
This virtual function is reimplemented from ListBase, where it does nothing at all.
const RuntimeType * getItemTypeId() const override
This virtual function is reimplemented from ListBase, where it returns 0 in any case (also in ListTem...
virtual void doSelectItem(MLssize_t index)
Select item at position index, or deselect if item == -1, and set the corresponding ActSelect action.
BaseListTemplate(bool persistance)
Special constructor to explicitly enable/disable persistence.
virtual void usedId(MLssize_t id)
Notify list that id is used.
void clearList() override
Clear complete list.
virtual void resetId()
Reset next id.
virtual void doModifyItem(MLssize_t index, const T &item)
Store item at position index and set the corresponding ActModify action.
void setItemState(typename ListTemplate< T >::iterator it, const char *state) override
Initialize the item object from the string state.
Class representing general ML objects that support import/export via strings (setPersistentState() an...
Base object class ListBase managing a number of BaseItem objects.
virtual void clearList()
Clear complete list.
virtual MLssize_t getActionId() const
Get id of item affected by last action.
virtual MLssize_t getActionIndex() const
Get index of item affected by last action.
ML_SET_ADDSTATE_VERSION(0)
Set addState version number.
ActionClass
Constants to describe the type of action most recently performed.
@ ActInsert
List item inserted.
@ ActModify
Current list item modified.
@ ActNew
New list generated.
@ ActUnknown
Unknown action.
virtual ListBase * clone() const
Create a copy of (*this) using the ML runtime system and the '='-operator.
char * newString(const std::string &str) const
Convenience method to create a copy of the string str allocated on the heap.
virtual void selectItemAt(MLssize_t)
This virtual function is reimplemented in BaseListTemplate<T>, where it actually selects the item at ...
virtual const RuntimeType * getItemTypeId() const
This virtual function is reimplemented in BaseListTemplate<T>, where it actually returns the item cla...
virtual MLssize_t getCurrentIndex() const
Get index of currently selected item, or -1 if no item selected.
virtual bool hasPersistance() const
Test if persistence is available and enabled.
virtual void deleteItemAt(MLssize_t)
This virtual function is reimplemented in BaseListTemplate<T>, where it actually deletes the item at ...
virtual bool isModified() const
Tests, if the last action has been an action that has modified the content of the list.
virtual size_t getSize() const =0
Get number of list elements.
void addStateToTree(TreeNode *parent) const override
Attaches the state as children of the given parent node.
virtual ActionClass getActionClass() const
Get actionClass of last action.
void readStateFromTree(TreeNode *parent) override
Reads the object state from the children of the given parent node.
virtual void setAction(ActionClass actionClass, MLssize_t id, MLssize_t index)
Set actionClass, affected item id and index.
ListBase * deepCopy() const override
Create a deep copy of the list.
virtual void getAction(ActionClass &actionClass, MLssize_t &id, MLssize_t &index) const
Get actionClass, affected item id and index.
virtual void setPersistance(bool persistance)
Enable/disable persistence functionality.
virtual void setAction(ActionClass actionClass)
Set actionClass for actions affecting the whole list.
virtual const BaseItem * getConstItemAt(MLssize_t) const
Same as getItemAt(MLssize_t index) for constant access.
void deleteString(char *str) const
Dispose a string allocated with newString()
ListBase(const ListBase &other)
virtual BaseItem * getItemAt(MLssize_t)
This virtual function is reimplemented in BaseListTemplate<T>, where it actually returns the item at ...
virtual void modifyItemAt(MLssize_t, const BaseItem *)
This virtual function is reimplemented in BaseListTemplate<T>, where it actually modifies the item at...
virtual void insertItemAt(MLssize_t, const BaseItem *)
This virtual function is reimplemented in BaseListTemplate<T>, where it actually inserts the item at ...
ListBase(bool persistance)
Constructor. Derived class should indicate whether persistence is implemented.
Parser class for parsing persistent state strings of list objects.
std::pair< int, std::string > nextItem()
Return a string to next item's substring Return codes:
int init(const char *source) override
Initialize parser and proceed to first character of first item.
const char * getErrorMessage(int errorCode) override
Get error string for errorCode.
Basic list class template combining properties of ListBase and a vector of the template argument type...
virtual void setItemState(typename ListTemplate< T >::iterator, const std::string &)
Initialize the item object from the string state.
ML_SET_ADDSTATE_VERSION(2)
Set addState version number, version 2 indicates data saved by a 64 bit version.
void readStateFromTree(TreeNode *parent) override
Reads the object state from the children of the given parent node.
ListTemplate()
Standard constructor, disables persistence.
size_t getSize() const override
Get number of list elements.
ListTemplate< T > * clone() const override
Create a copy of (*this) using the ML runtime system and the '='-operator.
ListTemplate(bool persistance)
Special constructor to explicitly enable/disable persistence.
ListTemplate< T > * deepCopy() const override
Create a deep copy of the list.
T itemType
Declare type name for item type (this allows declarations like: ObjectList::itemType obj)
void clearList() override
Clear complete list.
virtual std::string itemState(typename ListTemplate< T >::const_iterator it) const
Return a string representation of the item object.
void addStateToTree(TreeNode *parent) const override
Attaches the state as children of the given parent node.
ListTemplate(const ListTemplate &other)
void setPersistentState(const std::string &state) override
Initialize the list object from the string state.
std::string persistentState() const override
Returns a string describing the object's internal state.
static char * newString(const std::string &str)
Convenience method to create a copy of the string str allocated on the heap.
static void deleteString(char *str)
Dispose a string allocated with newString()
RuntimeType contains type and inheritance information of a class and a static dictionary with informa...
void * createInstance() const
Creates and returns an instance of the of the class (NULL if such an instance is unavailable).
bool canCreateInstance() const
Returns true if this (runtime)type knows how to create an instance of the class.
The class TreeNodeException is the base class for all exceptions thrown by the class TreeNode and all...
The class TreeNode is the abstract base class for the import/export of ML objects.
virtual void addChild(bool val, const char *name) ADD_ULONG_CHILD
Factory method adding a child encapsulating a variable of type bool.
#define ML_ABSTRACT_CLASS_HEADER(className)
Same like ML_ABSTRACT_CLASS_HEADER_EXPORTED with a non existing export symbol.
#define ML_EMPTY_MESSAGE
The following error message describes more precise what has happened; if not then a non registered er...
#define ML_PRINT_ERROR(FUNC_NAME, REASON, HANDLING)
Like ML_PRINT_ERROR_DUMP(FUNC_NAME, REASON, HANDLING, RT_OBJ) without a runtime object to be dumped.
#define MLBASEEXPORT
defined Header file mlBaseInit.h
#define ML_READCHILD_OPTIONAL(obj, tagName, defaultVal)
Convenience macro that can be used to read an optional child with name tagName into obj and assign a ...
#define ML_ADDSTATE_VERSION(ThisClass)
Use this macro in addStateToTree() for classes that might need versioning in the future.
#define ML_ADDSTATE_SUPER(SuperClass)
Use this macro if you would like to store your super class members as well.
#define ML_READSTATE_SUPER(SuperClass)
Use this macro if you would like to store your super class members as well.
UINT64 MLuint64
Introduce platform independent 64 bit unsigned integer type.
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 ...
std::ostream & operator<<(std::ostream &out, const KeyFrame &frame)
KeyFrame stream output.
@ TNE_UnsupportedClassVersion