From bae91350cc4bad07b841037f078c0df282beffbf Mon Sep 17 00:00:00 2001 From: Vernon Mauery Date: Wed, 3 Apr 2019 12:11:08 -0700 Subject: Add support for returning optional values Some commands have optional return values. This allows the handlers to be defined as returning an optional value and then in the body of the handler set the value in the optional or not. Tested-by: unit test runs successfully Change-Id: Ib38a4589609fb1eb192106e511c9ee3a507ac42f Signed-off-by: Vernon Mauery --- include/ipmid/message/pack.hpp | 16 ++++++++++++++++ test/message/pack.cpp | 26 ++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/include/ipmid/message/pack.hpp b/include/ipmid/message/pack.hpp index 104354d..b458af4 100644 --- a/include/ipmid/message/pack.hpp +++ b/include/ipmid/message/pack.hpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -165,6 +166,21 @@ struct PackSingle> } }; +/** @brief Specialization of PackSingle for std::optional */ +template +struct PackSingle> +{ + static int op(Payload& p, const std::optional& t) + { + int ret = 0; + if (t) + { + ret = PackSingle::op(p, *t); + } + return ret; + } +}; + /** @brief Specialization of PackSingle for std::array */ template struct PackSingle> diff --git a/test/message/pack.cpp b/test/message/pack.cpp index b3957cc..03846c5 100644 --- a/test/message/pack.cpp +++ b/test/message/pack.cpp @@ -222,6 +222,32 @@ TEST(PackBasics, VectorUint8) ASSERT_EQ(p.raw, k); } +TEST(PackBasics, OptionalEmpty) +{ + // an optional will only pack if the value is present + ipmi::message::Payload p; + std::optional v; + p.pack(v); + // check that the number of bytes matches + ASSERT_EQ(p.size(), 0); + // check that the bytes were correctly packed (in byte order) + std::vector k = {}; + ASSERT_EQ(p.raw, k); +} + +TEST(PackBasics, OptionalContainsValue) +{ + // an optional will only pack if the value is present + ipmi::message::Payload p; + std::optional v(0x04860002); + p.pack(v); + // check that the number of bytes matches + ASSERT_EQ(p.size(), sizeof(uint32_t)); + // check that the bytes were correctly packed (in byte order) + std::vector k = {0x02, 0x00, 0x86, 0x04}; + ASSERT_EQ(p.raw, k); +} + TEST(PackAdvanced, Uints) { // all elements will be processed in order, with each multi-byte -- cgit v1.2.1