TrinityCore
Loading...
Searching...
No Matches
TypeContainer.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef TRINITY_TYPECONTAINER_H
19#define TRINITY_TYPECONTAINER_H
20
21/*
22 * Here, you'll find a series of containers that allow you to hold multiple
23 * types of object at the same time.
24 */
25
26#include "Define.h"
27#include "Types.h"
28
29template <template <typename> typename UnderlyingContainer, typename... Types>
31{
32 // empty case
33};
34
35template <template <typename> typename UnderlyingContainer, typename First, typename... Rest>
36struct TypeListContainerStorage<UnderlyingContainer, First, Rest...>
37{
38 typename UnderlyingContainer<First>::Container Head;
39 TypeListContainerStorage<UnderlyingContainer, Rest...> Tail;
40
41 template <typename ObjectType>
42 typename UnderlyingContainer<ObjectType>::Container& FindContainer()
43 {
44 if constexpr (std::is_same_v<First, ObjectType>)
45 return Head;
46 else
47 return Tail.template FindContainer<ObjectType>();
48 }
49
50 template <typename ObjectType>
51 typename UnderlyingContainer<ObjectType>::Container const& FindContainer() const
52 {
53 if constexpr (std::is_same_v<First, ObjectType>)
54 return Head;
55 else
56 return Tail.template FindContainer<ObjectType>();
57 }
58};
59
60template <template <typename> typename UnderlyingContainer, typename... Types>
62{
63 TypeListContainerStorage<UnderlyingContainer, Types...> Data;
64
65 template <typename ObjectType>
66 static constexpr bool TypeExists = Trinity::has_type_in_list_v<ObjectType, Types...>;
67
68 template <typename ObjectType>
69 using ValueType = typename UnderlyingContainer<ObjectType>::ValueType;
70
71 template <typename ObjectType> requires TypeExists<ObjectType>
73 {
74 return UnderlyingContainer<ObjectType>::Insert(Data.template FindContainer<ObjectType>(), object);
75 }
76
77 template <typename ObjectType> requires TypeExists<ObjectType>
79 {
80 return UnderlyingContainer<ObjectType>::Remove(Data.template FindContainer<ObjectType>(), object);
81 }
82
83 template <typename ObjectType> requires TypeExists<ObjectType>
84 std::size_t Size() const
85 {
86 return UnderlyingContainer<ObjectType>::Size(Data.template FindContainer<ObjectType>());
87 }
88
89 template <typename ObjectType> requires TypeExists<ObjectType> && requires { typename UnderlyingContainer<ObjectType>::KeyType; }
90 ValueType<ObjectType> Find(typename UnderlyingContainer<ObjectType>::KeyType const& key) const
91 {
92 return UnderlyingContainer<ObjectType>::Find(Data.template FindContainer<ObjectType>(), key);
93 }
94};
95
96#endif
constexpr bool has_type_in_list_v
Definition Types.h:67
TypeListContainerStorage< UnderlyingContainer, Rest... > Tail
UnderlyingContainer< ObjectType >::Container const & FindContainer() const
UnderlyingContainer< ObjectType >::Container & FindContainer()
TypeListContainerStorage< UnderlyingContainer, Types... > Data
ValueType< ObjectType > Find(typename UnderlyingContainer< ObjectType >::KeyType const &key) const
bool Insert(ValueType< ObjectType > object)
typename UnderlyingContainer< ObjectType >::ValueType ValueType
static constexpr bool TypeExists
std::size_t Size() const
bool Remove(ValueType< ObjectType > object)