Alexandria  2.16
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
InstOrRefHolder.icpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2020 Euclid Science Ground Segment
3  *
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)
7  * any later version.
8  *
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
12  * details.
13  *
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
17  */
18 
19 /*
20  * @file InstOrRefHolder.icpp
21  * @author nikoapos
22  */
23 
24 #include <type_traits>
26 
27 namespace Euclid {
28 
29 namespace InstOrRefHolder_Impl {
30 
31 template <typename InterfaceType, typename InstanceType>
32 struct InstHolder_Impl : public InstOrRefHolder<InterfaceType> {
33  template <typename... Args>
34  InstHolder_Impl(Args... args) : m_instance(std::forward<Args>(args)...) { }
35  virtual ~InstHolder_Impl() = default;
36  InterfaceType& ref() override {
37  return m_instance;
38  }
39 private:
40  InstanceType m_instance;
41 };
42 
43 template <typename InterfaceType>
44 struct RefHolder_Impl : public InstOrRefHolder<InterfaceType> {
45  RefHolder_Impl(InterfaceType& ref) : m_reference(ref) { }
46  virtual ~RefHolder_Impl() = default;
47  InterfaceType& ref() override {
48  return m_reference.get();
49  }
50 private:
52 };
53 
54 } // end of namespace InstOrRefHolder_Impl
55 
56 
57 template <typename InterfaceType>
58 template <typename InstanceType, typename... Args>
61  "InstanceType must be a subtype of InterfaceType");
62  return make_unique<InstOrRefHolder_Impl::InstHolder_Impl<InterfaceType, InstanceType>>(std::forward<Args>(args)...);
63 }
64 
65 template <typename InterfaceType>
67  return make_unique<InstOrRefHolder_Impl::RefHolder_Impl<InterfaceType>>(ref);
68 }
69 
70 } // end of namespace Euclid
71 
std::reference_wrapper< InterfaceType > m_reference
STL class.
static std::unique_ptr< InstOrRefHolder< InterfaceType > > create(Args...args)