1
0
Fork 0

- add Node.getValues() in analogy to Node.setValues(). Returns a hash with

all children, vectors for multiply used names, further hashes for subtrees
  etc. Meant for quickly pulling in config branches where (almost) all values
  are needed.
- usage of "var" keyword and named parameters in other (but not all) funcs
This commit is contained in:
mfranz 2007-05-14 15:15:00 +00:00
parent e6feb269c7
commit a9b9c86750

View file

@ -1,6 +1,6 @@
##
# Node class definition. The class methods simply wrap the
# low level exention functions which work on a "ghost" handle to a
# low level extension functions which work on a "ghost" handle to a
# SGPropertyNode object stored in the _g field.
#
# Not all of the features of SGPropertyNode are supported. There is
@ -69,8 +69,8 @@ Node.new = func {
# name : "exit", width : 180, height : 100, modal : 0,
# text : { x : 10, y : 70, label : "Hello World!" } };
#
Node.setValues = func {
foreach(k; keys(arg[0])) { me._setChildren(k, arg[0][k]); }
Node.setValues = func(val) {
foreach(var k; keys(val)) { me._setChildren(k, val[k]); }
}
##
@ -78,19 +78,45 @@ Node.setValues = func {
# The first argument is a child name, the second a nasal scalar,
# vector, or hash.
#
Node._setChildren = func {
name = arg[0]; val = arg[1];
subnode = me.getNode(name, 1);
Node._setChildren = func(name, val) {
var subnode = me.getNode(name, 1);
if(typeof(val) == "scalar") { subnode.setValue(val); }
elsif(typeof(val) == "hash") { subnode.setValues(val); }
elsif(typeof(val) == "vector") {
for(i=0; i<size(val); i+=1) {
iname = name ~ "[" ~ i ~ "]";
for(var i=0; i<size(val); i+=1) {
var iname = name ~ "[" ~ i ~ "]";
me._setChildren(iname, val[i]);
}
}
}
##
# Counter piece of setValues(). Returns a hash with all values
# in the subtree. Nodes with same name are returned as vector,
# where the original node indices are lost. The function should
# only be used if all or almost all values are needed, and never
# in performance-critical code paths. If it's called on a node
# without children, then the result is equivalent to getValue().
#
Node.getValues = func {
var children = me.getChildren();
if(!size(children)) return me.getValue();
var val = {};
var numchld = {};
foreach(var c; children) {
var name = c.getName();
if(contains(numchld, name)) { var nc = numchld[name]; }
else {
var nc = size(me.getChildren(name));
numchld[name] = nc;
if (nc > 1 and !contains(val, name)) val[name] = [];
}
if(nc > 1) append(val[name], c.getValues());
else val[name] = c.getValues();
}
return val;
}
##
# Useful debugging utility. Recursively dumps the full state of a
# Node object to the console. Try binding "props.dump(props.globals)"
@ -123,8 +149,8 @@ var dump = func {
# if optional third argument is set and non-zero.
#
var copy = func(src, dest, attr = 0) {
foreach(c; src.getChildren()) {
name = c.getName() ~ "[" ~ c.getIndex() ~ "]";
foreach(var c; src.getChildren()) {
var name = c.getName() ~ "[" ~ c.getIndex() ~ "]";
copy(src.getNode(name), dest.getNode(name, 1), attr);
}
var type = src.getType();