|
| 1 | +// Copyright 2022 Proyectos y Sistemas de Mantenimiento SL (eProsima). |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +/** |
| 16 | + * @file Atomicable.hpp |
| 17 | + * |
| 18 | + * This file contains class Atomicable definition. |
| 19 | + */ |
| 20 | + |
| 21 | +#ifndef _DDSROUTERUTILS_TYPES_ATOMICABLE_HPP_ |
| 22 | +#define _DDSROUTERUTILS_TYPES_ATOMICABLE_HPP_ |
| 23 | + |
| 24 | +#include <mutex> |
| 25 | +#include <shared_mutex> |
| 26 | + |
| 27 | +namespace eprosima { |
| 28 | +namespace ddsrouter { |
| 29 | +namespace utils { |
| 30 | + |
| 31 | +/** |
| 32 | + * @brief Utils class to join any type value and a mutex together for commodity and readability. |
| 33 | + * |
| 34 | + * @tparam Type actual type of the object. |
| 35 | + * @tparam Mutex class of the mutex. Default std::mutex but could be recursive, shared, etc. |
| 36 | + * |
| 37 | + * @note as primitive types are not classes, are not allowed in this class. Use std::atomic instead. |
| 38 | + * |
| 39 | + * EXAMPLE OF USE |
| 40 | + * Atomicable<Foo> f; |
| 41 | + * std::unique_lock lock(f); |
| 42 | + * f.foo(); |
| 43 | + */ |
| 44 | +template <class Type, class Mutex = std::mutex> |
| 45 | +class Atomicable : public Type, public Mutex |
| 46 | +{ |
| 47 | + // Nothing to add |
| 48 | +}; |
| 49 | + |
| 50 | +/** |
| 51 | + * Alias to use a Atomicable object with a shared time mutex. |
| 52 | + * |
| 53 | + * EXAMPLE OF USE |
| 54 | + * SharedAtomicable<Foo> f; |
| 55 | + * std::shared_lock lock(f); |
| 56 | + * f.foo(); |
| 57 | + */ |
| 58 | +template <class Type> |
| 59 | +using SharedAtomicable = Atomicable<Type, std::shared_timed_mutex>; |
| 60 | + |
| 61 | +} /* namespace utils */ |
| 62 | +} /* namespace ddsrouter */ |
| 63 | +} /* namespace eprosima */ |
| 64 | + |
| 65 | +#endif /* _DDSROUTERUTILS_TYPES_ATOMICABLE_HPP_ */ |
0 commit comments