/*----- Licensing notice --------------------------------------------------*
*
- * This file is part of the Sensble Object Design, an object system for C.
+ * This file is part of the Sensible Object Design, an object system for C.
*
- * SOD is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * The SOD Runtime Library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
*
- * SOD is distributed in the hope that it will be useful,
+ * The SOD Runtime is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
+ * GNU Library General Public License for more details.
*
- * You should have received a copy of the GNU General Public License
- * along with SOD; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * You should have received a copy of the GNU Library General Public
+ * License along with SOD; if not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
*/
/*----- Header files ------------------------------------------------------*/
* to know what C or S actually are.
*/
-void *sod_convert(const SodClass *cls, const void *p)
+void *sod_convert(const SodClass *cls, const void *obj)
{
- const struct sod_instance *inst = p;
+ const struct sod_instance *inst = obj;
const struct sod_vtable *vt = inst->_vt;
const SodClass *realcls = vt->_class;
const struct sod_chain *chain = find_chain(realcls, cls);
- if (!chain)
- return (0);
- return ((char *)p - vt->_base + chain->off_ichain);
+ if (!chain) return (0);
+ 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);
+}
+
+/* --- @sod_teardown@ --- *
+ *
+ * Arguments: @void *p@ = pointer to an instance to be torn down
+ *
+ * Returns: Zero if the object is torn down; nonzero if it refused for
+ * some reason.
+ *
+ * Use: Invokes the instance's `teardown' method to release any held
+ * resources.
+ *
+ * If this function returns nonzero, then the object is still
+ * active, and may still hold important resources. This is not
+ * intended to be a failure condition: failures in teardown are
+ * usually unrecoverable (or very hard to recover from) and
+ * should probably cause the program to abort. A refusal, on
+ * the other hand, means that the object is still live and
+ * shouldn't be deallocated, but that this is a normal situation
+ * and the caller shouldn't worry about it.
+ */
+
+int sod_teardown(void *p)
+{
+ SodObject *obj;
+
+ obj = SOD_CONVERT(SodObject, p);
+ return (SodObject_teardown(obj));
}
/*----- That's all, folks -------------------------------------------------*/