| 1 | /* |
| 2 | Copyright(C) 2019-2022 Sutou Kouhei <[email protected]> |
| 3 | |
| 4 | This library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Lesser General Public |
| 6 | License as published by the Free Software Foundation; either |
| 7 | version 2.1 of the License, or (at your option) any later version. |
| 8 | |
| 9 | This library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with this library; if not, write to the Free Software |
| 16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 | |
| 19 | #pragma once |
| 20 | |
| 21 | #include "grn.h" |
| 22 | |
| 23 | #ifdef __cplusplus |
| 24 | extern "C" { |
| 25 | #endif |
| 26 | |
| 27 | #define CAST_FAILED(caster) do { \ |
| 28 | uint32_t invalid_mode = \ |
| 29 | ((caster)->flags & GRN_OBJ_INVALID_MASK); \ |
| 30 | if (invalid_mode != GRN_OBJ_INVALID_IGNORE) { \ |
| 31 | GRN_DEFINE_NAME_CUSTOM((caster)->target, target_name); \ |
| 32 | grn_obj *type = \ |
| 33 | grn_ctx_at(ctx, (caster)->dest->header.domain); \ |
| 34 | GRN_DEFINE_NAME_CUSTOM(type, type_name); \ |
| 35 | grn_obj inspected; \ |
| 36 | GRN_TEXT_INIT(&inspected, 0); \ |
| 37 | grn_inspect(ctx, &inspected, (caster)->src); \ |
| 38 | if (invalid_mode == GRN_OBJ_INVALID_WARN) { \ |
| 39 | GRN_LOG(ctx, \ |
| 40 | GRN_LOG_WARNING, \ |
| 41 | "<%.*s>: failed to cast to <%.*s>: <%.*s>", \ |
| 42 | target_name_size, \ |
| 43 | target_name, \ |
| 44 | type_name_size, \ |
| 45 | type_name, \ |
| 46 | (int)GRN_TEXT_LEN(&inspected), \ |
| 47 | GRN_TEXT_VALUE(&inspected)); \ |
| 48 | } else { \ |
| 49 | ERR(GRN_INVALID_ARGUMENT, \ |
| 50 | "<%.*s>: failed to cast to <%.*s>: <%.*s>", \ |
| 51 | target_name_size, \ |
| 52 | target_name, \ |
| 53 | type_name_size, \ |
| 54 | type_name, \ |
| 55 | (int)GRN_TEXT_LEN(&inspected), \ |
| 56 | GRN_TEXT_VALUE(&inspected)); \ |
| 57 | } \ |
| 58 | GRN_OBJ_FIN(ctx, &inspected); \ |
| 59 | grn_obj_unref(ctx, type); \ |
| 60 | } \ |
| 61 | } while (0) |
| 62 | |
| 63 | #define ERR_CAST(column, range, src) do { \ |
| 64 | grn_obj dest; \ |
| 65 | dest.header.domain = DB_OBJ(range)->id; \ |
| 66 | grn_caster caster = { \ |
| 67 | src, \ |
| 68 | &dest, \ |
| 69 | GRN_OBJ_INVALID_ERROR, \ |
| 70 | column, \ |
| 71 | }; \ |
| 72 | CAST_FAILED(&caster); \ |
| 73 | } while (false) |
| 74 | |
| 75 | grn_rc |
| 76 | grn_caster_cast_text_to_uvector(grn_ctx *ctx, grn_caster *caster); |
| 77 | grn_rc |
| 78 | grn_caster_cast_text_to_text_vector(grn_ctx *ctx, grn_caster *caster); |
| 79 | |
| 80 | #ifdef __cplusplus |
| 81 | } |
| 82 | #endif |