|
| 1 | +/******************************************************************************* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * This file is part of Mt-KaHyPar. |
| 5 | + * |
| 6 | + * Copyright (C) 2025 Nikolai Maas <nikolai.maas@kit.edu> |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in all |
| 16 | + * copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | + * SOFTWARE. |
| 25 | + ******************************************************************************/ |
| 26 | + |
| 27 | +#pragma once |
| 28 | + |
| 29 | +#include <memory> |
| 30 | +#include <unordered_map> |
| 31 | +#include <mutex> |
| 32 | +#include <sstream> |
| 33 | +#include <type_traits> |
| 34 | + |
| 35 | +#include "kahypar-resources/meta/function_traits.h" |
| 36 | +#include "kahypar-resources/meta/template_parameter_to_string.h" |
| 37 | + |
| 38 | +#include "mt-kahypar/macros.h" |
| 39 | +#include "mt-kahypar/utils/exception.h" |
| 40 | + |
| 41 | + |
| 42 | +namespace mt_kahypar { |
| 43 | +template <typename IdentifierType, typename ProductCreator> |
| 44 | +class ThreadSafeFactory { |
| 45 | + private: |
| 46 | + using AbstractProduct = typename std::remove_pointer_t< |
| 47 | + typename kahypar::meta::FunctionTraits<ProductCreator>::result_type>; |
| 48 | + using AbstractProductPtr = std::unique_ptr<AbstractProduct>; |
| 49 | + using UnderlyingIdentifierType = typename std::underlying_type_t<IdentifierType>; |
| 50 | + using CallbackMap = std::unordered_map<UnderlyingIdentifierType, ProductCreator>; |
| 51 | + |
| 52 | + public: |
| 53 | + ThreadSafeFactory(const ThreadSafeFactory&) = delete; |
| 54 | + ThreadSafeFactory(ThreadSafeFactory&&) = delete; |
| 55 | + ThreadSafeFactory& operator= (const ThreadSafeFactory&) = delete; |
| 56 | + ThreadSafeFactory& operator= (ThreadSafeFactory&&) = delete; |
| 57 | + |
| 58 | + ~ThreadSafeFactory() = default; |
| 59 | + |
| 60 | + template <typename I, typename ... ProductParameters> |
| 61 | + AbstractProductPtr createObject(const I& id, ProductParameters&& ... params) { |
| 62 | + const auto creator = _callbacks.find(static_cast<UnderlyingIdentifierType>(id)); |
| 63 | + if (creator != _callbacks.end()) { |
| 64 | + return AbstractProductPtr((creator->second)(std::forward<ProductParameters>(params) ...)); |
| 65 | + } |
| 66 | + std::stringstream ss; |
| 67 | + ss << "Could not load " << kahypar::meta::templateToString<IdentifierType>() << ": " << id << std::endl; |
| 68 | + ss << "Please check your .ini config file."; |
| 69 | + throw InvalidParameterException(ss.str()); |
| 70 | + } |
| 71 | + |
| 72 | + static ThreadSafeFactory & getInstance() { |
| 73 | + static ThreadSafeFactory _factory_instance; |
| 74 | + return _factory_instance; |
| 75 | + } |
| 76 | + |
| 77 | + static bool registerObject(const IdentifierType& id, ProductCreator creator) { |
| 78 | + static std::mutex lock; |
| 79 | + |
| 80 | + std::lock_guard<std::mutex> guard(lock); |
| 81 | + auto& instance = getInstance(); |
| 82 | + return instance._callbacks.insert({ static_cast<UnderlyingIdentifierType>(id), creator }).second; |
| 83 | + } |
| 84 | + |
| 85 | + private: |
| 86 | + |
| 87 | + ThreadSafeFactory() : |
| 88 | + _callbacks() { } |
| 89 | + |
| 90 | + CallbackMap _callbacks; |
| 91 | +}; |
| 92 | +} // namespace mtkahypar |
0 commit comments