Exemplo n.º 1
0
ib_status_t ib_field_alias(
    ib_field_t **pf,
    ib_mpool_t  *mp,
    const char  *name,
    size_t       nlen,
    ib_field_t  *src
)
{
    ib_status_t rc;

    if (ib_field_is_dynamic(src)) {
        return IB_EINVAL;
    }

    rc = ib_field_create_alias(
        pf, mp, name, nlen,
        src->type,
        src->val->pval
    );
    if (rc != IB_OK) {
        goto failed;
    }

    ib_field_util_log_debug("FIELD_ALIAS", (*pf));

    return IB_OK;

failed:
    *pf = NULL;
    return rc;
}
Exemplo n.º 2
0
ib_status_t ib_field_mutable_value(
    ib_field_t *f,
    void       *mutable_out_pval
)
{
    if (ib_field_is_dynamic(f)) {
        return IB_EINVAL;
    }

    if (f->val->pval == NULL) {
        return IB_ENOENT;
    }

    switch(f->type) {
        case IB_FTYPE_NUM:
        case IB_FTYPE_FLOAT:
        case IB_FTYPE_TIME:
            *(void**)mutable_out_pval = f->val->pval;
            break;
        default:
            *(void**)mutable_out_pval = *(void **)f->val->pval;
    }

    return IB_OK;
}
Exemplo n.º 3
0
ib_status_t ib_field_setv_no_copy(
    ib_field_t *f,
    void *mutable_in_pval
)
{
    if (f->type == IB_FTYPE_NUM || ib_field_is_dynamic(f)) {
        return ib_field_setv(f, mutable_in_pval);
    }

    *(void **)(f->val->pval) = mutable_in_pval;


    return IB_OK;
}
Exemplo n.º 4
0
ib_status_t ib_field_value_ex(
    const ib_field_t *f,
    void             *out_pval,
    const void       *arg,
    size_t            alen
)
{
    /* If there is not a stored value, then attempt to use the
     * fn_get call to retrieve the value.
     */
    if (ib_field_is_dynamic(f)) {
        if (f->val->fn_get == NULL) {
            return IB_EINVAL;
        }
        return f->val->fn_get(f, out_pval, arg, alen, f->val->cbdata_get);
    }

    if (arg != NULL) {
        return IB_EINVAL;
    }

    switch (f->type) {
    case IB_FTYPE_TIME:
    {
        ib_time_t *n = (ib_time_t *)out_pval;
        *n = *(ib_time_t *)(f->val->pval);
        break;
    }
    case IB_FTYPE_NUM:
    {
        ib_num_t *n = (ib_num_t *)out_pval;
        *n = *(ib_num_t *)(f->val->pval);
        break;
    }
    case IB_FTYPE_FLOAT:
    {
        ib_float_t *fl = (ib_float_t *)out_pval;
        *fl = *(ib_float_t *)(f->val->pval);
        break;
    }
    default:
        *(void **)out_pval = *(void **)(f->val->pval);
    }

    return IB_OK;
}
Exemplo n.º 5
0
ib_status_t ib_field_make_static(
    ib_field_t* f
)
{
    if (! ib_field_is_dynamic(f)) {
        return IB_EINVAL;
    }

    f->val->pval       = &(f->val->u);
    *(void**)(f->val->pval) = NULL;
    f->val->fn_get     = NULL;
    f->val->fn_set     = NULL;
    f->val->cbdata_get = NULL;
    f->val->cbdata_set = NULL;

    ib_field_util_log_debug("FIELD_MAKE_STATIC", f);

    return IB_OK;
}
Exemplo n.º 6
0
ib_status_t ib_field_mutable_value(
    ib_field_t *f,
    void       *mutable_out_pval
)
{
    if (ib_field_is_dynamic(f)) {
        return IB_EINVAL;
    }

    if (f->val->pval == NULL) {
        return IB_ENOENT;
    }

    if (f->type == IB_FTYPE_NUM || f->type == IB_FTYPE_FLOAT)
    {
        *(void**)mutable_out_pval = f->val->pval;
    }
    else {
        *(void**)mutable_out_pval = *(void **)f->val->pval;
    }

    return IB_OK;
}
Exemplo n.º 7
0
ib_status_t ib_field_setv_ex(
    ib_field_t *f,
    void       *in_pval,
    const void *arg,
    size_t      alen
)
{
    ib_status_t rc;

    if (ib_field_is_dynamic(f)) {
        if (f->val->fn_set == NULL) {
            return IB_EINVAL;
        }
        return f->val->fn_set(f, arg, alen, in_pval, f->val->cbdata_set);
    }

    /* No dynamic setter */
    if (arg != NULL) {
        return IB_EINVAL;
    }

    /* What and how it is stored depends on the field type. */
    switch (f->type) {
    case IB_FTYPE_BYTESTR:
    {
        const ib_bytestr_t *bs = (const ib_bytestr_t *)in_pval;
        if (bs != NULL) {
            rc = ib_bytestr_dup((ib_bytestr_t **)f->val->pval, f->mp, bs);
            if (rc != IB_OK) {
                goto failed;
            }
        }
        break;
    }
    case IB_FTYPE_LIST:
    {
        /// @todo Fix const once lists are const aware/copying.
        ib_list_t *l = (ib_list_t *)in_pval;
        if (l != NULL) {
            /// @todo Should do shallow copy
            *(ib_list_t **)f->val->pval = l;
        }
        else {
            rc = ib_list_create((ib_list_t **)f->val->pval, f->mp);
            if (rc != IB_OK) {
                goto failed;
            }
        }
        break;
    }
    case IB_FTYPE_SBUFFER:
    {
        /// @todo Fix once stream is const aware/copying
        ib_stream_t *s = (ib_stream_t *)in_pval;
        if (s != NULL) {
            /// @todo Should do shallow copy
            *(ib_stream_t **)f->val->pval = s;
        }
        else {
            rc = ib_stream_create((ib_stream_t **)f->val->pval, f->mp);
            if (rc != IB_OK) {
                goto failed;
            }
        }
        break;
    }
    case IB_FTYPE_NULSTR:
    {
        const char *s = (const char *)in_pval;
        if (s != NULL) {
            *(char **)(f->val->pval) = ib_mpool_strdup(f->mp, s);
            if (*(void **)(f->val->pval) == NULL) {
                rc = IB_EALLOC;
                goto failed;
            }
        }
        break;
    }
    case IB_FTYPE_NUM:
    {
        ib_num_t n = (in_pval != NULL) ? *(ib_num_t *)in_pval : 0;
        *(ib_num_t *)(f->val->pval) = n;
        break;
    }
    case IB_FTYPE_FLOAT:
    {
        ib_float_t fl = (in_pval != NULL) ? *(ib_float_t *)in_pval : 0;
        *(ib_float_t *)(f->val->pval) = fl;
        break;
    }
    case IB_FTYPE_GENERIC:
    default:
    {
        void *p = in_pval;
        *(void **)(f->val->pval) = p;
        break;
    }
    }

    ib_field_util_log_debug("FIELD_SETV", f);

    return IB_OK;

failed:
    return rc;
}
Exemplo n.º 8
0
ib_status_t ib_field_copy(
    ib_field_t       **pf,
    ib_mpool_t        *mp,
    const char        *name,
    size_t             nlen,
    const ib_field_t  *src
)
{
    ib_status_t rc;

    if (ib_field_is_dynamic(src)) {
        rc = ib_field_create_dynamic(
            pf, mp, name, nlen,
            src->type,
            src->val->fn_get,
            src->val->cbdata_get,
            src->val->fn_set,
            src->val->cbdata_set
        );
    }
    else {
        switch (src->type) {
        case IB_FTYPE_NUM:
        {
            ib_num_t v;
            rc = ib_field_value(src, ib_ftype_num_out(&v));
            if (rc != IB_OK) {
                goto failed;
            }
            rc = ib_field_create(
                pf, mp, name, nlen,
                src->type,
                ib_ftype_num_in(&v)
            );
            break;
        }
        case IB_FTYPE_FLOAT:
        {
            ib_float_t v;
            rc = ib_field_value(src, ib_ftype_float_out(&v));
            if (rc != IB_OK) {
                goto failed;
            }
            rc = ib_field_create(
                pf, mp, name, nlen,
                src->type,
                ib_ftype_float_in(&v)
            );
            break;
        }
        default:
        {
            void *v;
            rc = ib_field_value(src, &v);
            if (rc != IB_OK) {
                goto failed;
            }
            rc = ib_field_create(
                pf, mp, name, nlen,
                src->type,
                v
            );
        }
        }
    }

    if (rc != IB_OK) {
        goto failed;
    }

    ib_field_util_log_debug("FIELD_COPY", (*pf));

    return rc;

failed:
    *pf = NULL;
    return rc;
}
Exemplo n.º 9
0
void ib_field_util_log_debug(
    const char       *prefix,
    const ib_field_t *f
)
{
    assert(prefix != NULL);
    assert(f != NULL);

    ib_status_t rc;

    if (ib_util_get_log_level() < IB_LOG_DEBUG) {
        return;
    }

    if (ib_field_is_dynamic(f)) {
        ib_util_log_debug(
            "%s is dynamic: fn_get=%p cbdata_get=%p fn_set=%p cbdata_set=%p",

          prefix,
            f->val->fn_get, f->val->cbdata_get,
            f->val->fn_set, f->val->cbdata_set
        );
    }

    ib_util_log_debug("%s name=%.*s type=%d",
                      prefix, (int)f->nlen, f->name, f->type
    );

    if (ib_field_is_dynamic(f)) {
        return;
    }

    assert(f->val->pval);

    if (*(void **)(f->val->pval) == NULL) {
        ib_util_log_debug(
            "%s has no value.",
            prefix
        );
    }
    else {
        switch (f->type) {
        case IB_FTYPE_GENERIC:
        {
            void *v;
            rc = ib_field_value(f, ib_ftype_generic_out(&v));
            if (rc == IB_OK) {
                ib_util_log_debug("%s value=%p", prefix, v);
            }
            break;
        }
        case IB_FTYPE_NUM:
        {
            ib_num_t v;
            rc = ib_field_value(f, ib_ftype_num_out(&v));
            if (rc == IB_OK) {
                ib_util_log_debug("%s value=%"PRId64, prefix, v);
            }
            break;
        }
        case IB_FTYPE_FLOAT:
        {
            ib_float_t v;
            rc = ib_field_value(f, ib_ftype_float_out(&v));
            if (rc == IB_OK) {
                ib_util_log_debug("%s value=%Lf", prefix, v);
            }
            break;
        }
        case IB_FTYPE_NULSTR:
        {
            const char *v;
            rc = ib_field_value(f, ib_ftype_nulstr_out(&v));
            if (rc == IB_OK) {
                ib_util_log_debug("%s value=%s", prefix, v);
            }
            break;
        }
        case IB_FTYPE_BYTESTR:
        {
            const ib_bytestr_t *v;
            rc = ib_field_value(f, ib_ftype_bytestr_out(&v));
            if (rc == IB_OK) {
                ib_util_log_debug("%s value=%" IB_BYTESTR_FMT,
                                  prefix, IB_BYTESTR_FMT_PARAM(v));
            }
            break;
        }
        case IB_FTYPE_LIST:
        {
            const ib_list_t* v;
            rc = ib_field_value(f, ib_ftype_list_out(&v));
            if (rc == IB_OK) {
                ib_util_log_debug("%s &value=%p", prefix, v);
            }
            break;
        }
        case IB_FTYPE_SBUFFER:
        {
            const ib_stream_t* v;
            rc = ib_field_value(f, ib_ftype_sbuffer_out(&v));
            if (rc == IB_OK) {
                ib_util_log_debug("%s &value=%p", prefix, v);
            }
            break;
        }
        default:
            ib_util_log_debug("%s Unknown field type: %u",
                              prefix, f->type
            );
        }
    }
}
Exemplo n.º 10
0
bool ConstField::is_dynamic() const
{
    return ib_field_is_dynamic(ib()) == 1;
}