OLD | NEW |
---|---|
(Empty) | |
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. | |
6 | |
7 namespace remoting { | |
8 namespace protocol { | |
9 | |
10 template <typename T> | |
11 struct NameMapElement { | |
Wez
2012/08/14 20:56:32
nit: If you make both |value| and |name| template
Sergey Ulanov
2012/08/14 21:21:36
We need to store char* in the static array (to avo
| |
12 const T value; | |
13 const char* const name; | |
14 }; | |
15 | |
16 template <typename T, size_t N> | |
17 const char* ValueToName(const NameMapElement<T> (&map)[N], T value) { | |
18 for (size_t i = 0; i < N; ++i) { | |
19 if (map[i].value == value) | |
20 return map[i].name; | |
21 } | |
22 return NULL; | |
23 } | |
24 | |
25 template <typename T, size_t N> | |
26 bool NameToValue(const NameMapElement<T> (&map)[N], | |
27 const std::string& name, | |
28 T* result) { | |
Wez
2012/08/14 20:56:32
nit: Why not return const T* and have the caller c
Sergey Ulanov
2012/08/14 21:21:36
I think that would be more confusing and harder to
| |
29 for (size_t i = 0; i < N; ++i) { | |
30 if (map[i].name == name) { | |
31 *result = map[i].value; | |
32 return true; | |
33 } | |
34 } | |
35 return false; | |
36 } | |
37 | |
38 } // namespace protocol | |
39 } // namespace remoting | |
OLD | NEW |