Esempio n. 1
0
TEST_F(TestIBUtilField, Alias)
{
    ib_num_t   num1;
    ib_num_t   num2;
    ib_float_t flt1;
    ib_float_t flt2;
    char *s = NULL;
    const char *v;
    ib_field_t *f;
    ib_status_t rc;

    rc = ib_field_create_alias(&f, MM(), "foo", 3,
                               IB_FTYPE_NULSTR,
                               ib_ftype_nulstr_mutable_out(&s));
    ASSERT_EQ(IB_OK, rc);
    v = "hello";
    rc = ib_field_setv(f, ib_ftype_nulstr_in(v));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_STREQ(v, s);

    /*
     * Alias a numeric field
     */
    num1 = 1;
    rc = ib_field_create_alias(&f, MM(), "num", 3,
                               IB_FTYPE_NUM,
                               ib_ftype_num_in(&num1));
    ASSERT_EQ(IB_OK, rc);

    rc = ib_field_value(f, ib_ftype_num_out(&num2));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(num1, num2);

    num1 = 3;
    rc = ib_field_value(f, ib_ftype_num_out(&num2));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(num1, num2);


    /*
     * Alias a floating point field
     */
    flt1 = 1.1;
    rc = ib_field_create_alias(&f, MM(), "flt", 3,
                               IB_FTYPE_FLOAT,
                               ib_ftype_float_in(&flt1));
    ASSERT_EQ(IB_OK, rc);

    rc = ib_field_value(f, ib_ftype_float_out(&flt2));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(flt1, flt2);

    flt1 = 1.5;
    rc = ib_field_value(f, ib_ftype_float_out(&flt2));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(flt1, flt2);
}
Esempio n. 2
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;
}
Esempio n. 3
0
ib_status_t ib_field_create_dynamic(
    ib_field_t        **pf,
    ib_mpool_t         *mp,
    const char         *name,
    size_t              nlen,
    ib_ftype_t          type,
    ib_field_get_fn_t   fn_get,
    void               *cbdata_get,
    ib_field_set_fn_t   fn_set,
    void               *cbdata_set
)
{
    ib_status_t rc;

    rc = ib_field_create_alias(pf, mp, name, nlen, type, NULL);
    if (rc != IB_OK) {
        return rc;
    }

    (*pf)->val->fn_get     = fn_get;
    (*pf)->val->fn_set     = fn_set;
    (*pf)->val->cbdata_get = cbdata_get;
    (*pf)->val->cbdata_set = cbdata_set;

    ib_field_util_log_debug("FIELD_CREATE_DYNAMIC", (*pf));
    return IB_OK;
}
Esempio n. 4
0
ib_status_t ib_field_create_no_copy(
    ib_field_t **pf,
    ib_mpool_t  *mp,
    const char  *name,
    size_t       nlen,
    ib_ftype_t   type,
    void        *mutable_in_pval
)
{
    ib_status_t rc;

    rc = ib_field_create_alias(pf, mp, name, nlen, type, NULL);
    if (rc != IB_OK) {
        goto failed;
    }

    /* Point to internal memory */
    (*pf)->val->pval = &((*pf)->val->u);

    rc = ib_field_setv_no_copy((*pf), mutable_in_pval);
    if (rc != IB_OK) {
        goto failed;
    }

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

    return IB_OK;

failed:
    /* Make sure everything is cleaned up on failure. */
    *pf = NULL;

    return rc;
}
Esempio n. 5
0
TEST_F(TestIBUtilField, Alias)
{
    char *s = NULL;
    const char *v;
    ib_field_t *f;
    ib_status_t rc;

    rc = ib_field_create_alias(&f, MemPool(), "foo", 3, IB_FTYPE_NULSTR,
        ib_ftype_nulstr_mutable_out(&s));
    ASSERT_EQ(IB_OK, rc);
    v = "hello";
    rc = ib_field_setv(f, ib_ftype_nulstr_in(v));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(std::string(v), std::string(s));
}
Esempio n. 6
0
Field create_alias(
    MemoryPool    pool,
    const char*   name,
    size_t        name_length,
    Field::type_e type,
    void*         mutable_out_value
)
{
    ib_field_t* f = NULL;

    ib_status_t rc = ib_field_create_alias(
        &f,
        pool.ib(),
        name, name_length,
        static_cast<ib_ftype_t>(type),
        mutable_out_value
    );
    throw_if_error(rc);

    return Field(f);
}
Esempio n. 7
0
TEST_F(TestIBUtilField, AliasConvert)
{
    char       *str;
    ib_field_t *f1;
    ib_field_t *f2;
    ib_status_t rc;
    ib_num_t    num;
    ib_float_t  flt;

    /*
     * Convert numeric string to number
     */

    /* Copy a number into the string */
    str = ib_mm_strdup(MM(), "1");
    ASSERT_TRUE(str != NULL);

    /* Create the aliased field */
    rc = ib_field_create_alias(&f1, MM(), "one", 3,
                               IB_FTYPE_NULSTR,
                               ib_ftype_nulstr_mutable_out(&str));
    ASSERT_EQ(IB_OK, rc);

    /* Attempt a numeric conversion. */
    rc = ib_field_convert(MM(), IB_FTYPE_NUM, f1, &f2);
    ASSERT_EQ(IB_OK, rc);

    /* Pull out param value for check. */
    rc = ib_field_value(f2, ib_ftype_num_out(&num));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(1, num);

    /*
     * Convert floating-point string to float
     */

    /* Copy a number into the string */
    str = ib_mm_strdup(MM(), "1.1");
    ASSERT_TRUE(str != NULL);

    /* Create the aliased field */
    rc = ib_field_create_alias(&f1, MM(), "one", 3,
                               IB_FTYPE_NULSTR,
                               ib_ftype_nulstr_mutable_out(&str));
    ASSERT_EQ(IB_OK, rc);

    /* Attempt a numeric conversion. */
    rc = ib_field_convert(MM(), IB_FTYPE_FLOAT, f1, &f2);
    ASSERT_EQ(IB_OK, rc);

    /* Pull out param value for check. */
    rc = ib_field_value(f2, ib_ftype_float_out(&flt));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_FLOAT_EQ(1.1, flt);

    /*
     * Convert non-numeric string to number
     */

    /* Copy a number into the string */
    str = ib_mm_strdup(MM(), "x1");
    ASSERT_TRUE(str != NULL);

    /* Create the aliased field */
    rc = ib_field_create_alias(&f1, MM(), "one", 3,
                               IB_FTYPE_NULSTR,
                               ib_ftype_nulstr_mutable_out(&str));
    ASSERT_EQ(IB_OK, rc);

    /* Attempt a numeric conversion. */
    rc = ib_field_convert(MM(), IB_FTYPE_NUM, f1, &f2);
    ASSERT_EQ(IB_EINVAL, rc);

    /*
     * Convert floating-point string to number
     */

    /* Copy a number into the string */
    str = ib_mm_strdup(MM(), "1.1");
    ASSERT_TRUE(str != NULL);

    /* Create the aliased field */
    rc = ib_field_create_alias(&f1, MM(), "one", 3,
                               IB_FTYPE_NULSTR,
                               ib_ftype_nulstr_mutable_out(&str));
    ASSERT_EQ(IB_OK, rc);

    /* Attempt a numeric conversion. */
    rc = ib_field_convert(MM(), IB_FTYPE_NUM, f1, &f2);
    ASSERT_EQ(IB_EINVAL, rc);


    /*
     * Convert non-numeric string to float
     */

    /* Copy a number into the string */
    str = ib_mm_strdup(MM(), "1.1");
    ASSERT_TRUE(str != NULL);

    /* Create the aliased field */
    rc = ib_field_create_alias(&f1, MM(), "one", 3,
                               IB_FTYPE_NULSTR,
                               ib_ftype_nulstr_mutable_out(&str));
    ASSERT_EQ(IB_OK, rc);

    /* Attempt a numeric conversion. */
    rc = ib_field_convert(MM(), IB_FTYPE_FLOAT, f1, &f2);
    ASSERT_EQ(IB_OK, rc);

    /* Pull out param value for check. */
    rc = ib_field_value(f2, ib_ftype_float_out(&flt));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_FLOAT_EQ(1.1, flt);
}
Esempio n. 8
0
ib_status_t ib_cfgmap_init(ib_cfgmap_t *cm,
                           void *base,
                           const ib_cfgmap_init_t *init)
{
    ib_cfgmap_init_t *rec = (ib_cfgmap_init_t *)init;
    ib_field_t *f;
    ib_status_t rc;

    /* Add tree entries that just point into the base structure. */
    ib_util_log_debug("Initializing: t=%p init=%p", cm, init);
    cm->base = base;
    while (rec->name != NULL) {
        if (rec->fn_set != NULL && rec->fn_get != NULL) {
            ib_util_log_debug("INIT: %s type=%d set=%p/%p get=%p/%p",
                              rec->name, rec->type,
                              rec->fn_set, rec->cbdata_set,
                              rec->fn_get, rec->cbdata_get);

            ib_cfgmap_handlers_data_t *data =
                (ib_cfgmap_handlers_data_t *)ib_mpool_alloc(
                    cm->mp,
                    sizeof(*data)
                );
            if (data == NULL) {
                return IB_EALLOC;
            }
            data->base = base;
            data->init = rec;

            rc = ib_field_create_dynamic(
                &f,
                cm->mp,
                rec->name, strlen(rec->name),
                rec->type,
                ib_cfgmap_handle_get, data,
                ib_cfgmap_handle_set, data
            );
            if (rc != IB_OK) {
                return rc;
            }
        }
        else {
            if (rec->fn_get != NULL || rec->fn_set != NULL) {
                return IB_EINVAL;
            }

            void *val = (void *)(((uint8_t *)base) + rec->offset);

            ib_util_log_debug("INIT: %s type=%d base=%p offset=%d dlen=%d %p",
                              rec->name, rec->type, base,
                              (int)rec->offset, (int)rec->dlen, val);

            /* Create a field with data that points to the base+offset. */
            rc = ib_field_create_alias(
                &f,
                cm->mp,
                rec->name, strlen(rec->name),
                rec->type,
                val
            );
            if (rc != IB_OK) {
                return rc;
            }
        }

        /* Add the field. */
        rc = ib_hash_set(cm->hash, rec->name, (void *)f);
        if (rc != IB_OK) {
            return rc;
        }

        ++rec;
    }

    return IB_OK;
}