// // Copyright (C) 2012 The Android Open Source Project // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // #include "shill/key_value_store.h" #include #include "shill/logging.h" using std::map; using std::string; using std::vector; namespace shill { KeyValueStore::KeyValueStore() {} void KeyValueStore::Clear() { properties_.clear(); } bool KeyValueStore::IsEmpty() { return properties_.empty(); } void KeyValueStore::CopyFrom(const KeyValueStore& b) { properties_ = b.properties_; } bool KeyValueStore::operator==(const KeyValueStore& rhs) const { return properties_ == rhs.properties_; } bool KeyValueStore::operator!=(const KeyValueStore& rhs) const { return properties_ != rhs.properties_; } bool KeyValueStore::ContainsBool(const string& name) const { // TODO(zqiu): Replace typeid comparison with chromeos::Any::IsTypeCompatible. return ContainsKey(properties_, name) && properties_.find(name)->second.GetType() == typeid(bool); // NOLINT } bool KeyValueStore::ContainsByteArrays(const string& name) const { return ContainsKey(properties_, name) && properties_.find(name)->second.GetType() == typeid(vector>); } bool KeyValueStore::ContainsInt(const string& name) const { return ContainsKey(properties_, name) && properties_.find(name)->second.GetType() == typeid(int32_t); } bool KeyValueStore::ContainsInt16(const string& name) const { return ContainsKey(properties_, name) && properties_.find(name)->second.GetType() == typeid(int16_t); } bool KeyValueStore::ContainsKeyValueStore(const string& name) const { return ContainsKey(properties_, name) && properties_.find(name)->second.GetType() == typeid(KeyValueStore); } bool KeyValueStore::ContainsRpcIdentifier(const string& name) const { return ContainsKey(properties_, name) && properties_.find(name)->second.GetType() == typeid(dbus::ObjectPath); } bool KeyValueStore::ContainsRpcIdentifiers(const string& name) const { return ContainsKey(properties_, name) && properties_.find(name)->second.GetType() == typeid(vector); } bool KeyValueStore::ContainsString(const string& name) const { return ContainsKey(properties_, name) && properties_.find(name)->second.GetType() == typeid(string); } bool KeyValueStore::ContainsStringmap(const std::string& name) const { return ContainsKey(properties_, name) && properties_.find(name)->second.GetType() == typeid(Stringmap); } bool KeyValueStore::ContainsStrings(const string& name) const { return ContainsKey(properties_, name) && properties_.find(name)->second.GetType() == typeid(Strings); } bool KeyValueStore::ContainsUint(const string& name) const { return ContainsKey(properties_, name) && properties_.find(name)->second.GetType() == typeid(uint32_t); } bool KeyValueStore::ContainsUint8(const string& name) const { return ContainsKey(properties_, name) && properties_.find(name)->second.GetType() == typeid(uint8_t); } bool KeyValueStore::ContainsUint16(const string& name) const { return ContainsKey(properties_, name) && properties_.find(name)->second.GetType() == typeid(uint16_t); } bool KeyValueStore::ContainsUint8s(const string& name) const { return ContainsKey(properties_, name) && properties_.find(name)->second.GetType() == typeid(vector); } bool KeyValueStore::ContainsUint32s(const string& name) const { return ContainsKey(properties_, name) && properties_.find(name)->second.GetType() == typeid(vector); } bool KeyValueStore::Contains(const string& name) const { return ContainsKey(properties_, name); } bool KeyValueStore::GetBool(const string& name) const { const auto it(properties_.find(name)); // TODO(zqiu): Replace typeid comparison with chromeos::Any::IsTypeCompatible. CHECK(it != properties_.end() && it->second.GetType() == typeid(bool)) << "for bool property " << name; return it->second.Get(); } const vector>& KeyValueStore::GetByteArrays( const string& name) const { const auto it(properties_.find(name)); CHECK(it != properties_.end() && it->second.GetType() == typeid(vector>)) << "for byte arrays property " << name; return it->second.Get>>(); } int32_t KeyValueStore::GetInt(const string& name) const { const auto it(properties_.find(name)); CHECK(it != properties_.end() && it->second.GetType() == typeid(int32_t)) << "for int property " << name; return it->second.Get(); } int16_t KeyValueStore::GetInt16(const string& name) const { const auto it(properties_.find(name)); CHECK(it != properties_.end() && it->second.GetType() == typeid(int16_t)) << "for int16 property " << name; return it->second.Get(); } const KeyValueStore& KeyValueStore::GetKeyValueStore(const string& name) const { const auto it(properties_.find(name)); CHECK(it != properties_.end() && it->second.GetType() == typeid(KeyValueStore)) << "for key value store property " << name; return it->second.Get(); } const string& KeyValueStore::GetRpcIdentifier(const string& name) const { const auto it(properties_.find(name)); CHECK(it != properties_.end() && it->second.GetType() == typeid(dbus::ObjectPath)) << "for rpc identifier property " << name; return it->second.Get().value(); } vector KeyValueStore::GetRpcIdentifiers(const string& name) const { const auto it(properties_.find(name)); CHECK(it != properties_.end() && it->second.GetType() == typeid(vector)) << "for rpc identifier property " << name; RpcIdentifiers ids; KeyValueStore::ConvertPathsToRpcIdentifiers( it->second.Get>(), &ids); return ids; } const string& KeyValueStore::GetString(const string& name) const { const auto it(properties_.find(name)); CHECK(it != properties_.end() && it->second.GetType() == typeid(string)) << "for string property " << name; return it->second.Get(); } const map& KeyValueStore::GetStringmap( const string& name) const { const auto it(properties_.find(name)); CHECK(it != properties_.end() && it->second.GetType() == typeid(Stringmap)) << "for stringmap property " << name; return it->second.Get(); } const vector& KeyValueStore::GetStrings(const string& name) const { const auto it(properties_.find(name)); CHECK(it != properties_.end() && it->second.GetType() == typeid(Strings)) << "for strings property " << name; return it->second.Get(); } uint32_t KeyValueStore::GetUint(const string& name) const { const auto it(properties_.find(name)); CHECK(it != properties_.end() && it->second.GetType() == typeid(uint32_t)) << "for uint32 property " << name; return it->second.Get(); } uint16_t KeyValueStore::GetUint16(const string& name) const { const auto it(properties_.find(name)); CHECK(it != properties_.end() && it->second.GetType() == typeid(uint16_t)) << "for uint16 property " << name; return it->second.Get(); } uint8_t KeyValueStore::GetUint8(const string& name) const { const auto it(properties_.find(name)); CHECK(it != properties_.end() && it->second.GetType() == typeid(uint8_t)) << "for uint8 property " << name; return it->second.Get(); } const vector& KeyValueStore::GetUint8s(const string& name) const { const auto it(properties_.find(name)); CHECK(it != properties_.end() && it->second.GetType() == typeid(vector)) << "for uint8s property " << name; return it->second.Get>(); } const vector& KeyValueStore::GetUint32s(const string& name) const { const auto it(properties_.find(name)); CHECK(it != properties_.end() && it->second.GetType() == typeid(vector)) << "for uint32s property " << name; return it->second.Get>(); } const chromeos::Any& KeyValueStore::Get(const string& name) const { const auto it(properties_.find(name)); CHECK(it != properties_.end()); return it->second; } void KeyValueStore::SetBool(const string& name, bool value) { properties_[name] = chromeos::Any(value); } void KeyValueStore::SetByteArrays(const string& name, const vector>& value) { properties_[name] = chromeos::Any(value); } void KeyValueStore::SetInt(const string& name, int32_t value) { properties_[name] = chromeos::Any(value); } void KeyValueStore::SetInt16(const string& name, int16_t value) { properties_[name] = chromeos::Any(value); } void KeyValueStore::SetKeyValueStore(const string& name, const KeyValueStore& value) { properties_[name] = chromeos::Any(value); } void KeyValueStore::SetRpcIdentifier(const string& name, const string& value) { properties_[name] = chromeos::Any(dbus::ObjectPath(value)); } void KeyValueStore::SetRpcIdentifiers(const string& name, const vector& value) { vector paths; for (const auto& rpcid : value) { paths.push_back(dbus::ObjectPath(rpcid)); } properties_[name] = chromeos::Any(paths); } void KeyValueStore::SetString(const string& name, const string& value) { properties_[name] = chromeos::Any(value); } void KeyValueStore::SetStringmap(const string& name, const map& value) { properties_[name] = chromeos::Any(value); } void KeyValueStore::SetStrings(const string& name, const vector& value) { properties_[name] = chromeos::Any(value); } void KeyValueStore::SetUint(const string& name, uint32_t value) { properties_[name] = chromeos::Any(value); } void KeyValueStore::SetUint16(const string& name, uint16_t value) { properties_[name] = chromeos::Any(value); } void KeyValueStore::SetUint8(const string& name, uint8_t value) { properties_[name] = chromeos::Any(value); } void KeyValueStore::SetUint8s(const string& name, const vector& value) { properties_[name] = chromeos::Any(value); } void KeyValueStore::SetUint32s(const string& name, const vector& value) { properties_[name] = chromeos::Any(value); } void KeyValueStore::Set(const string& name, const chromeos::Any& value) { properties_[name] = value; } void KeyValueStore::RemoveByteArrays(const string& name) { properties_.erase(name); } void KeyValueStore::RemoveInt(const string& name) { properties_.erase(name); } void KeyValueStore::RemoveInt16(const string& name) { properties_.erase(name); } void KeyValueStore::RemoveKeyValueStore(const string& name) { properties_.erase(name); } void KeyValueStore::RemoveRpcIdentifier(const string& name) { properties_.erase(name); } void KeyValueStore::RemoveString(const string& name) { properties_.erase(name); } void KeyValueStore::RemoveStringmap(const string& name) { properties_.erase(name); } void KeyValueStore::RemoveStrings(const string& name) { properties_.erase(name); } void KeyValueStore::RemoveUint16(const string& name) { properties_.erase(name); } void KeyValueStore::RemoveUint8(const string& name) { properties_.erase(name); } void KeyValueStore::RemoveUint8s(const string& name) { properties_.erase(name); } void KeyValueStore::RemoveUint32s(const string& name) { properties_.erase(name); } void KeyValueStore::Remove(const string& name) { properties_.erase(name); } bool KeyValueStore::LookupBool(const string& name, bool default_value) const { const auto it(properties_.find(name)); if (it == properties_.end()) { return default_value; } CHECK(it->second.GetType() == typeid(bool)) << "type mismatched"; return it->second.Get(); } int KeyValueStore::LookupInt(const string& name, int default_value) const { const auto it(properties_.find(name)); if (it == properties_.end()) { return default_value; } CHECK(it->second.GetType() == typeid(int32_t)) << "type mismatched"; return it->second.Get(); } string KeyValueStore::LookupString(const string& name, const string& default_value) const { const auto it(properties_.find(name)); if (it == properties_.end()) { return default_value; } CHECK(it->second.GetType() == typeid(string)) << "type mismatched"; return it->second.Get(); } // static. void KeyValueStore::ConvertToVariantDictionary( const KeyValueStore& in_store, chromeos::VariantDictionary* out_dict) { for (const auto& key_value_pair : in_store.properties_) { if (key_value_pair.second.IsTypeCompatible()) { // Special handling for nested KeyValueStore (convert it to // nested chromeos::VariantDictionary). chromeos::VariantDictionary dict; ConvertToVariantDictionary( key_value_pair.second.Get(), &dict); out_dict->emplace(key_value_pair.first, dict); } else { out_dict->insert(key_value_pair); } } } // static. void KeyValueStore::ConvertFromVariantDictionary( const chromeos::VariantDictionary& in_dict, KeyValueStore* out_store) { for (const auto& key_value_pair : in_dict) { if (key_value_pair.second.IsTypeCompatible()) { // Special handling for nested chromeos::VariantDictionary (convert it to // nested KeyValueStore). KeyValueStore store; ConvertFromVariantDictionary( key_value_pair.second.Get(), &store); out_store->properties_.emplace(key_value_pair.first, store); } else { out_store->properties_.insert(key_value_pair); } } } // static. void KeyValueStore::ConvertPathsToRpcIdentifiers( const vector& paths, vector* rpc_identifiers) { for (const auto& path : paths) { rpc_identifiers->push_back(path.value()); } } } // namespace shill