added FieldP to values and styles, implemented reflection for IndexMap

git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@36 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
twanvl
2006-10-19 18:44:27 +00:00
parent 46a6ed39dc
commit ff96f1522a
30 changed files with 290 additions and 122 deletions
+16 -12
View File
@@ -13,11 +13,19 @@
// ----------------------------------------------------------------------------- : IndexMap
/// A kind of map of K->V, with the following properties:
/// A kind of map of Key->Value, with the following properties:
/** - K must have a unique member ->index of type UInt
* - There must exist a function initObject(K, V&)
* - There must exist a function void init_object(Key, Value&)
* that stores a new V object for a given key in the reference
* - There must exist a function Key get_key(Value)
* that returns a key for a given value
* - For reflection there must exist a function String get_key_name(Value)
* that returns the key in string form
* - O(1) inserts and lookups
*
* The 'map' is actually just a vector of values, each key has an index
* which is used for the vector.
* Values know their keys, so there is no need to store them separately.
*/
template <typename Key, typename Value>
class IndexMap : private vector<Value> {
@@ -25,20 +33,18 @@ class IndexMap : private vector<Value> {
using vector<Value>::empty;
using vector<Value>::size;
using vector<Value>::iterator;
using vector<Value>::const_iterator;
using vector<Value>::begin;
using vector<Value>::end;
/// Initialize this map with default values given a list of keys, has no effect if !empty()
/** Requires a function
* void initObject(Key, Value&)
*/
void init(const vector<Key>& keys) {
if (!this->empty()) return;
this->reserve(keys.size());
FOR_EACH_CONST(key, keys) {
assert(key);
if (key->index >= this->size()) this->resize(key->index + 1);
initObject(key, (*this)[key->index]);
init_object(key, (*this)[key->index]);
}
}
@@ -48,20 +54,18 @@ class IndexMap : private vector<Value> {
assert(this->size() > key->index);
return at(key->index);
}
/// Is a value contained in this index map?
/// requires a function Key Value::getKey()
inline bool contains(const Value& value) const {
assert(value);
size_t index = value->getKey()->index;
size_t index = get_key(value)->index;
return index < this.size() && (*this)[index] == value
}
/// Is a key in the domain of this index map?
/// requires a function Key Value::getKey()
inline bool containsKey(const Key& key) const {
assert(key);
return key->index < this.size() && (*this)[key->index]->getKey() == key
return key->index < this.size() && get_key((*this)[key->index]) == key
}
private:
+11
View File
@@ -46,6 +46,7 @@ class GetDefaultMember {
template <typename T> void handle(const Scriptable<T>&);
template <typename T> void handle(const vector<T>& c) { value = toScript(&c); }
template <typename K, typename V> void handle(const map<K,V>& c) { value = toScript(&c); }
template <typename K, typename V> void handle(const IndexMap<K,V>& c) { value = toScript(&c); }
template <typename T> void handle(const shared_ptr<T>& p) { value = toScript(p); }
void handle(const ScriptValueP&);
void handle(const ScriptP&);
@@ -78,6 +79,16 @@ class GetMember : private GetDefaultMember {
}
/// Handle an object: investigate children
template <typename T> void handle(const T&);
/// Handle an index map: invistigate keys
template <typename K, typename V> void handle(const IndexMap<K,V>& m) {
if (gdm.result()) return;
for (typename IndexMap<K,V>::const_iterator it = m.begin() ; it != m.end() ; ++it) {
if (get_key_name(*it) == target_name) {
gdm.handle(*it);
return;
}
}
}
private:
const String& target_name; ///< The name we are looking for
+13 -2
View File
@@ -65,7 +65,9 @@ class Reader {
/// Reads a shared_ptr from the input stream
template <typename T> void handle(shared_ptr<T>& pointer);
/// Reads a map from the input stream
template <typename K, typename V> void handle(map<K,V>& map);
template <typename K, typename V> void handle(map<K,V>& m);
/// Reads an IndexMap from the input stream, reads only keys that already exist in the map
template <typename K, typename V> void handle(IndexMap<K,V>& m);
/// Reads a Defaultable from the input stream
template <typename T> void handle(Defaultable<T>&);
/// Reads a Scriptable from the input stream
@@ -150,10 +152,19 @@ void Reader::handle(shared_ptr<T>& pointer) {
}
template <typename K, typename V>
void Reader::handle(map<K,V>& map) {
void Reader::handle(map<K,V>& m) {
// TODO
}
template <typename K, typename V>
void Reader::handle(IndexMap<K,V>& m) {
while (indent >= expected_indent) {
for (typename IndexMap<K,V>::iterator it = m.begin() ; it != m.end() ; ++it) {
handle(get_key_name(*it).c_str(), *it);
}
}
}
// ----------------------------------------------------------------------------- : Reflection
/// Implement reflection as used by Reader
+11
View File
@@ -52,6 +52,8 @@ class Writer {
template <typename T> void handle(const shared_ptr<T>& pointer);
/// Write a map to the output stream
template <typename K, typename V> void handle(const map<K,V>& map);
/// Write an IndexMap to the output stream
template <typename K, typename V> void handle(const IndexMap<K,V>& map);
/// Write an object of type Defaultable<T> to the output stream
template <typename T> void handle(const Defaultable<T>&);
/// Write an object of type Scriptable<T> to the output stream
@@ -98,6 +100,7 @@ template <typename T>
void Writer::handle(const shared_ptr<T>& pointer) {
if (pointer) handle(*pointer);
}
template <typename K, typename V>
void Writer::handle(const map<K,V>& m) {
for (typename map<K,V>::const_iterator it = m.begin() ; it != m.end() ; ++it) {
@@ -105,6 +108,14 @@ void Writer::handle(const map<K,V>& m) {
}
}
template <typename K, typename V>
void Writer::handle(const IndexMap<K,V>& m) {
for (typename IndexMap<K,V>::const_iterator it = m.begin() ; it != m.end() ; ++it) {
handle(get_key_name(*it).c_str(), *it);
}
}
// ----------------------------------------------------------------------------- : Reflection
/// Implement reflection as used by Writer