OLD | NEW |
1 // Copyright (c) 2009 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 BASE_GLOBAL_DESCRIPTORS_POSIX_H_ | 5 #ifndef BASE_GLOBAL_DESCRIPTORS_POSIX_H_ |
6 #define BASE_GLOBAL_DESCRIPTORS_POSIX_H_ | 6 #define BASE_GLOBAL_DESCRIPTORS_POSIX_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
10 | 10 |
11 #include <vector> | 11 #include <vector> |
(...skipping 18 matching lines...) Expand all Loading... |
30 // fragile solution since global constructors etc are able to open files. | 30 // fragile solution since global constructors etc are able to open files. |
31 // | 31 // |
32 // Instead, we retreat from the idea of installing descriptors in specific | 32 // Instead, we retreat from the idea of installing descriptors in specific |
33 // slots and add a layer of indirection in the form of this singleton object. | 33 // slots and add a layer of indirection in the form of this singleton object. |
34 // It maps from an abstract key to a descriptor. If independent modules each | 34 // It maps from an abstract key to a descriptor. If independent modules each |
35 // need to define keys, then values should be chosen randomly so as not to | 35 // need to define keys, then values should be chosen randomly so as not to |
36 // collide. | 36 // collide. |
37 class GlobalDescriptors { | 37 class GlobalDescriptors { |
38 public: | 38 public: |
39 typedef uint32_t Key; | 39 typedef uint32_t Key; |
| 40 typedef std::vector<std::pair<Key, int> > Mapping; |
| 41 |
40 // Often we want a canonical descriptor for a given Key. In this case, we add | 42 // Often we want a canonical descriptor for a given Key. In this case, we add |
41 // the following constant to the key value: | 43 // the following constant to the key value: |
42 static const int kBaseDescriptor = 3; // 0, 1, 2 are already taken. | 44 static const int kBaseDescriptor = 3; // 0, 1, 2 are already taken. |
43 | 45 |
44 // Return the singleton instance of GlobalDescriptors. | 46 // Return the singleton instance of GlobalDescriptors. |
45 static GlobalDescriptors* GetInstance(); | 47 static GlobalDescriptors* GetInstance(); |
46 | 48 |
47 // Get a descriptor given a key. It is a fatal error if the key is not known. | 49 // Get a descriptor given a key. It is a fatal error if the key is not known. |
48 int Get(Key key) const; | 50 int Get(Key key) const; |
| 51 |
49 // Get a descriptor give a key. Returns -1 on error. | 52 // Get a descriptor give a key. Returns -1 on error. |
50 int MaybeGet(Key key) const; | 53 int MaybeGet(Key key) const; |
51 | 54 |
52 typedef std::vector<std::pair<Key, int> > Mapping; | |
53 | |
54 // Set the descriptor for the given key. | 55 // Set the descriptor for the given key. |
55 void Set(Key key, int fd); | 56 void Set(Key key, int fd); |
56 | 57 |
57 void Reset(const Mapping& mapping) { | 58 void Reset(const Mapping& mapping) { |
58 descriptors_ = mapping; | 59 descriptors_ = mapping; |
59 } | 60 } |
60 | 61 |
61 private: | 62 private: |
| 63 friend struct DefaultSingletonTraits<GlobalDescriptors>; |
62 GlobalDescriptors(); | 64 GlobalDescriptors(); |
63 ~GlobalDescriptors(); | 65 ~GlobalDescriptors(); |
64 friend struct DefaultSingletonTraits<GlobalDescriptors>; | |
65 | 66 |
66 Mapping descriptors_; | 67 Mapping descriptors_; |
67 }; | 68 }; |
68 | 69 |
69 } // namespace base | 70 } // namespace base |
70 | 71 |
71 #endif // BASE_GLOBAL_DESCRIPTORS_POSIX_H_ | 72 #endif // BASE_GLOBAL_DESCRIPTORS_POSIX_H_ |
OLD | NEW |