properties.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 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. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  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. #include "libguile/properties.h"
  29. /* {Properties}
  30. */
  31. SCM_DEFINE (scm_primitive_make_property, "primitive-make-property", 1, 0, 0,
  32. (SCM not_found_proc),
  33. "Create a @dfn{property token} that can be used with\n"
  34. "@code{primitive-property-ref} and @code{primitive-property-set!}.\n"
  35. "See @code{primitive-property-ref} for the significance of\n"
  36. "@var{not_found_proc}.")
  37. #define FUNC_NAME s_scm_primitive_make_property
  38. {
  39. if (not_found_proc != SCM_BOOL_F)
  40. SCM_VALIDATE_PROC (SCM_ARG1, not_found_proc);
  41. return scm_cons (not_found_proc, SCM_EOL);
  42. }
  43. #undef FUNC_NAME
  44. SCM_DEFINE (scm_primitive_property_ref, "primitive-property-ref", 2, 0, 0,
  45. (SCM prop, SCM obj),
  46. "Return the property @var{prop} of @var{obj}.\n"
  47. "\n"
  48. "When no value has yet been associated with @var{prop} and\n"
  49. "@var{obj}, the @var{not-found-proc} from @var{prop} is used. A\n"
  50. "call @code{(@var{not-found-proc} @var{prop} @var{obj})} is made\n"
  51. "and the result set as the property value. If\n"
  52. "@var{not-found-proc} is @code{#f} then @code{#f} is the\n"
  53. "property value.")
  54. #define FUNC_NAME s_scm_primitive_property_ref
  55. {
  56. SCM h;
  57. SCM_VALIDATE_CONS (SCM_ARG1, prop);
  58. h = scm_hashq_get_handle (scm_properties_whash, obj);
  59. if (scm_is_true (h))
  60. {
  61. SCM assoc = scm_assq (prop, SCM_CDR (h));
  62. if (scm_is_true (assoc))
  63. return SCM_CDR (assoc);
  64. }
  65. if (scm_is_false (SCM_CAR (prop)))
  66. return SCM_BOOL_F;
  67. else
  68. {
  69. SCM val = scm_call_2 (SCM_CAR (prop), prop, obj);
  70. if (scm_is_false (h))
  71. h = scm_hashq_create_handle_x (scm_properties_whash, obj, SCM_EOL);
  72. SCM_SETCDR (h, scm_acons (prop, val, SCM_CDR (h)));
  73. return val;
  74. }
  75. }
  76. #undef FUNC_NAME
  77. SCM_DEFINE (scm_primitive_property_set_x, "primitive-property-set!", 3, 0, 0,
  78. (SCM prop, SCM obj, SCM val),
  79. "Set the property @var{prop} of @var{obj} to @var{val}.")
  80. #define FUNC_NAME s_scm_primitive_property_set_x
  81. {
  82. SCM h, assoc;
  83. SCM_VALIDATE_CONS (SCM_ARG1, prop);
  84. h = scm_hashq_create_handle_x (scm_properties_whash, obj, SCM_EOL);
  85. assoc = scm_assq (prop, SCM_CDR (h));
  86. if (SCM_NIMP (assoc))
  87. SCM_SETCDR (assoc, val);
  88. else
  89. {
  90. assoc = scm_acons (prop, val, SCM_CDR (h));
  91. SCM_SETCDR (h, assoc);
  92. }
  93. return SCM_UNSPECIFIED;
  94. }
  95. #undef FUNC_NAME
  96. SCM_DEFINE (scm_primitive_property_del_x, "primitive-property-del!", 2, 0, 0,
  97. (SCM prop, SCM obj),
  98. "Remove any value associated with @var{prop} and @var{obj}.")
  99. #define FUNC_NAME s_scm_primitive_property_del_x
  100. {
  101. SCM h;
  102. SCM_VALIDATE_CONS (SCM_ARG1, prop);
  103. h = scm_hashq_get_handle (scm_properties_whash, obj);
  104. if (scm_is_true (h))
  105. SCM_SETCDR (h, scm_assq_remove_x (SCM_CDR (h), prop));
  106. return SCM_UNSPECIFIED;
  107. }
  108. #undef FUNC_NAME
  109. void
  110. scm_init_properties ()
  111. {
  112. scm_properties_whash = scm_make_weak_key_hash_table (SCM_UNDEFINED);
  113. #include "libguile/properties.x"
  114. }
  115. /*
  116. Local Variables:
  117. c-file-style: "gnu"
  118. End:
  119. */