alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Liam Barry | 677d43f | 2023-08-03 18:21:58 +0100 | [diff] [blame] | 2 | * SPDX-FileCopyrightText: Copyright 2021, 2023 Arm Limited and/or its affiliates |
| 3 | * <open-source-office@arm.com> SPDX-License-Identifier: Apache-2.0 |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 4 | * |
| 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 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 20 | #include <map> |
Liam Barry | 677d43f | 2023-08-03 18:21:58 +0100 | [diff] [blame] | 21 | #include <memory> |
| 22 | #include <string> |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 23 | namespace arm { |
| 24 | namespace 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 Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 38 | explicit Attribute(const T value): m_value(value){} |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 39 | |
| 40 | T Get() |
| 41 | { |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 42 | return m_value; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 43 | } |
| 44 | private: |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 45 | T m_value; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 46 | }; |
| 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 Barry | 677d43f | 2023-08-03 18:21:58 +0100 | [diff] [blame] | 61 | /* Attribute exists; reset the smart pointer */ |
| 62 | if (this->Has(name)) { |
| 63 | this->m_attributes.at(name).reset(); |
Cisco Cervellera | d996cc5 | 2021-09-15 09:35:24 +0100 | [diff] [blame] | 64 | } |
Liam Barry | 677d43f | 2023-08-03 18:21:58 +0100 | [diff] [blame] | 65 | this->m_attributes[name] = std::make_unique<Attribute<T>>(object); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 66 | } |
| 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 Barry | 677d43f | 2023-08-03 18:21:58 +0100 | [diff] [blame] | 74 | |
| 75 | template <typename T> |
| 76 | T Get(const std::string& name) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 77 | { |
Liam Barry | 677d43f | 2023-08-03 18:21:58 +0100 | [diff] [blame] | 78 | //TODO Add logic to handle access of non-existent attribute |
| 79 | auto attributeValue = (Attribute<T>*)m_attributes.at(name).get(); |
| 80 | return attributeValue->Get(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 81 | } |
| 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 Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 90 | return m_attributes.find(name) != m_attributes.end(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | ApplicationContext() = default; |
| 94 | |
Liam Barry | 677d43f | 2023-08-03 18:21:58 +0100 | [diff] [blame] | 95 | ~ApplicationContext() = default; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 96 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 97 | private: |
Liam Barry | 677d43f | 2023-08-03 18:21:58 +0100 | [diff] [blame] | 98 | std::map<std::string, std::unique_ptr<IAttribute>> m_attributes; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | } /* namespace app */ |
| 102 | } /* namespace arm */ |
| 103 | |
| 104 | #endif /* APP_CTX_HPP */ |