blob: 13f26867ab764ebc301582cf074f0d03798eb347 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
2 * Copyright (c) 2021 Arm Limited. All rights reserved.
3 * SPDX-License-Identifier: Apache-2.0
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
20#include <string>
21#include <map>
22
23namespace 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 {
Cisco Cervellerad996cc52021-09-15 09:35:24 +010061 /* check if we have already the attribute allocated. */
62 if( true == this->Has(name) ){
63 //delete its value
64 delete this->m_attributes[name];
65 }
66 /* allocate new value */
Isabella Gottardi56ee6202021-05-12 08:27:15 +010067 this->m_attributes[name] = new Attribute<T>(object);
alexander3c798932021-03-26 21:42:19 +000068 }
69
70 /**
71 * @brief Gets the saved attribute from the context by the given name.
72 * @tparam T value type.
73 * @param[in] name Context attribute name.
74 * @return Value saved in the context.
75 */
76 template<typename T>
77 T Get(const std::string &name)
78 {
Isabella Gottardi56ee6202021-05-12 08:27:15 +010079 auto a = (Attribute<T>*)m_attributes[name];
alexander3c798932021-03-26 21:42:19 +000080 return a->Get();
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 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
95 ~ApplicationContext() {
Isabella Gottardi56ee6202021-05-12 08:27:15 +010096 for (auto& attribute : m_attributes)
alexander3c798932021-03-26 21:42:19 +000097 delete attribute.second;
98
Isabella Gottardi56ee6202021-05-12 08:27:15 +010099 this->m_attributes.clear();
alexander3c798932021-03-26 21:42:19 +0000100 }
101 private:
Isabella Gottardi56ee6202021-05-12 08:27:15 +0100102 std::map<std::string, IAttribute*> m_attributes;
alexander3c798932021-03-26 21:42:19 +0000103 };
104
105} /* namespace app */
106} /* namespace arm */
107
108#endif /* APP_CTX_HPP */