| 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 #ifndef REMOTING_PROTOCOL_NAME_VALUE_MAP_H_ | |
| 8 #define REMOTING_PROTOCOL_NAME_VALUE_MAP_H_ | |
| 9 | |
| 10 #include <stddef.h> | |
| 11 | |
| 12 #include "base/logging.h" | |
| 13 | |
| 14 namespace remoting { | |
| 15 namespace protocol { | |
| 16 | |
| 17 template <typename T> | |
| 18 struct NameMapElement { | |
| 19 const T value; | |
| 20 const char* const name; | |
| 21 }; | |
| 22 | |
| 23 template <typename T, size_t N> | |
| 24 const char* ValueToName(const NameMapElement<T> (&map)[N], T value) { | |
| 25 for (size_t i = 0; i < N; ++i) { | |
| 26 if (map[i].value == value) | |
| 27 return map[i].name; | |
| 28 } | |
| 29 NOTREACHED(); | |
| 30 return nullptr; | |
| 31 } | |
| 32 | |
| 33 template <typename T, size_t N> | |
| 34 bool NameToValue(const NameMapElement<T> (&map)[N], | |
| 35 const std::string& name, | |
| 36 T* result) { | |
| 37 for (size_t i = 0; i < N; ++i) { | |
| 38 if (map[i].name == name) { | |
| 39 *result = map[i].value; | |
| 40 return true; | |
| 41 } | |
| 42 } | |
| 43 return false; | |
| 44 } | |
| 45 | |
| 46 } // namespace protocol | |
| 47 } // namespace remoting | |
| 48 | |
| 49 #endif // REMOTING_PROTOCOL_NAME_VALUE_MAP_H_ | |
| OLD | NEW |