The goal is to transform a C++ struct provided to a bond struct. Is this supported by Bond?
Otherwise are there any good examples available to generically transform a C++ struct to a compiled bond struct? I'd prefer to centralize this logic opposed to creating a custom transform for every bond struct.
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. For example, serialization of std::tuple<T...>
is implemented by specializing bond::schema<std::tuple<T...>>
.
C++ does not--yet--have compile-time reflection for structs. There are various ways of simulating this (e.g., Boost::Fusion, macro-based schemes, code generation tools). A large part of Bond's C++ code generation is adding compile-time schema that the library later uses.
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. I'd wrap such behavior in a helper method with generous static_assert
checks to avoid undefined behavior as the structs evolve. 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. (See commit fa02289f which removed many non-default ctors.)