custom.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* Declarations for custom, native types.
  2. This file is part of khipu.
  3. khipu is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. #ifndef __KP_CUSTOM__
  14. #define __KP_CUSTOM__
  15. #include "interp.hpp"
  16. KP_DECLS_BEGIN
  17. struct visitor
  18. {
  19. virtual void operator() (interpreter *, object)
  20. {
  21. }
  22. };
  23. struct custom_base : public finobj
  24. {
  25. typedef void (*constructor) (void *);
  26. static const int code = typecode::CUSTOM;
  27. object *type_ptr;
  28. object type () const
  29. {
  30. return (*this->type_ptr);
  31. }
  32. virtual void do_visit (interpreter *, visitor&)
  33. {
  34. }
  35. void visit (interpreter *interp, visitor& vs)
  36. {
  37. vs (interp, this->type ());
  38. this->do_visit (interp, vs);
  39. }
  40. virtual void fill_type (interpreter *, object&, object&)
  41. {
  42. }
  43. static custom_base* alloc_raw (size_t size);
  44. static result<custom_base*> xalloc (interpreter *interp, object *tp,
  45. const char *name, custom_base *bp,
  46. constructor cfn);
  47. static result<custom_base*> xalloc (interpreter *interp, object *tp,
  48. const char *name, uint32_t len,
  49. custom_base *bp, constructor cfn);
  50. static result<custom_base*> xalloc (interpreter *interp, object *tp,
  51. object name, custom_base *bp,
  52. constructor cfn);
  53. virtual ~custom_base ()
  54. {
  55. }
  56. };
  57. inline custom_base*
  58. as_custom (object obj)
  59. {
  60. /* We need to jump through finobj because that's where the method 'as_obj'
  61. * is called from. And since custom types have a vtable, we need to preserve
  62. * the original chain of casts. */
  63. return ((custom_base *)(finobj *)unmask (obj));
  64. }
  65. template <typename T>
  66. struct custom : public custom_base
  67. {
  68. static object type_desc;
  69. typedef custom<T> self_t;
  70. static void
  71. construct (void *arg)
  72. {
  73. new (arg) T ();
  74. }
  75. static result<T*> alloc (interpreter *interp, const char *name)
  76. {
  77. auto ret = KP_TRY (self_t::xalloc (interp, &self_t::type_desc, name,
  78. self_t::alloc_raw (sizeof (T)),
  79. self_t::construct));
  80. return ((T *)ret);
  81. }
  82. static result<T*> alloc (interpreter *interp, const char *name, size_t len)
  83. {
  84. auto ret = KP_TRY (self_t::xalloc (interp, &self_t::type_desc, name,
  85. len, self_t::alloc_raw (sizeof (T)),
  86. self_t::construct));
  87. return ((T *)ret);
  88. }
  89. static result<T*> alloc (interpreter *interp, object name)
  90. {
  91. auto ret = KP_TRY (self_t::xalloc (interp, &self_t::type_desc, name,
  92. self_t::alloc_raw (sizeof (T)),
  93. self_t::construct));
  94. return ((T *)ret);
  95. }
  96. static bool type_p (object obj)
  97. {
  98. return (itype (obj) == typecode::CUSTOM &&
  99. as_custom(obj)->type_ptr == &custom<T>::type_desc);
  100. }
  101. };
  102. template <typename T>
  103. object custom<T>::type_desc = 0;
  104. KP_DECLS_END
  105. #endif