global_descriptors_posix.cc revision 731df977c0511bca2206b5f333555b1205ff1f43
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
12namespace base {
13
14GlobalDescriptors::GlobalDescriptors() {}
15
16GlobalDescriptors::~GlobalDescriptors() {}
17
18int GlobalDescriptors::MaybeGet(Key key) const {
19  for (Mapping::const_iterator
20       i = descriptors_.begin(); i != descriptors_.end(); ++i) {
21    if (i->first == key)
22      return i->second;
23  }
24
25  // In order to make unittests pass, we define a default mapping from keys to
26  // descriptors by adding a fixed offset:
27  return kBaseDescriptor + key;
28}
29
30int GlobalDescriptors::Get(Key key) const {
31  const int ret = MaybeGet(key);
32
33  if (ret == -1)
34    LOG(FATAL) << "Unknown global descriptor: " << key;
35  return ret;
36}
37
38void GlobalDescriptors::Set(Key key, int fd) {
39  for (Mapping::iterator
40       i = descriptors_.begin(); i != descriptors_.end(); ++i) {
41    if (i->first == key) {
42      i->second = fd;
43      return;
44    }
45  }
46
47  descriptors_.push_back(std::make_pair(key, fd));
48}
49
50}  // namespace base
51