Skip to content

SiddiqSoft/RunOnEnd

Repository files navigation

RunOnEnd : Run a lambda in destructor

Build Status

Objective

  • A simpler "run on end" vs. the custom deletor available via std::unique_ptr<>. It is important to note that we do not provide an "owning" helper as this is better accomplished via the destructor code for std::unique_ptr<>.
  • Use the nuget SiddiqSoft.RunOnEnd
  • Copy paste..whatever works.

Usage

The utility can be used to ensure you cleanup within a scope, update some global variable or use the definition to build your own auto-cleanup.

Consider the following example where the utility UseWinsock builds on RunOnEnd for its functionality.

struct UseWinsock : private siddiqsoft::RunOnEnd
{
    UseWinsock() noexcept
        : m_rc(E_FAIL)
        , siddiqsoft::RunOnEnd([&]() {
            if (m_rc == S_OK) WSACleanup();
        })
    {
        ZeroMemory(&m_wsaData, sizeof(m_wsaData));
        m_rc = WSAStartup(MAKEWORD(2, 2), &m_wsaData);
        m_rc = (m_rc == 0) ? S_OK : WSAGetLastError();
    }

    operator bool() { return (m_rc == S_OK); }

private:
    int     m_rc;
    WSADATA m_wsaData;
};

Import

  • Use the SiddiqSoft.RunOnEnd nuget package.
  • Use CMP package import it into your project's CMakeLists
    CPMAddPackage("gh:SiddiqSoft/RunOnEnd#1.3.0");
    ..
    ..
    target_link_libraries(your-project PRIVATE runonend::runonend)

© 2021-2024 Siddiq Software LLC. All rights reserved.

About

Run lambda on scope exit via destructor

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •