General Utility Library for C++14  2.12
Public Member Functions | List of all members
gul14::FinalAction< F > Class Template Reference

Detailed Description

template<typename F>
class gul14::FinalAction< F >

FinalAction allows us to execute something if the FinalAction object leaves the scope.

A FinalAction can be used to add RAII like behavior for non RAII object or to do timing measurements.

To generate this object you can use the function finally, that leverages template argument deduction of the action's type. This simplifies instantiation a lot.

(Implementation is quite similar to what's in the Guideline-Support-Library.)

A good example is function call duration measurement. The FinalAction just needs to be created in the beginning of a function and no hassle with diverse possible return points.

#include <cstdlib>
#include <ctime>
#include <gul14/gul.h>
using gul14::tic;
using gul14::toc;
void foo() {
auto _ = finally([start = tic()] {
std::cerr << "Function foo() took " << toc(start) << " s.\n";
});
std::srand(std::time(nullptr));
if (std::rand() % 2) {
std::cout << "Premature exit\n";
return;
}
sleep(10); // do important stuff (like sleeping)
std::cout << "Normal exit\n";
}
FinalAction< typename std::decay_t< F > > finally(F &&f) noexcept
finally() - convenience function to generate a FinalAction
Definition: finalizer.h:169
auto toc(std::chrono::steady_clock::time_point t0)
Return the elapsed time in seconds (or a different unit) since the given time point.
Definition: time_util.h:92
bool sleep(const std::chrono::duration< Rep, Period > &duration, const Trigger &trg)
Sleep for at least the given time span, with the option of being woken up from another thread.
Definition: time_util.h:114
std::chrono::steady_clock::time_point tic()
Return the current time as a std::chrono time_point.
Definition: time_util.h:54
Main include file for the General Utility Library.

A (bad; use containers instead) example is allocation with RAII:

#include <new>
#include <string>
#include <gul14/gul.h>
std::string bar(float some_float) {
char* buffer = new char[100];
if (buffer == nullptr)
return;
auto _ = finally([&] { delete[] buffer; buffer = nullptr; });
// do stuff that might throw here
snprintf(buffer, 100, "%.1f", some_float);
return { buffer };
// get rid of buffer automagically
}
Template Parameters
FThe type of the closure/function to be called.
Since
GUL version 1.1

#include <finalizer.h>

Public Member Functions

 FinalAction (F f) noexcept
 Creates a new FinalAction object. More...
 
 FinalAction (FinalAction &&other) noexcept
 Move constructor.
 
FinalActionoperator= (FinalAction &&other) noexcept
 Move assignment operator.
 
 FinalAction ()=delete
 FinalAction is not is_default_constructible.
 
 FinalAction (const FinalAction &)=delete
 FinalAction is not copyable.
 
FinalActionoperator= (const FinalAction &)=delete
 FinalAction is not copyable.
 
 ~FinalAction () noexcept
 Destructor Calls action except when in move contexts.
 

Constructor & Destructor Documentation

◆ FinalAction()

template<typename F >
gul14::FinalAction< F >::FinalAction ( f)
inlineexplicitnoexcept

Creates a new FinalAction object.

It takes any callable as action to be called when the FinalAction destructs (lifetime ends / leaves the scope).

The template parameter F has to be specified; this can be avoided by using the convenience function finally.

Template Parameters
FThe type of the closure/function to be called.
Parameters
fThe closure or function to be called on destruction.

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