1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 #if ! defined(INCLUDED_COMPHELPER_SCOPEGUARD_HXX) 25 #define INCLUDED_COMPHELPER_SCOPEGUARD_HXX 26 27 #if ! defined(INCLUDED_COMPHELPERDLLAPI_H) 28 #include "comphelper/comphelperdllapi.h" 29 #endif 30 #include "boost/function.hpp" 31 #include "boost/noncopyable.hpp" 32 #include "boost/bind.hpp" 33 34 namespace comphelper { 35 36 /** ScopeGuard to ease writing exception-safe code. 37 */ 38 class COMPHELPER_DLLPUBLIC ScopeGuard : private ::boost::noncopyable 39 // noncopyable until we have 40 // good reasons... 41 { 42 public: 43 enum exc_handling { IGNORE_EXCEPTIONS, ALLOW_EXCEPTIONS }; 44 45 /** @param func function object to be executed in dtor 46 @param excHandling switches whether thrown exceptions in dtor will be 47 silently ignored (but OSL_ asserted) 48 */ 49 template <typename func_type> ScopeGuard(func_type const & func,exc_handling excHandling=IGNORE_EXCEPTIONS)50 explicit ScopeGuard( func_type const & func, 51 exc_handling excHandling = IGNORE_EXCEPTIONS ) 52 : m_func( func ), m_excHandling( excHandling ) {} 53 54 ~ScopeGuard(); 55 56 /** Dismisses the scope guard, i.e. the function won't 57 be executed. 58 */ 59 void dismiss(); 60 61 private: 62 ::boost::function0<void> m_func; // preferring portable syntax 63 exc_handling const m_excHandling; 64 }; 65 66 } // namespace comphelper 67 68 #endif // ! defined(INCLUDED_COMPHELPER_SCOPEGUARD_HXX) 69 70