cppcore
A small, portable C++ core library
Loading...
Searching...
No Matches
cppcore::TAsyncLoader< T > Class Template Reference

This template class implements a simple loader which runs load requests asynchronously. More...

#include <TAsyncLoader.h>

Public Types

using LoadFunc = std::function<T()>
 The load function type, describes the blocking load operation to run asynchronously.

Public Member Functions

 TAsyncLoader ()=default
 The default class constructor.
 ~TAsyncLoader ()
 The class destructor, will wait for all pending loads to finish.
std::future< T > load (LoadFunc loadFunc)
 Will start a new load request which runs asynchronously.
size_t numPending () const
 Returns the number of load requests which have not finished yet.
bool hasPending () const
 Returns true, if there is at least one load request which has not finished yet.
void waitForAll ()
 Blocks the calling thread until all pending load requests have finished.

Detailed Description

template<class T>
class cppcore::TAsyncLoader< T >

This template class implements a simple loader which runs load requests asynchronously.

A load request is described by a user-provided load function which returns the loaded resource of type T. Each request is dispatched onto its own worker via std::async and a std::future is handed back to the caller to retrieve the result once it is ready. The loader keeps track of the number of outstanding requests, so you can wait until all of them have finished. The class destructor will block until all pending loads are done, so no request can outlive the loader instance it was started from.

std::future<Buffer> request = loader.load([]() { return readFileBlocking("data.bin"); });
// ... do other work ...
Buffer buffer = request.get();
TAsyncLoader()=default
The default class constructor.
std::future< T > load(LoadFunc loadFunc)
Will start a new load request which runs asynchronously.

Definition at line 55 of file TAsyncLoader.h.

Member Typedef Documentation

◆ LoadFunc

template<class T>
using cppcore::TAsyncLoader< T >::LoadFunc = std::function<T()>

The load function type, describes the blocking load operation to run asynchronously.

Definition at line 58 of file TAsyncLoader.h.

Constructor & Destructor Documentation

◆ TAsyncLoader()

template<class T>
cppcore::TAsyncLoader< T >::TAsyncLoader ( )
default

The default class constructor.

References TAsyncLoader().

Referenced by load(), and TAsyncLoader().

◆ ~TAsyncLoader()

template<class T>
cppcore::TAsyncLoader< T >::~TAsyncLoader ( )
inline

The class destructor, will wait for all pending loads to finish.

Definition at line 95 of file TAsyncLoader.h.

References waitForAll().

Member Function Documentation

◆ hasPending()

template<class T>
bool cppcore::TAsyncLoader< T >::hasPending ( ) const
inline

Returns true, if there is at least one load request which has not finished yet.

Returns
true if any request is still pending, else false.

Definition at line 139 of file TAsyncLoader.h.

References numPending().

◆ load()

template<class T>
std::future< T > cppcore::TAsyncLoader< T >::load ( LoadFunc loadFunc)
inline

Will start a new load request which runs asynchronously.

Parameters
[in]loadFuncThe load function to execute, must be valid.
Returns
A future which will hold the loaded resource once the request has finished.

Definition at line 100 of file TAsyncLoader.h.

References TAsyncLoader().

◆ numPending()

template<class T>
size_t cppcore::TAsyncLoader< T >::numPending ( ) const
inline

Returns the number of load requests which have not finished yet.

Returns
The number of pending load requests.

Definition at line 133 of file TAsyncLoader.h.

Referenced by hasPending().

◆ waitForAll()

template<class T>
void cppcore::TAsyncLoader< T >::waitForAll ( )
inline

Blocks the calling thread until all pending load requests have finished.

Definition at line 144 of file TAsyncLoader.h.

Referenced by ~TAsyncLoader().


The documentation for this class was generated from the following file: