Horizon Official Technical Documentation
GridReferenceContainer.hpp
Go to the documentation of this file.
1/***************************************************
2 * _ _ _ *
3 * | | | | (_) *
4 * | |_| | ___ _ __ _ _______ _ __ *
5 * | _ |/ _ \| '__| |_ / _ \| '_ \ *
6 * | | | | (_) | | | |/ / (_) | | | | *
7 * \_| |_/\___/|_| |_/___\___/|_| |_| *
8 ***************************************************
9 * This file is part of Horizon (c).
10 *
11 * Copyright (c) 2019 Sagun K. (sagunxp@gmail.com).
12 * Copyright (c) 2019 Horizon Dev Team.
13 *
14 * Base Author - Sagun K. (sagunxp@gmail.com)
15 *
16 * This library is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
20 *
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this library. If not, see <http://www.gnu.org/licenses/>.
28 **************************************************/
29
30#ifndef HORIZON_CORE_STRUCTURES_GRIDREFERENCECONTAINER_HPP
31#define HORIZON_CORE_STRUCTURES_GRIDREFERENCECONTAINER_HPP
32
35
36/*============================*
37 * Container Data Type
38 *============================*/
39template<class OBJECT>
41{
43};
44
45template<>
46struct GridTypeListContainer<TypeNull>
47{
48};
49
50template<class HEAD, class TAIL>
52{
55};
56
57/*============================*
58 * TypeListIterator Insert Functions
59 *============================*/
60// non-const insert functions
62{
63 // Invoked in the condition that the SPECIFIC_TYPE has been visited.
64 template<class SPECIFIC_TYPE>
65 SPECIFIC_TYPE *Insert(GridTypeListContainer<SPECIFIC_TYPE> &elements, SPECIFIC_TYPE *obj)
66 {
67 obj->add_grid_reference(elements._element);
68 return obj;
69 }
70 // Invoked in the condition that a TypeNull has been visited.
71 template<class SPECIFIC_TYPE>
72 SPECIFIC_TYPE *Insert(GridTypeListContainer<TypeNull> &/*elements*/, SPECIFIC_TYPE */*obj*/)
73 {
74 return nullptr;
75 }
76 // In a case such as the following, where the GridTypeListContainer is not one of the specific type
77 // that we are in search of, the resulting return of the function is a nullptr.
78 template<class SPECIFIC_TYPE, class T>
79 SPECIFIC_TYPE *Insert(GridTypeListContainer<T> &/*elements*/, SPECIFIC_TYPE */*obj*/)
80 {
81 return nullptr;
82 }
83 // In a condition such as the following where a GridTypeListContainer of a TypeList<HEAD, TAIL> is invoked,
84 // and the specific object that is to be inserted is also a valid argument. The function definition matches the
85 // search criteria of the template function that is to be invoked. We try recursing to iterate through the TypeLists
86 // contained in the GridTypeListContainer until a reference has been added or the end of the TypeList (TypeNull) is met and the
87 // appropriate function has been invoked to return a nullptr or a valid pointer to the object.
88 template<class SPECIFIC_TYPE, class HEAD, class TAIL>
89 SPECIFIC_TYPE* Insert(GridTypeListContainer<TypeList<HEAD, TAIL>> &elements, SPECIFIC_TYPE* obj)
90 {
91 SPECIFIC_TYPE *t = Insert(elements._elements, obj);
92 return (t != nullptr ? t : Insert(elements._tail_elements, obj));
93 }
94/*============================*
95 * TypeListIterator Count Functions
96 *============================*/
97 // Function for when the specific type has been invoked and the requirement of meeting the function
98 // definition has been successful.
99 template <class SPECIFIC_TYPE>
100 size_t count(GridTypeListContainer<SPECIFIC_TYPE> const &elements, SPECIFIC_TYPE */*fake*/)
101 {
102 return elements._element.get_size();
103 }
104 // Function for when a TypeNull has been visited.
105 template <class SPECIFIC_TYPE>
106 size_t count(GridTypeListContainer<TypeNull> const &/*elements*/, SPECIFIC_TYPE */*fake*/)
107 {
108 return 0;
109 }
110 // Function containing any other type than the one we are in search of, will be invoked.
111 template <class SPECIFIC_TYPE, class T>
112 size_t count(GridTypeListContainer<T> const &/*elements*/, SPECIFIC_TYPE */*fake*/)
113 {
114 return 0;
115 }
116 // Invoked in the condition that the a TypeList of type <SPECIFIC_TYPE, T> has been met.
117 template <class SPECIFIC_TYPE, class T>
118 size_t count(GridTypeListContainer<TypeList<SPECIFIC_TYPE, T>> const &elements, SPECIFIC_TYPE *fake)
119 {
120 return count(elements._elements, fake);
121 }
122 // Invoked in the condition that any other TypeList, the ones that are not of the SPECIFIC_TYPE we
123 // are in search of, has been met. All of the element types will be visited, invoking the count() function
124 // for the specific type we are in search of.
125 template <class SPECIFIC_TYPE, class H, class T>
126 size_t count(GridTypeListContainer<TypeList<H, T>> const &elements, SPECIFIC_TYPE *fake)
127 {
128 return count(elements._tail_elements, fake);
129 }
130
131 template <class TypeNull>
133 {
134 return 0;
135 }
136
137 // Counts all of the objects in a GridTypeListContainer.
138 template <class H, class T>
140 {
141 return count(elements._elements, (H *) nullptr) + count_all(elements._tail_elements);
142 }
143}
144/*============================*
145 * GridReferenceContainer
146 *============================*/
147template<class OBJECT_TYPES>
149{
150public:
151 template <class SPECIFIC_TYPE>
152 size_t count() const
153 {
154 return GridTypeListIterator::count(_elements, (SPECIFIC_TYPE *) nullptr);
155 }
156
157 size_t count_all() const
158 {
160 }
161
163 template <class SPECIFIC_TYPE>
164 bool insert(SPECIFIC_TYPE *obj)
165 {
166 assert(obj && !obj->has_valid_grid_reference());
167 SPECIFIC_TYPE* t = GridTypeListIterator::Insert(_elements, obj);
168 return (t != nullptr);
169 }
170
173
174private:
176};
177
178#endif /* HORIZON_CORE_STRUCTURES_GRIDREFERENCECONTAINER_HPP */
Forward declaration of GridRefManager, the class that manages GridReference.
Definition: GridRefManager.hpp:41
Definition: GridReferenceContainer.hpp:149
GridTypeListContainer< OBJECT_TYPES > _elements
Definition: GridReferenceContainer.hpp:175
bool insert(SPECIFIC_TYPE *obj)
inserts a specific object into the container
Definition: GridReferenceContainer.hpp:164
size_t count() const
Definition: GridReferenceContainer.hpp:152
GridTypeListContainer< OBJECT_TYPES > & getElements(void)
Definition: GridReferenceContainer.hpp:171
size_t count_all() const
Definition: GridReferenceContainer.hpp:157
const GridTypeListContainer< OBJECT_TYPES > & getElements(void) const
Definition: GridReferenceContainer.hpp:172
Definition: GridReferenceContainer.hpp:62
SPECIFIC_TYPE * Insert(GridTypeListContainer< SPECIFIC_TYPE > &elements, SPECIFIC_TYPE *obj)
Definition: GridReferenceContainer.hpp:65
size_t count(GridTypeListContainer< SPECIFIC_TYPE > const &elements, SPECIFIC_TYPE *)
Definition: GridReferenceContainer.hpp:100
size_t count_all(GridTypeListContainer< TypeNull > const &elements)
Definition: GridReferenceContainer.hpp:132
GridTypeListContainer< HEAD > _elements
Definition: GridReferenceContainer.hpp:53
GridTypeListContainer< TAIL > _tail_elements
Definition: GridReferenceContainer.hpp:54
Definition: GridReferenceContainer.hpp:47
Definition: GridReferenceContainer.hpp:41
GridRefManager< OBJECT > _element
Definition: GridReferenceContainer.hpp:42
Definition: TypeList.hpp:8