简体   繁体   English

可以将C ++结构转换为Microsoft Bond结构吗?

[英]Can C++ structs be casted to Microsoft Bond structs?

The goal is to transform a C++ struct provided to a bond struct. 目标是将提供给键结构的C ++结构转换。 Is this supported by Bond? 邦德支持吗?

Otherwise are there any good examples available to generically transform a C++ struct to a compiled bond struct? 否则,有没有很好的例子可用于将C ++结构一般转换为编译的键结构? I'd prefer to centralize this logic opposed to creating a custom transform for every bond struct. 我宁愿集中这种逻辑,而不是为每个bond结构创建自定义转换。

If you can provide a specialization for bond::schema<T> for the non-Bond generated struct, that would give enough compile-time metadata about the non-Bond struct that many of the Bond APIs could be called on the non-Bond generated struct, like the transforms APIs. 如果您可以为非Bond生成的结构提供bond::schema<T>的专门化,那么将提供有关非Bond结构的足够的编译时元数据,从而可以在非Bond上调用许多Bond API生成的结构,例如transforms API。 For example, serialization of std::tuple<T...> is implemented by specializing bond::schema<std::tuple<T...>> . 例如,通过专用于bond::schema<std::tuple<T...>>来实现std::tuple<T...>序列化

C++ does not-- yet --have compile-time reflection for structs. C ++还没有对结构进行编译时反射。 There are various ways of simulating this (eg, Boost::Fusion , macro-based schemes, code generation tools). 有多种模拟方法(例如Boost :: Fusion ,基于宏的方案,代码生成工具)。 A large part of Bond's C++ code generation is adding compile-time schema that the library later uses. Bond的C ++代码生成的很大一部分是添加库以后使用的编译时模式

If the C++ struct and the Bond-generated C++ struct have compatible layout and both are trivially copyable , you can use std::memcpy to "cast-by-copying" to the Bond-generated struct and then manipulate the Bond struct as needed. 如果C ++结构和Bond生成的C ++结构具有兼容的布局,并且两者都是可复制的 ,则可以使用std::memcpy来“复制复制”到Bond生成的结构,然后根据需要操作Bond结构。 I'd wrap such behavior in a helper method with generous static_assert checks to avoid undefined behavior as the structs evolve. 我将这种行为包装在带有大量static_assert检查的辅助方法中,以避免结构演变时出现未定义的行为。 This will only work on very constrained struct shapes: collection fields, for example, will likely render the struct non-trivially copyable. 这仅适用于非常受约束的结构形状:例如,收集字段可能会使该结构不可平凡地复制。 You need to be using the 8.x or later version Bond and a recent version of your C++ compiler for the generated Bond structs to even have the ability to be trivially copyable at all. 您需要使用8.x或更高版本的Bond以及C ++编译器的最新版本来生成所生成的Bond结构,以便甚至可以完全复制。 (See commit fa02289f which removed many non-default ctors.) (请参阅commit fa02289f ,其中删除了许多非默认的ctor。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM