跳转至

cf::CallOnceInit

Thread-safe lazy initialization template. More...

#include <once_init.hpp>

Public Functions

Name
template <typename... Args>
CallOnceInit(Args &&... args)
Constructs with forwarded arguments for the resource.
CallOnceInit() =default
Default constructor. Does not initialize the resource.
Resources & get_resources()
Retrieves the resource, initializing if necessary.
void force_reinit()
Forces re-initialization of the resource.
template <typename... Args>
void
force_reinit(Args &&... args)
Forces re-initialization with new arguments.

Protected Functions

Name
virtual bool init_resources() =0
Performs the actual resource initialization.
virtual bool force_do_reinit() =0
Performs forced re-initialization.

Protected Attributes

Name
Resources resource
The managed resource. Ownership: owner.

Detailed Description

template <typenameResources >
struct cf::CallOnceInit;

Thread-safe lazy initialization template.

Template Parameters:

  • Resources Type of the resource to be lazily initialized.

Note: Not thread-safe during force_reinit() calls. External synchronization is required if concurrent re-initialization is possible.

Provides a base class for implementing lazy-initialized resources with thread-safe initialization and forced re-initialization capabilities. Derived classes must implement the initialization logic.

class MyInitializer : public cf::CallOnceInit<MyData> {
protected:
    bool init_resources() override {
        // Perform initialization
        resource.data = fetch_data();
        return true;
    }
    bool force_do_reinit() override {
        // Perform re-initialization
        resource.data = fetch_data();
        return true;
    }
};

Public Functions Documentation

function CallOnceInit

template <typename... Args>
inline explicit CallOnceInit(
    Args &&... args
)

Constructs with forwarded arguments for the resource.

Parameters:

  • args Arguments to forward to the Resources constructor.

Exceptions:

  • May throw if Resources constructor throws.

Template Parameters:

  • Args Types of arguments to forward to Resources constructor.

Since: 0.1

Note: None.

Warning: None.

Initializes the resource with the provided arguments and marks it as initialized without calling init_resources().

function CallOnceInit

CallOnceInit() =default

Default constructor. Does not initialize the resource.

Exceptions:

  • None.

Since: 0.1

Note: None.

Warning: None.

The resource is initialized on the first call to get_resources().

function get_resources

inline Resources & get_resources()

Retrieves the resource, initializing if necessary.

Exceptions:

Return: Reference to the initialized resource.

Since: 0.1

Note: Thread-safe for initialization. Concurrent calls block until initialization completes.

Warning: Concurrent re-initialization via force_reinit() requires external synchronization.

If the resource has not been initialized, this method triggers thread-safe initialization using std::call_once. Subsequent calls return the cached resource.

function force_reinit

inline void force_reinit()

Forces re-initialization of the resource.

Exceptions:

Since: 0.1

Note: Blocks until the internal mutex is acquired.

Warning: Not thread-safe with respect to concurrent get_resources() calls. External synchronization is required.

Locks the internal mutex and calls force_do_reinit() to re-initialize the resource, even if it was already initialized.

function force_reinit

template <typename... Args>
inline void force_reinit(
    Args &&... args
)

Forces re-initialization with new arguments.

Parameters:

  • args Arguments to forward to the Resources constructor.

Exceptions:

  • May throw if Resources constructor throws.

Template Parameters:

  • Args Types of arguments to forward to Resources constructor.

Since: 0.1

Note: Blocks until the internal mutex is acquired.

Warning: Not thread-safe with respect to concurrent get_resources() calls. External synchronization is required.

Locks the internal mutex, replaces the resource with a new instance constructed from the provided arguments, and marks it as initialized.

Protected Functions Documentation

function init_resources

virtual bool init_resources() =0

Performs the actual resource initialization.

Exceptions:

  • Implementation-defined exceptions.

Return: true if initialization succeeded, false otherwise.

Since: 0.1

Note: Called at most once during the lifetime of the object unless force_reinit() is called.

Warning: None.

Called during first access to initialize the resource. Implementations should populate the resource member.

function force_do_reinit

virtual bool force_do_reinit() =0

Performs forced re-initialization.

Exceptions:

  • Implementation-defined exceptions.

Return: true if re-initialization succeeded, false otherwise.

Since: 0.1

Note: May be called multiple times.

Warning: None.

Called when force_reinit() is invoked to re-initialize the resource.

Protected Attributes Documentation

variable resource

Resources resource;

The managed resource. Ownership: owner.


Updated on 2026-03-09 at 10:14:00 +0000