19 #include <type_traits>
30 namespace ds = paal::data_structures;
33 template <
typename... Args>
37 void example_default_constructor_and_basic_usage() {
39 typedef Comps<int, double, int> MyComps;
42 assert(comps.get<names::A>() == 0);
45 comps.set<names::C>(7);
46 assert(comps.get<names::C>() == 7);
48 const MyComps &constAlias = comps;
50 constAlias.get<names::B>();
53 void example_constructor_with_all_arguments() {
55 Comps<int, double, int> comps(5, 4, 3);
56 assert(comps.get<names::A>() == 5);
57 assert(comps.get<names::B>() == 4);
58 assert(comps.get<names::C>() == 3);
61 void example_constructor_with_some_arguments() {
63 Comps<int, double, int> comps(5, 4);
64 assert(comps.get<names::A>() == 5);
65 assert(comps.get<names::B>() == 4);
68 int f(
int i) {
return i; }
69 void example_calling_function_from_components() {
71 typedef Comps<int (*)(int), int (*)(int), int> CompsF;
74 CompsF comps(f, f, 17);
75 const CompsF &constAlias = comps;
78 assert(comps.call<names::A>(2) == 2);
81 assert(constAlias.call<names::B>(2) == 2);
84 void example_replacing() {
86 typedef Comps<int (*)(int), double, int> CompsF;
90 typedef Comps<std::pair<int, int>, double,
int> ReplacedCheck;
91 static_assert(std::is_same<Replaced, ReplacedCheck>::value,
92 "Invalid replaceped type");
95 CompsF comps(f, 2, 17);
96 Replaced replaced = ds::replace<names::A>(std::make_pair(11, 12), comps);
98 auto p = replaced.get<names::A>();
99 assert(p.first == 11);
100 assert(p.second == 12);
101 assert(replaced.get<names::B>() == 2);
102 assert(replaced.get<names::C>() == 17);
108 explicit X(
int _x) : x(_x) {}
110 bool operator==(
X xx)
const {
return x == xx.x; }
116 explicit Y(
int _y) : y(_y) {}
123 explicit Z(
int _z) : z(_z) {}
130 template <
typename... Args>
134 void example_default_parameters() {
136 CompsWithDefaults<int, double, float> compsDef;
139 CompsWithDefaults<int, double> compsDef2(1, 2, 3);
140 assert(compsDef2.get<names::C>() ==
X(3));
147 template <
typename... Args>
148 using CompsToReplace =
151 void example_replacing_struct_without_default_constructors() {
156 CompsToReplace<X, Y> compsToReplace(x, y);
158 auto s = ds::replace<names::A>(z, compsToReplace);
159 assert(s.get<names::A>().z == 3);
161 auto s2 = ds::replace<names::B>(z, compsToReplace);
162 assert(s2.get<names::B>().z == 3);
165 void example_create_using_make() {
167 typedef Comps<int, double, float> SomeComps;
168 auto someComps = SomeComps::make<names::A, names::C>(1, 2.f);
169 assert(someComps.get<names::A>() == 1);
170 assert(someComps.get<names::C>() == 2.f);
172 auto someComps2 = SomeComps::make<names::C, names::A>(1.f, 2);
173 assert(someComps2.get<names::C>() == 1.f);
174 assert(someComps2.get<names::A>() == 2);
177 void example_create_using_copy_tag() {
178 typedef Comps<int, double, float> SomeComps;
179 SomeComps someComps(CompsToReplace<int, int>(1, 2),
ds::copy_tag());
180 assert(someComps.get<names::A>() == 1);
181 assert(someComps.get<names::B>() == 2.);
184 void example_references() {
187 typedef Comps<int, const int &, int &> CompsWithRefs;
188 CompsWithRefs compsWithRefs(a, a, a);
190 CompsWithRefs::make<names::B, names::C>(a, a);
193 void example_make_components() {
198 auto mComps = MComps::make_components(1, std::ref(a));
199 static_assert(std::is_same<std::remove_reference<decltype(mComps)>::type,
200 MComps::type<int, int &>>::value,
210 example_default_constructor_and_basic_usage();
211 example_constructor_with_all_arguments();
212 example_constructor_with_some_arguments();
213 example_calling_function_from_components();
215 example_default_parameters();
216 example_replacing_struct_without_default_constructors();
217 example_create_using_make();
218 example_create_using_copy_tag();
219 example_references();
220 example_make_components();
This structure can be passed on Names list and represents Name and the default type value...
Indicates that components constructor is in fact a Copy/Move Constructor.
Generic version of replaced_type.