2015-08-12 20:21:10 +00:00
|
|
|
#ifndef DEFINITIONNODE_H
|
|
|
|
#define DEFINITIONNODE_H
|
2013-10-30 11:46:18 +00:00
|
|
|
|
|
|
|
#include "definition_global.h"
|
|
|
|
|
2015-12-29 23:43:35 +00:00
|
|
|
#include <string>
|
2015-11-18 18:37:00 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2013-10-30 11:46:18 +00:00
|
|
|
namespace paysages {
|
|
|
|
namespace definition {
|
|
|
|
|
|
|
|
/**
|
2015-08-12 20:21:10 +00:00
|
|
|
* Base class for all nodes of the definition tree.
|
2013-10-30 11:46:18 +00:00
|
|
|
*/
|
2015-11-09 21:30:46 +00:00
|
|
|
class DEFINITIONSHARED_EXPORT DefinitionNode {
|
|
|
|
public:
|
2015-12-10 23:36:50 +00:00
|
|
|
DefinitionNode(DefinitionNode *parent, const string &name, const string &type_name = "");
|
2015-08-12 20:21:10 +00:00
|
|
|
virtual ~DefinitionNode();
|
2013-10-30 11:46:18 +00:00
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
virtual void save(PackStream *stream) const;
|
|
|
|
virtual void load(PackStream *stream);
|
2013-10-30 11:46:18 +00:00
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
virtual void copy(DefinitionNode *destination) const;
|
2013-10-31 16:59:18 +00:00
|
|
|
virtual void validate();
|
|
|
|
|
2015-12-10 23:36:50 +00:00
|
|
|
inline const string &getName() const {
|
2015-11-09 21:30:46 +00:00
|
|
|
return name;
|
|
|
|
}
|
2015-12-10 23:36:50 +00:00
|
|
|
virtual void setName(const string &name);
|
2013-10-31 16:59:18 +00:00
|
|
|
|
2015-12-10 23:36:50 +00:00
|
|
|
inline const string &getTypeName() const {
|
2015-11-09 21:30:46 +00:00
|
|
|
return type_name;
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:38:00 +00:00
|
|
|
virtual const Scenery *getScenery() const;
|
2015-11-09 21:30:46 +00:00
|
|
|
|
|
|
|
inline const DefinitionNode *getParent() const {
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
inline const DefinitionNode *getRoot() const {
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
inline DiffManager *getDiffManager() const {
|
|
|
|
return diffs;
|
|
|
|
}
|
|
|
|
inline int getChildrenCount() const {
|
|
|
|
return children.size();
|
|
|
|
}
|
2013-10-31 21:53:22 +00:00
|
|
|
|
2015-08-12 15:02:27 +00:00
|
|
|
/**
|
|
|
|
* Return a string representation of the tree (mainly for debugging purposes).
|
|
|
|
*/
|
2015-12-10 23:36:50 +00:00
|
|
|
virtual string toString(int indent = 0) const;
|
2015-08-12 15:02:27 +00:00
|
|
|
|
2015-08-16 22:29:54 +00:00
|
|
|
/**
|
|
|
|
* Return the path to this node, using '/' delimited syntax, with the first '/' being the root node.
|
|
|
|
*/
|
2015-12-10 23:36:50 +00:00
|
|
|
string getPath() const;
|
2015-08-16 22:29:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find a node in this tree, by its path (as returned by getPath).
|
|
|
|
*
|
|
|
|
* Return NULL if the path does not exists.
|
|
|
|
*/
|
2015-12-10 23:36:50 +00:00
|
|
|
DefinitionNode *findByPath(const string &path) const;
|
2015-08-16 22:29:54 +00:00
|
|
|
|
2015-08-16 21:01:56 +00:00
|
|
|
/**
|
|
|
|
* Apply a diff to the internal value of this node.
|
|
|
|
*
|
|
|
|
* All internal node modifications should be done using this method, to be reversible.
|
|
|
|
*
|
2016-01-06 18:55:49 +00:00
|
|
|
* If 'backward' is true, the diff will be reversed, instead of applied.
|
2015-08-16 21:01:56 +00:00
|
|
|
*
|
|
|
|
* Return true if the diff could be applied.
|
|
|
|
*/
|
2015-11-09 21:30:46 +00:00
|
|
|
virtual bool applyDiff(const DefinitionDiff *diff, bool backward = false);
|
2015-08-16 21:01:56 +00:00
|
|
|
|
2015-08-17 20:55:30 +00:00
|
|
|
/**
|
|
|
|
* Fill a diff array to be applied to initialize a proper state for a watcher.
|
|
|
|
*
|
|
|
|
* This method should be overridden by subclasses.
|
|
|
|
*/
|
2015-12-10 23:36:50 +00:00
|
|
|
virtual void generateInitDiffs(vector<const DefinitionDiff *> *diffs) const;
|
2015-08-17 20:55:30 +00:00
|
|
|
|
2015-08-16 22:29:54 +00:00
|
|
|
/**
|
|
|
|
* Add a watcher over this node.
|
|
|
|
*
|
|
|
|
* The watcher will receive DefinitionDiff objects when this node changes.
|
2015-08-17 20:55:30 +00:00
|
|
|
*
|
2016-01-06 18:55:49 +00:00
|
|
|
* If 'init_diff' is set to true, a first diff (or several) will be be pushed immediately to initialize the state.
|
2015-08-16 22:29:54 +00:00
|
|
|
*/
|
2015-11-09 21:30:46 +00:00
|
|
|
void addWatcher(DefinitionWatcher *watcher, bool init_diff = false);
|
2015-08-16 22:29:54 +00:00
|
|
|
|
2015-09-21 21:10:43 +00:00
|
|
|
/**
|
|
|
|
* Get the current number of watchers.
|
|
|
|
*/
|
|
|
|
int getWatcherCount() const;
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
protected:
|
|
|
|
void addChild(DefinitionNode *child);
|
|
|
|
void removeChild(DefinitionNode *child);
|
2015-12-15 23:38:28 +00:00
|
|
|
virtual DefinitionNode *findChildByName(const string &name) const;
|
2015-08-13 21:46:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the size in bytes this child will consume when serialized to a stream.
|
|
|
|
*
|
|
|
|
* Return -1 if it can't be known. In this case, the saving will be done in a temporary
|
|
|
|
* stream to know the exact size, which will not be very efficient.
|
|
|
|
*/
|
|
|
|
int getStreamSize() const;
|
2013-10-30 14:39:56 +00:00
|
|
|
|
2015-08-16 22:29:54 +00:00
|
|
|
/**
|
|
|
|
* Add a diff to the DiffManager of this definition tree, for the current node.
|
|
|
|
*
|
|
|
|
* The manager will take immediate ownership of the diff, handling its freeing.
|
|
|
|
*
|
2016-01-06 18:55:49 +00:00
|
|
|
* The manager will decide if the diff should be committed and will call 'applyDiff' if needed.
|
2015-08-16 22:29:54 +00:00
|
|
|
*/
|
|
|
|
void addDiff(const DefinitionDiff *diff);
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
private:
|
2015-08-16 22:29:54 +00:00
|
|
|
DefinitionNode *parent;
|
|
|
|
DefinitionNode *root;
|
|
|
|
DiffManager *diffs;
|
2015-12-10 23:36:50 +00:00
|
|
|
string type_name;
|
|
|
|
string name;
|
|
|
|
vector<DefinitionNode *> children;
|
2013-10-30 11:46:18 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-12 20:21:10 +00:00
|
|
|
#endif // DEFINITIONNODE_H
|