Welcome to mirror list, hosted at ThFree Co, Russian Federation.

scope_guard.hpp « base - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: df907096f42f9f98855b8cb4220ca77040359379 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/// @author	Yury Melnichek mailto:melnichek@gmail.com
/// @date	07.01.2005
/// @brief	See http://gzip.rsdn.ru/forum/Message.aspx?mid=389127&only=1.

#pragma once

#include "base/base.hpp"

namespace base
{
namespace impl
{
  /// Base class for all ScopeGuards.
  class GuardBase
  {
  public:
    /// Disable ScopeGuard functionality on it's destruction
    void release() const
    {
      m_bDoRollback = false;
    }

  protected:

    GuardBase() : m_bDoRollback(true)
    {
    }

    // Do something in the destructor
    mutable bool m_bDoRollback;
  };

  /// ScopeGuard for specific functor
  template <typename TFunctor> class GuardImpl : public GuardBase
  {
  public:
    explicit GuardImpl(TFunctor const & F) : m_Functor(F)
    {
    }

    ~GuardImpl()
    {
      if (m_bDoRollback)
        m_Functor();
    }

  private:
    TFunctor m_Functor;
  };
} // namespace impl

typedef impl::GuardBase const & scope_guard;

/// Create scope_guard
template <typename TFunctor>
impl::GuardImpl<TFunctor> make_scope_guard(TFunctor const & F)
{
  return impl::GuardImpl<TFunctor>(F);
}
} // namespace base

#define SCOPE_GUARD(name, func)                            \
  ::base::scope_guard name = base::make_scope_guard(func); \
  static_cast<void>(name);