C 的变体数据类型库

发布于 2024-08-30 07:51:24 字数 598 浏览 7 评论 0原文

是否有一个像样的开源 C 库用于存储和操作
动态类型变量(又名变体)?我主要对原子值(int8、int16、int32、uint、字符串、blob 等)感兴趣,而 JSON 样式的数组和对象以及自定义对象也很好。此类库的一个主要用途是使用 SQL 数据库。

这种库最明显的特征是所有受支持值的单一类型,例如:

struct Variant {
    enum Type type;
    union {
        int8_t int8_;
        int16_t int16_;
        // ...
    };
};

其他特征可能包括将 Variant 对象转换为 C 结构或从 C 结构转换(使用绑定表)、将值转换为字符串或从字符串转换,以及与现有的数据库,例如 SQLite。

注意:我不认为这个问题是 任何通用数据类型库的重复在C?中,它指的是“队列、树、映射、列表”。我所说的更多地侧重于使 SQL 数据库的使用与使用解释语言的使用一样顺利。

Is there a decent open-source C library for storing and manipulating
dynamically-typed variables (a.k.a. variants)? I'm primarily interested in atomic values (int8, int16, int32, uint, strings, blobs, etc.), while JSON-style arrays and objects as well as custom objects would also be nice. A major case where such a library would be useful is in working with SQL databases.

The most obvious feature of such a library would be a single type for all supported values, e.g.:

struct Variant {
    enum Type type;
    union {
        int8_t int8_;
        int16_t int16_;
        // ...
    };
};

Other features might include converting Variant objects to/from C structures (using a binding table), converting values to/from strings, and integration with an existing database library such as SQLite.

Note: I do not believe this is question is a duplicate of Any library for generic datatypes in C? , which refers to "queues, trees, maps, lists". What I'm talking about focuses more on making working with SQL databases roughly as smooth as working with them in interpreted languages.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

荆棘i 2024-09-06 07:51:24

虽然我怀疑原作者是否仍然需要答案(希望至少在 4 年后),但我想添加我的 2ct。

首先,让我声明您要求的东西称为求和类型,并且通常在函数语言中支持(也就是说,它是一种语言设计功能而不是库问题)。

其次,您是否会找到适合这种情况的 C 库是非常值得怀疑的,原因很简单,任何此类库都支持一组可能不适合您的需求的固定变体。

但是,为了完整起见,您可能需要给出 msgpack 尝试一下。

Although I doubt that the original author still needs an answer (hopefully at least after 4 years), I wanted to add my 2ct.

First, let me state that the thing you ask for is called a sum-type and usually supported in functional languages (that is, it is rather a language design feature and not a library issue).

Second, it is highly doubtful that you will find a C-library for that case for the simple reason that any such library would support a fixed set of variants that probably does not fit your needs.

Howerver, for the sake of completeness, you might want to give msgpack a try.

做个ˇ局外人 2024-09-06 07:51:24

GLib 以 GValue 的形式实现了通用值类型:
http://library.gnome.org/devel/gobject/unstable /gobject-Generic-values.html

GLib has implementation of generic value types in form of GValue:
http://library.gnome.org/devel/gobject/unstable/gobject-Generic-values.html

煮茶煮酒煮时光 2024-09-06 07:51:24

我建议阅读有关 SQL 数据库连接器的手册。 MySQL 连接器提供了一个 API,用于获取结果中的字段类型。

您可以创建一个工厂函数来根据字段类型填充结构。讽刺的是,由于 C 没有基本类型,因此您必须使用 void * 指针并重新转换为已知的结构类型。 (尽管 void * 是您试图摆脱的类型。)

I suggest reading the manual on the SQL database connector. The MySQL connector provides an API for obtaining the field types in the result.

You could create a Factory function that fills a structure based on the field type. Ironically, since C doesn't have Base types, you'll have to use a void * pointer and recast to the known structure type. (Even though void * is the type you are trying to get rid of.)

一笑百媚生 2024-09-06 07:51:24

C 是一种非常强类型的语言,变体不是其哲学的一部分。联合不能成为解决方案,因为您仍然必须选择要使用的数据类型,它通常用于在 intchar[4] 上存储颜色代码。

如果您查看 C-SQLite 接口,就会发现提供了此功能:

int sqlite_step(
  sqlite_vm *pVm,          /* The virtual machine to execute */
  int *pN,                 /* OUT: Number of columns in result */
  const char ***pazValue,  /* OUT: Column data */
  const char ***pazColName /* OUT: Column names and datatypes */
);

数据类型由 char* 表示,开发人员的任务是弄清楚如何从中获取类型。我认为任何类型的变体类型都会更好,但它不是 C。
C 不实现变体,也无意实现变体。

C is a very strong typed language, variants are not part of its philosophy. An union can't be a solution because you still have to choose the data type you want to use, it's typically used to store color codes on int and char[4].

If you look at the C-SQLite interface, this function is provided :

int sqlite_step(
  sqlite_vm *pVm,          /* The virtual machine to execute */
  int *pN,                 /* OUT: Number of columns in result */
  const char ***pazValue,  /* OUT: Column data */
  const char ***pazColName /* OUT: Column names and datatypes */
);

Data types are represented by char* and it's the developer's task to figure how to get types from these. I think any kind of variant type would have been better but it's just not C.
C does not implement variants and is not meant to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文