File: README.md

package info (click to toggle)
iceoryx 2.0.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 11,260 kB
  • sloc: cpp: 94,127; ansic: 1,443; sh: 1,436; python: 1,377; xml: 80; makefile: 61
file content (213 lines) | stat: -rw-r--r-- 8,844 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# complexdata

## Introduction

To implement zero-copy data transfer we use a shared memory approach. This requires that every data structure needs to be entirely
contained in the shared memory and must not internally use pointers or references. The complete list of restrictions can be found
[here](https://iceoryx.io/v2.0.0/getting-started/overview/#restrictions). Therefore, most of the STL types cannot be used, but we
reimplemented some [constructs](https://github.com/eclipse-iceoryx/iceoryx/tree/v2.0.0/iceoryx_hoofs#cxx). This example shows how
to send/receive a iox::cxx::vector and how to send/receive a complex data structure containing some of our STL container surrogates.

## Expected Output

[![asciicast](https://asciinema.org/a/410662.svg)](https://asciinema.org/a/410662)

## Code Walkthrough

The following examples demonstrate how to send/receive the STL containers that were reimplemented in iceoryx so that they meet
our requirements.

### Publisher application sending a `iox::cxx::vector`

In this example we want our publisher to send a vector containing double. Since we cannot use dynamic memory, we use the
`iox::cxx::vector` with a capacity of 5.

<!--[geoffrey][iceoryx_examples/complexdata/iox_publisher_vector.cpp][create publisher]-->
```cpp
iox::popo::Publisher<iox::cxx::vector<double, 5>> publisher({"Radar", "FrontRight", "VectorData"});
```

We use a while-loop similar to the one described in the
[icedelivery example](https://github.com/eclipse-iceoryx/iceoryx/tree/v2.0.0/iceoryx_examples/icedelivery) to send the
vector to the subscriber. After successfully loaning memory we append elements to the vector until it's full.

<!--[geoffrey][iceoryx_examples/complexdata/iox_publisher_vector.cpp][vector emplace_back]-->
```cpp
for (uint64_t i = 0U; i < sample->capacity(); ++i)
{
    // we can omit the check of the return value since the loop doesn't exceed the capacity of the
    // vector
    sample->emplace_back(static_cast<double>(ct + i));
}
```

The only difference here to the `std::vector` is that `emplace_back` returns a bool - true if the appending was successful,
false otherwise. `emplace_back` fails when the vector is already full. In our case, we can omit the check of the return value
since the for-loop doesn't exceed the capacity of the vector.

### Subscriber application receiving a `iox::cxx::vector`

Our subscriber application iterates over the received vector to print its entries to the console. Note that the `separator` is only
used for a easy to read output.

<!--[geoffrey][iceoryx_examples/complexdata/iox_subscriber_vector.cpp][vector output]-->
```cpp
for (const auto& entry : *sample)
{
    s << separator << entry;
    separator = ", ";
}
```

### Publisher application sending a complex data structure

In this example our publisher will send a more complex data structure. It contains some of the STL containers that are reimplemented
in iceoryx. A list of all reimplemented containers can be found
[here](https://github.com/eclipse-iceoryx/iceoryx/tree/v2.0.0/iceoryx_hoofs#cxx).

<!--[geoffrey][iceoryx_examples/complexdata/topic_data.hpp][complexdata type]-->
```cpp
struct ComplexDataType
{
    forward_list<string<10>, 5> stringForwardList;
    list<uint64_t, 10> integerList;
    list<optional<int32_t>, 15> optionalList;
    stack<float, 5> floatStack;
    string<20> someString;
    vector<double, 5> doubleVector;
    vector<variant<string<10>, double>, 10> variantVector;
};
```

Contrary to the STL containers, the iceoryx containers have a static size, i.e. you have to provide the capacity (= max. size).

We use again a while-loop to loan memory, add data to our containers and send it to the subscriber. Since we must not throw exceptions
all used insertion methods return a bool that indicates whether the insertion was successful. It will fail when a container is already
full. To handle the return value we introduce a helper function.

<!--[geoffrey][iceoryx_examples/complexdata/iox_publisher_complexdata.cpp][handle return val]-->
```cpp
void handleInsertionReturnVal(const bool success)
{
    if (!success)
    {
        std::cerr << "Failed to insert element." << std::endl;
        std::exit(EXIT_FAILURE);
    }
}
```

Now let's add some data to our containers. For the lists we use the `push_front` methods which can be used similar to the
corresponding STL methods.

<!--[geoffrey][iceoryx_examples/complexdata/iox_publisher_complexdata.cpp][fill lists]-->
```cpp
// forward_list<string<10>, 5>
handleInsertionReturnVal(sample->stringForwardList.push_front("world"));
handleInsertionReturnVal(sample->stringForwardList.push_front("hello"));
// list<uint64_t, 10>;
handleInsertionReturnVal(sample->integerList.push_front(ct));
handleInsertionReturnVal(sample->integerList.push_front(ct * 2));
handleInsertionReturnVal(sample->integerList.push_front(ct + 4));
// list<optional<int32_t>, 15>
handleInsertionReturnVal(sample->optionalList.push_front(42));
handleInsertionReturnVal(sample->optionalList.push_front(nullopt));
```

!!! note
    If you're not familiar with `optional`, please have a look at
    [How optional and error values are returned in iceoryx](https://github.com/eclipse-iceoryx/iceoryx/blob/v2.0.0/doc/website/concepts/how-optional-and-error-values-are-returned-in-iceoryx.md/#optional).

Now we fill the stack

<!--[geoffrey][iceoryx_examples/complexdata/iox_publisher_complexdata.cpp][fill stack]-->
```cpp
for (uint64_t i = 0U; i < sample->floatStack.capacity(); ++i)
{
    handleInsertionReturnVal(sample->floatStack.push(static_cast<float>(ct * i)));
}
```

and assign a greeting to the string.

<!--[geoffrey][iceoryx_examples/complexdata/iox_publisher_complexdata.cpp][assign string]-->
```cpp
sample->someString = "hello iceoryx";
```

For the vectors we use the `emplace_back` method, which can be used similar to the corresponding `std::vector` method.

<!--[geoffrey][iceoryx_examples/complexdata/iox_publisher_complexdata.cpp][fill vectors]-->
```cpp
for (uint64_t i = 0U; i < sample->doubleVector.capacity(); ++i)
{
    handleInsertionReturnVal(sample->doubleVector.emplace_back(static_cast<double>(ct + i)));
}
// vector<variant<string<10>, double>, 10>;
handleInsertionReturnVal(sample->variantVector.emplace_back(in_place_index<0>(), "seven"));
handleInsertionReturnVal(sample->variantVector.emplace_back(in_place_index<1>(), 8.0));
handleInsertionReturnVal(sample->variantVector.emplace_back(in_place_index<0>(), "nine"));
```

With `in_place_index` the passed object is constructed in-place at the given index.

### Subscriber application receiving a complex data structure

The subscriber application just prints the received data to the console. For the `optionalList` we have to check whether the
`optional` contains a value. As in the first example, the `separator` is used for a clear output.

<!--[geoffrey][iceoryx_examples/complexdata/iox_subscriber_complexdata.cpp][read optional list]-->
```cpp
for (const auto& entry : sample->optionalList)
{
    (entry.has_value()) ? s << separator << entry.value() : s << separator << "optional is empty";
    separator = ", ";
}
```

To print the elements of the `floatStack`, we pop elements until the stack is empty.

<!--[geoffrey][iceoryx_examples/complexdata/iox_subscriber_complexdata.cpp][read stack]-->
```cpp
auto stackCopy = sample->floatStack;
while (stackCopy.size() > 0U)
{
    auto result = stackCopy.pop();
    s << separator << result.value();
    separator = ", ";
}
```

Please note that `pop` returns a `iox::cxx::optional` which contains the last pushed element or a `nullopt` if the stack is
empty. Here, we don't have to check whether the `optional` contains a value since the loop ensures that we only pop elements
when the stack contains some.

To print the elements of the `variantVector` we iterate over the vector entries and access the alternative that is held by the
variant via its index. We use the not STL compliant `get_at_index` method which returns a pointer to the type stored at the
index. If the variant does not contain any type, `index()` will return an `INVALID_VARIANT_INDEX`.

<!--[geoffrey][iceoryx_examples/complexdata/iox_subscriber_complexdata.cpp][read variant vector]-->
```cpp
for (const auto& i : sample->variantVector)
{
    switch (i.index())
    {
    case 0:
        s << separator << *i.template get_at_index<0>();
        break;
    case 1:
        s << separator << *i.template get_at_index<1>();
        break;
    case INVALID_VARIANT_INDEX:
        s << separator << "variant does not contain a type";
        break;
    default:
        s << separator << "this is a new type";
    }
    separator = ", ";
}
```

<center>
[Check out complexdata on GitHub :fontawesome-brands-github:](https://github.com/eclipse-iceoryx/iceoryx/tree/v2.0.0/iceoryx_examples/complexdata){ .md-button }
</center>