further ecmascript work
[bpt/guile.git] / libguile / properties.c
1 /* Copyright (C) 1995,1996,2000,2001, 2003, 2006, 2008 Free Software Foundation, Inc.
2 *
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.
7 *
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.
12 *
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18
19 \f
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include "libguile/_scm.h"
25 #include "libguile/hashtab.h"
26 #include "libguile/alist.h"
27 #include "libguile/root.h"
28 #include "libguile/weaks.h"
29 #include "libguile/validate.h"
30 #include "libguile/eval.h"
31
32 #include "libguile/properties.h"
33 \f
34
35 /* {Properties}
36 */
37
38 SCM_DEFINE (scm_primitive_make_property, "primitive-make-property", 1, 0, 0,
39 (SCM not_found_proc),
40 "Create a @dfn{property token} that can be used with\n"
41 "@code{primitive-property-ref} and @code{primitive-property-set!}.\n"
42 "See @code{primitive-property-ref} for the significance of\n"
43 "@var{not_found_proc}.")
44 #define FUNC_NAME s_scm_primitive_make_property
45 {
46 if (not_found_proc != SCM_BOOL_F)
47 SCM_VALIDATE_PROC (SCM_ARG1, not_found_proc);
48 return scm_cons (not_found_proc, SCM_EOL);
49 }
50 #undef FUNC_NAME
51
52
53 SCM_DEFINE (scm_primitive_property_ref, "primitive-property-ref", 2, 0, 0,
54 (SCM prop, SCM obj),
55 "Return the property @var{prop} of @var{obj}.\n"
56 "\n"
57 "When no value has yet been associated with @var{prop} and\n"
58 "@var{obj}, the @var{not-found-proc} from @var{prop} is used. A\n"
59 "call @code{(@var{not-found-proc} @var{prop} @var{obj})} is made\n"
60 "and the result set as the property value. If\n"
61 "@var{not-found-proc} is @code{#f} then @code{#f} is the\n"
62 "property value.")
63 #define FUNC_NAME s_scm_primitive_property_ref
64 {
65 SCM h;
66
67 SCM_VALIDATE_CONS (SCM_ARG1, prop);
68
69 h = scm_hashq_get_handle (scm_properties_whash, obj);
70 if (scm_is_true (h))
71 {
72 SCM assoc = scm_assq (prop, SCM_CDR (h));
73 if (scm_is_true (assoc))
74 return SCM_CDR (assoc);
75 }
76
77 if (scm_is_false (SCM_CAR (prop)))
78 return SCM_BOOL_F;
79 else
80 {
81 SCM val = scm_call_2 (SCM_CAR (prop), prop, obj);
82 if (scm_is_false (h))
83 h = scm_hashq_create_handle_x (scm_properties_whash, obj, SCM_EOL);
84 SCM_SETCDR (h, scm_acons (prop, val, SCM_CDR (h)));
85 return val;
86 }
87 }
88 #undef FUNC_NAME
89
90
91 SCM_DEFINE (scm_primitive_property_set_x, "primitive-property-set!", 3, 0, 0,
92 (SCM prop, SCM obj, SCM val),
93 "Set the property @var{prop} of @var{obj} to @var{val}.")
94 #define FUNC_NAME s_scm_primitive_property_set_x
95 {
96 SCM h, assoc;
97 SCM_VALIDATE_CONS (SCM_ARG1, prop);
98 h = scm_hashq_create_handle_x (scm_properties_whash, obj, SCM_EOL);
99 assoc = scm_assq (prop, SCM_CDR (h));
100 if (SCM_NIMP (assoc))
101 SCM_SETCDR (assoc, val);
102 else
103 {
104 assoc = scm_acons (prop, val, SCM_CDR (h));
105 SCM_SETCDR (h, assoc);
106 }
107 return SCM_UNSPECIFIED;
108 }
109 #undef FUNC_NAME
110
111
112 SCM_DEFINE (scm_primitive_property_del_x, "primitive-property-del!", 2, 0, 0,
113 (SCM prop, SCM obj),
114 "Remove any value associated with @var{prop} and @var{obj}.")
115 #define FUNC_NAME s_scm_primitive_property_del_x
116 {
117 SCM h;
118 SCM_VALIDATE_CONS (SCM_ARG1, prop);
119 h = scm_hashq_get_handle (scm_properties_whash, obj);
120 if (scm_is_true (h))
121 SCM_SETCDR (h, scm_assq_remove_x (SCM_CDR (h), prop));
122 return SCM_UNSPECIFIED;
123 }
124 #undef FUNC_NAME
125
126
127 void
128 scm_init_properties ()
129 {
130 scm_properties_whash = scm_make_weak_key_hash_table (SCM_UNDEFINED);
131 #include "libguile/properties.x"
132 }
133
134
135 /*
136 Local Variables:
137 c-file-style: "gnu"
138 End:
139 */