Implementation for the R6RS (rnrs sorting) library.
[bpt/guile.git] / libguile / objprop.c
1 /* Copyright (C) 1995,1996, 2000, 2001, 2003, 2006, 2008, 2009 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 License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful, but
9 * 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
16 * 02110-1301 USA
17 */
18
19
20 \f
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include "libguile/_scm.h"
26 #include "libguile/async.h"
27 #include "libguile/hashtab.h"
28 #include "libguile/alist.h"
29 #include "libguile/root.h"
30 #include "libguile/weaks.h"
31
32 #include "libguile/objprop.h"
33 \f
34
35 /* {Object Properties}
36 */
37
38 static SCM object_whash;
39
40 SCM_DEFINE (scm_object_properties, "object-properties", 1, 0, 0,
41 (SCM obj),
42 "Return @var{obj}'s property list.")
43 #define FUNC_NAME s_scm_object_properties
44 {
45 return scm_hashq_ref (object_whash, obj, SCM_EOL);
46 }
47 #undef FUNC_NAME
48
49
50 SCM_DEFINE (scm_set_object_properties_x, "set-object-properties!", 2, 0, 0,
51 (SCM obj, SCM alist),
52 "Set @var{obj}'s property list to @var{alist}.")
53 #define FUNC_NAME s_scm_set_object_properties_x
54 {
55 SCM handle = scm_hashq_create_handle_x (object_whash, obj, alist);
56 SCM_SETCDR (handle, alist);
57 return alist;
58 }
59 #undef FUNC_NAME
60
61 SCM_DEFINE (scm_object_property, "object-property", 2, 0, 0,
62 (SCM obj, SCM key),
63 "Return the property of @var{obj} with name @var{key}.")
64 #define FUNC_NAME s_scm_object_property
65 {
66 SCM assoc;
67 assoc = scm_assq (key, scm_object_properties (obj));
68 return (SCM_NIMP (assoc) ? SCM_CDR (assoc) : SCM_BOOL_F);
69 }
70 #undef FUNC_NAME
71
72 SCM_DEFINE (scm_set_object_property_x, "set-object-property!", 3, 0, 0,
73 (SCM obj, SCM key, SCM value),
74 "In @var{obj}'s property list, set the property named @var{key}\n"
75 "to @var{value}.")
76 #define FUNC_NAME s_scm_set_object_property_x
77 {
78 SCM h;
79 SCM assoc;
80 h = scm_hashq_create_handle_x (object_whash, obj, SCM_EOL);
81 SCM_CRITICAL_SECTION_START;
82 assoc = scm_assq (key, SCM_CDR (h));
83 if (SCM_NIMP (assoc))
84 SCM_SETCDR (assoc, value);
85 else
86 {
87 assoc = scm_acons (key, value, SCM_CDR (h));
88 SCM_SETCDR (h, assoc);
89 }
90 SCM_CRITICAL_SECTION_END;
91 return value;
92 }
93 #undef FUNC_NAME
94
95
96 void
97 scm_init_objprop ()
98 {
99 object_whash = scm_make_weak_key_hash_table (SCM_UNDEFINED);
100 #include "libguile/objprop.x"
101 }
102
103
104 /*
105 Local Variables:
106 c-file-style: "gnu"
107 End:
108 */