indestructible

Overview

estd::indestructible provides transparent wrapper for global objects that are never destructed, which means encapsulating global objects within a class that ensures these objects persist throughout the lifetime of the program and are never explicitly destroyed during the runtime of the program.

This is utilized in situations requiring a global or singleton like object that remains constant throughout the program’s life. Such objects are not meant to be dynamically created or destroyed.

Usage

Use estd::indestructible when you need global objects that should not be explicitly destroyed.

Example

The example shows the usage of Immutable class to create a global object that is never destructed.

    // Creating a structure 'Constructible'.
    struct Constructible
    {
        uint8_t _data;

        explicit Constructible(uint8_t data) : _data(data) {}
    };

    // Creating an instance of indestructible class.
    indestructible<Constructible> instance(0xAA);

    // Dereferencing the value using 'operator->()'.
    ASSERT_EQ(instance->_data, 0xAA);

    // Dereferencing the value using 'get()'
    ASSERT_EQ(instance.get()._data, 0xAA);
    // Dereferencing the value using 'operator T&()'
    Constructible& ref = instance;
    EXPECT_EQ(&ref, &instance.get());