summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--struct.c16
-rw-r--r--struct.h1
-rw-r--r--txr.133
3 files changed, 50 insertions, 0 deletions
diff --git a/struct.c b/struct.c
index 064d0e21..7162dc3e 100644
--- a/struct.c
+++ b/struct.c
@@ -113,6 +113,7 @@ void struct_init(void)
reg_fun(intern(lit("copy-struct"), user_package), func_n1(copy_struct));
reg_fun(intern(lit("replace-struct"), user_package), func_n2(replace_struct));
reg_fun(intern(lit("clear-struct"), user_package), func_n2o(clear_struct, 1));
+ reg_fun(intern(lit("reset-struct"), user_package), func_n1(reset_struct));
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));
@@ -425,6 +426,21 @@ val replace_struct(val target, val source)
return target;
}
+val reset_struct(val strct)
+{
+ const val self = lit("reset-struct");
+ struct struct_inst *si = struct_handle(strct, self);
+ struct struct_type *st = coerce(struct struct_type *, si->type->co.handle);
+ cnum i;
+
+ for (i = 0; i < st->nslots; i++)
+ si->slot[i] = nil;
+
+ call_initfun_chain(st, strct);
+
+ return strct;
+}
+
static int cache_set_lookup(slot_cache_entry_t *set, cnum id)
{
if (set[0].id == id)
diff --git a/struct.h b/struct.h
index dff375eb..52a62fa8 100644
--- a/struct.h
+++ b/struct.h
@@ -34,6 +34,7 @@ val make_struct(val type, val plist, struct args *);
val copy_struct(val strct);
val clear_struct(val strct, val value);
val replace_struct(val target, val source);
+val reset_struct(val strct);
val find_struct_type(val sym);
val slot(val strct, val sym);
val slotset(val strct, val sym, val newval);
diff --git a/txr.1 b/txr.1
index 0259b8b3..6e612436 100644
--- a/txr.1
+++ b/txr.1
@@ -18745,6 +18745,39 @@ if omitted.
Note that finalizers are not executed prior to replacing
the slot values.
+.coNP Function @ reset-struct
+.synb
+.mets (reset-struct << struct-obj )
+.syne
+.desc
+The
+.code reset-struct
+function reinitializes the structure object
+.meta struct-obj
+as if it were being newly created.
+First, all the slots are set to
+.code nil
+as if by the
+.code clear-struct
+function. Then the slots are initialized by invoking the
+initialization functions, in order of the supertype ancestry, just as would be
+done for a new structure object created by
+.code make-struct
+with an empty
+.meta slot-init-plist
+and no boa arguments.
+
+Note that finalizers registered against
+.meta struct-obj
+are not invoked, and remain registered.
+If the structure has state which is cleaned up by
+finalizers, it is advisable to invoke them using
+.code call-finalizers
+prior to using
+.codn reset-struct ,
+or to take other measures to deal with the
+situation.
+
.coNP Function @ replace-struct
.synb
.mets (replace-struct < target-obj << source-obj )