Ponca  fd5150bf9079aabdb82a6f05e419b76111c1f788
Point Cloud Analysis library
Loading...
Searching...
No Matches
typelist.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 <tuple>
9#include <cstddef>
10#include <algorithm>
11
12#include <Ponca/src/Common/typeutils.h>
13
14namespace Ponca
15{
27 template <Ponca::StringLiteral _Name, class Type>
29 {
30 using type = Type;
31
32 private:
33 static constexpr Ponca::StringLiteral _name = _Name;
34
35 public:
36 static constexpr const char* name = _name.value;
37
38 FactoryEntry(size_t i) : idx(i) {}
39
40 // This index (which is the index within the list) is not part of the type
41 // because this would make the code horrendous. The factory specification
42 // would need to wait for the list to be complete and hence store "to be templated classes".
43 // Filling this index can be done in 3 lines and will work flawlessly without
44 // any runtime cost (most of it will be at compile time) and the cost of an int in memory...
45 const size_t idx;
46 Type object;
47 };
48
49 namespace internal
50 {
51 // Another namespace layer to avoid future potential conflicts in the name choices
52 namespace Factory
53 {
54 // Convert of std::tuple<FactoryEntry> to a std::tuple<FactoryEntry::type>
55 template <typename EntryList>
57
58 // Specialized version for std::tuple
59 template <typename... Ts>
60 struct FactoryEntryTypeExtractor<std::tuple<Ts...>>
61 {
62 using type = std::tuple<typename Ts::type...>;
63 };
64
65 // Base filtering template interface
66 template <class Pred, typename... Ts>
67 struct filter;
68
69 // Base case nothing to filter
70 template <class Pred>
71 struct filter<Pred>
72 {
73 using type = std::tuple<>;
74 };
75
76 // Shortcut to concatenate std::tuple
77 template <typename... Tuples>
78 using tuple_cat_t = decltype(std::tuple_cat(std::declval<Tuples>()...));
79
80 // General case, head conditionnaly adds current type to the list;
81 template <class Pred, typename T, typename... Ts>
82 struct filter<Pred, T, Ts...>
83 {
84 static constexpr bool keepT = Pred::template value<T>;
85 using head = std::conditional_t<keepT, std::tuple<T>, std::tuple<>>;
86 using tail = typename filter<Pred, Ts...>::type;
87 using type = tuple_cat_t<head, tail>;
88 };
89
90 // Type to allow type indirection. ie being able to extract all
91 // types from a tuple while letting the class being templated
92 // with a tuple
93 template <class Pred, typename... Ts>
94 struct filterList;
95
96 template <typename Pred, typename... Ts>
97 struct filterList<Pred, std::tuple<Ts...>>
98 {
99 using type = typename filter<Pred, Ts...>::type;
100 };
101
102 // Combination of multiple predicates
103 template <template <typename> class... Preds>
105 {
106 template <typename T>
107 static constexpr bool value = (Preds<T>::value && ...);
108 };
109
110 }; // namespace Factory
111 } // namespace internal
112} // namespace Ponca
113
This Source Code Form is subject to the terms of the Mozilla Public License, v.
Definition concepts.h:11
Factory entry.
Definition typelist.h:29
Default factory templated on Point, Filter and Derivative.
Definition factory.h:218
StringLiteral as template parameters.
Definition typeutils.h:18