| 1 |
#ifndef RBIMPL_INTERN_ENUMERATOR_H /*-*-C++-*-vi:se ft=cpp:*/ |
| 2 |
#define RBIMPL_INTERN_ENUMERATOR_H |
| 3 |
/** |
| 4 |
* @file |
| 5 |
* @author Ruby developers <[email protected]> |
| 6 |
* @copyright This file is a part of the programming language Ruby. |
| 7 |
* Permission is hereby granted, to either redistribute and/or |
| 8 |
* modify this file, provided that the conditions mentioned in the |
| 9 |
* file COPYING are met. Consult the file for details. |
| 10 |
* @warning Symbols prefixed with either `RBIMPL` or `rbimpl` are |
| 11 |
* implementation details. Don't take them as canon. They could |
| 12 |
* rapidly appear then vanish. The name (path) of this header file |
| 13 |
* is also an implementation detail. Do not expect it to persist |
| 14 |
* at the place it is now. Developers are free to move it anywhere |
| 15 |
* anytime at will. |
| 16 |
* @note To ruby-core: remember that this header can be possibly |
| 17 |
* recursively included from extension libraries written in C++. |
| 18 |
* Do not expect for instance `__VA_ARGS__` is always available. |
| 19 |
* We assume C99 for ruby itself but we don't assume languages of |
| 20 |
* extension libraries. They could be written in C++98. |
| 21 |
* @brief Public APIs related to ::rb_cEnumerator. |
| 22 |
*/ |
| 23 |
#include "ruby/internal/attr/nonnull.h" |
| 24 |
#include "ruby/internal/dllexport.h" |
| 25 |
#include "ruby/internal/intern/eval.h" /* rb_frame_this_func */ |
| 26 |
#include "ruby/internal/iterator.h" /* rb_block_given_p */ |
| 27 |
#include "ruby/internal/symbol.h" |
| 28 |
#include "ruby/internal/value.h" |
| 29 |
|
| 30 |
RBIMPL_SYMBOL_EXPORT_BEGIN() |
| 31 |
|
| 32 |
/** |
| 33 |
* This is the type of functions that rb_enumeratorize_with_size() expects. In |
| 34 |
* theory an enumerator can have indefinite number of elements, but in practice |
| 35 |
* it often is the case we can compute the size of an enumerator beforehand. |
| 36 |
* If your enumerator has such property, supply a function that calculates such |
| 37 |
* values. |
| 38 |
* |
| 39 |
* @param[in] recv The original receiver of the enumerator. |
| 40 |
* @param[in] argv Arguments passed to `Object#enum_for` etc. |
| 41 |
* @param[in] eobj The enumerator object. |
| 42 |
* @return The size of `eobj`, in ::rb_cNumeric, or ::RUBY_Qnil if the size |
| 43 |
* is not known until we actually iterate. |
| 44 |
*/ |
| 45 |
typedef VALUE rb_enumerator_size_func(VALUE recv, VALUE argv, VALUE eobj); |
| 46 |
|
| 47 |
/** |
| 48 |
* Decomposed `Enumerator::ArithmeicSequence`. This is a subclass of |
| 49 |
* ::rb_cEnumerator, which represents a sequence of numbers with common |
| 50 |
* difference. Internal data structure of the class is opaque to users, but |
| 51 |
* you can obtain a decomposed one using rb_arithmetic_sequence_extract(). |
| 52 |
*/ |
| 53 |
typedef struct { |
| 54 |
VALUE begin; /**< "Left" or "lowest" endpoint of the sequence. */ |
| 55 |
VALUE end; /**< "Right" or "highest" endpoint of the sequence.*/ |
| 56 |
VALUE step; /**< Step between a sequence. */ |
| 57 |
int exclude_end; /**< Whether the endpoint is open or closed. */ |
| 58 |
} rb_arithmetic_sequence_components_t; |
| 59 |
|
| 60 |
/* enumerator.c */ |
| 61 |
|
| 62 |
/** |
| 63 |
* Constructs an enumerator. This roughly resembles `Object#enum_for`. |
| 64 |
* |
| 65 |
* @param[in] recv A receiver of `meth`. |
| 66 |
* @param[in] meth Method ID in a symbol object. |
| 67 |
* @param[in] argc Number of objects of `argv`. |
| 68 |
* @param[in] argv Arguments passed to `meth`. |
| 69 |
* @exception rb_eTypeError `meth` is not an instance of ::rb_cSymbol. |
| 70 |
* @return A new instance of ::rb_cEnumerator which, when yielded, |
| 71 |
* enumerates by calling `meth` on `recv` with `argv`. |
| 72 |
*/ |
| 73 |
VALUE rb_enumeratorize(VALUE recv, VALUE meth, int argc, const VALUE *argv); |
| 74 |
|
| 75 |
/** |
| 76 |
* Identical to rb_enumeratorize(), except you can additionally specify the |
| 77 |
* size function of return value. |
| 78 |
* |
| 79 |
* @param[in] recv A receiver of `meth`. |
| 80 |
* @param[in] meth Method ID in a symbol object. |
| 81 |
* @param[in] argc Number of objects of `argv`. |
| 82 |
* @param[in] argv Arguments passed to `meth`. |
| 83 |
* @param[in] func Size calculator. |
| 84 |
* @exception rb_eTypeError `meth` is not an instance of ::rb_cSymbol. |
| 85 |
* @return A new instance of ::rb_cEnumerator which, when yielded, |
| 86 |
* enumerates by calling `meth` on `recv` with `argv`. |
| 87 |
* @note `func` can be zero, which means the size is unknown. |
| 88 |
*/ |
| 89 |
VALUE rb_enumeratorize_with_size(VALUE recv, VALUE meth, int argc, const VALUE *argv, rb_enumerator_size_func *func); |
| 90 |
|
| 91 |
/** |
| 92 |
* Identical to rb_enumeratorize_with_func(), except you can specify how to |
| 93 |
* handle the last element of the given array. |
| 94 |
* |
| 95 |
* @param[in] recv A receiver of `meth`. |
| 96 |
* @param[in] meth Method ID in a symbol object. |
| 97 |
* @param[in] argc Number of objects of `argv`. |
| 98 |
* @param[in] argv Arguments passed to `meth`. |
| 99 |
* @param[in] func Size calculator. |
| 100 |
* @param[in] kw_splat Handling of keyword parameters: |
| 101 |
* - RB_NO_KEYWORDS `argv`'s last is not a keyword argument. |
| 102 |
* - RB_PASS_KEYWORDS `argv`'s last is a keyword argument. |
| 103 |
* - RB_PASS_CALLED_KEYWORDS it depends if there is a passed block. |
| 104 |
* @exception rb_eTypeError `meth` is not an instance of ::rb_cSymbol. |
| 105 |
* @return A new instance of ::rb_cEnumerator which, when yielded, |
| 106 |
* enumerates by calling `meth` on `recv` with `argv`. |
| 107 |
* @note `func` can be zero, which means the size is unknown. |
| 108 |
*/ |
| 109 |
VALUE rb_enumeratorize_with_size_kw(VALUE recv, VALUE meth, int argc, const VALUE *argv, rb_enumerator_size_func *func, int kw_splat); |
| 110 |
|
| 111 |
RBIMPL_ATTR_NONNULL(()) |
| 112 |
/** |
| 113 |
* Extracts components of the passed arithmetic sequence. This can be seen as |
| 114 |
* an extended version of rb_range_values(). |
| 115 |
* |
| 116 |
* @param[in] as Target instance of `Enumerator::ArithmericSequence`. |
| 117 |
* @param[out] buf Decomposed results buffer. |
| 118 |
* @return 0 `as` is not `Enumerator::ArithmericSequence`. |
| 119 |
* @return 1 Success. |
| 120 |
* @post `buf` is filled. |
| 121 |
*/ |
| 122 |
int rb_arithmetic_sequence_extract(VALUE as, rb_arithmetic_sequence_components_t *buf); |
| 123 |
|
| 124 |
RBIMPL_ATTR_NONNULL(()) |
| 125 |
/** |
| 126 |
* Identical to rb_range_beg_len(), except it takes an instance of |
| 127 |
* `Enumerator::ArithmericSequence`. |
| 128 |
* |
| 129 |
* @param[in] as An `Enumerator::ArithmericSequence` instance. |
| 130 |
* @param[out] begp Return value buffer. |
| 131 |
* @param[out] lenp Return value buffer. |
| 132 |
* @param[out] stepp Return value buffer. |
| 133 |
* @param[in] len Updated length. |
| 134 |
* @param[in] err In case `len` is out of range... |
| 135 |
* - `0`: returns ::RUBY_Qnil. |
| 136 |
* - `1`: raises ::rb_eRangeError. |
| 137 |
* - `2`: `beg` and `len` expanded accordingly. |
| 138 |
* @exception rb_eRangeError `as` cannot fit into `long`. |
| 139 |
* @retval RUBY_Qfalse `as` is not `Enumerator::ArithmericSequence`. |
| 140 |
* @retval RUBY_Qnil `len` is out of `as` but `err` is zero. |
| 141 |
* @retval RUBY_Qtrue Otherwise. |
| 142 |
* @post `beg` is the (possibly updated) left endpoint. |
| 143 |
* @post `len` is the (possibly updated) length of the range. |
| 144 |
* |
| 145 |
* @internal |
| 146 |
* |
| 147 |
* Currently no 3rd party applications of this function is found. But that can |
| 148 |
* be because this function is relatively new. |
| 149 |
*/ |
| 150 |
VALUE rb_arithmetic_sequence_beg_len_step(VALUE as, long *begp, long *lenp, long *stepp, long len, int err); |
| 151 |
|
| 152 |
RBIMPL_SYMBOL_EXPORT_END() |
| 153 |
|
| 154 |
/** @cond INTERNAL_MACRO */ |
| 155 |
#ifndef RUBY_EXPORT |
| 156 |
# define rb_enumeratorize_with_size(obj, id, argc, argv, size_fn) \ |
| 157 |
rb_enumeratorize_with_size(obj, id, argc, argv, (rb_enumerator_size_func *)(size_fn)) |
| 158 |
# define rb_enumeratorize_with_size_kw(obj, id, argc, argv, size_fn, kw_splat) \ |
| 159 |
rb_enumeratorize_with_size_kw(obj, id, argc, argv, (rb_enumerator_size_func *)(size_fn), kw_splat) |
| 160 |
#endif |
| 161 |
/** @endcond */ |
| 162 |
|
| 163 |
/** |
| 164 |
* This is an implementation detail of #RETURN_SIZED_ENUMERATOR(). You could |
| 165 |
* use it directly, but can hardly be handy. |
| 166 |
* |
| 167 |
* @param[in] obj A receiver. |
| 168 |
* @param[in] argc Number of objects of `argv`. |
| 169 |
* @param[in] argv Arguments passed to the current method. |
| 170 |
* @param[in] size_fn Size calculator. |
| 171 |
* @return A new instance of ::rb_cEnumerator which, when yielded, |
| 172 |
* enumerates by calling the current method on `recv` with `argv`. |
| 173 |
*/ |
| 174 |
#define SIZED_ENUMERATOR(obj, argc, argv, size_fn) \ |
| 175 |
rb_enumeratorize_with_size((obj), ID2SYM(rb_frame_this_func()), \ |
| 176 |
(argc), (argv), (size_fn)) |
| 177 |
|
| 178 |
/** |
| 179 |
* This is an implementation detail of #RETURN_SIZED_ENUMERATOR_KW(). You |
| 180 |
* could use it directly, but can hardly be handy. |
| 181 |
* |
| 182 |
* @param[in] obj A receiver. |
| 183 |
* @param[in] argc Number of objects of `argv`. |
| 184 |
* @param[in] argv Arguments passed to the current method. |
| 185 |
* @param[in] size_fn Size calculator. |
| 186 |
* @param[in] kw_splat Handling of keyword parameters: |
| 187 |
* - RB_NO_KEYWORDS `argv`'s last is not a keyword argument. |
| 188 |
* - RB_PASS_KEYWORDS `argv`'s last is a keyword argument. |
| 189 |
* - RB_PASS_CALLED_KEYWORDS it depends if there is a passed block. |
| 190 |
* @return A new instance of ::rb_cEnumerator which, when yielded, |
| 191 |
* enumerates by calling the current method on `recv` with `argv`. |
| 192 |
*/ |
| 193 |
#define SIZED_ENUMERATOR_KW(obj, argc, argv, size_fn, kw_splat) \ |
| 194 |
rb_enumeratorize_with_size_kw((obj), ID2SYM(rb_frame_this_func()), \ |
| 195 |
(argc), (argv), (size_fn), (kw_splat)) |
| 196 |
|
| 197 |
/** |
| 198 |
* This roughly resembles `return enum_for(__callee__) unless block_given?`. |
| 199 |
* |
| 200 |
* @param[in] obj A receiver. |
| 201 |
* @param[in] argc Number of objects of `argv`. |
| 202 |
* @param[in] argv Arguments passed to the current method. |
| 203 |
* @param[in] size_fn Size calculator. |
| 204 |
* @note This macro may return inside. |
| 205 |
*/ |
| 206 |
#define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn) do { \ |
| 207 |
if (!rb_block_given_p()) \ |
| 208 |
return SIZED_ENUMERATOR(obj, argc, argv, size_fn); \ |
| 209 |
} while (0) |
| 210 |
|
| 211 |
|
| 212 |
/** |
| 213 |
* Identical to #RETURN_SIZED_ENUMERATOR(), except you can specify how to |
| 214 |
* handle the last element of the given array. |
| 215 |
* |
| 216 |
* @param[in] obj A receiver. |
| 217 |
* @param[in] argc Number of objects of `argv`. |
| 218 |
* @param[in] argv Arguments passed to the current method. |
| 219 |
* @param[in] size_fn Size calculator. |
| 220 |
* @param[in] kw_splat Handling of keyword parameters: |
| 221 |
* - RB_NO_KEYWORDS `argv`'s last is not a keyword argument. |
| 222 |
* - RB_PASS_KEYWORDS `argv`'s last is a keyword argument. |
| 223 |
* - RB_PASS_CALLED_KEYWORDS it depends if there is a passed block. |
| 224 |
* @note This macro may return inside. |
| 225 |
*/ |
| 226 |
#define RETURN_SIZED_ENUMERATOR_KW(obj, argc, argv, size_fn, kw_splat) do { \ |
| 227 |
if (!rb_block_given_p()) \ |
| 228 |
return SIZED_ENUMERATOR_KW(obj, argc, argv, size_fn, kw_splat); \ |
| 229 |
} while (0) |
| 230 |
|
| 231 |
/** |
| 232 |
* Identical to #RETURN_SIZED_ENUMERATOR(), except its size is unknown. |
| 233 |
* |
| 234 |
* @param[in] obj A receiver. |
| 235 |
* @param[in] argc Number of objects of `argv`. |
| 236 |
* @param[in] argv Arguments passed to the current method. |
| 237 |
* @note This macro may return inside. |
| 238 |
*/ |
| 239 |
#define RETURN_ENUMERATOR(obj, argc, argv) \ |
| 240 |
RETURN_SIZED_ENUMERATOR(obj, argc, argv, 0) |
| 241 |
|
| 242 |
/** |
| 243 |
* Identical to #RETURN_SIZED_ENUMERATOR_KW(), except its size is unknown. It |
| 244 |
* can also be seen as a routine identical to #RETURN_ENUMERATOR(), except you |
| 245 |
* can specify how to handle the last element of the given array. |
| 246 |
* |
| 247 |
* @param[in] obj A receiver. |
| 248 |
* @param[in] argc Number of objects of `argv`. |
| 249 |
* @param[in] argv Arguments passed to the current method. |
| 250 |
* @param[in] kw_splat Handling of keyword parameters: |
| 251 |
* - RB_NO_KEYWORDS `argv`'s last is not a keyword argument. |
| 252 |
* - RB_PASS_KEYWORDS `argv`'s last is a keyword argument. |
| 253 |
* - RB_PASS_CALLED_KEYWORDS it depends if there is a passed block. |
| 254 |
* @note This macro may return inside. |
| 255 |
*/ |
| 256 |
#define RETURN_ENUMERATOR_KW(obj, argc, argv, kw_splat) \ |
| 257 |
RETURN_SIZED_ENUMERATOR_KW(obj, argc, argv, 0, kw_splat) |
| 258 |
|
| 259 |
#endif /* RBIMPL_INTERN_ENUMERATOR_H */ |