xref: /aosp_15_r20/test/dittosuite/src/shared_variables.cpp (revision 6fa2df46f119dce7527f5beb2814eca0e6f886ac)
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 
Exists(const std::list<int> & thread_ids,const std::string & variable_name)21 bool SharedVariables::Exists(const std::list<int>& thread_ids, const std::string& variable_name) {
22   for (auto it = thread_ids.rbegin(); it != thread_ids.rend(); ++it) {
23     if (keys_.find(*it) == keys_.end() || keys_[*it].find(variable_name) == keys_[*it].end()) {
24       continue;
25     }
26     return true;
27   }
28   return false;
29 }
30 
31 // Matches variable_name to the integer key value.
32 //
33 // If variable_name already exists in the map for the current thread or parent threads,
34 // then the key is returned.
35 //
36 // If variable_name does not exist in that map, size of the vector of shared_variables
37 // is increased by one and the new key (index of the last element in the resized vector)
38 // 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)39 int SharedVariables::GetKey(const std::list<int>& thread_ids, const std::string& variable_name) {
40   // If the key exists in the current or parent threads, return it
41   for (auto it = thread_ids.rbegin(); it != thread_ids.rend(); ++it) {
42     if (keys_.find(*it) == keys_.end() || keys_[*it].find(variable_name) == keys_[*it].end()) {
43       continue;
44     }
45     return SharedVariables::keys_[*it][variable_name];
46   }
47 
48   // If the key does not exist, create it for the current thread
49   std::size_t key = variables_.size();
50   keys_[thread_ids.back()].insert({variable_name, key});
51   variables_.resize(variables_.size() + 1);
52   return key;
53 }
54 
GetPointer(int key)55 SharedVariables::Variant* SharedVariables::GetPointer(int key) {
56   if (key < 0 || static_cast<unsigned int>(key) >= variables_.size()) {
57     LOGF("Shared variable with the provided key does not exist");
58   }
59   return &variables_[key];
60 }
61 
Get(int key)62 SharedVariables::Variant SharedVariables::Get(int key) {
63   if (key < 0 || static_cast<unsigned int>(key) >= variables_.size()) {
64     LOGF("Shared variable with the provided key does not exist");
65   }
66   return variables_[key];
67 }
68 
Get(const std::list<int> & thread_ids,const std::string & variable_name)69 SharedVariables::Variant SharedVariables::Get(const std::list<int>& thread_ids,
70                                               const std::string& variable_name) {
71   return Get(GetKey(thread_ids, variable_name));
72 }
73 
Set(int key,const SharedVariables::Variant & value)74 void SharedVariables::Set(int key, const SharedVariables::Variant& value) {
75   if (key < 0 || static_cast<unsigned int>(key) >= variables_.size()) {
76     LOGF("Shared variable with the provided key does not exist");
77   }
78   variables_[key] = value;
79 }
80 
Set(const std::list<int> & thread_ids,const std::string & variable_name,const Variant & value)81 void SharedVariables::Set(const std::list<int>& thread_ids, const std::string& variable_name,
82                           const Variant& value) {
83   Set(GetKey(thread_ids, variable_name), value);
84 }
85 
ClearKeys()86 void SharedVariables::ClearKeys() {
87   keys_.clear();
88 }
89 
90 std::vector<SharedVariables::Variant> SharedVariables::variables_;
91 std::unordered_map<int, std::unordered_map<std::string, int>> SharedVariables::keys_;
92 
93 }  // namespace dittosuite
94