JSON Voorhees
Killer JSON for C++
scope_exit.hpp
Go to the documentation of this file.
1 /** \file jsonv/detail/scope_exit.hpp
2  * Definition of the \c on_scope_exit utility.
3  *
4  * Copyright (c) 2015 by Travis Gockel. All rights reserved.
5  *
6  * This program is free software: you can redistribute it and/or modify it under the terms of the Apache License
7  * as published by the Apache Software Foundation, either version 2 of the License, or (at your option) any later
8  * version.
9  *
10  * \author Travis Gockel (travis@gockelhut.com)
11 **/
12 #ifndef __JSONV_DETAIL_SCOPE_EXIT_HPP_INCLUDED__
13 #define __JSONV_DETAIL_SCOPE_EXIT_HPP_INCLUDED__
14 
15 #include <jsonv/config.hpp>
16 
17 #include <utility>
18 
19 namespace jsonv
20 {
21 namespace detail
22 {
23 
24 template <typename Function>
26 {
27 public:
28  explicit scope_exit_invoker(Function&& func) :
29  _func(std::move(func)),
30  _responsible(true)
31  { }
32 
34  _func(std::move(src._func)),
35  _responsible(src._responsible)
36  {
37  src._responsible = false;
38  }
39 
40  scope_exit_invoker(const scope_exit_invoker&) = delete;
41  scope_exit_invoker& operator=(const scope_exit_invoker&) = delete;
42  scope_exit_invoker& operator=(scope_exit_invoker&&) = delete;
43 
45  {
46  if (_responsible)
47  _func();
48  }
49 
50  void release()
51  {
52  _responsible = false;
53  }
54 
55 private:
56  Function _func;
57  bool _responsible;
58 };
59 
60 template <typename Function>
61 scope_exit_invoker<Function> on_scope_exit(Function func)
62 {
63  return scope_exit_invoker<Function>(std::move(func));
64 }
65 
66 }
67 }
68 
69 #endif/*__JSONV_DETAIL_SCOPE_EXIT_HPP_INCLUDED__*/
Copyright (c) 2014-2019 by Travis Gockel.