From: Samuel Just Date: Tue, 20 Dec 2016 23:04:49 +0000 (-0800) Subject: src/include: add scope_guard.h X-Git-Tag: v12.0.0~306^2~1 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=9c547076d70e4350d259276204b5f65bbeb88bdc;p=ceph.git src/include: add scope_guard.h Signed-off-by: Samuel Just --- diff --git a/src/include/scope_guard.h b/src/include/scope_guard.h new file mode 100644 index 000000000000..0d06ce93618f --- /dev/null +++ b/src/include/scope_guard.h @@ -0,0 +1,37 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2016 Red Hat + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software + * Foundation. See file COPYING. + * + */ + +#ifndef SCOPE_GUARD +#define SCOPE_GUARD + +template +struct scope_guard { + F f; + scope_guard() = delete; + scope_guard(const scope_guard &) = delete; + scope_guard(scope_guard &&) = default; + scope_guard & operator=(const scope_guard &) = delete; + scope_guard & operator=(scope_guard &&) = default; + scope_guard(F &&f) : f(std::move(f)) {} + ~scope_guard() { + f(); + } +}; + +template +scope_guard make_scope_guard(F &&f) { + return scope_guard(std::forward(f)); +} + +#endif