blob: 0b71e9cbb0c6276f7b140658f2cfb968779a09f0 [file] [log] [blame]
Alex Vakulenkod550ef92014-09-22 16:49:45 -07001// Copyright 2014 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Alex Vakulenkofed60b02015-10-27 09:53:05 -07005#ifndef LIBBRILLO_BRILLO_VARIANT_DICTIONARY_H_
6#define LIBBRILLO_BRILLO_VARIANT_DICTIONARY_H_
Alex Vakulenkod550ef92014-09-22 16:49:45 -07007
8#include <map>
9#include <string>
10
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -070011#include <brillo/any.h>
12#include <brillo/brillo_export.h>
Alex Vakulenkod550ef92014-09-22 16:49:45 -070013
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -070014namespace brillo {
Alex Vakulenkod550ef92014-09-22 16:49:45 -070015
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -070016using VariantDictionary = std::map<std::string, brillo::Any>;
Alex Vakulenkod550ef92014-09-22 16:49:45 -070017
Aaron Kemp9447e5a2015-03-12 14:25:46 -040018// GetVariantValueOrDefault tries to retrieve the named key from the dictionary
19// and convert it to the type T. If the value does not exist, or the type
20// conversion fails, the default value of type T is returned.
21template<typename T>
22const T GetVariantValueOrDefault(const VariantDictionary& dictionary,
23 const std::string& key) {
24 VariantDictionary::const_iterator it = dictionary.find(key);
25 if (it == dictionary.end()) {
26 return T();
27 }
28 return it->second.TryGet<T>();
29}
30
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -070031} // namespace brillo
Alex Vakulenkod550ef92014-09-22 16:49:45 -070032
Alex Vakulenkofed60b02015-10-27 09:53:05 -070033#endif // LIBBRILLO_BRILLO_VARIANT_DICTIONARY_H_