-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathFactory.hxx
More file actions
36 lines (31 loc) · 720 Bytes
/
Factory.hxx
File metadata and controls
36 lines (31 loc) · 720 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
* Factory.hxx
*
* Created on: Oct 23, 2021
* Author: <a href="mailto:damirlj@yahoo.com">Damir Ljubic</a>
*/
#ifndef DI_FACTORY_HXX_
#define DI_FACTORY_HXX_
#include <memory>
#include <type_traits>
namespace utils::di
{
/**
* Universal factory
*
* @tparam Object: Object to be created
*/
template <class Object, class...Args>
std::unique_ptr<Object> factory(Args&&...args)
{
if constexpr ((std::is_constructible_v<Object, Args&&> && ...)) // unary right fold expression
{
return std::make_unique<Object>(std::forward<Args>(args)...);
}
else
{
return nullptr;
}
}
}
#endif /* DI_FACTORY_HXX_ */