objprop.c 3.1 KB

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