1.14.0.5
Hummingbird
A modern user interface library for games
Vector.h
1 /*
2 This file is part of Hummingbird, a modern user interface library.
3 
4 Copyright (c) 2012-2017 Coherent Labs AD and/or its licensors. All
5 rights reserved in all media.
6 
7 The coded instructions, statements, computer programs, and/or related
8 material (collectively the "Data") in these files contain confidential
9 and unpublished information proprietary Coherent Labs and/or its
10 licensors, which is protected by United States of America federal
11 copyright law and by international treaties.
12 
13 This software or source code is supplied under the terms of a license
14 agreement and nondisclosure agreement with Coherent Labs AD and may
15 not be copied, disclosed, or exploited except in accordance with the
16 terms of that agreement. The Data may not be disclosed or distributed to
17 third parties, in whole or in part, without the prior written consent of
18 Coherent Labs AD.
19 
20 COHERENT LABS MAKES NO REPRESENTATION ABOUT THE SUITABILITY OF THIS
21 SOURCE CODE FOR ANY PURPOSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
22 HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
23 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE
25 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER, ITS AFFILIATES,
26 PARENT COMPANIES, LICENSORS, SUPPLIERS, OR CONTRIBUTORS BE LIABLE FOR
27 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 ANY WAY OUT OF THE USE OR PERFORMANCE OF THIS SOFTWARE OR SOURCE CODE,
33 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35 #pragma once
36 #include "Property.h"
37 
38 namespace cohtml
39 {
40 class Binder;
41 
42 template <typename VectorType>
43 void CoherentVectorElementReader(Binder* binder, void* data, size_t position)
44 {
45  auto castedData = (VectorType*)data;
46  CoherentReadInternal(binder, castedData[position]);
47 }
48 
49 template <typename VectorType>
50 void* CoherentVectorNativeElementReader(void* data, size_t position)
51 {
52  return &(*(VectorType*)data)[position];
53 }
54 
55 template <typename VectorType>
56 size_t CoherentVectorLength(void* data)
57 {
58  return ((VectorType*)data)->size();
59 }
60 
61 template <typename VectorType, typename T>
62 typename EnableIf<!cohtml::PointerTrait<T>::Value, void>::Type CoherentVectorElementReader(Binder* binder, void* data, size_t position)
63 {
64  CoherentReadInternal(binder, (*(VectorType*)data)[position]);
65 }
66 
67 template <typename VectorType, typename T>
68 typename EnableIf<cohtml::PointerTrait<T>::Value, void>::Type CoherentVectorElementReader(Binder* binder, void* data, size_t position)
69 {
70  typedef typename UnwrapPointerType<T, PointerTrait<T>::Value>::Type UnwrappedType;
71 
72  if (auto unwrappedObjectPtr = (UnwrappedType*)UnwrapPointer<T>::Unwrap(&(*(VectorType*)data)[position]))
73  {
74  CoherentReadInternal(binder, *unwrappedObjectPtr);
75  }
76 }
77 
78 template <typename VectorType, typename T>
79 typename EnableIf<!cohtml::PointerTrait<T>::Value, void*>::Type CoherentVectorNativeElementReader(void* data, size_t position)
80 {
81  return &(*(VectorType*)data)[position];
82 }
83 
84 template <typename VectorType, typename T>
85 typename EnableIf<cohtml::PointerTrait<T>::Value, void*>::Type CoherentVectorNativeElementReader(void* data, size_t position)
86 {
87  return UnwrapPointer<T>::Unwrap(&(*(VectorType*)data)[position]);
88 }
89 
90 typedef void(*Reader)(Binder*, void*, size_t);
91 typedef void*(*NativeReader)(void*, size_t);
92 typedef size_t(*VectorLengthFunc)(void*);
93 
94 template <typename T>
95 class VectorReaderFactory
96 {
97 public:
98  static Reader ElementReader()
99  {
100  return cohtml::CoherentVectorElementReader<T>;
101  }
102 
103  static NativeReader NativeElementReader()
104  {
105  return cohtml::CoherentVectorNativeElementReader<T>;
106  }
107 
108  static VectorLengthFunc VectorLength()
109  {
110  return CoherentVectorLength<T>;
111  }
112 };
113 
114 template <typename T, typename A>
115 class VectorReaderFactory<std::vector<T, A>>
116 {
117 public:
118  typedef std::vector<T, A> VectorType;
119 
120  static Reader ElementReader()
121  {
122  return cohtml::CoherentVectorElementReader<VectorType, T>;
123  }
124 
125  static NativeReader NativeElementReader()
126  {
127  return cohtml::CoherentVectorNativeElementReader<VectorType, T>;
128  }
129 
130  static VectorLengthFunc VectorLength()
131  {
132  return CoherentVectorLength<VectorType>;
133  }
134 };
135 
136 template <typename VectorType>
137 void CoherentVectorElementBinder(Binder* binder, void* arr, size_t index)
138 {
139  auto castedArray = (VectorType*)arr;
140  if (arr)
141  {
142  CoherentBindInternal(binder, castedArray[index]);
143  }
144  else
145  {
146  binder->BindNull();
147  }
148 }
149 
150 template <typename T, typename A>
151 void CoherentBindInternal(Binder* binder, std::vector<T, A>& value)
152 {
153  if (!binder->TryBindArrayByRef(value.data(),
154  value.size(),
155  CoherentVectorElementBinder<T>,
156  VectorReaderFactory<std::vector<T, A>>::ElementReader()))
157  {
158  binder->ArrayBegin(value.size());
159  typedef typename std::vector<T, A>::const_iterator Iterator;
160 
161  Iterator end = value.end();
162  for (Iterator i = value.begin(); i != end; ++i)
163  {
164  CoherentBindInternal(binder, *i);
165  }
166 
167  binder->ArrayEnd();
168  }
169 }
170 
171 template <typename T, typename A>
172 void CoherentBindInternal(Binder* binder, const std::vector<T, A>& value)
173 {
174  if (!binder->TryBindArrayByRef(const_cast<T*>(value.data()),
175  value.size(),
176  CoherentVectorElementBinder<T>,
177  VectorReaderFactory<std::vector<T, A>>::ElementReader()))
178  {
179  binder->ArrayBegin(value.size());
180  typedef typename std::vector<T, A>::const_iterator Iterator;
181 
182  Iterator end = value.end();
183  for (Iterator i = value.begin(); i != end; ++i)
184  {
185  CoherentBindInternal(binder, *i);
186  }
187 
188  binder->ArrayEnd();
189  }
190 }
191 
192 template <typename A>
193 void CoherentBindInternal(Binder* binder, std::vector<float, A>& value)
194 {
195  if (!binder->TryBindArrayByRef(value.data(),
196  value.size(),
197  CoherentVectorElementBinder<float>,
198  VectorReaderFactory<float>::ElementReader()))
199  {
200  binder->BindArray(&value[0], value.size());
201  }
202 }
203 
204 template <typename A>
205 void CoherentBindInternal(Binder* binder, const std::vector<float, A>& value)
206 {
207  if (!binder->TryBindArrayByRef(const_cast<float*>(value.data()),
208  value.size(),
209  CoherentVectorElementBinder<float>,
210  VectorReaderFactory<float>::ElementReader()))
211  {
212  binder->BindArray(&value[0], value.size());
213  }
214 }
215 
216 template <typename A>
217 void CoherentBindInternal(Binder* binder, std::vector<int, A>& value)
218 {
219  if (!binder->TryBindArrayByRef(value.data(),
220  value.size(),
221  CoherentVectorElementBinder<int>,
222  VectorReaderFactory<int>::ElementReader()))
223  {
224  binder->BindArray(&value[0], value.size());
225  }
226 }
227 
228 template <typename A>
229 void CoherentBindInternal(Binder* binder, const std::vector<int, A>& value)
230 {
231  if (!binder->TryBindArrayByRef(const_cast<int*>(value.data()),
232  value.size(),
233  CoherentVectorElementBinder<int>,
234  VectorReaderFactory<int>::ElementReader()))
235  {
236  binder->BindArray(&value[0], value.size());
237  }
238 }
239 
240 template <typename T, typename A>
241 void CoherentReadInternal(Binder* binder, std::vector<T, A>& value)
242 {
243  typedef typename std::vector<T, A>::iterator Iterator;
244 
245  size_t size = binder->ReadArrayBegin();
246 
247  value.resize(size);
248 
249  Iterator end = value.end();
250  size_t index = 0;
251  for (Iterator i = value.begin(); i != end; ++i)
252  {
253  binder->ReadArrayElement(index++);
254  CoherentReadInternal(binder, *i);
255  }
256 
257  binder->ReadArrayEnd();
258 }
259 
260 template<typename T, typename A>
261 struct TypeToElementType<const std::vector<T, A>>
262 {
263  static constexpr ElementType value = ElementType::ET_Array;
264 };
265 
266 template<typename T, typename A>
267 struct TypeToElementType<std::vector<T, A>>
268 {
269  static constexpr ElementType value = ElementType::ET_Array;
270 };
271 
272 template<typename T, typename A>
273 struct TypeToElementType<std::vector<T, A>&>
274 {
275  static constexpr ElementType value = ElementType::ET_Array;
276 };
277 
278 template<typename T, typename A>
279 struct TypeToElementType<const std::vector<T, A>&>
280 {
281  static constexpr ElementType value = ElementType::ET_Array;
282 };
283 
284 template <typename T>
285 struct IsVector : FalseType
286 {
287  typedef T ElementType;
288  typedef T AllocatorType;
289 };
290 
291 template <typename T, typename A>
292 struct IsVector<std::vector<T, A>> : TrueType
293 {
294  typedef T ElementType;
295  typedef A AllocatorType;
296 };
297 
298 template <typename T>
299 typename EnableIf<!IsVector<typename UnwrapPointerType<T, PointerTrait<T>::Value>::Type>::Value, bool>::Type
300  GetArrayValueInvoke(Binder* binder, void* object, ArrayInfo* arrayInfo, const TypedProperty<const T&>* prop)
301 {
302  UNUSED_PARAM(binder);
303  UNUSED_PARAM(object);
304  UNUSED_PARAM(arrayInfo);
305  UNUSED_PARAM(prop);
306  return false;
307 }
308 
309 template <typename T>
310 typename EnableIf<!IsVector<typename UnwrapPointerType<T, PointerTrait<T>::Value>::Type>::Value, bool>::Type
311  GetArrayValueInvoke(Binder* binder, void* object, ArrayInfo* arrayInfo, const TypedProperty<T&>* prop)
312 {
313  UNUSED_PARAM(binder);
314  UNUSED_PARAM(object);
315  UNUSED_PARAM(arrayInfo);
316  UNUSED_PARAM(prop);
317  return false;
318 }
319 
320 template <typename T, typename A>
321 bool GetArrayValueInvoke(Binder* binder, ArrayInfo* arrayInfo, std::vector<T, A>* pVector)
322 {
323  if (!pVector)
324  {
325  return false;
326  }
327 
328  *arrayInfo = ArrayInfo{
329  TypeToElementType<T>::value,
330  VectorReaderFactory<std::vector<T, A>>::VectorLength(),
331  VectorReaderFactory<std::vector<T, A>>::NativeElementReader(),
332  CoherentTypeInfo<T>::Get(binder),
333  pVector
334  };
335 
336  return true;
337 }
338 
339 template <typename T>
340 typename EnableIf<IsVector<typename UnwrapPointerType<T, PointerTrait<T>::Value>::Type>::Value, bool>::Type
341  GetArrayValueInvoke(Binder* binder, void* object, ArrayInfo* arrayInfo, const TypedProperty<const T&>* prop)
342 {
343  typedef typename UnwrapPointerType<T, PointerTrait<T>::Value>::Type UnwrappedType;
344  auto pVector = (UnwrappedType*)UnwrapPointer<T>::Unwrap(const_cast<T*>(&prop->GetValue(object)));
345 
346  return GetArrayValueInvoke(binder, arrayInfo, pVector);
347 }
348 
349 template <typename T>
350 typename EnableIf<IsVector<typename UnwrapPointerType<T, PointerTrait<T>::Value>::Type>::Value, bool>::Type
351  GetArrayValueInvoke(Binder* binder, void* object, ArrayInfo* arrayInfo, const TypedProperty<T&>* prop)
352 {
353  typedef typename UnwrapPointerType<T, PointerTrait<T>::Value>::Type UnwrappedType;
354  auto pVector = (UnwrappedType*)UnwrapPointer<T>::Unwrap(const_cast<T*>(&prop->GetValue(object)));
355 
356  return GetArrayValueInvoke(binder, arrayInfo, pVector);
357 }
358 
359 template<typename T>
360 struct GetArrayValue<const T&>
361 {
362  static bool Invoke(Binder* binder, void* object, ArrayInfo* arrayInfo, const TypedProperty<const T&>* prop)
363  {
364  return GetArrayValueInvoke(binder, object, arrayInfo, prop);
365  }
366 };
367 
368 template<typename T>
369 struct GetArrayValue<T&>
370 {
371  static bool Invoke(Binder* binder, void* object, ArrayInfo* arrayInfo, const TypedProperty<T&>* prop)
372  {
373  return GetArrayValueInvoke(binder, object, arrayInfo, prop);
374  }
375 };
376 
377 }
Contains almost all Coherent namespaces, classes and functions.
Definition: DataStorage.h:38