1// 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
5#ifndef LIBBRILLO_BRILLO_VARIANT_DICTIONARY_H_
6#define LIBBRILLO_BRILLO_VARIANT_DICTIONARY_H_
7
8#include <map>
9#include <string>
10
11#include <brillo/any.h>
12#include <brillo/brillo_export.h>
13
14namespace brillo {
15
16using VariantDictionary = std::map<std::string, brillo::Any>;
17
18// 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
31}  // namespace brillo
32
33#endif  // LIBBRILLO_BRILLO_VARIANT_DICTIONARY_H_
34