xbmc
Map.h
1 
2 /*
3  * Copyright (C) 2005-2022 Team Kodi
4  * This file is part of Kodi - https://kodi.tv
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  * See LICENSES/README.md for more information.
8  */
9 
10 #pragma once
11 
12 #include <algorithm>
13 #include <array>
14 #include <stdexcept>
15 
31 template<typename Key, typename Value, size_t Size>
32 class CMap
33 {
34 public:
35  template<typename Iterable>
36  constexpr CMap(Iterable begin, Iterable end)
37  {
38  size_t index = 0;
39  while (begin != end)
40  {
41  // c++17 doesn't have constexpr assignment operator for std::pair
42  auto& first = m_map[index].first;
43  auto& second = m_map[index].second;
44  ++index;
45 
46  first = std::move(begin->first);
47  second = std::move(begin->second);
48  ++begin;
49 
51  // auto& p = data[index];
52  // ++index;
53 
54  // p = std::move(*begin);
55  // ++begin;
56  //
57  }
58  }
59 
60  ~CMap() = default;
61 
62  constexpr const Value& at(const Key& key) const
63  {
64  const auto it = find(key);
65  if (it != m_map.cend())
66  {
67  return it->second;
68  }
69  else
70  {
71  throw std::range_error("Not Found");
72  }
73  }
74 
75  constexpr auto find(const Key& key) const
76  {
77  return std::find_if(m_map.cbegin(), m_map.cend(),
78  [&key](const auto& pair) { return pair.first == key; });
79  }
80 
81  constexpr size_t size() const { return Size; }
82 
83  constexpr auto cbegin() const { return m_map.cbegin(); }
84  constexpr auto cend() const { return m_map.cend(); }
85 
86 private:
87  CMap() = delete;
88 
89  std::array<std::pair<Key, Value>, Size> m_map;
90 };
91 
98 template<typename Key, typename Value, std::size_t Size>
99 constexpr auto make_map(std::pair<Key, Value>(&&m)[Size]) -> CMap<Key, Value, Size>
100 {
101  return CMap<Key, Value, Size>(std::begin(m), std::end(m));
102 }
This class is designed to implement a constexpr version of std::map. The standard library std::map do...
Definition: Map.h:32
constexpr CMap(Iterable begin, Iterable end)
Definition: Map.h:36