<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Mon, Aug 1, 2016 at 2:25 PM Justin Bogner <<a href="mailto:mail@justinbogner.com">mail@justinbogner.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Tim Shen via llvm-commits <<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>> writes:<br>
> timshen created this revision.<br>
> timshen added a reviewer: chandlerc.<br>
> timshen added a subscriber: llvm-commits.<br>
><br>
> make_scope_exit() is described in C++ proposal p0052r2, which uses<br>
> RAII to do cleanup works at scope exit.<br>
<br>
Do you need this for something in particular? We don't usually add<br>
utilities until we need them.<br></blockquote><div><br></div><div>I did when I created the patch, but I don't need it anymore. However Kyle Butt seems to have a need here.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
> <a href="https://reviews.llvm.org/D22796" rel="noreferrer" target="_blank">https://reviews.llvm.org/D22796</a><br>
><br>
> Files:<br>
>   include/llvm/ADT/ScopeExit.h<br>
>   unittests/ADT/CMakeLists.txt<br>
>   unittests/ADT/ScopeExitTest.cpp<br>
><br>
> Index: unittests/ADT/ScopeExitTest.cpp<br>
> ===================================================================<br>
> --- /dev/null<br>
> +++ unittests/ADT/ScopeExitTest.cpp<br>
> @@ -0,0 +1,45 @@<br>
> +//===- llvm/unittest/ADT/ScopeExit.cpp - Scope exit unit tests --*- C++ -*-===//<br>
> +//<br>
> +//                     The LLVM Compiler Infrastructure<br>
> +//<br>
> +// This file is distributed under the University of Illinois Open Source<br>
> +// License. See LICENSE.TXT for details.<br>
> +//<br>
> +//===----------------------------------------------------------------------===//<br>
> +<br>
> +#include "llvm/ADT/ScopeExit.h"<br>
> +#include "gtest/gtest.h"<br>
> +<br>
> +#include <functional><br>
> +#include <vector><br>
> +<br>
> +using namespace llvm;<br>
> +<br>
> +namespace {<br>
> +<br>
> +TEST(ScopeExitTest, Copy) {<br>
> +  std::vector<int> v;<br>
> +  const auto f = [&](int counter) {<br>
> +    return [&v, counter] { v.push_back(counter); };<br>
> +  };<br>
> +  {<br>
> +    auto g1 = make_scope_exit(f(0));<br>
> +    { auto g2 = make_scope_exit(f(1)); }<br>
> +    auto g3 = make_scope_exit(f(2));<br>
> +    EXPECT_EQ((std::vector<int>{1}), v);<br>
> +  }<br>
> +  EXPECT_EQ((std::vector<int>{1, 2, 0}), v);<br>
> +}<br>
> +<br>
> +TEST(ScopeExitTest, Move) {<br>
> +  bool succ = false;<br>
> +  std::function<void()> f([&succ] { succ = true; });<br>
> +  ASSERT_TRUE(static_cast<bool>(f));<br>
> +  {<br>
> +    auto g4 = make_scope_exit(std::move(f));<br>
> +    EXPECT_FALSE(f);<br>
> +    EXPECT_FALSE(succ);<br>
> +  }<br>
> +  EXPECT_TRUE(succ);<br>
> +}<br>
> +}<br>
> Index: unittests/ADT/CMakeLists.txt<br>
> ===================================================================<br>
> --- unittests/ADT/CMakeLists.txt<br>
> +++ unittests/ADT/CMakeLists.txt<br>
> @@ -34,6 +34,7 @@<br>
>    PriorityWorklistTest.cpp<br>
>    RangeAdapterTest.cpp<br>
>    SCCIteratorTest.cpp<br>
> +  ScopeExitTest.cpp<br>
>    SequenceTest.cpp<br>
>    SetVectorTest.cpp<br>
>    SmallPtrSetTest.cpp<br>
> Index: include/llvm/ADT/ScopeExit.h<br>
> ===================================================================<br>
> --- /dev/null<br>
> +++ include/llvm/ADT/ScopeExit.h<br>
> @@ -0,0 +1,54 @@<br>
> +//===- llvm/ADT/ScopeExit.h - Execute code at scope exit --------*- C++ -*-===//<br>
> +//<br>
> +//                     The LLVM Compiler Infrastructure<br>
> +//<br>
> +// This file is distributed under the University of Illinois Open Source<br>
> +// License. See LICENSE.TXT for details.<br>
> +//<br>
> +//===----------------------------------------------------------------------===//<br>
> +//<br>
> +// This file defines the make_scope_exit function.<br>
<br>
It's probably worth mentioning briefly what the function does here.<br></blockquote><div><br></div><div>Done.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
> +//<br>
> +//===----------------------------------------------------------------------===//<br>
> +<br>
> +#ifndef LLVM_ADT_SCOPE_EXIT_H<br>
> +#define LLVM_ADT_SCOPE_EXIT_H<br>
> +<br>
> +#include "llvm/Support/Compiler.h"<br>
> +<br>
> +#include <type_traits><br>
> +#include <utility><br>
> +<br>
> +namespace llvm {<br>
> +namespace detail {<br>
> +<br>
> +template <typename Callable> class scope_exit {<br>
> +  Callable ExitFunction;<br>
> +<br>
> +public:<br>
> +  scope_exit() = delete;<br>
> +<br>
> +  template <typename Fp><br>
> +  explicit scope_exit(Fp &&F) : ExitFunction(std::forward<Fp>(F)) {}<br>
> +<br>
> +  ~scope_exit() { ExitFunction(); }<br>
> +};<br>
> +<br>
> +} // detail<br>
<br>
I think "end namespace detail" is more common.<br></blockquote><div><br></div><div>Done.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
> +<br>
> +// Keeps the callable object that is passed in, and execute it at the<br>
> +// destruction of the returned object (usually at the scope exit where the<br>
> +// returned object is kept).<br>
> +//<br>
> +// Interface is specified by p0052r2.<br>
> +template <typename Callable><br>
> +LLVM_ATTRIBUTE_UNUSED_RESULT<br>
> +detail::scope_exit<typename std::decay<Callable>::type><br>
> +make_scope_exit(Callable &&F) {<br>
> +  return detail::scope_exit<typename std::decay<Callable>::type>(<br>
> +      std::forward<Callable>(F));<br>
> +}<br>
> +<br>
> +} // llvm<br>
> +<br>
> +#endif<br>
> _______________________________________________<br>
> llvm-commits mailing list<br>
> <a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br>
> <a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div></div>