Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
fixed_map_base.h
Go to the documentation of this file.
1#pragma once
2
3#include <utility>
4#include <tuple>
5#include <type_traits>
6
7namespace ph
8{
9
10template<typename KeyType, KeyType KEY_VAL, typename ValueType>
11class TFixedMapEntry final
12{
13public:
14 using Key = KeyType;
15 using Value = ValueType;
16
17 static constexpr Key KEY = KEY_VAL;
18
19 Value& getValue() { return m_value; }
20 const Value& getValue() const { return m_value; }
21
22private:
23 Value m_value;
24};
25
26// TODO: ensure all keys are the same type, and entries are all TFixedMapEntry
27
28template<typename... Entries>
29class TFixedMapBase final
30{
31public:
32 using EntryArray = std::tuple<Entries...>;
33
34 static constexpr std::size_t ENTRY_ARRAY_SIZE = std::tuple_size_v<EntryArray>;
35 static_assert(ENTRY_ARRAY_SIZE > 0);
36
37 using Key = typename std::tuple_element_t<0, EntryArray>::Key;
38
39 template<std::size_t ENTRY_INDEX>
40 using Entry = std::tuple_element_t<ENTRY_INDEX, EntryArray>;
41
42 TFixedMapBase() = default;
43
44 TFixedMapBase(TFixedMapBase&& other) = default;
45
46 TFixedMapBase(Entries&&... entries) :
47 m_entries(std::move(entries)...)
48 {}
49
50 template<Key KEY, std::size_t D_ENTRY_INDEX = 0>
51 static constexpr bool hasKey()
52 {
53 if constexpr(D_ENTRY_INDEX == ENTRY_ARRAY_SIZE)
54 {
55 return false;
56 }
57
58 if constexpr(Entry<D_ENTRY_INDEX>::KEY == KEY)
59 {
60 return true;
61 }
62 else
63 {
65 }
66 }
67
68 template<std::size_t ENTRY_INDEX>
69 static constexpr Key entryKey()
70 {
71 static_assert(ENTRY_INDEX < ENTRY_ARRAY_SIZE);
72
74 }
75
76 template<Key KEY, std::size_t D_ENTRY_INDEX = 0>
77 static constexpr std::size_t entryIndex()
78 {
79 static_assert(hasKey<KEY>());
80
81 if constexpr(Entry<D_ENTRY_INDEX>::KEY == KEY)
82 {
83 return D_ENTRY_INDEX;
84 }
85 else
86 {
88 }
89 }
90
91 template<std::size_t ENTRY_INDEX>
92 decltype(auto) getEntry()
93 {
94 static_assert(ENTRY_INDEX < ENTRY_ARRAY_SIZE);
95
96 return std::get<ENTRY_INDEX>(m_entries);
97 }
98
99 template<std::size_t ENTRY_INDEX>
100 decltype(auto) getEntry() const
101 {
102 static_assert(ENTRY_INDEX < ENTRY_ARRAY_SIZE);
103
104 return std::get<ENTRY_INDEX>(m_entries);
105 }
106
107 template<Key KEY>
108 decltype(auto) get()
109 {
110 return getEntry<entryIndex<KEY>()>().getValue();
111 }
112
113 template<Key KEY>
114 decltype(auto) get() const
115 {
116 return getEntry<entryIndex<KEY>()>().getValue();
117 }
118
120
121private:
122 EntryArray m_entries;
123};
124
125}// end namespace ph
Definition fixed_map_base.h:30
decltype(auto) getEntry() const
Definition fixed_map_base.h:100
TFixedMapBase(TFixedMapBase &&other)=default
static constexpr bool hasKey()
Definition fixed_map_base.h:51
TFixedMapBase(Entries &&... entries)
Definition fixed_map_base.h:46
static constexpr std::size_t ENTRY_ARRAY_SIZE
Definition fixed_map_base.h:34
TFixedMapBase()=default
static constexpr Key entryKey()
Definition fixed_map_base.h:69
decltype(auto) getEntry()
Definition fixed_map_base.h:92
std::tuple_element_t< ENTRY_INDEX, EntryArray > Entry
Definition fixed_map_base.h:40
std::tuple< Entries... > EntryArray
Definition fixed_map_base.h:32
TFixedMapBase & operator=(TFixedMapBase &&rhs)=default
decltype(auto) get() const
Definition fixed_map_base.h:114
typename std::tuple_element_t< 0, EntryArray >::Key Key
Definition fixed_map_base.h:37
static constexpr std::size_t entryIndex()
Definition fixed_map_base.h:77
decltype(auto) get()
Definition fixed_map_base.h:108
Definition fixed_map_base.h:12
Value & getValue()
Definition fixed_map_base.h:19
ValueType Value
Definition fixed_map_base.h:15
KeyType Key
Definition fixed_map_base.h:14
static constexpr Key KEY
Definition fixed_map_base.h:17
const Value & getValue() const
Definition fixed_map_base.h:20
The root for all renderer implementations.
Definition EEngineProject.h:6
Definition TAABB2D.h:96