元语言代码生成包装结构为ANSI-C和C#结构体 [英] Meta language to code generate packed structs for ANSI-C and C# Structs

查看:216
本文介绍了元语言代码生成包装结构为ANSI-C和C#结构体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图寻找可用于定义结构和获取/设置代码成员元语言。美中不足的是,结构代码已经存在,而这个元语言将作为位对位替换原有的手工编码结构,以允许生成描述结构的标头。的一点是,这些结构体被用作一个C#应用程序和嵌入式设备之间的协议的一部分(未基于Linux,认为更小,更约束像PIC或C 1-10。)
的元语言将充当

I'm trying to find a "meta language" that can be used to define a structure and get/set code for members. The catch is that the structure already exists in code, and this "meta language" would serve as bit-for-bit replacement of the original hand-coded structure to allow the headers describing the structures to be generated. The point is that the structures are used as part of a protocol between a C# application and an embedded device (not linux based, think smaller and more constrained like a PIC or CM0.) The meta language would act as


  • 文档的结构成员

  • 生成C#结构和实施的get / set操作

  • 生成包装ANSI-C的结构和get / set函数

的元语言需要支持


  • 枚举定义(指定大小的 - 即uint16_t,uint8_t有,或作为多位枚举小)

  • (指定大小的 - 即48位阵列被打包成6个字节,)
  • 位阵列

  • 位结构/枚举阵列(指定大小的 - 即2位48索引结构是12个字节,)

  • 规范字节序和位阶,

  • 生成可直接由要么被读取的二进制结构产生ANSI-C代码或发送通过网络为目的的C-尖锐代码。

  • enumeration definitions (of a specified size - ie uint16_t, uint8_t, or smaller as for multi-bit enumerations)
  • bit-arrays (of specified size - ie 48-bit array is packed into 6 bytes,)
  • bit-structure/enumeration arrays (of specified size - ie a 2-bit structure of 48 indexes is 12 bytes,)
  • specification for endianness and bit-order,
  • generate binary structures that can be read directly by either the generated ANSI-C code or the C-sharp code for the purpose of sending over a network.

这也将是不错的当接收到数据的一些有限的验证。

It would also be nice to have some limited validation of the data when received.

到目前为止,我已经看了

So far I've looked at


  • BSON

  • 蚀刻

  • 黑森州的Avro

  • ICE

  • MessagePack

  • Protocol Buffers的

  • 节俭

  • BSON
  • Etch
  • Hessian Avro
  • ICE
  • MessagePack
  • Protocol Buffers
  • Thrift

所有这些都是伟大的文档和建设时,一个新的协议,而是试图保持与现有协议兼容性和这些功亏一篑由于编码数据编组固有的类型。

All of these are great for documentation and when building a new protocol, but trying to maintain compatibility with an existing protocol and these fall short due to the type encoding inherent in the data marshaling.

我也看了ASN.1的ECN编码,但似乎过于不可读导致问题与文档。

I've also looked at ASN.1 for ECN encoding, but that seems to be too unreadable causing issue with Documentation.

我看了< A HREF =http://stackoverflow.com/questions/12117407/generating-c-sharp-structure-from-c-structure>从C结构自动生成C#结构但没有一个很好的选择有

I've looked at Generating C# structure from C structure but there wasn't a good option there.

任何建议?

推荐答案

你想要的是一个< A HREF =https://en.wikipedia.org/wiki/Program_transformation相对=nofollow>程序变换系统。

这些工具能够读取任意计算机语言实例,然后改变这些成其他有效的语言的情况下,有时在同一语言,有时成不同的语言。他们是普通的,你可以为他们提供你想要操纵语言的描述,然后他们可以在这些语言进行操作。

These are tools that can read arbitrary computer language instances, and then transform those into other valid language instances, sometimes in the same language, sometimes into a different language. They are general in that you can provide them the description of the languages you want to manipulate, and they can then operate on those languages.

在这一领域良好的工具,让你写代码的转换在感兴趣的语言的(面)语法方面,基本上是的形式,如果你看到的这个的,被替换的

Good tools in this space let you write the code transformations in terms of the ("surface") syntax of the languages of interest, essentially in the form of "if you see this, replace it by that".

有关OP的情况下,重要的转变是如果你看到的此插槽中的结构的通过的更换相应的getter和setter ,并且替换结构成员的目标语言

For OP's scenario, the essential transformations are "if you see this slot in a structure replace it by corresponding getters and setters, and a replacement struct member for the target language.

在你的情况,你需要3个方案之间进行选择:

In your case, you need to choose between 3 scenarios:


  • 定义的抽象语言,用于指定数据结构,并建立程序转变,从规范的语言使用C#和C地图。

  • 决定C数据的声明是参考,并生成相应的C#代码。

  • 决定了C#数据声明是参考,并生成相应的C代码。

那么你就必须坐下来,定义语言工具(如果它们尚未定义),构建的转换。

Then you'll have to sit down, define the languages to the tool (if they aren't already defined), and construct the transforms.

(全面披露:我建立一个这样的工具。见我的生物)。

(Full disclosure: I build such a tool. See my bio).

这篇关于元语言代码生成包装结构为ANSI-C和C#结构体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆