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