1 /*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
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
16 #include "generic_values.h"
17
18 namespace OHOS {
19 namespace Security {
20 namespace AccessToken {
Put(const std::string & key,int32_t value)21 void GenericValues::Put(const std::string& key, int32_t value)
22 {
23 map_.insert(std::make_pair(key, VariantValue(value)));
24 }
25
Put(const std::string & key,int64_t value)26 void GenericValues::Put(const std::string& key, int64_t value)
27 {
28 map_.insert(std::make_pair(key, VariantValue(value)));
29 }
30
Put(const std::string & key,const std::string & value)31 void GenericValues::Put(const std::string& key, const std::string& value)
32 {
33 map_.insert(std::make_pair(key, VariantValue(value)));
34 }
35
Put(const std::string & key,const VariantValue & value)36 void GenericValues::Put(const std::string& key, const VariantValue& value)
37 {
38 map_.insert(std::make_pair(key, value));
39 }
40
Get(const std::string & key) const41 VariantValue GenericValues::Get(const std::string& key) const
42 {
43 auto iter = map_.find(key);
44 if (iter == map_.end()) {
45 return VariantValue();
46 }
47 return iter->second;
48 }
49
GetInt(const std::string & key) const50 int32_t GenericValues::GetInt(const std::string& key) const
51 {
52 auto it = map_.find(key);
53 if (it == map_.end()) {
54 return VariantValue::DEFAULT_VALUE;
55 }
56 return it->second.GetInt();
57 }
58
GetInt64(const std::string & key) const59 int64_t GenericValues::GetInt64(const std::string& key) const
60 {
61 auto it = map_.find(key);
62 if (it == map_.end()) {
63 return VariantValue::DEFAULT_VALUE;
64 }
65 return it->second.GetInt64();
66 }
67
GetString(const std::string & key) const68 std::string GenericValues::GetString(const std::string& key) const
69 {
70 auto it = map_.find(key);
71 if (it == map_.end()) {
72 return std::string();
73 }
74 return it->second.GetString();
75 }
76
GetAllKeys() const77 std::vector<std::string> GenericValues::GetAllKeys() const
78 {
79 std::vector<std::string> keys;
80 for (auto it = map_.begin(); it != map_.end(); ++it) {
81 keys.emplace_back(it->first);
82 }
83 return keys;
84 }
85
Remove(const std::string & key)86 void GenericValues::Remove(const std::string& key)
87 {
88 if (map_.count(key) > 0) {
89 map_.erase(key);
90 }
91 }
92 } // namespace AccessToken
93 } // namespace Security
94 } // namespace OHOS
95