Skip to content

AUI_DO_ONCE#

Executes following function call or {} block once per program execution

Header:#include <AUI/Util/kAUI.h>
CMake:aui_link(my_target PUBLIC aui::core)

Definition#

#define AUI_DO_ONCE if (static bool _aui_once = false; (!_aui_once && (_aui_once = true)))

Detailed Description#

Guarantees that the following code block will be executed only once per application execution.

without with
static bool done = false;
if (!done) {
    done = true;
    std::printf("Only once!");
}
AUI_DO_ONCE {
    std::printf("Only once!");
}

Examples#

examples/ui/views/src/ExampleWindow.cpp

Views Example — All-in-one views building example.

                            _new<AButton>("Add").connect(
                                &AButton::clicked, this,
                                [state] {
                                    static std::default_random_engine re;
                                    AUI_DO_ONCE { re.seed(std::time(nullptr)); };
                                    static std::uniform_real_distribution<float> d(0.f, 1.f);
                                    state->colors.writeScope()->push_back({ AColor(d(re), d(re), d(re), 1.f) });
                                }),
                            _new<AButton>("Remove").connect(
                                &AButton::clicked, this,

Examples#