peary/hal/Components.hpp

Components Base Class. More…

Namespaces

Name
peary
peary::hal

Classes

Name
class peary::hal::Component This class serves as base class for a unified interface to hardware components.

Detailed Description

Components Base Class.

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 <concepts>
#include <functional>
#include <memory>
#include <mutex>
#include <tuple>
#include <utility>

namespace peary::hal {

    template <typename Interface> class Component {
    public:
        template <typename... Args>
            requires std::constructible_from<Interface, Args...>
        explicit Component(Args&&... args)
            : generator_([args_tuple = std::make_tuple(std::forward<Args>(args)...)]() mutable {
                  return std::apply([](auto&&... a) { return std::make_unique<Interface>(std::forward<decltype(a)>(a)...); },
                                    args_tuple);
              }) {};

        /* Deleted default constructor, default destructor */
        Component() = delete;
        virtual ~Component() = default;

        Interface& interface() {
            std::call_once(init_flag_, [this] { interface_ = generator_(); });
            return *interface_;
        }

    private:
        /* Unique pointer to the interface endpoint */
        std::unique_ptr<Interface> interface_ {nullptr};
        /* Generator function to create the interface when required */
        std::function<std::unique_ptr<Interface>()> generator_;
        std::once_flag init_flag_;
    };

} // namespace peary::hal
  

Updated on 2026-01-30 at 22:01:05 +0100