Skip to content

[Feat] Expose any tuple from cache C impl #32

Description

@haochengxia
  • Expose one function inner cache impl, dump to export tuples from C to Python, current design is:
    // C codetypedefstruct {
    ValueType type;
    union {
    int i;
    double d;
    constchar* s;
    void* ptr;
    } data;
    } AnyValue;
    typedefstruct {
    AnyValue* items;
    size_t size;
    } C_Tuple_Like;
    C_Tuple_Like* make_sample_tuple() {
    C_Tuple_Like* tuple = malloc(sizeof(C_Tuple_Like));
    tuple->size = 3;
    tuple->items = malloc(sizeof(AnyValue) * 3);
    tuple->items[0] = (AnyValue){ .type = TYPE_INT, .data.i = 42 };
    tuple->items[1] = (AnyValue){ .type = TYPE_DOUBLE, .data.d = 3.14 };
    tuple->items[2] = (AnyValue){ .type = TYPE_STRING, .data.s = strdup("hello") };
    return tuple;
    }
    // C++ code
    std::tuple<int, double, std::string> convert(const C_Tuple_Like* ctuple);
    std::tuple<int, double, std::string> convert_to_tuple(const C_Tuple_Like* input) {
    int i = input->items[0].data.i;
    double d = input->items[1].data.d;
    std::string s = input->items[2].data.s;
    returnstd::make_tuple(i, d, s);
    }
    // To Python
    py::tuple convert_to_py_tuple(const C_Tuple_Like* input) {
    py::tuple result(input->size);
    for (size_t i = 0; i < input->size; ++i) {
    constauto& item = input->items[i];
    switch (item.type) {
    caseTYPE_INT:
    result[i] = py::int_(item.data.i);
    break;
    caseTYPE_DOUBLE:
    result[i] = py::float_(item.data.d);
    break;
    caseTYPE_STRING:
    result[i] = py::str(item.data.s);
    break;
    default:
    result[i] = py::none(); // fallback
    }
    }
    return result;
    }
    m.def("get_tuple", []() {
    C_Tuple_Like* c_tuple = make_sample_tuple();
    auto py_tuple = convert_to_py_tuple(c_tuple);
    // free if neededreturn py_tuple;
    });

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions