peary/interfaces/InterfaceManager.hpp

Peary Interface Manager. More…

Namespaces

Name
peary
peary::interface

Classes

Name
class peary::interface::InterfaceManager Singleton class to manage interface instances.

Detailed Description

Peary Interface Manager.

Copyright: Copyright (c) 2016-2025 CERN and the Peary Caribou authors. This software is distributed under the terms of the LGPL-3.0-only License, copied verbatim in the file “LICENSE.md”. SPDX-License-Identifier: LGPL-3.0-only

Source code

  
#pragma once

#include <map>
#include <memory>
#include <mutex>
#include <string>
#include <vector>

#include "peary/interfaces/Interface.hpp"

namespace peary::interface {

    class InterfaceManager {

    protected:
        InterfaceManager() = default;

    private:
        // Static instance of this singleton class
        static InterfaceManager instance;

        // Mutex protecting interface generation
        static std::mutex mutex;

    public:
        template <typename T> static T& getInterface(typename T::configuration_type const& config) {
            static std::map<typename T::configuration_type, std::unique_ptr<T, void (*)(T*)>> interfaces;

            std::lock_guard<std::mutex> lock(mutex);

            try {
                return *interfaces.at(config);
            }
            // such interface has not been opened yet
            catch(const std::out_of_range&) {

                interfaces.emplace(std::piecewise_construct,
                                   std::forward_as_tuple(config),
                                   std::forward_as_tuple(new T(config), InterfaceManager::deleteInterface<T>));
                return *interfaces.at(config);
            }
        }

        template <typename T> static void deleteInterface(T* interface) { delete interface; }

        InterfaceManager(InterfaceManager const&) = delete;
        void operator=(InterfaceManager const&) = delete;
    };

} // namespace peary::interface
  

Updated on 2025-11-14 at 11:31:23 +0100