blob: 869d196bc2418c287ce76f87c87c5e4fcbab5739 [file] [log] [blame]
[email protected]cc8f1462009-06-12 17:36:551// 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
14int 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
26int 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
34void 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