Sequoia
Loading...
Searching...
No Matches
Creator.hpp
Go to the documentation of this file.
1
2// Copyright Oliver J. Rosten 2022. //
3// Distributed under the GNU GENERAL PUBLIC LICENSE, Version 3.0. //
4// (See accompanying file LICENSE.md or copy at //
5// https://www.gnu.org/licenses/gpl-3.0.en.html) //
7
8#pragma once
9
15
16namespace sequoia::object
17{
18 template<class T>
19 concept creator = requires(T& a) {
20 requires has_value_type_v<T> || has_element_type_v<T>;
21 typename T::product_type;
22
23 { a.make(std::declval<typename T::product_type>()) } -> std::same_as<typename T::product_type>;
24 };
25
26 namespace impl
27 {
28 template<class Product, class T>
29 inline constexpr bool dependent_value_type_same_as_v{
30 requires{
31 typename Product::value_type;
32
33 requires std::same_as<T, typename Product::value_type>;
34 }
35 };
36
37 template<class Product, class T>
38 inline constexpr bool dependent_element_type_same_as_v{
39 requires{
40 typename Product::element_type;
41
42 requires std::same_as<T, typename Product::element_type>;
43 }
44 };
45 }
46
47 template<class Product, class T>
48 inline constexpr bool product_for_v{
49 (impl::dependent_value_type_same_as_v<Product, T> && initializable_from<Product, T>)
50 || (impl::dependent_element_type_same_as_v<Product, T> && initializable_from<Product, T*>)
51 };
52
53 template<class Product, class T>
54 concept makeable_from = initializable_from<Product, T> || product_for_v<Product, T>;
55
56 template<std::movable T, makeable_from<T> Product>
58 {
59 template<class... Args>
60 requires initializable_from<Product, Args...>
61 [[nodiscard]]
62 constexpr Product operator()(Args&&... args) const
63 {
64 return Product{std::forward<Args>(args)...};
65 }
66 };
67
72 template<std::movable T, makeable_from<T> Product, class Transformer=direct_forwarder<T, Product>>
74 {
75 public:
76 using product_type = Product;
77 using value_type = T;
78 using transfomer_type = Transformer;
79
80 template<class... Args>
81 requires initializable_from<product_type, std::invoke_result_t<Transformer, Args...>>
82 [[nodiscard]]
83 constexpr static product_type make(Args&&... args)
84 {
85 return product_type{Transformer{}(std::forward<Args>(args)...)};
86 }
87
88 [[nodiscard]]
89 friend constexpr bool operator==(const producer&, const producer&) noexcept = default;
90 };
91}
Concepts which are sufficiently general to appear in the sequoia namespace.
Creates a product for T.
Definition: Creator.hpp:74
A concept similar to std::constructible_from, but which considers braced-init.
Definition: Concepts.hpp:75
Definition: Creator.hpp:19
Definition: Creator.hpp:54
Definition: Creator.hpp:58