*** empty log message ***
[bpt/guile.git] / libguile / properties.c
CommitLineData
231a4ea8 1/* Copyright (C) 1995,1996,2000,2001, 2003 Free Software Foundation, Inc.
718eb176 2 *
73be1d9e
MV
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
718eb176 7 *
73be1d9e
MV
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
718eb176 12 *
73be1d9e
MV
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
718eb176 17
718eb176
MV
18
19\f
20
718eb176
MV
21#include "libguile/_scm.h"
22#include "libguile/hashtab.h"
23#include "libguile/alist.h"
24#include "libguile/root.h"
25#include "libguile/weaks.h"
26#include "libguile/validate.h"
27#include "libguile/eval.h"
28
29#include "libguile/properties.h"
30\f
31
32/* {Properties}
33 */
34
35SCM_DEFINE (scm_primitive_make_property, "primitive-make-property", 1, 0, 0,
36 (SCM not_found_proc),
37 "Create a @dfn{property token} that can be used with\n"
38 "@code{primitive-property-ref} and @code{primitive-property-set!}.\n"
39 "See @code{primitive-property-ref} for the significance of\n"
40 "@var{not_found_proc}.")
41#define FUNC_NAME s_scm_primitive_make_property
42{
43 if (not_found_proc != SCM_BOOL_F)
44 SCM_VALIDATE_PROC (SCM_ARG1, not_found_proc);
45 return scm_cons (not_found_proc, SCM_EOL);
46}
47#undef FUNC_NAME
48
22a52da1 49
718eb176
MV
50SCM_DEFINE (scm_primitive_property_ref, "primitive-property-ref", 2, 0, 0,
51 (SCM prop, SCM obj),
52 "Return the property @var{prop} of @var{obj}. When no value\n"
53 "has yet been associated with @var{prop} and @var{obj}, call\n"
54 "@var{not-found-proc} instead (see @code{primitive-make-property})\n"
55 "and use its return value. That value is also associated with\n"
56 "@var{obj} via @code{primitive-property-set!}. When\n"
57 "@var{not-found-proc} is @code{#f}, use @code{#f} as the\n"
58 "default value of @var{prop}.")
59#define FUNC_NAME s_scm_primitive_property_ref
60{
22a52da1 61 SCM h;
718eb176
MV
62
63 SCM_VALIDATE_CONS (SCM_ARG1, prop);
64
65 h = scm_hashq_get_handle (scm_properties_whash, obj);
22a52da1
DH
66 if (!SCM_FALSEP (h))
67 {
68 SCM assoc = scm_assq (prop, SCM_CDR (h));
69 if (!SCM_FALSEP (assoc))
70 return SCM_CDR (assoc);
71 }
718eb176
MV
72
73 if (SCM_FALSEP (SCM_CAR (prop)))
74 return SCM_BOOL_F;
75 else
76 {
fdc28395 77 SCM val = scm_call_2 (SCM_CAR (prop), prop, obj);
22a52da1 78 if (SCM_FALSEP (h))
718eb176
MV
79 h = scm_hashq_create_handle_x (scm_properties_whash, obj, SCM_EOL);
80 SCM_SETCDR (h, scm_acons (prop, val, SCM_CDR (h)));
81 return val;
82 }
83}
84#undef FUNC_NAME
85
22a52da1 86
718eb176
MV
87SCM_DEFINE (scm_primitive_property_set_x, "primitive-property-set!", 3, 0, 0,
88 (SCM prop, SCM obj, SCM val),
89 "Associate @var{code} with @var{prop} and @var{obj}.")
90#define FUNC_NAME s_scm_primitive_property_set_x
91{
92 SCM h, assoc;
93 SCM_VALIDATE_CONS (SCM_ARG1, prop);
94 h = scm_hashq_create_handle_x (scm_properties_whash, obj, SCM_EOL);
95 assoc = scm_assq (prop, SCM_CDR (h));
96 if (SCM_NIMP (assoc))
97 SCM_SETCDR (assoc, val);
98 else
99 {
100 assoc = scm_acons (prop, val, SCM_CDR (h));
101 SCM_SETCDR (h, assoc);
102 }
103 return SCM_UNSPECIFIED;
104}
105#undef FUNC_NAME
106
22a52da1 107
718eb176
MV
108SCM_DEFINE (scm_primitive_property_del_x, "primitive-property-del!", 2, 0, 0,
109 (SCM prop, SCM obj),
110 "Remove any value associated with @var{prop} and @var{obj}.")
111#define FUNC_NAME s_scm_primitive_property_del_x
112{
113 SCM h;
114 SCM_VALIDATE_CONS (SCM_ARG1, prop);
115 h = scm_hashq_get_handle (scm_properties_whash, obj);
22a52da1 116 if (!SCM_FALSEP (h))
718eb176
MV
117 SCM_SETCDR (h, scm_assq_remove_x (SCM_CDR (h), prop));
118 return SCM_UNSPECIFIED;
119}
120#undef FUNC_NAME
121
22a52da1 122
718eb176
MV
123void
124scm_init_properties ()
125{
231a4ea8 126 scm_properties_whash = scm_make_weak_key_hash_table (SCM_UNDEFINED);
718eb176
MV
127#include "libguile/properties.x"
128}
129
130
131/*
132 Local Variables:
133 c-file-style: "gnu"
134 End:
135*/