blob: aae19446e88e9fb9b6bd6ecfa457a8ca45e758f6 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Liam Barry677d43f2023-08-03 18:21:58 +01002 * SPDX-FileCopyrightText: Copyright 2021, 2023 Arm Limited and/or its affiliates
3 * <open-source-office@arm.com> SPDX-License-Identifier: Apache-2.0
alexander3c798932021-03-26 21:42:19 +00004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17#ifndef APP_CTX_HPP
18#define APP_CTX_HPP
19
alexander3c798932021-03-26 21:42:19 +000020#include <map>
Liam Barry677d43f2023-08-03 18:21:58 +010021#include <memory>
22#include <string>
alexander3c798932021-03-26 21:42:19 +000023namespace arm {
24namespace app {
25
26 class IAttribute
27 {
28 public:
29 virtual ~IAttribute() = default;
30 };
31
32 template<typename T>
33 class Attribute : public IAttribute
34 {
35 public:
36 ~Attribute() override = default;
37
Isabella Gottardi56ee6202021-05-12 08:27:15 +010038 explicit Attribute(const T value): m_value(value){}
alexander3c798932021-03-26 21:42:19 +000039
40 T Get()
41 {
Isabella Gottardi56ee6202021-05-12 08:27:15 +010042 return m_value;
alexander3c798932021-03-26 21:42:19 +000043 }
44 private:
Isabella Gottardi56ee6202021-05-12 08:27:15 +010045 T m_value;
alexander3c798932021-03-26 21:42:19 +000046 };
47
48 /* Application context class */
49 class ApplicationContext {
50 public:
51
52 /**
53 * @brief Saves given value as a named attribute in the context.
54 * @tparam T value type.
55 * @param[in] name Context attribute name.
56 * @param[in] object Value to save in the context.
57 */
58 template<typename T>
59 void Set(const std::string &name, T object)
60 {
Liam Barry677d43f2023-08-03 18:21:58 +010061 /* Attribute exists; reset the smart pointer */
62 if (this->Has(name)) {
63 this->m_attributes.at(name).reset();
Cisco Cervellerad996cc52021-09-15 09:35:24 +010064 }
Liam Barry677d43f2023-08-03 18:21:58 +010065 this->m_attributes[name] = std::make_unique<Attribute<T>>(object);
alexander3c798932021-03-26 21:42:19 +000066 }
67
68 /**
69 * @brief Gets the saved attribute from the context by the given name.
70 * @tparam T value type.
71 * @param[in] name Context attribute name.
72 * @return Value saved in the context.
73 */
Liam Barry677d43f2023-08-03 18:21:58 +010074
75 template <typename T>
76 T Get(const std::string& name)
alexander3c798932021-03-26 21:42:19 +000077 {
Liam Barry677d43f2023-08-03 18:21:58 +010078 //TODO Add logic to handle access of non-existent attribute
79 auto attributeValue = (Attribute<T>*)m_attributes.at(name).get();
80 return attributeValue->Get();
alexander3c798932021-03-26 21:42:19 +000081 }
82
83 /**
84 * @brief Checks if an attribute for a given name exists in the context.
85 * @param[in] name Attribute name.
86 * @return true if attribute exists, false otherwise
87 */
88 bool Has(const std::string& name)
89 {
Isabella Gottardi56ee6202021-05-12 08:27:15 +010090 return m_attributes.find(name) != m_attributes.end();
alexander3c798932021-03-26 21:42:19 +000091 }
92
93 ApplicationContext() = default;
94
Liam Barry677d43f2023-08-03 18:21:58 +010095 ~ApplicationContext() = default;
alexander3c798932021-03-26 21:42:19 +000096
alexander3c798932021-03-26 21:42:19 +000097 private:
Liam Barry677d43f2023-08-03 18:21:58 +010098 std::map<std::string, std::unique_ptr<IAttribute>> m_attributes;
alexander3c798932021-03-26 21:42:19 +000099 };
100
101} /* namespace app */
102} /* namespace arm */
103
104#endif /* APP_CTX_HPP */