LibreOffice Module comphelper (master)  1
scopeguard.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #ifndef INCLUDED_COMPHELPER_SCOPEGUARD_HXX
21 #define INCLUDED_COMPHELPER_SCOPEGUARD_HXX
22 
23 #include <sal/log.hxx>
24 #include <com/sun/star/uno/Exception.hpp>
25 
26 
27 // For some reason, Android buildbot issues -Werror like this:
28 // In file included from
29 // /home/android/lo/master-android-arm/filter/source/xmlfilteradaptor/XmlFilterAdaptor.cxx:50:
30 // /home/android/lo/master-android-arm/include/comphelper/scopeguard.hxx:36:14:
31 // error: function 'comphelper::<deduction guide for ScopeGuard><(lambda at
32 // /home/android/lo/master-android-arm/filter/source/xmlfilteradaptor/XmlFilterAdaptor.cxx:146:34)>'
33 // has internal linkage but is not defined [-Werror,-Wundefined-internal]
34 // explicit ScopeGuard( Func && func ) : m_func( std::move(func) ) {}
35 // ^
36 // /home/android/lo/master-android-arm/filter/source/xmlfilteradaptor/XmlFilterAdaptor.cxx:146:28:
37 // note: used here
38 // comphelper::ScopeGuard guard([&]() {
39 // ^
40 #ifdef ANDROID
41 #if defined __clang__
42 #pragma clang diagnostic push
43 #pragma clang diagnostic ignored "-Wundefined-internal"
44 #endif
45 #endif
46 
47 
48 namespace comphelper {
49 
52 template <class Func> class ScopeGuard
53 {
54 public:
57  explicit ScopeGuard( Func && func ) : m_func( std::move(func) ) {}
58 
60  {
61  if (m_bDismissed)
62  return;
63  try
64  {
65  m_func();
66  }
67  catch (css::uno::Exception& exc)
68  {
69  SAL_WARN("comphelper", "UNO exception occurred: " << exc);
70  }
71  catch (...)
72  {
73  SAL_WARN("comphelper", "unknown exception occurred!");
74  }
75  }
76 
80  void dismiss() { m_bDismissed = true; }
81 
82 private:
83  // noncopyable until we have good reasons...
84  ScopeGuard(const ScopeGuard&) = delete;
85  ScopeGuard& operator=(const ScopeGuard&) = delete;
86 
87  Func m_func;
88  bool m_bDismissed = false;
89 };
90 
91 } // namespace comphelper
92 
93 #ifdef ANDROID
94 #if defined __clang__
95 #pragma clang diagnostic pop
96 #endif
97 #endif
98 
99 #endif // ! defined(INCLUDED_COMPHELPER_SCOPEGUARD_HXX)
100 
101 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
ScopeGuard to ease writing exception-safe code.
Definition: scopeguard.hxx:52
ScopeGuard & operator=(const ScopeGuard &)=delete
ScopeGuard(Func &&func)
Definition: scopeguard.hxx:57
void dismiss()
Dismisses the scope guard, i.e.
Definition: scopeguard.hxx:80
#define SAL_WARN(area, stream)