score/mw/com/impl/instance_identifier.h
Line | Count | Source |
1 | | /******************************************************************************** |
2 | | * Copyright (c) 2025 Contributors to the Eclipse Foundation |
3 | | * |
4 | | * See the NOTICE file(s) distributed with this work for additional |
5 | | * information regarding copyright ownership. |
6 | | * |
7 | | * This program and the accompanying materials are made available under the |
8 | | * terms of the Apache License Version 2.0 which is available at |
9 | | * https://www.apache.org/licenses/LICENSE-2.0 |
10 | | * |
11 | | * SPDX-License-Identifier: Apache-2.0 |
12 | | ********************************************************************************/ |
13 | | #ifndef SCORE_MW_COM_IMPL_INSTANCEIDENTIFIER_H |
14 | | #define SCORE_MW_COM_IMPL_INSTANCEIDENTIFIER_H |
15 | | |
16 | | #include "score/mw/com/impl/com_error.h" |
17 | | #include "score/mw/com/impl/configuration/configuration.h" |
18 | | #include "score/mw/com/impl/configuration/service_identifier_type.h" |
19 | | #include "score/mw/com/impl/configuration/service_instance_deployment.h" |
20 | | #include "score/mw/com/impl/configuration/service_instance_id.h" |
21 | | #include "score/mw/com/impl/configuration/service_type_deployment.h" |
22 | | #include "score/mw/com/impl/configuration/service_version_type.h" |
23 | | |
24 | | #include "score/result/result.h" |
25 | | |
26 | | #include <functional> |
27 | | #include <string> |
28 | | #include <string_view> |
29 | | #include <utility> |
30 | | |
31 | | namespace score::mw::com::impl |
32 | | { |
33 | | |
34 | | /** |
35 | | * \api |
36 | | * \brief Represents a specific instance of a given service |
37 | | * \requirement SWS_CM_00302 |
38 | | */ |
39 | | class InstanceIdentifier final |
40 | | { |
41 | | public: |
42 | | /** |
43 | | * \api |
44 | | * \brief Exception-less constructor to create InstanceIdentifier from a serialized InstanceIdentifier created with |
45 | | * InstanceIdentifier::ToString() |
46 | | * \param serialized_format The serialized format to create the InstanceIdentifier from |
47 | | * \return The created InstanceIdentifier or an error |
48 | | * |
49 | | * \pre The score::mw::com::impl::Runtime singleton must have been created (then the configuration is "locked") |
50 | | * before calling this API, i.e. Only then has a valid Configuration pointer been registered (by the Runtime |
51 | | * constructor) into which the reconstructed deployments are added. |
52 | | * Currently, there is no public API whose sole purpose is to initialize/create the Runtime singleton; in practice |
53 | | * any prior public score::mw::com call that reaches Runtime::getInstance() |
54 | | * (e.g. creating a Skeleton/Proxy, FindService, or - as a last resort - score::mw::com::runtime::ResolveInstanceIDs |
55 | | * called with a dummy InstanceSpecifier) triggers this. |
56 | | * If the configuration has not been set, kInvalidConfiguration is returned. |
57 | | */ |
58 | | static score::Result<InstanceIdentifier> Create(std::string&& serialized_format); |
59 | | |
60 | | InstanceIdentifier() = delete; |
61 | 23.6k | ~InstanceIdentifier() noexcept = default; |
62 | | |
63 | | /** |
64 | | * ctor from serialized representation. |
65 | | * |
66 | | * Constructor is required by adaptive AUTOSAR Standard. But it uses exceptions, thus we will not implement it. |
67 | | * |
68 | | * explicit InstanceIdentifier(std::string_view value); |
69 | | */ |
70 | | |
71 | | /** |
72 | | * \api |
73 | | * \brief CopyAssignment for InstanceIdentifier |
74 | | * \post *this == other |
75 | | * \param other The InstanceIdentifier *this shall be constructed from |
76 | | * \return The InstanceIdentifier that was constructed |
77 | | */ |
78 | 2 | InstanceIdentifier& operator=(const InstanceIdentifier& other) = default; |
79 | | /** |
80 | | * \api |
81 | | * \brief Copy constructor for InstanceIdentifier |
82 | | * |
83 | | * \param other The InstanceIdentifier to copy from |
84 | | * \return The InstanceIdentifier that was constructed |
85 | | */ |
86 | 9.00k | InstanceIdentifier(const InstanceIdentifier&) = default; |
87 | | /** |
88 | | * \api |
89 | | * \brief Move constructor for InstanceIdentifier |
90 | | * |
91 | | * \post *this == other |
92 | | * \param other The InstanceIdentifier to move from |
93 | | * \return The moved InstanceIdentifier |
94 | | */ |
95 | 13.0k | InstanceIdentifier(InstanceIdentifier&&) noexcept = default; |
96 | | /** |
97 | | * \api |
98 | | * \brief MoveAssignment for InstanceIdentifier |
99 | | * |
100 | | * \post *this == other |
101 | | * \param other The InstanceIdentifier *this shall be constructed from |
102 | | * \return The InstanceIdentifier that was constructed |
103 | | */ |
104 | 12 | InstanceIdentifier& operator=(InstanceIdentifier&& other) noexcept = default; |
105 | | |
106 | | /** |
107 | | * \api |
108 | | * \brief Returns the serialized form of the unknown internals of this class as a meaningful string |
109 | | * |
110 | | * \return A non-owning string representation of the internals of this class |
111 | | */ |
112 | | std::string_view ToString() const noexcept; |
113 | | |
114 | | /** |
115 | | * \api |
116 | | * \brief Compares two instances for equality |
117 | | * |
118 | | * \param lhs The first instance to check for equality |
119 | | * \param rhs The second instance to check for equality |
120 | | * \return true if other and *this equal, false otherwise |
121 | | */ |
122 | | friend bool operator==(const InstanceIdentifier& lhs, const InstanceIdentifier& rhs); |
123 | | |
124 | | /** |
125 | | * \api |
126 | | * \brief LessThanComparable operator |
127 | | * |
128 | | * \param lhs The first InstanceIdentifier instance to compare |
129 | | * \param rhs The second InstanceIdentifier instance to compare |
130 | | * \return true if *this is less then other, false otherwise |
131 | | */ |
132 | | friend bool operator<(const InstanceIdentifier& lhs, const InstanceIdentifier& rhs); |
133 | | |
134 | | private: |
135 | | const ServiceInstanceDeployment* instance_deployment_; |
136 | | const ServiceTypeDeployment* type_deployment_; |
137 | | |
138 | | /** |
139 | | * @brief Internal constructor to construct an InstanceIdentifier from a json-serialized InstanceIdentifier |
140 | | * |
141 | | * @param json_object Used to construct the InstanceIdentifier (no copies of json_object are made internally). |
142 | | * @param serialized_string Serialized string which the json_object is derived from. Used to set serialized_string_. |
143 | | */ |
144 | | explicit InstanceIdentifier(const json::Object& json_object, std::string&& serialized_string); |
145 | | |
146 | | /** |
147 | | * @brief internal impl. specific ctor. |
148 | | * |
149 | | * @param service identification of service |
150 | | * @param version version info |
151 | | * @param deployment deployment info |
152 | | */ |
153 | | explicit InstanceIdentifier(const ServiceInstanceDeployment&, const ServiceTypeDeployment&); |
154 | | |
155 | | static void SetConfiguration(Configuration* const configuration) noexcept |
156 | 63 | { |
157 | 63 | InstanceIdentifier::configuration_ = configuration; |
158 | 63 | } |
159 | | |
160 | | json::Object Serialize() const; |
161 | | |
162 | | /** |
163 | | * @brief serialized format of this InstanceIdentifier instance |
164 | | */ |
165 | | std::string serialized_string_; |
166 | | |
167 | | /** |
168 | | * @brief serialization format version. |
169 | | * |
170 | | * Whenever the state/content of this class changes in a way, which has effect on serialization, this version |
171 | | * has to be incremented! We potentially transfer instances of this class in a serialized form between processes |
172 | | * and need to know in the receiver process, if this serialized instance can be understood. |
173 | | */ |
174 | | constexpr static std::uint32_t serializationVersion{1U}; |
175 | | |
176 | | /** |
177 | | * \brief Global configuration object which is parsed from a json file and loaded by the runtime |
178 | | * |
179 | | * Whenever an InstanceIdentifier is created from another serialized InstanceIdentifier, the ServiceTypeDeployment / |
180 | | * ServiceInstanceDeployment held by the serialized InstanceIdentifier needs to be added to the maps within the |
181 | | * global configuration object. The newly created InstanceIdentifier will then store pointers to these structs. |
182 | | */ |
183 | | static Configuration* configuration_; |
184 | | |
185 | | // Suppress "AUTOSAR C++14 A11-3-1", The rule states: "Friend declarations shall not be used". |
186 | | // Design decision: Hide the constructor of the instance identifier. |
187 | | // This way more implementation details can be hidden from the user. |
188 | | // coverity[autosar_cpp14_a11_3_1_violation] |
189 | | friend InstanceIdentifier make_InstanceIdentifier(const ServiceInstanceDeployment& instance_deployment, |
190 | | const ServiceTypeDeployment& type_deployment); |
191 | | |
192 | | // Suppress "AUTOSAR C++14 A11-3-1", The rule states: "Friend declarations shall not be used". |
193 | | // Design decision. This class provides a view to the private members of this class. |
194 | | // coverity[autosar_cpp14_a11_3_1_violation] |
195 | | friend class InstanceIdentifierView; |
196 | | |
197 | | // Suppress "AUTOSAR C++14 A11-3-1", The rule declares: "Friend declarations shall not be used". |
198 | | // Design dessision: The "*Attorney" class is a helper, which sets the internal state of this class accessing |
199 | | // private members and used for testing purposes only. |
200 | | // coverity[autosar_cpp14_a11_3_1_violation] |
201 | | friend class InstanceIdentifierAttorney; |
202 | | |
203 | | // Suppress "AUTOSAR C++14 A11-3-1", The rule states: "Friend declarations shall not be used". |
204 | | // Design decision: Instance identifier is used by the runtime which requires access to its internals. |
205 | | // coverity[autosar_cpp14_a11_3_1_violation] |
206 | | friend class Runtime; |
207 | | }; |
208 | | |
209 | | /** |
210 | | * \brief A make_ function is introduced to hide the Constructor of InstanceIdentifier. |
211 | | * The InstanceIdentifier will be exposed to the API user and by not having a public constructor |
212 | | * we can avoid that by chance the user will construct this class. Introducing a custom make method |
213 | | * that is _not_ mentioned in the standard, will avoid this! |
214 | | * |
215 | | * \param service The service this instance is revering to |
216 | | * \param version The version of the service this instances revers to |
217 | | * \param deployment The deployment specific information for this instance |
218 | | * \return A constructed InstanceIdentifier |
219 | | */ |
220 | | inline InstanceIdentifier make_InstanceIdentifier(const ServiceInstanceDeployment& instance_deployment, |
221 | | const ServiceTypeDeployment& type_deployment) |
222 | 1.76k | { |
223 | 1.76k | return InstanceIdentifier{instance_deployment, type_deployment}; |
224 | 1.76k | } |
225 | | |
226 | | /** |
227 | | * \brief The score::mw::com::InstanceIdentifiers API is described by the ara::com standard. |
228 | | * But we also need to use it for internal purposes, why we need to access some state information |
229 | | * that is not exposed by the public API described in the adaptive AUTOSAR Standard. |
230 | | * In order to not leak implementation details, we come up with a `View` onto the InstanceIdentifier. |
231 | | * Since our view is anyhow _only_ located in the `impl` namespace, there is zero probability that |
232 | | * any well minded user would depend on it. |
233 | | */ |
234 | | class InstanceIdentifierView final |
235 | | { |
236 | | public: |
237 | | explicit InstanceIdentifierView(const InstanceIdentifier&); |
238 | | |
239 | | json::Object Serialize() const |
240 | 4 | { |
241 | 4 | return identifier_.Serialize(); |
242 | 4 | }; |
243 | | |
244 | | std::optional<ServiceInstanceId> GetServiceInstanceId() const; |
245 | | const ServiceInstanceDeployment& GetServiceInstanceDeployment() const noexcept; |
246 | | const ServiceTypeDeployment& GetServiceTypeDeployment() const; |
247 | | bool isCompatibleWith(const InstanceIdentifier&) const; |
248 | | bool isCompatibleWith(const InstanceIdentifierView&) const; |
249 | | constexpr static std::uint32_t GetSerializationVersion() |
250 | 0 | { |
251 | 0 | return InstanceIdentifier::serializationVersion; |
252 | 0 | }; |
253 | | |
254 | | private: |
255 | | const InstanceIdentifier& identifier_; |
256 | | }; |
257 | | |
258 | | } // namespace score::mw::com::impl |
259 | | |
260 | | template <> |
261 | | class std::hash<score::mw::com::impl::InstanceIdentifier> |
262 | | { |
263 | | public: |
264 | | std::size_t operator()(const score::mw::com::impl::InstanceIdentifier&) const noexcept; |
265 | | }; |
266 | | |
267 | | #endif // SCORE_MW_COM_IMPL_INSTANCEIDENTIFIER_H |