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/ISomeIpSerializable.h"
14 : #include "someip/SomeIpParser.h"
15 : #include "someip/SomeIpSerializer.h"
16 :
17 : #include <util/types/Enum.h>
18 :
19 : namespace someip
20 : {
21 : /**
22 : * A base class object for all generated enum types.
23 : *
24 : * \tparam Values The possible enum values for this class.
25 : * \tparam Underlying The underlying type that can store all enum elements in the Values type.
26 : */
27 : template<class Values, class Underlying>
28 : class EnumSerializable
29 : : public ::util::types::Enum<Values, Underlying>
30 : , public ISomeIpSerializable
31 : {
32 : protected:
33 : /** Helper typedef for accessing the underlying Enum types. */
34 : using base_type = ::util::types::Enum<Values, Underlying>;
35 :
36 : public:
37 : using enum_type = typename base_type::type;
38 :
39 : /** The underlying type for this enum. */
40 : using value_type = typename base_type::value_type;
41 :
42 : /** Initializes this instance to a default value */
43 : EnumSerializable();
44 :
45 : /** Initialize this instance from the underlying enum */
46 : explicit EnumSerializable(enum_type value);
47 :
48 : /** Initialize this instance from a uint8_t */
49 : explicit EnumSerializable(value_type value);
50 :
51 : /** A simple copy constructor */
52 : EnumSerializable(EnumSerializable<Values, Underlying> const& other);
53 :
54 : /** A simple assignment operator */
55 : EnumSerializable<Values, Underlying>&
56 : operator=(EnumSerializable<Values, Underlying> const& other);
57 :
58 : /** Serializes this enum to a byte array. */
59 : void serializeToArray(SomeIpSerializer& serializer) const override;
60 :
61 : /** Parses this enum from a byte array. */
62 : void parseFromArray(SomeIpParser& parser) override;
63 :
64 : /** Returns the number of bytes necessary to save this enum in a byte array. */
65 : uint32_t getSize() const override;
66 : };
67 :
68 : /*
69 : * inline implementation
70 : */
71 : template<class Values, class Underlying>
72 1 : inline EnumSerializable<Values, Underlying>::EnumSerializable() : base_type()
73 1 : {}
74 :
75 : template<class Values, class Underlying>
76 1 : inline EnumSerializable<Values, Underlying>::EnumSerializable(
77 : EnumSerializable<Values, Underlying> const& other)
78 1 : : base_type(other), ISomeIpSerializable()
79 1 : {}
80 :
81 : template<class Values, class Underlying>
82 4 : inline EnumSerializable<Values, Underlying>::EnumSerializable(enum_type const value)
83 4 : : base_type(value)
84 4 : {}
85 :
86 : template<class Values, class Underlying>
87 1 : inline EnumSerializable<Values, Underlying>::EnumSerializable(value_type const value)
88 1 : : base_type(base_type::fromUnderlying(value))
89 1 : {}
90 :
91 : template<class Values, class Underlying>
92 : inline EnumSerializable<Values, Underlying>&
93 2 : EnumSerializable<Values, Underlying>::operator=(EnumSerializable const& other)
94 : {
95 2 : base_type::operator=(other);
96 2 : return *this;
97 : }
98 :
99 : template<class Values, class Underlying>
100 : inline void
101 1 : EnumSerializable<Values, Underlying>::serializeToArray(SomeIpSerializer& serializer) const
102 : {
103 1 : serializer << base_type::_value;
104 1 : }
105 :
106 : template<class Values, class Underlying>
107 1 : inline void EnumSerializable<Values, Underlying>::parseFromArray(SomeIpParser& parser)
108 : {
109 1 : parser >> base_type::_value;
110 1 : }
111 :
112 : template<class Values, class Underlying>
113 2 : inline uint32_t EnumSerializable<Values, Underlying>::getSize() const
114 : {
115 2 : return static_cast<uint32_t>(sizeof(base_type::_value));
116 : }
117 :
118 : } // namespace someip
|