Line data Source code
1 : /********************************************************************************
2 : * Copyright (c) 2026 Accenture
3 : *
4 : * This program and the accompanying materials are made available under the
5 : * terms of the Apache License Version 2.0 which is available at
6 : * https://www.apache.org/licenses/LICENSE-2.0
7 : *
8 : * SPDX-License-Identifier: Apache-2.0
9 : ********************************************************************************/
10 :
11 : #pragma once
12 :
13 : #include "someip/PrimitiveTypes.h"
14 : #include "someip/SomeIpParser.h"
15 : #include "someip/SomeIpSerializer.h"
16 :
17 : #include <etl/array.h>
18 :
19 : namespace someip
20 : {
21 : namespace internal
22 : {
23 : /// \cond INTERNAL
24 : template<class T, size_t S>
25 : struct GetSizeHelper<::etl::array<T, S>>
26 : {
27 8 : static uint32_t getSize(::etl::array<T, S> const& t, uint32_t const lengthSize)
28 : {
29 8 : uint32_t size = 0U;
30 :
31 78 : for (auto const entry : t)
32 : {
33 70 : size += GetSizeHelper<T>::getSize(entry, lengthSize);
34 : }
35 :
36 8 : return size;
37 : }
38 :
39 : static uint32_t
40 1 : getSize(::etl::array<T, S> const& t, uint32_t const lengthSize, Encoding const encoding)
41 : {
42 1 : uint32_t size = lengthSize;
43 :
44 6 : for (auto const entry : t)
45 : {
46 5 : size += GetSizeHelper<T>::getSize(entry, lengthSize, encoding);
47 : }
48 :
49 1 : return size;
50 : }
51 : };
52 :
53 : /// \endcond
54 :
55 : } // namespace internal
56 : } // namespace someip
57 :
58 : namespace someip
59 : {
60 : /**
61 : * ArrayParser is a helper class used for reading a SOME/IP fixed-length array from
62 : * an array of bytes. This class is used by the SOME/IP code generator.
63 : *
64 : * \tparam T The type of the underlying fixed-length array.
65 : * \tparam SIZE The length of the array.
66 : */
67 : template<class T, size_t SIZE>
68 : class ArrayParser
69 : {
70 : public:
71 : ArrayParser() = delete;
72 :
73 : /**
74 : * Create a ArrayParser that will fill the specified 'obj' with objects of type 'T'.
75 : *
76 : * \param obj The estd array to fill.
77 : */
78 : static void parse(::etl::array<T, SIZE>& obj, SomeIpParser& parser, bool isBigEndian);
79 : };
80 :
81 : /**
82 : * ArraySerializer is a helper class used for writing a SOME/IP fixed-length array to
83 : * an array of bytes.
84 : *
85 : * \tparam T The type of the underlying fixed-length array.
86 : * \tparam SIZE The length of the fixed-length array.
87 : */
88 : template<class T, size_t SIZE>
89 : class ArraySerializer
90 : {
91 : public:
92 : ArraySerializer() = delete;
93 :
94 : /**
95 : * Create a ArraySerializer to serialize the specified array into an array of bytes.
96 : *
97 : * \param obj The array to serialize.
98 : */
99 : static void
100 : serialize(::etl::array<T, SIZE> const& obj, SomeIpSerializer& serializer, bool isBigEndian);
101 : };
102 :
103 : /*
104 : * inline implementation
105 : */
106 :
107 : /*
108 : * ArraySerializer
109 : */
110 : // static
111 : template<class T, size_t SIZE>
112 5 : void ArraySerializer<T, SIZE>::serialize(
113 : ::etl::array<T, SIZE> const& obj, SomeIpSerializer& serializer, bool const isBigEndian)
114 : {
115 5 : if (!serializer.isGood())
116 : {
117 1 : return;
118 : }
119 :
120 4 : if (isBigEndian == true)
121 : {
122 3 : serializer.bigEndian();
123 : }
124 : else
125 : {
126 1 : serializer.littleEndian();
127 : }
128 :
129 : // write out each element
130 28 : for (size_t i = 0U; i < SIZE; ++i)
131 : {
132 24 : serializer << obj[i];
133 : }
134 : }
135 :
136 : /*
137 : * ArrayParser
138 : */
139 : // static
140 : template<class T, size_t SIZE>
141 5 : void ArrayParser<T, SIZE>::parse(
142 : ::etl::array<T, SIZE>& obj, SomeIpParser& parser, bool const isBigEndian)
143 : {
144 5 : if (!parser.isGood())
145 : {
146 1 : return;
147 : }
148 :
149 4 : if (isBigEndian == true)
150 : {
151 3 : parser.bigEndian();
152 : }
153 : else
154 : {
155 1 : parser.littleEndian();
156 : }
157 :
158 23 : for (size_t i = 0U; i < SIZE; ++i)
159 : {
160 20 : if (!parser.isGood())
161 : {
162 : // fatal. failed to read in all values!
163 1 : parser.setFailure();
164 1 : return;
165 : }
166 :
167 19 : parser >> obj[i];
168 : }
169 : }
170 :
171 : } // namespace someip
|