system.cdc
./MinimalCore-2.0/src/system.cdc · 4.4 KB
From the archive MinimalCore-2.0.tar.gz
Download the archive « Back to the archive contents
// ------------------------------------------------------------------
// Root Object
// $root already exists
object $root;
var child_index = 0;
var child = 0;
var inited = 1;
private method initialize: nooverride {
var ancestors, ancestor, pos, len, method;
if (inited)
throw(~perm, "Already initialized.");
ancestors = ancestors();
len = ancestors.length();
for pos in [0 .. len - 1] {
ancestor = ancestors[len - pos];
method = tosym("init_" + tostr(ancestor.objname('symbol)));
catch ~methodnf {
if (.find_method(method) != ancestor) {
throw(~perm, "Initialization method for " + ancestor.objname()
+ " in wrong place("
+ find_method(method).objname() + ")");
}
.(method)();
}
}
inited = 1;
child = 1;
};
private method uninitialize: nooverride {
var ancestor, p;
for ancestor in (ancestors()) {
method = tosym("uninit_" + tostr(ancestor.objname('symbol)));
catch ~methodnf {
if (.find_method(method) != ancestor)
throw(~perm, "UnInitialization method for " +
ancestor.objname() + " in wrong place (" +
.find_method(method).objname() + ")");
.(method)();
}
}
for p in (.parameters())
.del_parameter('p);
};
public method spawn: nooverride {
arg [other_parents];
var base, obj, name;
if (child)
throw(~perm, "Attempt to spawn a non-defining object.");
name = .objname('symbol);
base = tostr(name);
while ((| get_dbref(name) |)) {
child_index = child_index + 1;
name = tosym(base + "_" + tostr(child_index));
}
obj = (> create([this()] + other_parents) <);
catch any {
(> obj.initialize() <);
} with handler {
.log($parse.traceback(traceback()));
if (!(| obj.destroy() |))
throw(~ack, "Unable to destroy aborted attempt", traceback());
rethrow(error());
}
(> set_objname(name) <);
return obj;
};
public method destroy: nooverride {
if (!child)
throw(~perm, "Attempt to destroy a defining parent.");
(| .uninitialize() |);
destroy(this());
};
public method add_objname: nooverride {
arg name;
var regexp;
if (type(name) != 'symbol)
throw(~perm, "Name is not a symbol.");
name = tostr(name);
regexp = "[a-z0-9_]*".match_regexp(name);
if (regexp[1][2] != name.length())
throw(~invname, "Names may only contain alphanumeric characters or \"_\".");
name = tosym(name);
(> set_objname(name) <);
};
public method del_objname: nooverride {
arg name;
(> del_objname(name) <);
};
public method objname: nooverride {
arg [args];
if (args)
return objname();
if (objname)
return "$" + tostr(objname());
return toliteral(this());
};
public method parents: nooverride {
return parents();
};
public method children: nooverride {
return children();
};
public method descendants: nooverride {
var kids, i, c;
kids = children();
while ((| c = kids[i = i + 1] |))
kids = union(kids, c.children());
return kids;
};
public method find_method {
arg method;
return (> find_method(method) <);
};
public method debug: nooverride {
arg [what];
var l, line;
line = "";
for l in (what)
line = line + " " + (type(l) == 'string ? l | $parse.unparse(l));
log("DEBUG: " + $parse.unparse(what));
};
public method log: nooverride {
arg what;
var line;
if (type(what) == 'string) {
log(what);
} else if (type(what) == 'list) {
for line in (what)
.log(line);
} else {
throw(~invarg, "Log must be called with a string or a list of strings");
}
};
// ------------------------------------------------------------------
// System Object
// $sys also exists already, as a child of $root
object $sys;
var $root inited = 1;
var $root objname = 'sys;
var startup = [$login_daemon];
driver method startup {
arg [args];
var obj, hmm, l;
for obj in (startup) {
catch any {
log("Calling " + obj.objname() + ".startup()");
(> obj.startup() <);
} with handler {
for l in ($parse.traceback(traceback()))
log(l);
}
}
set_heartbeat(5);
};
driver method signal {
arg [args];
};