Ponca  eb7239a9c07e156fb1edc6faac7fe89e2b256bda
Point Cloud Analysis library
Loading...
Searching...
No Matches
factory.h
1/*
2 This Source Code Form is subject to the terms of the Mozilla Public
3 License, v. 2.0. If a copy of the MPL was not distributed with this
4 file, You can obtain one at http://mozilla.org/MPL/2.0/.
5*/
6#pragma once
7
8#include "factorylist.h"
9#include "typelist.h"
10
11namespace Ponca
12{
13 namespace internal
14 {
23 template <typename _FactoryEntries>
24 struct ComputeObjectList
25 {
26 using FactoryEntries = _FactoryEntries;
27 static constexpr size_t N = std::tuple_size_v<FactoryEntries>;
28
29 constexpr ComputeObjectList()
30 : m_entries([]<std::size_t... Is>(std::index_sequence<Is...>) {
31 return FactoryEntries{std::tuple_element_t<Is, FactoryEntries>{Is}...};
32 }(std::make_index_sequence<std::tuple_size_v<FactoryEntries>>{}))
33 {
34 }
35
41 template <template <typename> class... Preds>
42 static decltype(auto) Filter()
43 {
44 using namespace Factory;
45 return ComputeObjectList<typename filterList<conjunction<Preds...>, FactoryEntries>::type>{};
46 };
47
53 template <StringLiteral... Names>
54 static decltype(auto) Filter()
55 {
57 }
58
69 template <StringLiteral Name>
70 static decltype(auto) GetMethod()
71 {
72 using MethodList = decltype(Filter<MethodProvider<Name>::template pred>());
73 static_assert(MethodList::N == 1);
74
75 // Const here to force copy in entries()!
76 const MethodList list;
77 return std::get<0>(list.entries()).object;
78 }
79
100 template <typename Func>
101 void foreach (Func&& fun)
102 {
103 std::apply([&](auto&&... xs) { (fun(xs), ...); }, m_entries);
104 }
105
106 template <typename Func>
107 void ApplyAt(size_t index, Func&& fun)
108 {
109 // Instead of foreach we could add some runtime helper to get the i-th element.
110 // However, this adds utility and compile time generated function for the same
111 // results and is not deemed worthwile at the time of writting this.
112 this->foreach ([&](auto& x) {
113 if (x.id == index)
114 fun(x);
115 });
116 }
117
118 template <typename Func>
119 void ApplyAt(const char* name, Func&& fun)
120 {
121 // Instead of foreach we could add some runtime helper to get the i-th element.
122 // However, this adds utility and compile time generated function for the same
123 // results and is not deemed worthwile at the time of writting this.
124 this->foreach ([&](auto& x) {
125 if (strcmp(name, x.name) == 0)
126 fun(x);
127 });
128 }
129
133 constexpr static size_t size() { return N; }
134
138 constexpr static decltype(auto) GetNames()
139 {
140 return []<std::size_t... Is>(std::index_sequence<Is...>) {
141 return std::array{std::tuple_element_t<Is, FactoryEntries>::name...};
142 }(std::make_index_sequence<std::tuple_size_v<FactoryEntries>>{});
143 }
144
148 FactoryEntries& entries() { return m_entries; }
149
153 FactoryEntries entries() const { return m_entries; }
154
155 private:
156 FactoryEntries m_entries;
157 };
158
167 template <typename _FactoryEntries>
169 {
170 using FactoryEntries = _FactoryEntries;
171 using FullComputeObjectList = ComputeObjectList<FactoryEntries>;
172
176 template <template <typename> class... Preds>
177 static decltype(auto) Filter()
178 {
179 return FullComputeObjectList::template Filter<Preds...>();
180 }
181
185 template <StringLiteral... Names>
186 static decltype(auto) Filter()
187 {
188 return FullComputeObjectList::template Filter<Names...>();
189 }
190
194 template <StringLiteral Name>
195 static decltype(auto) GetMethod()
196 {
197 return FullComputeObjectList::template GetMethod<Name>();
198 }
199
203 template <typename Func>
204 static void foreach (Func&& fun)
205 {
206 FullComputeObjectList{}.foreach (std::forward<Func>(fun));
207 }
208
212 constexpr static size_t size() { return FullComputeObjectList::N; }
213
217 constexpr static decltype(auto) GetNames() { return FullComputeObjectList::GetNames(); }
218 };
219 } // namespace internal
220
228 template <typename P, typename NF, DiffType DerType>
229 struct Factory : public internal::FactoryBase<FactoryGenericList<P, NF, DerType>>
230 {
231 };
232
242 template <typename P, typename NF, DiffType DerType>
243 requires Is3D<P>
244 struct Factory<P, NF, DerType> : public internal::FactoryBase<Factory3DList<P, NF, DerType>>
245 {
246 };
247
257 template <typename P, typename NF, DiffType DerType>
258 requires Is3D<P> && (bool(DerType & DiffType::FitSpaceDer))
259 struct Factory<P, NF, DerType> : public internal::FactoryBase<Factory3DSpaceDerivatives<P, NF, DerType>>
260 {
261 };
262} // namespace Ponca
Copyright (c) 2022 Jacques-Olivier Lachaud (jacques-olivier.lachaud@univ-savoie.fr) Laboratory of Mat...
This Source Code Form is subject to the terms of the Mozilla Public License, v.
Definition concepts.h:11
@ FitSpaceDer
Flag indicating a space differentiation.
Definition enums.h:36
Default factory templated on Point, Filter and Derivative.
Definition factory.h:230
StringLiteral as template parameters.
Definition typeutils.h:18
List of compute object that can be iterated over.
Definition factory.h:25
FactoryEntries entries() const
Return the list of entries.
Definition factory.h:153
static constexpr size_t size()
Return the number of object held within the object.
Definition factory.h:133
static decltype(auto) Filter()
Returns a new object that is a filtered version of the current one.
Definition factory.h:42
static constexpr decltype(auto) GetNames()
Return the names of the currently held objects.
Definition factory.h:138
static decltype(auto) GetMethod()
Return an object by it's id.
Definition factory.h:70
FactoryEntries & entries()
Return the list of entries.
Definition factory.h:148
Base class for factory.
Definition factory.h:169
static constexpr decltype(auto) GetNames()
Return the names of the currently held objects.
Definition factory.h:217
static decltype(auto) Filter()
Returns a new object that is a filtered version of the current one.
Definition factory.h:177
static decltype(auto) GetMethod()
Return an object by it's id.
Definition factory.h:195
static constexpr size_t size()
Return the number of object held within the Factory.
Definition factory.h:212