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 AccountSA {
Put(const std::string & key,int32_t value)20 void GenericValues::Put(const std::string& key, int32_t value)
21 {
22 map_.insert(std::make_pair(key, VariantValue(value)));
23 }
24
Put(const std::string & key,int64_t value)25 void GenericValues::Put(const std::string& key, int64_t value)
26 {
27 map_.insert(std::make_pair(key, VariantValue(value)));
28 }
29
Put(const std::string & key,const std::string & value)30 void GenericValues::Put(const std::string& key, const std::string& value)
31 {
32 map_.insert(std::make_pair(key, VariantValue(value)));
33 }
34
Put(const std::string & key,const VariantValue & value)35 void GenericValues::Put(const std::string& key, const VariantValue& value)
36 {
37 map_.insert(std::make_pair(key, value));
38 }
39
Get(const std::string & key) const40 VariantValue GenericValues::Get(const std::string& key) const
41 {
42 auto iter = map_.find(key);
43 if (iter == map_.end()) {
44 return VariantValue();
45 }
46 return iter->second;
47 }
48
GetInt(const std::string & key) const49 int32_t GenericValues::GetInt(const std::string& key) const
50 {
51 auto it = map_.find(key);
52 if (it == map_.end()) {
53 return VariantValue::DEFAULT_VALUE;
54 }
55 return it->second.GetInt();
56 }
57
GetInt64(const std::string & key) const58 int64_t GenericValues::GetInt64(const std::string& key) const
59 {
60 auto it = map_.find(key);
61 if (it == map_.end()) {
62 return VariantValue::DEFAULT_VALUE;
63 }
64 return it->second.GetInt64();
65 }
66
GetString(const std::string & key) const67 std::string GenericValues::GetString(const std::string& key) const
68 {
69 auto it = map_.find(key);
70 if (it == map_.end()) {
71 return std::string();
72 }
73 return it->second.GetString();
74 }
75
GetAllKeys() const76 std::vector<std::string> GenericValues::GetAllKeys() const
77 {
78 std::vector<std::string> keys;
79 for (auto it = map_.begin(); it != map_.end(); ++it) {
80 keys.emplace_back(it->first);
81 }
82 return keys;
83 }
84
Remove(const std::string & key)85 void GenericValues::Remove(const std::string& key)
86 {
87 if (map_.find(key) != map_.end()) {
88 map_.erase(key);
89 }
90 }
91 } // namespace AccountSA
92 } // namespace OHOS
93