Skip to content

Commit 306a237

Browse files
committed
Create 4.md
1 parent c159982 commit 306a237

File tree

1 file changed

+109
-0
lines changed
  • docs/documentation/tips_and_tricks

1 file changed

+109
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# xtd::collections::array_list a heterogeneous container supporting multiple types (🟢 Beginner)
2+
3+
How to store any type with a simple xtd collection, and allow them to be easily manipulated.
4+
5+
## Simple example
6+
7+
```cpp
8+
#include <xtd/xtd>
9+
10+
auto main() -> int {
11+
for (auto item : collections::array_list {"42", 42, 42_s, .42}) {
12+
console::write("{,-8} --> ", item);
13+
if (is<string>(item)) console::write_line("{}", as<string>(item).quoted());
14+
else if (is<int>(item)) console::write_line("0x{:X4}", as<int>(item));
15+
else if (is<time_span>(item)) console::write_line("{} seconds", as<time_span>(item).seconds());
16+
else console::write_line("[{}]", item);
17+
}
18+
}
19+
```
20+
21+
```cpp
22+
// This code can produce the following output:
23+
//
24+
// 42 --> "42"
25+
// 42 --> 0x002A
26+
// 00:00:42 --> 42 seconds
27+
// 0.42 --> [0.42]
28+
```
29+
30+
* [xtd::collections::array_list](https://gammasoft71.github.io/xtd/reference_guides/latest/group__collections.html#ga83a0a06ecb97330626993339079ebbf6) allows you to store any type.
31+
* The operator [xtd::is](https://gammasoft71.github.io/xtd/reference_guides/latest/group__xtd__core.html#ga0a8b1861b61e1d6371264460d8ee4bc6) easily determines the type of the stored value.
32+
* The operator [xtd::as](https://gammasoft71.github.io/xtd/reference_guides/latest/group__xtd__core.html#ga9b52c9f607207f23a4f4d2e4bd94eb29) intuitively converts the stored value into the desired type.
33+
* No macros or hidden tricks — only modern C++.
34+
35+
## Custom types
36+
37+
Yes of course you can store your own type in [xtd::collections::array_list](https://gammasoft71.github.io/xtd/reference_guides/latest/group__collections.html#ga83a0a06ecb97330626993339079ebbf6).
38+
39+
```cpp
40+
#include <xtd/xtd>
41+
42+
struct foo {
43+
int value;
44+
45+
bool operator ==(const foo& f) const noexcept {return value == f.value;}
46+
bool operator <(const foo& f) const noexcept {return value < f.value;}
47+
bool operator >(const foo& f) const noexcept {return value > f.value;}
48+
};
49+
50+
struct bar : public icomparable<bar>, public iequatable<bar> {
51+
bar() = default;
52+
bar(int value) : value {value} {}
53+
54+
int32 compare_to(const bar& obj) const noexcept override {return value < obj.value ? -1 : value > obj.value ? 1 : 0;}
55+
bool equals(const bar& obj) const noexcept override {return value == obj.value;}
56+
57+
int value;
58+
59+
};
60+
61+
auto main() -> int {
62+
for (auto item : collections::array_list {"42", 42, 42_s, .42, foo {42}, bar {42}}) {
63+
console::write("{,-8} --> ", item);
64+
if (is<string>(item)) console::write_line("{}", as<string>(item).quoted());
65+
else if (is<int>(item)) console::write_line("0x{:X4}", as<int>(item));
66+
else if (is<time_span>(item)) console::write_line("{} seconds", as<time_span>(item).seconds());
67+
else if (is<foo>(item)) console::write_line("f{}", as<foo>(item).value);
68+
else if (is<bar>(item)) console::write_line("b{}", as<bar>(item).value);
69+
else console::write_line("[{}]", item);
70+
}
71+
}
72+
```
73+
74+
```cpp
75+
// This code can produce the following output:
76+
//
77+
// 42 --> "42"
78+
// 42 --> 0x002A
79+
// 00:00:42 --> 42 seconds
80+
// 0.42 --> [0.42]
81+
// foo --> f42
82+
// bar --> b42
83+
```
84+
85+
* The only constraint is to have to declare at least the following three operators: `==`, `<` and `>` as in the example with the `foo` structure.
86+
* Or to inherit the interfaces [xtd::icomparable](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1icomparable.html) and [xtd::iequatable](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1iequatable.html) as in the example with the structure `bar`.
87+
88+
## Conclusion
89+
90+
* It has never been easier to store and manipulate a heterogeneous set of elements in a collection than with xtd.
91+
* In modern C++ we could use a [std::vector<std::any>](https://en.cppreference.com/w/cpp/container/vector.html) but not as easy and as advanced as [xtd::collections::array_list](https://gammasoft71.github.io/xtd/reference_guides/latest/group__collections.html#ga83a0a06ecb97330626993339079ebbf6) does not allow it.
92+
* [std::variant](https://en.cppreference.com/w/cpp/utility/variant.html) would also be a possibility but it forces you to know the types in advance and with the disadvantage also of being much more verbose.
93+
* Other libraries / frameworks such as Boost and Qt also allow you to store a set of varied types but not with the simplicity and elegance of [xtd::collections:array_list](https://gammasoft71.github.io/xtd/reference_guides/latest/group__collections.html#ga83a0a06ecb97330626993339079ebbf6).
94+
95+
> **Did you know?**
96+
>
97+
> There is also a container [xtd::collections::hash_table](https://gammasoft71.github.io/xtd/reference_guides/latest/group__collections.html#ga5a1cbe0162263e2c6626f996ca032078) which allows as [xtd::collections::array_list](https://gammasoft71.github.io/xtd/reference_guides/latest/group__collections.html#ga83a0a06ecb97330626993339079ebbf6) to manage a collection of heterogeneous types but sorted on a key that can itself be of different types.
98+
99+
## To go further
100+
101+
* [xtd::collections::array_list](https://gammasoft71.github.io/xtd/reference_guides/latest/group__collections.html#ga83a0a06ecb97330626993339079ebbf6) uses a [xtd::any_object](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1any__object.html) to store the different types.
102+
* [xtd::any_object](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1any__object.html) est utilisé par différents composants de xtd. Comme par example :
103+
* [xtd::threading::parameterized_thread_start](https://gammasoft71.github.io/xtd/reference_guides/latest/group__delegates.html#ga1961ded285db6ac6d364694eda5379a3) using by [xtd::threading::thread](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1threading_1_1thread.html) to start a thread with parameter.
104+
* [Xtd::forms::control::tag](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1forms_1_1control.html#a688b27af8a0fa5572022c422bcb3eba1) to store a user object, like all other components using the tag.
105+
106+
## See also
107+
108+
* [Tips & Tricks](/docs/documentation/tips_and_tricks)
109+
* [Documentation](/docs/documentation)

0 commit comments

Comments
 (0)