diff options
-rw-r--r-- | struct.c | 15 | ||||
-rw-r--r-- | struct.h | 1 | ||||
-rw-r--r-- | txr.1 | 18 |
3 files changed, 34 insertions, 0 deletions
@@ -111,6 +111,7 @@ void struct_init(void) reg_fun(intern(lit("super"), user_package), func_n1(super)); reg_fun(intern(lit("make-struct"), user_package), func_n2v(make_struct)); reg_fun(intern(lit("copy-struct"), user_package), func_n1(copy_struct)); + reg_fun(intern(lit("clear-struct"), user_package), func_n2o(clear_struct, 1)); reg_fun(intern(lit("slot"), user_package), func_n2(slot)); reg_fun(intern(lit("slotset"), user_package), func_n3(slotset)); reg_fun(intern(lit("static-slot"), user_package), func_n2(static_slot)); @@ -393,6 +394,20 @@ val copy_struct(val strct) return copy; } +val clear_struct(val strct, val value) +{ + const val self = lit("clear-struct"); + struct struct_inst *si = struct_handle(strct, self); + struct struct_type *st = coerce(struct struct_type *, si->type->co.handle); + val clear_val = default_bool_arg(value); + cnum i; + + for (i = 0; i < st->nslots; i++) + si->slot[i] = clear_val; + + return strct; +} + static int cache_set_lookup(slot_cache_entry_t *set, cnum id) { if (set[0].id == id) @@ -32,6 +32,7 @@ val struct_type_p(val obj); val super(val type); val make_struct(val type, val plist, struct args *); val copy_struct(val strct); +val clear_struct(val strct, val value); val find_struct_type(val sym); val slot(val strct, val sym); val slotset(val strct, val sym, val newval); @@ -18727,6 +18727,24 @@ function returns the structure type object which defines the type of the structure object .metn struct-obj . +.coNP Function @ clear-struct +.synb +.mets (clear-struct < struct-obj <> [ value ]) +.syne +.desc +The +.code clear-struct +replaces all instance slots of +.meta struct-obj +with +.metn value , +which defaults to +.code nil +if omitted. + +Note that finalizers are not executed prior to replacing +the slot values. + .coNP Function @ method .synb .mets (method < struct-obj << slot-name ) |