objprop.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* Copyright (C) 1995,1996, 2000, 2001, 2003, 2006, 2008, 2009, 2010, 2011 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/async.h"
  23. #include "libguile/hashtab.h"
  24. #include "libguile/alist.h"
  25. #include "libguile/root.h"
  26. #include "libguile/objprop.h"
  27. /* {Object Properties}
  28. */
  29. static SCM object_whash;
  30. SCM_DEFINE (scm_object_properties, "object-properties", 1, 0, 0,
  31. (SCM obj),
  32. "Return @var{obj}'s property list.")
  33. #define FUNC_NAME s_scm_object_properties
  34. {
  35. return scm_weak_table_refq (object_whash, obj, SCM_EOL);
  36. }
  37. #undef FUNC_NAME
  38. SCM_DEFINE (scm_set_object_properties_x, "set-object-properties!", 2, 0, 0,
  39. (SCM obj, SCM alist),
  40. "Set @var{obj}'s property list to @var{alist}.")
  41. #define FUNC_NAME s_scm_set_object_properties_x
  42. {
  43. scm_weak_table_putq_x (object_whash, obj, alist);
  44. return alist;
  45. }
  46. #undef FUNC_NAME
  47. SCM_DEFINE (scm_object_property, "object-property", 2, 0, 0,
  48. (SCM obj, SCM key),
  49. "Return the property of @var{obj} with name @var{key}.")
  50. #define FUNC_NAME s_scm_object_property
  51. {
  52. SCM assoc;
  53. assoc = scm_assq (key, scm_object_properties (obj));
  54. return (scm_is_pair (assoc) ? SCM_CDR (assoc) : SCM_BOOL_F);
  55. }
  56. #undef FUNC_NAME
  57. SCM_DEFINE (scm_set_object_property_x, "set-object-property!", 3, 0, 0,
  58. (SCM obj, SCM key, SCM value),
  59. "In @var{obj}'s property list, set the property named @var{key}\n"
  60. "to @var{value}.")
  61. #define FUNC_NAME s_scm_set_object_property_x
  62. {
  63. SCM alist;
  64. SCM assoc;
  65. scm_i_pthread_mutex_lock (&scm_i_misc_mutex);
  66. alist = scm_weak_table_refq (object_whash, obj, SCM_EOL);
  67. assoc = scm_assq (key, alist);
  68. if (scm_is_pair (assoc))
  69. SCM_SETCDR (assoc, value);
  70. else
  71. scm_weak_table_putq_x (object_whash, obj, scm_acons (key, value, alist));
  72. scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
  73. return value;
  74. }
  75. #undef FUNC_NAME
  76. void
  77. scm_init_objprop ()
  78. {
  79. object_whash = scm_c_make_weak_table (0, SCM_WEAK_TABLE_KIND_KEY);
  80. #include "libguile/objprop.x"
  81. }
  82. /*
  83. Local Variables:
  84. c-file-style: "gnu"
  85. End:
  86. */