cppden / med Goto Github PK
View Code? Open in Web Editor NEWMeta-Encoder/Decoder
License: MIT License
Meta-Encoder/Decoder
License: MIT License
In file included from ../med/med.hpp:9,
from ../ut/ut.hpp:14,
from ../ut/med.cpp:1:
../med/choice.hpp:13:10: fatal error: brigand.hpp: No such file or directory
13 | #include <brigand.hpp>
to avoid complicated constructions in the code like:
auto& teps = m_data.ref<udsp::smf::qos_info>().ref<udsp::smf::traffic_endpoints>();
for (auto& tep : teps.get())
{
if (!tep.get<udsp::smf::valid>().get())
{
udsp::smf::traffic_endpoint& nc_tep = const_cast<udsp::smf::traffic_endpoint&>(tep);
nc_tep.ref<udsp::smf::local_f_teid>().clear();
nc_tep.ref<udsp::smf::network_instance>().clear();
return &nc_tep;
}
}
should be like std::array w/o ability to erase, etc.
when exceptions are used for error reporting and malformed IE appears inside length constrained sequence the exception will be caught and treated as end of sequence.
need to introduce typed exceptions to resolve this but 1st include UT to detect such case.
could be useful in some cases:
some ie1, ie2;
...
if (ie1 == ie2)
may need to operate on items in multi_field.
at least erase is needed so far. anything more?
possible to use type-erasure at some common functions to reduce code bloat
Add an operator() in the class dumper (printer.hpp) for IE_TAG that is used in print_all()
template <class IE>
constexpr void operator() (IE const& ie, IE_TAG) const noexcept {}
Example:
sink s;
diameter::slh::proto proto;
med2::print_all(s, proto); // error
..
//print all (named and not) IEs in full depth
template <class SINK, class IE, std::size_t MAX_LINE = 128>
void print_all(SINK&& sink, IE const& ie)
Compile error:
cannot convert 'med2::IE_TAG{}' (type 'med2::IE_TAG') to type 'med2::PRIMITIVE'
encoder(ie, IE_TAG{});
Provide separate control on error propagation for encoding and decoding.
Default is throw during encode but bool for encode to reduce sensitivity to DoS attacks (see benchmarks).
med::placeholder::_length
lacks of the clear()
method.
public:
template <class PAD_TRAITS, class FUNC>
struct padder_type
{
constexpr explicit padder_type(FUNC& func) noexcept {}
static constexpr void enable_padding(bool) noexcept { }
static constexpr void add_padding() noexcept {}
};
Example:
gtpu::proto proto;
char const* err = gtpu::decode(data, size, proto);
sink s;
med2::print(s, proto, max_depth); // error
Compile error:
../microcore/gate/rfms/src/msg_dump.cpp:217:13: required from here
../microcore/common/codec/med2/encode.hpp:274:47: error: no class template named 'padder_type' in 'class med2::printer<sink_t<100000>&, 128>'
274 | using pad_t = typename ENCODER::template padder_type<pad_traits, ENCODER>;
seems to be possible to factor out loops in one template, put ies in typelist and iterate with functor instead of multiple crafted loops.
now padding is only supported in container but should be available for single IE with length (e.g. like in RTCP User-Id).
padding also s.b. defined via meta-info
perhaps it's worth to have the same interface as std::pmr::memory_resource to allow use of any external allocators
extend mandatory (optional?) with condition and default value for example to describe GTP-C message priority.
clear in choice only resets the header w/o clearing currently selected value
Денис, привет.
В продолжение нашего разговора, оставляю обновление для ascii_string. Предлагаю добавить этот метод в выше упомянутый класс.
template <std::size_t N>
bool set(char const(&arr)[N]) { return this->set_encoded(std::strlen(arr), arr); }
Тест для этого случая
TEST(common, ascii_string_set)
{
char arr[8] = {0};
strcpy(arr, "str");
med2::ascii_string s;
s.set(arr);
EXPECT_EQ(s.get().size(), strlen(arr));
}
Test:
#include "ut.hpp"
#include "ut_proto.hpp"
struct o_params : med::sequence<
O<C<0x01>, FLD_UC>,
O<C<0x02>, FLD_U8>,
O<C<0x03>, FLD_U16>>
{
};
struct m_params : med::sequence<
M<FLD_UC>,
M<FLD_U8>,
M<FLD_U16>>
{
};
struct params : med::sequence<
M<FLD_U8>,
O<C<0x04>, o_params>,
O<C<0x05>, m_params>>
{
};
TEST(sequence, optional)
{
params p1;
p1.ref<FLD_U8>().set(0);
p1.ref<o_params>().ref<FLD_U8>().set(1);
p1.ref<m_params>().ref<FLD_UC>().set(2);
p1.ref<m_params>().ref<FLD_U8>().set(3);
p1.ref<m_params>().ref<FLD_U16>().set(4);
uint8_t e_buffer[64];
med::encoder_context e_ctx{e_buffer};
encode(med::octet_encoder{e_ctx}, p1);
uint8_t d_buffer[64];
med::allocator d_alloc{d_buffer};
med::decoder_context<med::allocator> d_ctx{e_ctx.buffer().get_start(), e_ctx.buffer().get_offset(), &d_alloc};
params p2;
med::decode(med::octet_decoder{d_ctx}, p2);
EXPECT_NE(p2.get<m_params>(), nullptr);
}
Adding length to o_params fixes the problem (O<C<0x04>, L, o_params>
) .
Line 155 in 0d96da3
Case:
struct MySeq : med::sequence<
M<Flag>,
O<Case1, Flag::is_case1>, // is_case1 returns true if Flag.get() == 1
O<Case2, Flag::is_case2, med::inf>// is_case1 returns true if Flag.get() == 2
>
struct Msg : med::sequence<
M<L, MySeq>
>
{
};
Decoder will try to decode Case2
until the end of Msg
, but not end of MySeq
with specified length. Probably, it happens because the loop
Line 155 in 0d96da3
decoder(CHECK_STATE{}, ie)
and IE::condition{}(to)
return constant value for specific Msg. So loop ends only when decoding one more Case2
fails.A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.