2 * Copyright (C) 2012-2022 Euclid Science Ground Segment
4 * This library is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU Lesser General Public License as published by the Free
6 * Software Foundation; either version 3.0 of the License, or (at your option)
9 * This library is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 * @file InstOrRefHolder.icpp
24#include "AlexandriaKernel/memory_tools.h"
29namespace InstOrRefHolder_Impl {
31template <typename InterfaceType, typename InstanceType>
32struct InstHolder_Impl : public InstOrRefHolder<InterfaceType> {
33 template <typename... Args>
34 explicit InstHolder_Impl(Args&&... args) : m_instance(std::forward<Args>(args)...) {}
35 virtual ~InstHolder_Impl() = default;
36 InterfaceType& ref() override {
41 InstanceType m_instance;
44template <typename InterfaceType>
45struct RefHolder_Impl : public InstOrRefHolder<InterfaceType> {
46 explicit RefHolder_Impl(InterfaceType& ref_) : m_reference(ref_) {}
47 virtual ~RefHolder_Impl() = default;
48 InterfaceType& ref() override {
49 return m_reference.get();
53 std::reference_wrapper<InterfaceType> m_reference;
56} // end of namespace InstOrRefHolder_Impl
58template <typename InterfaceType>
59template <typename InstanceType, typename... Args>
60std::unique_ptr<InstOrRefHolder<InterfaceType>> InstOrRefHolder<InterfaceType>::create(Args&&... args) {
61 static_assert(std::is_same<InterfaceType, InstanceType>::value || std::is_base_of<InterfaceType, InstanceType>::value,
62 "InstanceType must be a subtype of InterfaceType");
63 return Euclid::make_unique<InstOrRefHolder_Impl::InstHolder_Impl<InterfaceType, InstanceType>>(
64 std::forward<Args>(args)...);
67template <typename InterfaceType>
68std::unique_ptr<InstOrRefHolder<InterfaceType>> InstOrRefHolder<InterfaceType>::create(InterfaceType& ref) {
69 return Euclid::make_unique<InstOrRefHolder_Impl::RefHolder_Impl<InterfaceType>>(ref);
72} // end of namespace Euclid