mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-10 13:06:59 -04:00
added order_by support to position function, orders are cached; TODO: clear the cache when a card changes
git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@109 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
#include <script/value.hpp>
|
||||
#include <script/context.hpp>
|
||||
#include <util/tagged_string.hpp>
|
||||
#include <data/set.hpp>
|
||||
#include <wx/regex.h>
|
||||
|
||||
DECLARE_TYPEOF_COLLECTION(UInt);
|
||||
@@ -370,14 +371,53 @@ SCRIPT_RULE_1(tag_remove, String, tag) {
|
||||
|
||||
// ----------------------------------------------------------------------------- : Vector stuff
|
||||
|
||||
/// compare script values for equallity
|
||||
bool equal(const ScriptValue& a, const ScriptValue& b) {
|
||||
ScriptType at = a.type(), bt = b.type();
|
||||
if (at != bt) {
|
||||
return false;
|
||||
} else if (at == SCRIPT_INT) {
|
||||
return (int)a == (int)b;
|
||||
} else if (at == SCRIPT_DOUBLE) {
|
||||
return (double)a == (double)b;
|
||||
} else if (at == SCRIPT_STRING) {
|
||||
return (String)a == (String)b;
|
||||
} else if (at == SCRIPT_OBJECT) {
|
||||
// HACK for ScriptObject<shared_ptr<X> >
|
||||
// assumes different types are layed out the same, and that
|
||||
// should be void*, but then we need getMember for void
|
||||
const ScriptObject<int*>& av = reinterpret_cast<const ScriptObject<int*>&>(a);
|
||||
const ScriptObject<int*>& bv = reinterpret_cast<const ScriptObject<int*>&>(b);
|
||||
return av.getValue() == bv.getValue();
|
||||
}
|
||||
return &a == &b;
|
||||
}
|
||||
|
||||
/// position of some element in a vector
|
||||
/** 0 based index, -1 if not found */
|
||||
int position_in_vector(const ScriptValueP& of, const ScriptValueP& in, const ScriptValueP& order_by) {
|
||||
ScriptType of_t = of->type(), in_t = in->type();
|
||||
if (of_t == SCRIPT_STRING || in_t == SCRIPT_STRING) {
|
||||
// string finding
|
||||
return (int)((String)*of).find(*in); // (int)npos == -1
|
||||
} else if (order_by) {
|
||||
ScriptObject<Set*>* s = dynamic_cast<ScriptObject<Set*>* >(in.get());
|
||||
ScriptObject<CardP>* c = dynamic_cast<ScriptObject<CardP>*>(of.get());
|
||||
if (s && c) {
|
||||
return s->getValue()->positionOfCard(c->getValue(), order_by);
|
||||
} else {
|
||||
throw ScriptError(_("position: using 'order_by' is only supported for finding cards in the set"));
|
||||
}
|
||||
} else {
|
||||
// unordered position
|
||||
ScriptValueP it = in->makeIterator();
|
||||
int i = 0;
|
||||
while (ScriptValueP v = it->next()) {
|
||||
if (equal(*of, *v)) return i;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return -1; // TODO
|
||||
return -1; // TODO?
|
||||
}
|
||||
|
||||
// finding positions, also of substrings
|
||||
|
||||
@@ -29,7 +29,7 @@ ScriptValueP ScriptValue::dependencies(Context&, const Dependency&
|
||||
|
||||
// ----------------------------------------------------------------------------- : Iterators
|
||||
|
||||
ScriptType ScriptIterator::type() const { return SCRIPT_OBJECT; }
|
||||
ScriptType ScriptIterator::type() const { return SCRIPT_ITERATOR; }
|
||||
String ScriptIterator::typeName() const { return _("iterator"); }
|
||||
|
||||
// Iterator over a range of integers
|
||||
|
||||
+17
-5
@@ -10,6 +10,7 @@
|
||||
// ----------------------------------------------------------------------------- : Includes
|
||||
|
||||
#include <util/prec.hpp>
|
||||
#include <util/reflect.hpp>
|
||||
#include <util/error.hpp>
|
||||
class Context;
|
||||
class Dependency;
|
||||
@@ -37,7 +38,9 @@ enum ScriptType
|
||||
, SCRIPT_COLOR
|
||||
, SCRIPT_IMAGE
|
||||
, SCRIPT_FUNCTION
|
||||
, SCRIPT_OBJECT
|
||||
, SCRIPT_OBJECT // Only ScriptObject
|
||||
, SCRIPT_COLLECTION
|
||||
, SCRIPT_ITERATOR
|
||||
, SCRIPT_DUMMY
|
||||
};
|
||||
|
||||
@@ -121,7 +124,7 @@ extern ScriptValueP dependency_dummy; ///< Dummy value used during dependency an
|
||||
|
||||
// Iterator over a collection
|
||||
struct ScriptIterator : public ScriptValue {
|
||||
virtual ScriptType type() const;// { return SCRIPT_OBJECT; }
|
||||
virtual ScriptType type() const;// { return SCRIPT_ITERATOR; }
|
||||
virtual String typeName() const;// { return "iterator"; }
|
||||
|
||||
/// Return the next item for this iterator, or ScriptValueP() if there is no such item
|
||||
@@ -155,7 +158,7 @@ template <typename Collection>
|
||||
class ScriptCollection : public ScriptValue {
|
||||
public:
|
||||
inline ScriptCollection(const Collection* v) : value(v) {}
|
||||
virtual ScriptType type() const { return SCRIPT_OBJECT; }
|
||||
virtual ScriptType type() const { return SCRIPT_COLLECTION; }
|
||||
virtual String typeName() const { return _("collection"); }
|
||||
virtual ScriptValueP getMember(const String& name) const {
|
||||
long index;
|
||||
@@ -201,7 +204,7 @@ template <typename Collection>
|
||||
class ScriptMap : public ScriptValue {
|
||||
public:
|
||||
inline ScriptMap(const Collection* v) : value(v) {}
|
||||
virtual ScriptType type() const { return SCRIPT_OBJECT; }
|
||||
virtual ScriptType type() const { return SCRIPT_COLLECTION; }
|
||||
virtual String typeName() const { return _("collection"); }
|
||||
virtual ScriptValueP getMember(const String& name) const {
|
||||
return get_member(*value, name);
|
||||
@@ -219,6 +222,11 @@ template <typename T>
|
||||
int item_count(const T& v) {
|
||||
return -1;
|
||||
}
|
||||
/// Return an iterator for some collection, can be overloaded
|
||||
template <typename T>
|
||||
ScriptValueP make_iterator(const T& v) {
|
||||
return ScriptValueP();
|
||||
}
|
||||
|
||||
/// Mark a dependency on a member of value, can be overloaded
|
||||
template <typename T>
|
||||
@@ -253,12 +261,16 @@ class ScriptObject : public ScriptValue {
|
||||
mark_dependency_member(value, name, dep);
|
||||
return getMember(name);
|
||||
}
|
||||
virtual ScriptValueP makeIterator() const {
|
||||
ScriptValueP it = make_iterator(*value);
|
||||
return it ? it : ScriptValue::makeIterator();
|
||||
}
|
||||
virtual int itemCount() const {
|
||||
int i = item_count(*value);
|
||||
return i >= 0 ? i : ScriptValue::itemCount();
|
||||
}
|
||||
/// Get access to the value
|
||||
inline T getValue() { return value; }
|
||||
inline T getValue() const { return value; }
|
||||
private:
|
||||
T value; ///< The object
|
||||
ScriptValueP getDefault() const {
|
||||
|
||||
Reference in New Issue
Block a user