iceoryx_hoofs 2.0.3
generic_raii.hpp
1// Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
2// Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16// SPDX-License-Identifier: Apache-2.0
17#ifndef IOX_HOOFS_CXX_GENERIC_RAII_HPP
18#define IOX_HOOFS_CXX_GENERIC_RAII_HPP
19
20#include "iceoryx_hoofs/cxx/function_ref.hpp"
21
22#include <functional>
23
24namespace iox
25{
26namespace cxx
27{
46{
47 public:
50 explicit GenericRAII(const std::function<void()>& cleanupFunction) noexcept;
51
56 GenericRAII(const function_ref<void()>& initFunction, const std::function<void()>& cleanupFunction) noexcept;
57
59 ~GenericRAII() noexcept;
60
61 GenericRAII(const GenericRAII&) = delete;
62 GenericRAII& operator=(const GenericRAII&) = delete;
63
65 GenericRAII(GenericRAII&& rhs) noexcept;
66
68 GenericRAII& operator=(GenericRAII&& rhs) noexcept;
69
70 private:
71 void destroy() noexcept;
72
73 private:
74 std::function<void()> m_cleanupFunction;
75};
76
77} // namespace cxx
78} // namespace iox
79
80#endif // IOX_HOOFS_CXX_GENERIC_RAII_HPP
The GenericRAII class is a simple helper class to apply the C++ RAII idiom quickly....
Definition: generic_raii.hpp:46
GenericRAII(const std::function< void()> &cleanupFunction) noexcept
constructor which creates GenericRAII that calls only the cleanupFunction on destruction
GenericRAII(const function_ref< void()> &initFunction, const std::function< void()> &cleanupFunction) noexcept
constructor which calls initFunction and stores the cleanupFunction which will be called in the destr...
~GenericRAII() noexcept
calls m_cleanupFunction callable if it was set in the constructor
Definition: function_ref.hpp:34
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:29