unpack: Support std::span as package arguments
Change-Id: Iae594c0d1b10e96dd4fd1a83cdf60c0757f9f3bd
Signed-off-by: Willy Tu <wltu@google.com>
diff --git a/include/ipmid/message/pack.hpp b/include/ipmid/message/pack.hpp
index 9f42494..00fcf46 100644
--- a/include/ipmid/message/pack.hpp
+++ b/include/ipmid/message/pack.hpp
@@ -21,6 +21,7 @@
#include <array>
#include <memory>
#include <optional>
+#include <span>
#include <string_view>
#include <tuple>
#include <utility>
@@ -269,6 +270,22 @@
}
};
+/** @brief Specialization of PackSingle for std::span<const uint8_t> */
+template <>
+struct PackSingle<std::span<const uint8_t>>
+{
+ static int op(Payload& p, const std::span<const uint8_t>& t)
+ {
+ if (p.bitCount != 0)
+ {
+ return 1;
+ }
+ p.raw.reserve(p.raw.size() + t.size());
+ p.raw.insert(p.raw.end(), t.begin(), t.end());
+ return 0;
+ }
+};
+
/** @brief Specialization of PackSingle for std::string_view */
template <>
struct PackSingle<std::string_view>
diff --git a/include/ipmid/message/unpack.hpp b/include/ipmid/message/unpack.hpp
index 5eb8054..8340ce3 100644
--- a/include/ipmid/message/unpack.hpp
+++ b/include/ipmid/message/unpack.hpp
@@ -19,6 +19,7 @@
#include <array>
#include <optional>
+#include <span>
#include <string>
#include <tuple>
#include <vector>
@@ -343,6 +344,19 @@
}
};
+/** @brief Specialization of UnpackSingle for std::span<const uint8_t> */
+template <>
+struct UnpackSingle<std::span<const uint8_t>>
+{
+ static int op(Payload& p, std::span<const uint8_t>& t)
+ {
+ // copy out the remainder of the message
+ t = std::span<const uint8_t>(p.raw.begin() + p.rawIndex, p.raw.end());
+ p.rawIndex = p.raw.size();
+ return 0;
+ }
+};
+
/** @brief Specialization of UnpackSingle for Payload */
template <>
struct UnpackSingle<Payload>