Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / msg / async / dpdk / array_map.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
2 /*
3  * This file is open source software, licensed to you under the terms
4  * of the Apache License, Version 2.0 (the "License").  See the NOTICE file
5  * distributed with this work for additional information regarding copyright
6  * ownership.  You may not use this file except in compliance with the License.
7  *
8  * You may obtain a copy of the License at
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 /*
20  * Copyright (C) 2014 Cloudius Systems, Ltd.
21  */
22
23 #ifndef CEPH_ARRAY_MAP_HH_
24 #define CEPH_ARRAY_MAP_HH_
25
26 #include <array>
27
28 // unordered_map implemented as a simple array
29
30 template <typename Value, size_t Max>
31 class array_map {
32   std::array<Value, Max> _a {};
33  public:
34   array_map(std::initializer_list<std::pair<size_t, Value>> i) {
35     for (auto kv : i) {
36       _a[kv.first] = kv.second;
37     }
38   }
39   Value& operator[](size_t key) { return _a[key]; }
40   const Value& operator[](size_t key) const { return _a[key]; }
41
42   Value& at(size_t key) {
43     if (key >= Max) {
44       throw std::out_of_range(std::to_string(key) + " >= " + std::to_string(Max));
45     }
46     return _a[key];
47   }
48 };
49
50 #endif /* ARRAY_MAP_HH_ */