1 // Copyright (C) 2021 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <ditto/shared_variables.h>
16
17 #include <ditto/logger.h>
18
19 namespace dittosuite {
20
21 // Matches variable_name to the integer key value.
22 //
23 // If variable_name already exists in the map for the current thread or parent threads,
24 // then the key is returned.
25 //
26 // If variable_name does not exist in that map, size of the vector of shared_variables
27 // is increased by one and the new key (index of the last element in the resized vector)
28 // is saved together with variable_name in the map for the current thread and returned.
GetKey(const std::list<int> & thread_ids,const std::string & variable_name)29 int SharedVariables::GetKey(const std::list<int>& thread_ids, const std::string& variable_name) {
30 // If the key exists in the current or parent threads, return it
31 for (auto it = thread_ids.rbegin(); it != thread_ids.rend(); ++it) {
32 if (keys_.find(*it) == keys_.end() || keys_[*it].find(variable_name) == keys_[*it].end()) {
33 continue;
34 }
35 return SharedVariables::keys_[*it][variable_name];
36 }
37
38 // If the key does not exist, create it for the current thread
39 std::size_t key = variables_.size();
40 keys_[thread_ids.back()].insert({variable_name, key});
41 variables_.resize(variables_.size() + 1);
42 return key;
43 }
44
Get(int key)45 SharedVariables::Variant SharedVariables::Get(int key) {
46 if (key < 0 || static_cast<unsigned int>(key) >= variables_.size()) {
47 LOGF("Shared variable with the provided key does not exist");
48 }
49 return variables_[key];
50 }
51
Get(const std::list<int> & thread_ids,const std::string & variable_name)52 SharedVariables::Variant SharedVariables::Get(const std::list<int>& thread_ids,
53 const std::string& variable_name) {
54 return Get(GetKey(thread_ids, variable_name));
55 }
56
Set(int key,const SharedVariables::Variant & value)57 void SharedVariables::Set(int key, const SharedVariables::Variant& value) {
58 if (key < 0 || static_cast<unsigned int>(key) >= variables_.size()) {
59 LOGF("Shared variable with the provided key does not exist");
60 }
61 variables_[key] = value;
62 }
63
Set(const std::list<int> & thread_ids,const std::string & variable_name,const Variant & value)64 void SharedVariables::Set(const std::list<int>& thread_ids, const std::string& variable_name,
65 const Variant& value) {
66 Set(GetKey(thread_ids, variable_name), value);
67 }
68
ClearKeys()69 void SharedVariables::ClearKeys() {
70 keys_.clear();
71 }
72
73 std::vector<SharedVariables::Variant> SharedVariables::variables_;
74 std::unordered_map<int, std::unordered_map<std::string, int>> SharedVariables::keys_;
75
76 } // namespace dittosuite
77