generalized-vectors.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* Copyright 1995-1998,2000-2006,2009-2014,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 "error.h"
  19. #include "gsubr.h"
  20. #include "generalized-vectors.h"
  21. struct scm_t_vector_ctor
  22. {
  23. SCM tag;
  24. SCM (*ctor)(SCM, SCM);
  25. };
  26. #define VECTOR_CTORS_N_STATIC_ALLOC 20
  27. static struct scm_t_vector_ctor vector_ctors[VECTOR_CTORS_N_STATIC_ALLOC];
  28. static int num_vector_ctors_registered = 0;
  29. void
  30. scm_i_register_vector_constructor (SCM type, SCM (*ctor)(SCM, SCM))
  31. {
  32. if (num_vector_ctors_registered >= VECTOR_CTORS_N_STATIC_ALLOC)
  33. /* need to increase VECTOR_CTORS_N_STATIC_ALLOC, buster */
  34. abort ();
  35. else
  36. {
  37. vector_ctors[num_vector_ctors_registered].tag = type;
  38. vector_ctors[num_vector_ctors_registered].ctor = ctor;
  39. num_vector_ctors_registered++;
  40. }
  41. }
  42. SCM_DEFINE (scm_make_generalized_vector, "make-generalized-vector", 2, 1, 0,
  43. (SCM type, SCM len, SCM fill),
  44. "Make a generalized vector")
  45. #define FUNC_NAME s_scm_make_generalized_vector
  46. {
  47. int i;
  48. for (i = 0; i < num_vector_ctors_registered; i++)
  49. if (scm_is_eq (vector_ctors[i].tag, type))
  50. return vector_ctors[i].ctor(len, fill);
  51. scm_wrong_type_arg_msg (FUNC_NAME, SCM_ARG1, type, "array type");
  52. }
  53. #undef FUNC_NAME
  54. void
  55. scm_init_generalized_vectors ()
  56. {
  57. #include "generalized-vectors.x"
  58. }