Re-land: C++ readability review
[chromium-blink-merge.git] / remoting / protocol / name_value_map.h
blob587188a269bf35303c58484bba7fe406bdf1ef44
1 // Copyright (c) 2012 The Chromium 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 // Helper functions that allow to map enum values to strings.
7 #include <stddef.h>
9 #include "base/logging.h"
11 namespace remoting {
12 namespace protocol {
14 template <typename T>
15 struct NameMapElement {
16 const T value;
17 const char* const name;
20 template <typename T, size_t N>
21 const char* ValueToName(const NameMapElement<T> (&map)[N], T value) {
22 for (size_t i = 0; i < N; ++i) {
23 if (map[i].value == value)
24 return map[i].name;
26 NOTREACHED();
27 return nullptr;
30 template <typename T, size_t N>
31 bool NameToValue(const NameMapElement<T> (&map)[N],
32 const std::string& name,
33 T* result) {
34 for (size_t i = 0; i < N; ++i) {
35 if (map[i].name == name) {
36 *result = map[i].value;
37 return true;
40 return false;
43 } // namespace protocol
44 } // namespace remoting