mirror of
https://github.com/MariaDB/server.git
synced 2025-08-08 11:22:35 +03:00
MDEV-22441 SCOPE_VALUE macro for temporary values
- Needless engaged_ removed; - SCOPE_VALUE, SCOPE_SET, SCOPE_CLEAR macros for neater declaration; - IF_CLASS / IF_NOT_CLASS SFINAE checkers to pass arg by value or reference; - inline keyword; - couple of refactorings of temporary free_list.
This commit is contained in:
@@ -32,6 +32,11 @@ public:
|
||||
{
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
scope_exit(F &&f, bool engaged) : function_(std::forward<F>(f)), engaged_(engaged)
|
||||
{
|
||||
}
|
||||
|
||||
scope_exit(scope_exit &&rhs)
|
||||
: function_(std::move(rhs.function_)), engaged_(rhs.engaged_)
|
||||
{
|
||||
@@ -43,6 +48,7 @@ public:
|
||||
scope_exit &operator=(const scope_exit &)= delete;
|
||||
|
||||
void release() { engaged_= false; }
|
||||
void engage() { DBUG_ASSERT(!engaged_); engaged_= true; }
|
||||
|
||||
~scope_exit()
|
||||
{
|
||||
@@ -58,38 +64,51 @@ private:
|
||||
} // end namespace detail
|
||||
|
||||
template <typename Callable>
|
||||
detail::scope_exit<typename std::decay<Callable>::type>
|
||||
make_scope_exit(Callable &&f)
|
||||
inline
|
||||
::detail::scope_exit<typename std::decay<Callable>::type>
|
||||
make_scope_exit(Callable &&f, bool engaged= true)
|
||||
{
|
||||
return detail::scope_exit<typename std::decay<Callable>::type>(
|
||||
std::forward<Callable>(f));
|
||||
return ::detail::scope_exit<typename std::decay<Callable>::type>(
|
||||
std::forward<Callable>(f), engaged);
|
||||
}
|
||||
|
||||
#define CONCAT_IMPL(x, y) x##y
|
||||
|
||||
#define CONCAT(x, y) CONCAT_IMPL(x, y)
|
||||
|
||||
#define ANONYMOUS_VARIABLE CONCAT(_anonymous_variable, __LINE__)
|
||||
|
||||
#define SCOPE_EXIT auto ANONYMOUS_VARIABLE= make_scope_exit
|
||||
|
||||
#define IF_CLASS(C) typename std::enable_if<std::is_class<C>::value>::type
|
||||
#define IF_NOT_CLASS(C) typename std::enable_if<!std::is_class<C>::value>::type
|
||||
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template <typename T> class Scope_value
|
||||
template <typename T>
|
||||
class Scope_value
|
||||
{
|
||||
public:
|
||||
// Use SFINAE for passing structs by reference and plain types by value.
|
||||
// This ctor is defined only if T is a class or struct:
|
||||
template <typename U = T, typename = IF_CLASS(U)>
|
||||
Scope_value(T &variable, const T &scope_value)
|
||||
: variable_(variable), saved_value_(variable)
|
||||
: variable_(&variable), saved_value_(variable)
|
||||
{
|
||||
variable= scope_value;
|
||||
}
|
||||
|
||||
// This ctor is defined only if T is NOT a class or struct:
|
||||
template <typename U = T, typename = IF_NOT_CLASS(U)>
|
||||
Scope_value(T &variable, const T scope_value)
|
||||
: variable_(&variable), saved_value_(variable)
|
||||
{
|
||||
variable= scope_value;
|
||||
}
|
||||
|
||||
Scope_value(Scope_value &&rhs)
|
||||
: variable_(rhs.variable_), saved_value_(rhs.saved_value_),
|
||||
engaged_(rhs.engaged_)
|
||||
: variable_(rhs.variable_), saved_value_(rhs.saved_value_)
|
||||
{
|
||||
rhs.engaged_= false;
|
||||
rhs.variable_= NULL;
|
||||
}
|
||||
|
||||
Scope_value(const Scope_value &)= delete;
|
||||
@@ -98,22 +117,50 @@ public:
|
||||
|
||||
~Scope_value()
|
||||
{
|
||||
if (engaged_)
|
||||
variable_= saved_value_;
|
||||
if (variable_)
|
||||
*variable_= saved_value_;
|
||||
}
|
||||
|
||||
private:
|
||||
T &variable_;
|
||||
T *variable_;
|
||||
T saved_value_;
|
||||
bool engaged_= true;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
// Use like this:
|
||||
// auto _= make_scope_value(var, tmp_value);
|
||||
template <typename T>
|
||||
detail::Scope_value<T> make_scope_value(T &variable, const T &scope_value)
|
||||
|
||||
template <typename T, typename = IF_CLASS(T)>
|
||||
inline
|
||||
::detail::Scope_value<T> make_scope_value(T &variable, const T &scope_value)
|
||||
{
|
||||
return detail::Scope_value<T>(variable, scope_value);
|
||||
return ::detail::Scope_value<T>(variable, scope_value);
|
||||
}
|
||||
|
||||
template <typename T, typename = IF_NOT_CLASS(T)>
|
||||
inline
|
||||
::detail::Scope_value<T> make_scope_value(T &variable, T scope_value)
|
||||
{
|
||||
return ::detail::Scope_value<T>(variable, scope_value);
|
||||
}
|
||||
|
||||
/*
|
||||
Note: perfect forwarding version can not pass const:
|
||||
|
||||
template <typename T, typename U>
|
||||
inline
|
||||
detail::Scope_value<T> make_scope_value(T &variable, U &&scope_value)
|
||||
{
|
||||
return detail::Scope_value<T>(variable, std::forward<U>(scope_value));
|
||||
}
|
||||
|
||||
as `const U &&` fails with error `expects an rvalue for 2nd argument`. That
|
||||
happens because const U && is treated as rvalue only (this is the exact syntax
|
||||
for declaring rvalues).
|
||||
*/
|
||||
|
||||
|
||||
#define SCOPE_VALUE auto ANONYMOUS_VARIABLE= make_scope_value
|
||||
#define SCOPE_SET(VAR, MASK) auto ANONYMOUS_VARIABLE= make_scope_value(VAR, VAR | MASK)
|
||||
#define SCOPE_CLEAR(VAR, MASK) auto ANONYMOUS_VARIABLE= make_scope_value(VAR, VAR & ~MASK)
|
||||
|
Reference in New Issue
Block a user