mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-10 04:57:00 -04:00
implemented reflection
git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@27 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
@@ -24,7 +24,7 @@ void store(const ScriptValueP& val, Defaultable<String>& var) { var.assign(*val)
|
|||||||
|
|
||||||
OptionalScript::~OptionalScript() {}
|
OptionalScript::~OptionalScript() {}
|
||||||
|
|
||||||
ScriptValueP OptionalScript::invoke(Context& ctx) {
|
ScriptValueP OptionalScript::invoke(Context& ctx) const {
|
||||||
if (script) {
|
if (script) {
|
||||||
return ctx.eval(*script);
|
return ctx.eval(*script);
|
||||||
} else {
|
} else {
|
||||||
@@ -44,14 +44,11 @@ template <> void Writer::handle(const OptionalScript& os) {
|
|||||||
handle(os.unparsed);
|
handle(os.unparsed);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <> void GetMember::handle(const OptionalScript& os) {
|
template <> void GetDefaultMember::handle(const OptionalScript& os) {
|
||||||
// no members
|
|
||||||
}
|
|
||||||
template <> void GetMember::store(const OptionalScript& os) {
|
|
||||||
// reflect as the script itself
|
// reflect as the script itself
|
||||||
if (os.script) {
|
if (os.script) {
|
||||||
store(os.script);
|
handle(os.script);
|
||||||
} else {
|
} else {
|
||||||
store(script_nil);
|
handle(script_nil);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
#include <util/reflect.hpp>
|
#include <util/reflect.hpp>
|
||||||
#include <util/defaultable.hpp>
|
#include <util/defaultable.hpp>
|
||||||
#include <script/script.hpp>
|
#include <script/script.hpp>
|
||||||
|
#include <script/parser.hpp>
|
||||||
|
|
||||||
DECLARE_INTRUSIVE_POINTER_TYPE(Script);
|
DECLARE_INTRUSIVE_POINTER_TYPE(Script);
|
||||||
class Context;
|
class Context;
|
||||||
@@ -33,17 +34,17 @@ class OptionalScript {
|
|||||||
public:
|
public:
|
||||||
~OptionalScript();
|
~OptionalScript();
|
||||||
/// Is the script set?
|
/// Is the script set?
|
||||||
inline operator bool() { return !!script; }
|
inline operator bool() const { return !!script; }
|
||||||
|
|
||||||
/// Invoke the script, return the result, or script_nil if there is no script
|
/// Invoke the script, return the result, or script_nil if there is no script
|
||||||
ScriptValueP invoke(Context& ctx);
|
ScriptValueP invoke(Context& ctx) const;
|
||||||
|
|
||||||
/// Invoke the script on a value
|
/// Invoke the script on a value
|
||||||
/** Assigns the result to value if it has changed.
|
/** Assigns the result to value if it has changed.
|
||||||
* Returns true if the value has changed.
|
* Returns true if the value has changed.
|
||||||
*/
|
*/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
bool invokeOn(Context& ctx, T& value) {
|
bool invokeOn(Context& ctx, T& value) const {
|
||||||
if (script) {
|
if (script) {
|
||||||
T new_value;
|
T new_value;
|
||||||
store(new_value, script->invoke(ctx));
|
store(new_value, script->invoke(ctx));
|
||||||
@@ -59,6 +60,7 @@ class OptionalScript {
|
|||||||
ScriptP script; ///< The script, may be null if there is no script
|
ScriptP script; ///< The script, may be null if there is no script
|
||||||
String unparsed; ///< Unparsed script, for writing back to a file
|
String unparsed; ///< Unparsed script, for writing back to a file
|
||||||
DECLARE_REFLECTION();
|
DECLARE_REFLECTION();
|
||||||
|
template <typename T> friend class Scriptable;
|
||||||
};
|
};
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------- : Scriptable
|
// ----------------------------------------------------------------------------- : Scriptable
|
||||||
@@ -86,5 +88,31 @@ class Scriptable {
|
|||||||
DECLARE_REFLECTION();
|
DECLARE_REFLECTION();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// we need some custom io, because the behaviour is different for each of Reader/Writer/GetMember
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void Reader::handle(Scriptable<T>& s) {
|
||||||
|
handle(s.script.unparsed);
|
||||||
|
if (starts_with(s.script.unparsed, _("script:"))) {
|
||||||
|
s.script.script = parse(s.script.unparsed);
|
||||||
|
} else {
|
||||||
|
handle(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
template <typename T>
|
||||||
|
void Writer::handle(const Scriptable<T>& s) {
|
||||||
|
if (s.script) {
|
||||||
|
handle(s.script);
|
||||||
|
} else {
|
||||||
|
handle(s.value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
template <typename T>
|
||||||
|
void GetDefaultMember::handle(const Scriptable<T>& s) {
|
||||||
|
// just handle as the value
|
||||||
|
handle(s.value);
|
||||||
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------- : EOF
|
// ----------------------------------------------------------------------------- : EOF
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user