1/* -----------------------------------------------------------------------------
2 * void.c
3 *
4 *     Implements a "void" object that is really just a DOH container around
5 *     an arbitrary C object represented as a void *.
6 *
7 * Author(s) : David Beazley (beazley@cs.uchicago.edu)
8 *
9 * Copyright (C) 1999-2000.  The University of Chicago
10 * See the file LICENSE for information on usage and redistribution.
11 * ----------------------------------------------------------------------------- */
12
13char cvsroot_void_c[] = "$Id: void.c 9607 2006-12-05 22:11:40Z beazley $";
14
15#include "dohint.h"
16
17typedef struct {
18  void *ptr;
19  void (*del) (void *);
20} VoidObj;
21
22/* -----------------------------------------------------------------------------
23 * Void_delete()
24 *
25 * Delete a void object. Invokes the destructor supplied at the time of creation.
26 * ----------------------------------------------------------------------------- */
27
28static void Void_delete(DOH *vo) {
29  VoidObj *v = (VoidObj *) ObjData(vo);
30  if (v->del)
31    (*v->del) (v->ptr);
32  DohFree(v);
33}
34
35/* -----------------------------------------------------------------------------
36 * Void_copy()
37 *
38 * Copies a void object.  This is only a shallow copy. The object destruction
39 * function is not copied in order to avoid potential double-free problems.
40 * ----------------------------------------------------------------------------- */
41
42static DOH *Void_copy(DOH *vo) {
43  VoidObj *v = (VoidObj *) ObjData(vo);
44  return NewVoid(v->ptr, 0);
45}
46
47/* -----------------------------------------------------------------------------
48 * Void_data()
49 *
50 * Returns the void * stored in the object.
51 * ----------------------------------------------------------------------------- */
52
53static void *Void_data(DOH *vo) {
54  VoidObj *v = (VoidObj *) ObjData(vo);
55  return v->ptr;
56}
57
58static DohObjInfo DohVoidType = {
59  "VoidObj",			/* objname */
60  Void_delete,			/* doh_del */
61  Void_copy,			/* doh_copy */
62  0,				/* doh_clear */
63  0,				/* doh_str */
64  Void_data,			/* doh_data */
65  0,				/* doh_dump */
66  0,				/* doh_len */
67  0,				/* doh_hash    */
68  0,				/* doh_cmp */
69  0,				/* doh_equal    */
70  0,				/* doh_first    */
71  0,				/* doh_next     */
72  0,				/* doh_setfile */
73  0,				/* doh_getfile */
74  0,				/* doh_setline */
75  0,				/* doh_getline */
76  0,				/* doh_mapping */
77  0,				/* doh_sequence */
78  0,				/* doh_file  */
79  0,				/* doh_string */
80  0,				/* doh_reserved */
81  0,				/* clientdata */
82};
83
84/* -----------------------------------------------------------------------------
85 * NewVoid()
86 *
87 * Creates a new Void object given a void * and an optional destructor function.
88 * ----------------------------------------------------------------------------- */
89
90DOH *DohNewVoid(void *obj, void (*del) (void *)) {
91  VoidObj *v;
92  v = (VoidObj *) DohMalloc(sizeof(VoidObj));
93  v->ptr = obj;
94  v->del = del;
95  return DohObjMalloc(&DohVoidType, v);
96}
97