memory

Overview

The memory provides utility functions which operate on any container that can be handled by slice. It consists of around 20 utility functions which makes our work easier to work on slices. set(), copy(), move(), take(), split(), emplace(), make() etc are some of the utility functions.

Usage Context

Preconditions

Make sure to include the appropriate library header for memory.

Assumptions of usage

The data used can be converted to slice.

Recommended usage guidance

The size of the underlying container of a slice should be greater than zero.

Usage Example

The code snippet below provides the example for the usage of memory:

Following are the examples for the operations on slices:

// Sets all elements of the destination to the given value.
TEST(Memory, set)
{
    uint8_t destination[5] = {0};
    ::estd::memory::set(destination, 0xAA);

    EXPECT_THAT(destination, Each(0xAA));
}

// Copy data from source slice to destination slice.
TEST(Memory, copy)
{
    uint8_t const source[]  = {1, 2, 3};
    uint8_t destination[10] = {0};

    ::estd::slice<uint8_t> r = ::estd::memory::copy(destination, source);

    EXPECT_EQ(1, destination[0]);
    EXPECT_THAT(r, ElementsAre(1, 2, 3));

    EXPECT_EQ(destination, r.data());
    EXPECT_EQ(3U, r.size());
}

Following is the example for comparison operations:

// Checks whether the memory referred to by two slices contains the same data.
TEST(Memory, is_equal)
{
    uint8_t const a[] = {10, 20, 45, 32, 60};
    uint8_t const b[] = {10, 20, 45, 32, 60};
    uint8_t const c[] = {10, 20, 35, 32, 60};
    uint8_t const d[] = {10, 20, 35, 32};

    EXPECT_TRUE(::estd::memory::is_equal(a, b));
    EXPECT_FALSE(::estd::memory::is_equal(a, c));
    EXPECT_FALSE(::estd::memory::is_equal(b, c));
    EXPECT_FALSE(::estd::memory::is_equal(c, d));
    EXPECT_FALSE(::estd::memory::is_equal(d, c));
    EXPECT_FALSE(::estd::memory::is_equal(d, ::estd::none));
    EXPECT_FALSE(::estd::memory::is_equal(::estd::none, a));
    EXPECT_TRUE(::estd::memory::is_equal(::estd::none, ::estd::none));
}