M
molw5.iwg
I'm looking for some feedback on an early version of a serialization
framework I've been working on, see:
http://www.github.com/molw5/framework
The idea was to use some of the new C++11 features to provide a natural
syntax for the definition of serializable objects down to their
representation on the wire. The motivating use-case here was protocol
analysis - I wanted the ability to specify protocols I was working with
naturally with zero duplication of information. The following example
illustrates the syntax used:
struct Object : serializable <Object,
value <NAME("Field 1"), little_endian <uint32_t>>,
value <NAME("Field 2"), big_endian <float>>,
optional_field <uint16_t,
optional_value <0x0001, NAME("Field 3"), stl_null_string>,
optional_value <0x0002, NAME("Field 4"), stl_null_wstring>,
optional_value <0x0004, NAME("Field 5"), little_endian <uint32_t>>,
optional_value <0x0008, NAME("Field 6"),
stl_vector <
little_endian <uint32_t>,
inline_object <
value <NAME("Field 1"), little_endain <uint32_t>>,
value <NAME("Field 2"), little_endain <uint32_t>>>>>>,
value <NAME("Field 7"),
stl_map <
little_endian <uint32_t>,
stl_string,
stl_wstring>>>
{
void foo ()
{
using x1 = get_base <Object, NAME("Field 2")>;
using x2 = get_base <Object, NAME("Field 4")>;
x2::set("Hello World!");
x1::set(x1::get() + x2::get().size());
}
};
Note that Visual C++ 2011 is not supported - the syntax above relies
heavily on the ability to translate a string literal to a typename
through the NAME macro. This macro requires the ability to perform
compile-time character extraction from string literals; in C++11
this was made possible through constexpr. Visual C++ lacks support
for this feature, even in CTP; recent versions of clang and GCC are
supported.
framework I've been working on, see:
http://www.github.com/molw5/framework
The idea was to use some of the new C++11 features to provide a natural
syntax for the definition of serializable objects down to their
representation on the wire. The motivating use-case here was protocol
analysis - I wanted the ability to specify protocols I was working with
naturally with zero duplication of information. The following example
illustrates the syntax used:
struct Object : serializable <Object,
value <NAME("Field 1"), little_endian <uint32_t>>,
value <NAME("Field 2"), big_endian <float>>,
optional_field <uint16_t,
optional_value <0x0001, NAME("Field 3"), stl_null_string>,
optional_value <0x0002, NAME("Field 4"), stl_null_wstring>,
optional_value <0x0004, NAME("Field 5"), little_endian <uint32_t>>,
optional_value <0x0008, NAME("Field 6"),
stl_vector <
little_endian <uint32_t>,
inline_object <
value <NAME("Field 1"), little_endain <uint32_t>>,
value <NAME("Field 2"), little_endain <uint32_t>>>>>>,
value <NAME("Field 7"),
stl_map <
little_endian <uint32_t>,
stl_string,
stl_wstring>>>
{
void foo ()
{
using x1 = get_base <Object, NAME("Field 2")>;
using x2 = get_base <Object, NAME("Field 4")>;
x2::set("Hello World!");
x1::set(x1::get() + x2::get().size());
}
};
Note that Visual C++ 2011 is not supported - the syntax above relies
heavily on the ability to translate a string literal to a typename
through the NAME macro. This macro requires the ability to perform
compile-time character extraction from string literals; in C++11
this was made possible through constexpr. Visual C++ lacks support
for this feature, even in CTP; recent versions of clang and GCC are
supported.