objprop.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* Copyright 1995-1996,2000-2001,2003,2006,2008-2011,2018
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include "alist.h"
  19. #include "async.h"
  20. #include "gsubr.h"
  21. #include "hashtab.h"
  22. #include "pairs.h"
  23. #include "weak-table.h"
  24. #include "objprop.h"
  25. /* {Object Properties}
  26. */
  27. static SCM object_whash;
  28. SCM_DEFINE (scm_object_properties, "object-properties", 1, 0, 0,
  29. (SCM obj),
  30. "Return @var{obj}'s property list.")
  31. #define FUNC_NAME s_scm_object_properties
  32. {
  33. return scm_weak_table_refq (object_whash, obj, SCM_EOL);
  34. }
  35. #undef FUNC_NAME
  36. SCM_DEFINE (scm_set_object_properties_x, "set-object-properties!", 2, 0, 0,
  37. (SCM obj, SCM alist),
  38. "Set @var{obj}'s property list to @var{alist}.")
  39. #define FUNC_NAME s_scm_set_object_properties_x
  40. {
  41. scm_weak_table_putq_x (object_whash, obj, alist);
  42. return alist;
  43. }
  44. #undef FUNC_NAME
  45. SCM_DEFINE (scm_object_property, "object-property", 2, 0, 0,
  46. (SCM obj, SCM key),
  47. "Return the property of @var{obj} with name @var{key}.")
  48. #define FUNC_NAME s_scm_object_property
  49. {
  50. SCM assoc;
  51. assoc = scm_assq (key, scm_object_properties (obj));
  52. return (scm_is_pair (assoc) ? SCM_CDR (assoc) : SCM_BOOL_F);
  53. }
  54. #undef FUNC_NAME
  55. SCM_DEFINE (scm_set_object_property_x, "set-object-property!", 3, 0, 0,
  56. (SCM obj, SCM key, SCM value),
  57. "In @var{obj}'s property list, set the property named @var{key}\n"
  58. "to @var{value}.")
  59. #define FUNC_NAME s_scm_set_object_property_x
  60. {
  61. SCM alist;
  62. SCM assoc;
  63. scm_i_pthread_mutex_lock (&scm_i_misc_mutex);
  64. alist = scm_weak_table_refq (object_whash, obj, SCM_EOL);
  65. assoc = scm_assq (key, alist);
  66. if (scm_is_pair (assoc))
  67. SCM_SETCDR (assoc, value);
  68. else
  69. scm_weak_table_putq_x (object_whash, obj, scm_acons (key, value, alist));
  70. scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
  71. return value;
  72. }
  73. #undef FUNC_NAME
  74. void
  75. scm_init_objprop ()
  76. {
  77. object_whash = scm_c_make_weak_table (0, SCM_WEAK_TABLE_KIND_KEY);
  78. #include "objprop.x"
  79. }