| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_CHROMEOS_CROS_NETWORK_PARSER_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_CROS_NETWORK_PARSER_H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_CROS_NETWORK_PARSER_H_ | 6 #define CHROME_BROWSER_CHROMEOS_CROS_NETWORK_PARSER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <map> | |
| 11 | 10 |
| 12 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "chrome/browser/chromeos/cros/enum_mapper.h" |
| 13 #include "chrome/browser/chromeos/cros/network_library.h" | 13 #include "chrome/browser/chromeos/cros/network_library.h" |
| 14 | 14 |
| 15 namespace base { | 15 namespace base { |
| 16 class DictionaryValue; | 16 class DictionaryValue; |
| 17 class Value; | 17 class Value; |
| 18 } | 18 } |
| 19 | 19 |
| 20 namespace chromeos { | 20 namespace chromeos { |
| 21 | 21 |
| 22 class NetworkDevice; | 22 class NetworkDevice; |
| 23 | 23 |
| 24 // This turns an array of string-to-enum-value mappings into a class | |
| 25 // that can cache the mapping and do quick lookups using an actual map | |
| 26 // class. Usage is something like: | |
| 27 // | |
| 28 // const char kKey1[] = "key1"; | |
| 29 // const char kKey2[] = "key2"; | |
| 30 // | |
| 31 // enum EnumFoo { | |
| 32 // UNKNOWN = 0, | |
| 33 // FOO = 1, | |
| 34 // BAR = 2, | |
| 35 // }; | |
| 36 // | |
| 37 // const EnumMapper<EnumFoo>::Pair index_table[] = { | |
| 38 // { kKey1, FOO }, | |
| 39 // { kKey2, BAR }, | |
| 40 // }; | |
| 41 // | |
| 42 // EnumMapper<EnumFoo> mapper(index_table, arraysize(index_table), UNKNOWN); | |
| 43 // EnumFoo value = mapper.Get(kKey1); // Returns FOO. | |
| 44 // EnumFoo value = mapper.Get('boo'); // Returns UNKNOWN. | |
| 45 template <typename EnumType> | |
| 46 class EnumMapper { | |
| 47 public: | |
| 48 struct Pair { | |
| 49 const char* key; | |
| 50 const EnumType value; | |
| 51 }; | |
| 52 | |
| 53 EnumMapper(const Pair* list, size_t num_entries, EnumType unknown) | |
| 54 : unknown_value_(unknown) { | |
| 55 for (size_t i = 0; i < num_entries; ++i, ++list) { | |
| 56 enum_map_[list->key] = list->value; | |
| 57 inverse_enum_map_[list->value] = list->key; | |
| 58 } | |
| 59 } | |
| 60 | |
| 61 EnumType Get(const std::string& type) const { | |
| 62 EnumMapConstIter iter = enum_map_.find(type); | |
| 63 if (iter != enum_map_.end()) | |
| 64 return iter->second; | |
| 65 return unknown_value_; | |
| 66 } | |
| 67 | |
| 68 std::string GetKey(EnumType type) const { | |
| 69 InverseEnumMapConstIter iter = inverse_enum_map_.find(type); | |
| 70 if (iter != inverse_enum_map_.end()) | |
| 71 return iter->second; | |
| 72 return std::string(); | |
| 73 } | |
| 74 | |
| 75 private: | |
| 76 typedef typename std::map<std::string, EnumType> EnumMap; | |
| 77 typedef typename std::map<EnumType, std::string> InverseEnumMap; | |
| 78 typedef typename EnumMap::const_iterator EnumMapConstIter; | |
| 79 typedef typename InverseEnumMap::const_iterator InverseEnumMapConstIter; | |
| 80 EnumMap enum_map_; | |
| 81 InverseEnumMap inverse_enum_map_; | |
| 82 EnumType unknown_value_; | |
| 83 DISALLOW_COPY_AND_ASSIGN(EnumMapper); | |
| 84 }; | |
| 85 | |
| 86 // This takes a Value of a particular form, and maps the keys in the | 24 // This takes a Value of a particular form, and maps the keys in the |
| 87 // dictionary to a NetworkDevice object to initialize it properly. | 25 // dictionary to a NetworkDevice object to initialize it properly. |
| 88 // Subclasses of this can then customize its methods to parse either | 26 // Subclasses of this can then customize its methods to parse either |
| 89 // libcros (flimflam) data or network setup information obtained from | 27 // libcros (flimflam) data or network setup information obtained from |
| 90 // policy or setup file import depending on the EnumMapper supplied. | 28 // policy or setup file import depending on the EnumMapper supplied. |
| 91 class NetworkDeviceParser { | 29 class NetworkDeviceParser { |
| 92 public: | 30 public: |
| 93 virtual ~NetworkDeviceParser(); | 31 virtual ~NetworkDeviceParser(); |
| 94 | 32 |
| 95 virtual NetworkDevice* CreateDeviceFromInfo( | 33 virtual NetworkDevice* CreateDeviceFromInfo( |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 } | 118 } |
| 181 | 119 |
| 182 private: | 120 private: |
| 183 const EnumMapper<PropertyIndex>* mapper_; | 121 const EnumMapper<PropertyIndex>* mapper_; |
| 184 DISALLOW_COPY_AND_ASSIGN(NetworkParser); | 122 DISALLOW_COPY_AND_ASSIGN(NetworkParser); |
| 185 }; | 123 }; |
| 186 | 124 |
| 187 } // namespace chromeos | 125 } // namespace chromeos |
| 188 | 126 |
| 189 #endif // CHROME_BROWSER_CHROMEOS_CROS_NETWORK_PARSER_H_ | 127 #endif // CHROME_BROWSER_CHROMEOS_CROS_NETWORK_PARSER_H_ |
| OLD | NEW |