OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 #include "base/global_descriptors_posix.h" | 5 #include "base/global_descriptors_posix.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 | 11 |
12 namespace base { | 12 namespace base { |
13 | 13 |
14 GlobalDescriptors::GlobalDescriptors() {} | |
15 | |
16 GlobalDescriptors::~GlobalDescriptors() {} | |
17 | |
18 // static | 14 // static |
19 GlobalDescriptors* GlobalDescriptors::GetInstance() { | 15 GlobalDescriptors* GlobalDescriptors::GetInstance() { |
20 typedef Singleton<base::GlobalDescriptors, | 16 typedef Singleton<base::GlobalDescriptors, |
21 LeakySingletonTraits<base::GlobalDescriptors> > | 17 LeakySingletonTraits<base::GlobalDescriptors> > |
22 GlobalDescriptorsSingleton; | 18 GlobalDescriptorsSingleton; |
23 return GlobalDescriptorsSingleton::get(); | 19 return GlobalDescriptorsSingleton::get(); |
24 } | 20 } |
25 | 21 |
| 22 int GlobalDescriptors::Get(Key key) const { |
| 23 const int ret = MaybeGet(key); |
| 24 |
| 25 if (ret == -1) |
| 26 LOG(FATAL) << "Unknown global descriptor: " << key; |
| 27 return ret; |
| 28 } |
| 29 |
26 int GlobalDescriptors::MaybeGet(Key key) const { | 30 int GlobalDescriptors::MaybeGet(Key key) const { |
27 for (Mapping::const_iterator | 31 for (Mapping::const_iterator |
28 i = descriptors_.begin(); i != descriptors_.end(); ++i) { | 32 i = descriptors_.begin(); i != descriptors_.end(); ++i) { |
29 if (i->first == key) | 33 if (i->first == key) |
30 return i->second; | 34 return i->second; |
31 } | 35 } |
32 | 36 |
33 // In order to make unittests pass, we define a default mapping from keys to | 37 // In order to make unittests pass, we define a default mapping from keys to |
34 // descriptors by adding a fixed offset: | 38 // descriptors by adding a fixed offset: |
35 return kBaseDescriptor + key; | 39 return kBaseDescriptor + key; |
36 } | 40 } |
37 | 41 |
38 int GlobalDescriptors::Get(Key key) const { | |
39 const int ret = MaybeGet(key); | |
40 | |
41 if (ret == -1) | |
42 LOG(FATAL) << "Unknown global descriptor: " << key; | |
43 return ret; | |
44 } | |
45 | |
46 void GlobalDescriptors::Set(Key key, int fd) { | 42 void GlobalDescriptors::Set(Key key, int fd) { |
47 for (Mapping::iterator | 43 for (Mapping::iterator |
48 i = descriptors_.begin(); i != descriptors_.end(); ++i) { | 44 i = descriptors_.begin(); i != descriptors_.end(); ++i) { |
49 if (i->first == key) { | 45 if (i->first == key) { |
50 i->second = fd; | 46 i->second = fd; |
51 return; | 47 return; |
52 } | 48 } |
53 } | 49 } |
54 | 50 |
55 descriptors_.push_back(std::make_pair(key, fd)); | 51 descriptors_.push_back(std::make_pair(key, fd)); |
56 } | 52 } |
57 | 53 |
| 54 void GlobalDescriptors::Reset(const Mapping& mapping) { |
| 55 descriptors_ = mapping; |
| 56 } |
| 57 |
| 58 GlobalDescriptors::GlobalDescriptors() {} |
| 59 |
| 60 GlobalDescriptors::~GlobalDescriptors() {} |
| 61 |
58 } // namespace base | 62 } // namespace base |
OLD | NEW |