Skip to content

Proposal: Generic RAII for Backend Integration in ImGui #8237

@pinwhell

Description

@pinwhell

Version/Branch of Dear ImGui:

master

Back-ends:

Any

Compiler, OS:

Default

Full config/build information:

Default

Details:

Guys... how about implementing generic RAII for basic backend aspects... for example:

namespace ImGui {
namespace RAII {

struct Frame {
    Frame() {
        NewFrame();
    }

    ~Frame() {
        Render();
    }
};

namespace Backend {

template<void (*NewFrame)()>
struct Frame {
    Frame() {
        NewFrame();
    }
};

template<void (*NewFrame)(), void (*RenderDrawData)(ImDrawData*)>
struct FrameWithRender : public Frame<NewFrame> {
    ~FrameWithRender() {
        RenderDrawData(GetDrawData());
    }
};

}  // namespace Backend

}  // namespace RAII

Then instantiating it... example:

namespace ImGui {

using OpenGL2Frame = Backend::FrameWithRender<
    ImGui_ImplOpenGL2_NewFrame, ImGui_ImplOpenGL2_RenderDrawData>;

using Win32Frame = Backend::Frame<
    ImGui_ImplWin32_NewFrame>;

}

Using it would be as easy. as:

int main() {
    while (/* condition */) {
        {
              Win32Frame imw32Frame;
              OpenGL2Frame imglFrame;
              ImGui::Frame imFrame;
              /*User Imgui Code Here*/
        }
        // Platform Present
    }
}

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions