chiark / gitweb /
Replace the `init' class-slot function with an `init' message.
[sod] / lib / sod.c
index 1e8d0619042b588ef19340718d7de4c28e083193..aee28d5c12d239b0f1a3c7674825c934db9ae89a 100644 (file)
--- a/lib/sod.c
+++ b/lib/sod.c
@@ -120,4 +120,43 @@ void *sod_convert(const SodClass *cls, const void *obj)
   return ((char *)obj - vt->_base + chain->off_ichain);
 }
 
+/* --- @sod_init@, @sod_initv@ --- *
+ *
+ * Arguments:  @const SodClass *cls@ = class object for new instance
+ *             @void *p@ = pointer to storage for new instance
+ *             @va_list ap, ...@ = initialization keyword arguments
+ *
+ * Returns:    Pointer to the initialized instance.
+ *
+ * Use:                Initializes an instance in pre-allocated storage, and returns
+ *             a pointer to it.
+ *
+ *             This function will imprint the storage, and then send an
+ *             `initialize' message to the fresh instance containing the
+ *             provided keyword arguments.
+ *
+ *             It's usually convenient to use the macro @SOD_INIT@ rather
+ *             than calling @sod_init@ directly.
+ */
+
+void *sod_init(const SodClass *cls, void *p, ...)
+{
+  va_list ap;
+
+  va_start(ap, p);
+  sod_initv(cls, p, ap);
+  va_end(ap);
+  return (p);
+}
+
+void *sod_initv(const SodClass *cls, void *p, va_list ap)
+{
+  SodObject *obj;
+
+  cls->cls.imprint(p);
+  obj = SOD_CONVERT(SodObject, p);
+  SodObject_init__v(obj, ap);
+  return (p);
+}
+
 /*----- That's all, folks -------------------------------------------------*/