diff --git a/ddsrouter_utils/include/ddsrouter_utils/types/Atomicable.hpp b/ddsrouter_utils/include/ddsrouter_utils/types/Atomicable.hpp new file mode 100644 index 000000000..4ae011e4d --- /dev/null +++ b/ddsrouter_utils/include/ddsrouter_utils/types/Atomicable.hpp @@ -0,0 +1,65 @@ +// Copyright 2022 Proyectos y Sistemas de Mantenimiento SL (eProsima). +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** + * @file Atomicable.hpp + * + * This file contains class Atomicable definition. + */ + +#ifndef _DDSROUTERUTILS_TYPES_ATOMICABLE_HPP_ +#define _DDSROUTERUTILS_TYPES_ATOMICABLE_HPP_ + +#include +#include + +namespace eprosima { +namespace ddsrouter { +namespace utils { + +/** + * @brief Utils class to join any type value and a mutex together for commodity and readability. + * + * @tparam Type actual type of the object. + * @tparam Mutex class of the mutex. Default std::mutex but could be recursive, shared, etc. + * + * @note as primitive types are not classes, are not allowed in this class. Use std::atomic instead. + * + * EXAMPLE OF USE + * Atomicable f; + * std::unique_lock lock(f); + * f.foo(); + */ +template +class Atomicable : public Type, public Mutex +{ + // Nothing to add +}; + +/** + * Alias to use a Atomicable object with a shared time mutex. + * + * EXAMPLE OF USE + * SharedAtomicable f; + * std::shared_lock lock(f); + * f.foo(); + */ +template +using SharedAtomicable = Atomicable; + +} /* namespace utils */ +} /* namespace ddsrouter */ +} /* namespace eprosima */ + +#endif /* _DDSROUTERUTILS_TYPES_ATOMICABLE_HPP_ */