khipu.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* Global declarations for the khipu API.
  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 __KHIPU__
  14. #define __KHIPU__ 1
  15. #include "integer.hpp"
  16. #include "floatp.hpp"
  17. #include "memory.hpp"
  18. #include "cons.hpp"
  19. #include "symbol.hpp"
  20. #include "stream.hpp"
  21. #include "array.hpp"
  22. #include "str.hpp"
  23. #include "table.hpp"
  24. #include "thread.hpp"
  25. #include "tuple.hpp"
  26. #include "function.hpp"
  27. #include "io.hpp"
  28. #include "xtime.hpp"
  29. #include "coro.hpp"
  30. #include "bytecode.hpp"
  31. #include "builtins.hpp"
  32. #include "event.hpp"
  33. #include "types.hpp"
  34. #include "custom.hpp"
  35. KP_DECLS_BEGIN
  36. KP_EXPORT bool khipu_init (char *base, uint32_t size);
  37. template <typename T, bool B>
  38. struct objtype_helper
  39. {
  40. static void* unmask_ptr (object obj)
  41. {
  42. return (unmask (obj));
  43. }
  44. static bool check_type (object)
  45. {
  46. return (true);
  47. }
  48. };
  49. template <typename T>
  50. struct objtype_helper<T, true>
  51. {
  52. static custom_base* unmask_ptr (object obj)
  53. {
  54. return (as_custom (obj));
  55. }
  56. static bool check_type (object obj)
  57. {
  58. return (T::type_p (obj));
  59. }
  60. };
  61. template <typename T>
  62. struct objtype
  63. {
  64. typedef T* ret_type;
  65. static const int code = T::code;
  66. static inline ret_type get (object obj, int tp, bool& got)
  67. {
  68. if (tp == code || (code == typecode::BVECTOR && tp == typecode::STR))
  69. return ((ret_type)objtype_helper<
  70. T, code == typecode::CUSTOM>::unmask_ptr (obj));
  71. else if (tp == typecode::INSTANCE)
  72. {
  73. object tmp = builtin_member (obj);
  74. return (objtype<T>::get (tmp, itype (tmp), got));
  75. }
  76. got = false;
  77. return (nullptr);
  78. }
  79. };
  80. template <typename T>
  81. inline T objtype_get_num (object obj, int tp, bool& got)
  82. {
  83. switch (tp)
  84. {
  85. case typecode::INT:
  86. return ((T)as_int (obj));
  87. case typecode::CHAR:
  88. return ((T)as_char (obj));
  89. case typecode::FLOAT:
  90. return ((T)as_float (obj));
  91. default:
  92. got = false;
  93. return (T ());
  94. }
  95. }
  96. #define OBJTYPE_NUM(typ) \
  97. template <> \
  98. struct objtype<typ> \
  99. { \
  100. typedef typ ret_type; \
  101. static const int code = typecode::INT; \
  102. \
  103. static inline ret_type get (object obj, int tp, bool& got) \
  104. { \
  105. return (objtype_get_num<typ> (obj, tp, got)); \
  106. } \
  107. }
  108. OBJTYPE_NUM (int16_t);
  109. OBJTYPE_NUM (uint16_t);
  110. OBJTYPE_NUM (int32_t);
  111. OBJTYPE_NUM (uint32_t);
  112. OBJTYPE_NUM (float);
  113. OBJTYPE_NUM (double);
  114. #undef OBJTYPE_NUM
  115. #define OBJTYPE_NUM(typ) \
  116. template <> \
  117. struct objtype<typ> \
  118. { \
  119. typedef typ ret_type; \
  120. static const int code = typecode::INT; \
  121. \
  122. static inline ret_type get (object obj, int tp, bool& got) \
  123. { \
  124. if (tp == typecode::BIGINT) \
  125. { \
  126. ret_type tmp; \
  127. got = bigint::cast (obj, tmp); \
  128. return (tmp); \
  129. } \
  130. \
  131. return (objtype_get_num<typ> (obj, tp, got)); \
  132. } \
  133. }
  134. OBJTYPE_NUM (int64_t);
  135. OBJTYPE_NUM (uint64_t);
  136. #undef OBJTYPE_NUM
  137. template <>
  138. struct objtype<char>
  139. {
  140. typedef uint32_t ret_type;
  141. static const int code = typecode::CHAR;
  142. static inline ret_type get (object obj, int tp, bool& got)
  143. {
  144. if (tp == typecode::CHAR)
  145. return (as_char (obj));
  146. else if (tp == typecode::INT)
  147. return (as_int (obj));
  148. got = false;
  149. return (0);
  150. }
  151. };
  152. template <typename T>
  153. bool as (object obj, typename objtype<T>::ret_type& out)
  154. {
  155. bool rv = true;
  156. out = objtype<T>::get (obj, itype (obj), rv);
  157. return (rv);
  158. }
  159. template <typename T>
  160. typename objtype<T>::ret_type as (object obj)
  161. {
  162. typename objtype<T>::ret_type ret = 0;
  163. (void)as<T> (obj, ret);
  164. return (ret);
  165. }
  166. KP_DECLS_END
  167. #endif