raw: Add equal method
Change-Id: I1a5dcadcb1b5a4c0450bba09a8e63b184c9588a3
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/src/stdplus/raw.hpp b/src/stdplus/raw.hpp
index 1f3893f..59e308e 100644
--- a/src/stdplus/raw.hpp
+++ b/src/stdplus/raw.hpp
@@ -22,6 +22,21 @@
} // namespace detail
+/** @brief Compares two containers to see if their raw bytes are equal
+ *
+ * @param[in] a - The first container
+ * @param[in] b - The second container
+ * @return True if they are the same, false otherwise
+ */
+template <typename A, typename B>
+bool equal(const A& a, const B& b)
+{
+ static_assert(std::is_trivially_copyable_v<A>);
+ static_assert(std::is_trivially_copyable_v<B>);
+ static_assert(sizeof(A) == sizeof(B));
+ return memcmp(&a, &b, sizeof(A)) == 0;
+}
+
/** @brief Copies data from a buffer into a copyable type
*
* @param[in] data - The data buffer being copied from
diff --git a/test/raw.cpp b/test/raw.cpp
index 81de96d..fd8ae71 100644
--- a/test/raw.cpp
+++ b/test/raw.cpp
@@ -12,6 +12,15 @@
namespace
{
+TEST_CASE("Equal", "[Equal]")
+{
+ int a = 4;
+ unsigned b = 4;
+ CHECK(equal(a, b));
+ b = 5;
+ CHECK(!equal(a, b));
+}
+
TEST_CASE("Copy From Empty", "[CopyFrom]")
{
const std::string_view s;