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 <map> |
9 #include <string> | 10 #include <string> |
10 #include <map> | |
11 | 11 |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "chrome/browser/chromeos/cros/enum_mapper.h" |
13 #include "chrome/browser/chromeos/cros/network_library.h" | 14 #include "chrome/browser/chromeos/cros/network_library.h" |
14 | 15 |
15 namespace base { | 16 namespace base { |
16 class DictionaryValue; | 17 class DictionaryValue; |
17 class Value; | 18 class Value; |
18 } | 19 } |
19 | 20 |
20 namespace chromeos { | 21 namespace chromeos { |
21 | 22 |
22 class NetworkDevice; | 23 class NetworkDevice; |
23 | 24 |
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 | 25 // This takes a Value of a particular form, and maps the keys in the |
87 // dictionary to a NetworkDevice object to initialize it properly. | 26 // dictionary to a NetworkDevice object to initialize it properly. |
88 // Subclasses of this can then customize its methods to parse either | 27 // Subclasses of this can then customize its methods to parse either |
89 // libcros (flimflam) data or network setup information obtained from | 28 // libcros (flimflam) data or network setup information obtained from |
90 // policy or setup file import depending on the EnumMapper supplied. | 29 // policy or setup file import depending on the EnumMapper supplied. |
91 class NetworkDeviceParser { | 30 class NetworkDeviceParser { |
92 public: | 31 public: |
93 virtual ~NetworkDeviceParser(); | 32 virtual ~NetworkDeviceParser(); |
94 | 33 |
95 virtual NetworkDevice* CreateDeviceFromInfo( | 34 virtual NetworkDevice* CreateDeviceFromInfo( |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 } | 119 } |
181 | 120 |
182 private: | 121 private: |
183 const EnumMapper<PropertyIndex>* mapper_; | 122 const EnumMapper<PropertyIndex>* mapper_; |
184 DISALLOW_COPY_AND_ASSIGN(NetworkParser); | 123 DISALLOW_COPY_AND_ASSIGN(NetworkParser); |
185 }; | 124 }; |
186 | 125 |
187 } // namespace chromeos | 126 } // namespace chromeos |
188 | 127 |
189 #endif // CHROME_BROWSER_CHROMEOS_CROS_NETWORK_PARSER_H_ | 128 #endif // CHROME_BROWSER_CHROMEOS_CROS_NETWORK_PARSER_H_ |
OLD | NEW |