Changeset View
Changeset View
Standalone View
Standalone View
source/simulation2/system/ParamNode.h
Show All 18 Lines | |||||
#define INCLUDED_PARAMNODE | #define INCLUDED_PARAMNODE | ||||
#include "lib/file/vfs/vfs_path.h" | #include "lib/file/vfs/vfs_path.h" | ||||
#include "maths/Fixed.h" | #include "maths/Fixed.h" | ||||
#include "ps/CStrIntern.h" | #include "ps/CStrIntern.h" | ||||
#include "ps/Errors.h" | #include "ps/Errors.h" | ||||
#include "scriptinterface/ScriptTypes.h" | #include "scriptinterface/ScriptTypes.h" | ||||
#include <map> | #include <memory> | ||||
#include <string> | #include <string> | ||||
#include <vector> | |||||
class XMBFile; | class XMBFile; | ||||
class XMBElement; | class XMBElement; | ||||
class ScriptRequest; | class ScriptRequest; | ||||
/** | /** | ||||
* An entity initialisation parameter node. | * An entity initialisation parameter node. | ||||
▲ Show 20 Lines • Show All 107 Lines • ▼ Show 20 Lines | |||||
* } | * } | ||||
* } | * } | ||||
* } | * } | ||||
* @endcode | * @endcode | ||||
* (Note the special @c _string for the hopefully-rare cases where a node contains both child nodes and text.) | * (Note the special @c _string for the hopefully-rare cases where a node contains both child nodes and text.) | ||||
*/ | */ | ||||
class CParamNode | class CParamNode | ||||
{ | { | ||||
// Some implementation details are hidden away as methods of this class. | |||||
class Impl; | |||||
public: | public: | ||||
typedef std::map<std::string, CParamNode> ChildrenMap; | typedef std::vector<CParamNode> ChildrenMap; | ||||
/** | /** | ||||
* Constructs a new, empty node. | * Constructs a new, empty node. | ||||
*/ | */ | ||||
CParamNode(bool isOk = true); | CParamNode(bool isOk = true); | ||||
/** | /** | ||||
* Loads the XML data specified by @a file into the node @a ret. | * Loads the XML data specified by @a file into the node @a ret. | ||||
Show All 17 Lines | public: | ||||
* @return error code if parsing failed, else @c PSRETURN_OK | * @return error code if parsing failed, else @c PSRETURN_OK | ||||
* | * | ||||
* @param sourceIdentifier Optional; string you can pass along to indicate the source of | * @param sourceIdentifier Optional; string you can pass along to indicate the source of | ||||
* the data getting loaded. Used for output to log messages if an error occurs. | * the data getting loaded. Used for output to log messages if an error occurs. | ||||
*/ | */ | ||||
static PSRETURN LoadXMLString(CParamNode& ret, const char* xml, const wchar_t* sourceIdentifier = NULL); | static PSRETURN LoadXMLString(CParamNode& ret, const char* xml, const wchar_t* sourceIdentifier = NULL); | ||||
/** | /** | ||||
* Returns the (unique) child node with the given name, or a node with IsOk() == false if there is none. | * Load the data specified by the Javascript variable @a value into the node @a ret. | ||||
* Existing data will be overwritten. This however does not support advanced filtering that XML supports (for now). | |||||
* @return true on success, false on failure. | |||||
*/ | */ | ||||
const CParamNode& GetChild(const char* name) const; | static bool LoadJS(CParamNode& ret, const ScriptInterface& scriptInterface, JS::HandleValue value); | ||||
// (Children are returned as const in order to allow future optimisations, where we assume | |||||
// a node is always modified explicitly and not indirectly via its children, e.g. to cache JS::Values) | |||||
/** | /** | ||||
* Returns true if this is a valid CParamNode, false if it represents a non-existent node | * Returns true if this is a valid CParamNode, false if it represents a non-existent node | ||||
*/ | */ | ||||
bool IsOk() const; | bool IsOk() const; | ||||
/** | /** | ||||
* Returns the content of this node as a wstring | * Returns the content of this node as a wstring | ||||
▲ Show 20 Lines • Show All 47 Lines • ▼ Show 20 Lines | public: | ||||
* this is called (regardless of whether you passed the same @p cx - be careful | * this is called (regardless of whether you passed the same @p cx - be careful | ||||
* to only use the cache in one context). | * to only use the cache in one context). | ||||
* When caching, the lifetime of @p cx must be longer than the lifetime of this node. | * When caching, the lifetime of @p cx must be longer than the lifetime of this node. | ||||
* The cache will be reset if *this* node is modified (e.g. by LoadXML), | * The cache will be reset if *this* node is modified (e.g. by LoadXML), | ||||
* but *not* if any child nodes are modified (so don't do that). | * but *not* if any child nodes are modified (so don't do that). | ||||
*/ | */ | ||||
void ToJSVal(const ScriptRequest& rq, bool cacheValue, JS::MutableHandleValue ret) const; | void ToJSVal(const ScriptRequest& rq, bool cacheValue, JS::MutableHandleValue ret) const; | ||||
const std::string& GetName() const { return m_Name; } | |||||
bool IsAttribute() const { return !m_Name.empty() && m_Name[0] == '@'; } | |||||
/** | |||||
* Returns the (unique) child node with the given name, or a node with IsOk() == false if there is none. | |||||
* Children are returned as const in order to allow future optimisations, where we assume | |||||
* a node is always modified explicitly and not indirectly via its children, e.g. to cache JS::Values. | |||||
*/ | |||||
const CParamNode& GetChild(const char* name) const; | |||||
/** | /** | ||||
* Returns the names/nodes of the children of this node, ordered by name | * Returns the names/nodes of the children of this node. | ||||
*/ | */ | ||||
const ChildrenMap& GetChildren() const; | const ChildrenMap& GetChildren() const; | ||||
/** | /** | ||||
* Sort children of this node. | |||||
*/ | |||||
void SortChildren(bool recursive); | |||||
/** | |||||
* Escapes a string so that it is well-formed XML content/attribute text. | * Escapes a string so that it is well-formed XML content/attribute text. | ||||
* (Replaces "&" with "&" etc) | * (Replaces "&" with "&" etc) | ||||
*/ | */ | ||||
static std::string EscapeXMLString(const std::string& str); | static std::string EscapeXMLString(const std::string& str); | ||||
std::string m_Name; | |||||
u32 m_Index; | |||||
private: | private: | ||||
/** | |||||
* Overlays the specified data onto this node. See class documentation for the concept and examples. | |||||
* | |||||
* @param xmb Representation of the XMB file containing an element with the data to apply. | |||||
* @param element Element inside the specified @p xmb file containing the data to apply. | |||||
* @param sourceIdentifier Optional; string you can pass along to indicate the source of | |||||
* the data getting applied. Used for output to log messages if an error occurs. | |||||
*/ | |||||
void ApplyLayer(const XMBFile& xmb, const XMBElement& element, const wchar_t* sourceIdentifier = NULL); | |||||
void ResetScriptVal(); | void ResetScriptVal(); | ||||
wraitii: TODO: encapsulate these two better, possibly just by making them const. | |||||
void ConstructJSVal(const ScriptRequest& rq, JS::MutableHandleValue ret) const; | void ConstructJSVal(const ScriptRequest& rq, JS::MutableHandleValue ret) const; | ||||
std::string m_Name; | |||||
u32 m_Index; | |||||
std::string m_Value; | std::string m_Value; | ||||
ChildrenMap m_Childs; | ChildrenMap m_Children; | ||||
bool m_IsOk; | bool m_IsOk; | ||||
/** | /** | ||||
* Caches the ToJSVal script representation of this node. | * Caches the ToJSVal script representation of this node. | ||||
* shared_ptr to enable 'shallow' copying. | |||||
*/ | */ | ||||
mutable std::shared_ptr<JS::PersistentRootedValue> m_ScriptVal; | mutable std::shared_ptr<JS::PersistentRootedValue> m_ScriptVal; | ||||
}; | }; | ||||
#endif // INCLUDED_PARAMNODE | #endif // INCLUDED_PARAMNODE |
Wildfire Games · Phabricator
TODO: encapsulate these two better, possibly just by making them const.