objprop.c 2.8 KB

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