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 { |
| 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 NOTREACHED(); |
| 23 return NULL; |
| 24 } |
| 25 |
| 26 template <typename T, size_t N> |
| 27 bool NameToValue(const NameMapElement<T> (&map)[N], |
| 28 const std::string& name, |
| 29 T* result) { |
| 30 for (size_t i = 0; i < N; ++i) { |
| 31 if (map[i].name == name) { |
| 32 *result = map[i].value; |
| 33 return true; |
| 34 } |
| 35 } |
| 36 return false; |
| 37 } |
| 38 |
| 39 } // namespace protocol |
| 40 } // namespace remoting |
OLD | NEW |