1*1a96fba6SXin Li // Copyright 2014 The Chromium OS Authors. All rights reserved. 2*1a96fba6SXin Li // Use of this source code is governed by a BSD-style license that can be 3*1a96fba6SXin Li // found in the LICENSE file. 4*1a96fba6SXin Li 5*1a96fba6SXin Li #ifndef LIBBRILLO_BRILLO_VARIANT_DICTIONARY_H_ 6*1a96fba6SXin Li #define LIBBRILLO_BRILLO_VARIANT_DICTIONARY_H_ 7*1a96fba6SXin Li 8*1a96fba6SXin Li #include <map> 9*1a96fba6SXin Li #include <string> 10*1a96fba6SXin Li 11*1a96fba6SXin Li #include <brillo/any.h> 12*1a96fba6SXin Li #include <brillo/brillo_export.h> 13*1a96fba6SXin Li 14*1a96fba6SXin Li namespace brillo { 15*1a96fba6SXin Li 16*1a96fba6SXin Li using VariantDictionary = std::map<std::string, brillo::Any>; 17*1a96fba6SXin Li 18*1a96fba6SXin Li // GetVariantValueOrDefault tries to retrieve the named key from the dictionary 19*1a96fba6SXin Li // and convert it to the type T. If the value does not exist, or the type 20*1a96fba6SXin Li // conversion fails, the default value of type T is returned. 21*1a96fba6SXin Li template<typename T> GetVariantValueOrDefault(const VariantDictionary & dictionary,const std::string & key)22*1a96fba6SXin Liconst T GetVariantValueOrDefault(const VariantDictionary& dictionary, 23*1a96fba6SXin Li const std::string& key) { 24*1a96fba6SXin Li VariantDictionary::const_iterator it = dictionary.find(key); 25*1a96fba6SXin Li if (it == dictionary.end()) { 26*1a96fba6SXin Li return T(); 27*1a96fba6SXin Li } 28*1a96fba6SXin Li return it->second.TryGet<T>(); 29*1a96fba6SXin Li } 30*1a96fba6SXin Li 31*1a96fba6SXin Li } // namespace brillo 32*1a96fba6SXin Li 33*1a96fba6SXin Li #endif // LIBBRILLO_BRILLO_VARIANT_DICTIONARY_H_ 34