• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 #include "base/global_descriptors_posix.h"
6 
7 #include <vector>
8 #include <utility>
9 
10 #include "base/logging.h"
11 
12 namespace base {
13 
MaybeGet(Key key) const14 int GlobalDescriptors::MaybeGet(Key key) const {
15   for (Mapping::const_iterator
16        i = descriptors_.begin(); i != descriptors_.end(); ++i) {
17     if (i->first == key)
18       return i->second;
19   }
20 
21   // In order to make unittests pass, we define a default mapping from keys to
22   // descriptors by adding a fixed offset:
23   return kBaseDescriptor + key;
24 }
25 
Get(Key key) const26 int GlobalDescriptors::Get(Key key) const {
27   const int ret = MaybeGet(key);
28 
29   if (ret == -1)
30     LOG(FATAL) << "Unknown global descriptor: " << key;
31   return ret;
32 }
33 
Set(Key key,int fd)34 void GlobalDescriptors::Set(Key key, int fd) {
35   for (Mapping::iterator
36        i = descriptors_.begin(); i != descriptors_.end(); ++i) {
37     if (i->first == key) {
38       i->second = fd;
39       return;
40     }
41   }
42 
43   descriptors_.push_back(std::make_pair(key, fd));
44 }
45 
46 }  // namespace base
47