1.4.0.3
Gameface
Vector.h
1 /*
2 This file is part of Cohtml, Gameface and Prysm - modern user interface technologies.
3 
4 Copyright (c) 2012-2018 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>
96 {
97 public:
99  {
100  return cohtml::CoherentVectorElementReader<T>;
101  }
103  {
104  return cohtml::CoherentVectorNativeElementReader<T>;
105  }
106 
108  {
109  return CoherentVectorLength<T>;
110  }
111 };
112 
113 template <typename T, typename A>
114 class VectorReaderFactory<std::vector<T, A>>
115 {
116 public:
117  typedef std::vector<T, A> VectorType;
118 
120  {
121  return cohtml::CoherentVectorElementReader<VectorType, T>;
122  }
123 
125  {
126  return cohtml::CoherentVectorNativeElementReader<VectorType, T>;
127  }
128 
130  {
131  return CoherentVectorLength<VectorType>;
132  }
133 };
134 
135 template <typename VectorType>
136 void CoherentVectorElementBinder(Binder* binder, void* arr, size_t index)
137 {
138  auto castedArray = (VectorType*)arr;
139  if (arr)
140  {
141  CoherentBindInternal(binder, castedArray[index]);
142  }
143  else
144  {
145  binder->BindNull();
146  }
147 }
148 
149 template <typename T, typename A>
150 void CoherentBindInternal(Binder* binder, std::vector<T, A>& value)
151 {
152  if (!binder->TryBindArrayByRef(value.data(),
153  value.size(),
154  CoherentVectorElementBinder<T>,
155  VectorReaderFactory<std::vector<T, A>>::ElementReader()))
156  {
157  binder->ArrayBegin(value.size());
158  typedef typename std::vector<T, A>::const_iterator Iterator;
159 
160  Iterator end = value.end();
161  for (Iterator i = value.begin(); i != end; ++i)
162  {
163  CoherentBindInternal(binder, *i);
164  }
165 
166  binder->ArrayEnd();
167  }
168 }
169 
170 template <typename T, typename A>
171 void CoherentBindInternal(Binder* binder, const std::vector<T, A>& value)
172 {
173  if (!binder->TryBindArrayByRef(const_cast<T*>(value.data()),
174  value.size(),
175  CoherentVectorElementBinder<T>,
176  VectorReaderFactory<std::vector<T, A>>::ElementReader()))
177  {
178  binder->ArrayBegin(value.size());
179  typedef typename std::vector<T, A>::const_iterator Iterator;
180 
181  Iterator end = value.end();
182  for (Iterator i = value.begin(); i != end; ++i)
183  {
184  CoherentBindInternal(binder, *i);
185  }
186 
187  binder->ArrayEnd();
188  }
189 }
190 
191 template <typename A>
192 void CoherentBindInternal(Binder* binder, std::vector<float, A>& value)
193 {
194  if (!binder->TryBindArrayByRef(value.data(),
195  value.size(),
196  CoherentVectorElementBinder<float>,
198  {
199  binder->BindArray(&value[0], value.size());
200  }
201 }
202 
203 template <typename A>
204 void CoherentBindInternal(Binder* binder, const std::vector<float, A>& value)
205 {
206  if (!binder->TryBindArrayByRef(const_cast<float*>(value.data()),
207  value.size(),
208  CoherentVectorElementBinder<float>,
210  {
211  binder->BindArray(&value[0], value.size());
212  }
213 }
214 
215 template <typename A>
216 void CoherentBindInternal(Binder* binder, std::vector<int, A>& value)
217 {
218  if (!binder->TryBindArrayByRef(value.data(),
219  value.size(),
220  CoherentVectorElementBinder<int>,
222  {
223  binder->BindArray(&value[0], value.size());
224  }
225 }
226 
227 template <typename A>
228 void CoherentBindInternal(Binder* binder, const std::vector<int, A>& value)
229 {
230  if (!binder->TryBindArrayByRef(const_cast<int*>(value.data()),
231  value.size(),
232  CoherentVectorElementBinder<int>,
234  {
235  binder->BindArray(&value[0], value.size());
236  }
237 }
238 
239 template <typename T, typename A>
240 void CoherentReadInternal(Binder* binder, std::vector<T, A>& value)
241 {
242  typedef typename std::vector<T, A>::iterator Iterator;
243 
244  size_t size = binder->ReadArrayBegin();
245 
246  value.resize(size);
247 
248  Iterator end = value.end();
249  size_t index = 0;
250  for (Iterator i = value.begin(); i != end; ++i)
251  {
252  binder->ReadArrayElement(index++);
253  CoherentReadInternal(binder, *i);
254  }
255 
256  binder->ReadArrayEnd();
257 }
258 template<typename T, typename A>
259 struct TypeToElementType<std::vector<T, A>>
260 {
261  static constexpr ElementType value = ElementType::ET_Array;
262 };
263 
264 template <typename T>
266 {
267  typedef T ElementType;
268  typedef T AllocatorType;
269 };
270 
271 template <typename T, typename A>
272 struct IsVector<std::vector<T, A>> : TrueType
273 {
274  typedef T ElementType;
275  typedef A AllocatorType;
276 };
277 
278 template <typename T>
279 inline typename EnableIf<!IsVector<typename UnwrapPointerType<T, PointerTrait<T>::Value>::Type>::Value, bool>::Type
280  GetArrayValueInvoke(Binder* binder, void* object, ArrayInfo* arrayInfo, const TypedProperty<T&>* prop)
281 {
282  UNUSED_PARAM(binder);
283  UNUSED_PARAM(object);
284  UNUSED_PARAM(arrayInfo);
285  UNUSED_PARAM(prop);
286  return false;
287 }
288 
289 template <typename T, typename A>
290 inline bool GetArrayValueInvoke(Binder* binder, ArrayInfo* arrayInfo, std::vector<T, A>* pVector)
291 {
292  if (!pVector)
293  {
294  return false;
295  }
296 
297  *arrayInfo = ArrayInfo{
299  VectorReaderFactory<std::vector<T, A>>::VectorLength(),
300  VectorReaderFactory<std::vector<T, A>>::NativeElementReader(),
301  CoherentTypeInfo<T>::Get(binder),
302  pVector
303  };
304 
305  return true;
306 }
307 
308 template <typename T>
309 typename EnableIf<IsVector<typename UnwrapPointerType<T, PointerTrait<T>::Value>::Type>::Value, bool>::Type
310  GetArrayValueInvoke(Binder* binder, void* object, ArrayInfo* arrayInfo, const TypedProperty<T&>* prop)
311 {
312  typedef typename UnwrapPointerType<T, PointerTrait<T>::Value>::Type UnwrappedType;
313  auto pVector = (UnwrappedType*)UnwrapPointer<T>::Unwrap(const_cast<T*>(&prop->GetValue(object)));
314 
315  return GetArrayValueInvoke(binder, arrayInfo, pVector);
316 }
317 
318 template<typename T>
319 struct GetArrayValue<T&>
320 {
321  static bool Invoke(Binder* binder, void* object, ArrayInfo* arrayInfo, const TypedProperty<T&>* prop)
322  {
323  return GetArrayValueInvoke(binder, object, arrayInfo, prop);
324  }
325 };
326 
327 }
EnableIf<!IsVector< typename UnwrapPointerType< T, PointerTrait< T >::Value >::Type >::Value, bool >::Type GetArrayValueInvoke(Binder *binder, void *object, ArrayInfo *arrayInfo, const TypedProperty< T &> *prop)
Definition: Vector.h:280
static Reader ElementReader()
Definition: Vector.h:119
virtual ValuePassType GetValue(void *object) const =0
static VectorLengthFunc VectorLength()
Definition: Vector.h:107
Definition: Property.h:554
void CoherentReadInternal(Binder *binder, ArrayType(&array)[ArraySize])
Definition: Array.h:118
Definition: TypeTraits.h:323
void * CoherentVectorNativeElementReader(void *data, size_t position)
Definition: Vector.h:50
A AllocatorType
Definition: Vector.h:275
void(* Reader)(Binder *, void *, size_t)
Definition: Array.h:50
virtual void BindArray(const int *values, size_t count)=0
Definition: Property.h:525
void CoherentBindInternal(Binder *binder, ArrayType(&array)[ArraySize])
Definition: Array.h:80
static void * Unwrap(T *ptr)
Definition: TypeTraits.h:325
Contains almost all Coherent namespaces, classes and functions.
Definition: CursorTypes.h:37
T ElementType
Definition: Vector.h:274
virtual void ArrayEnd()=0
End exporting an array.
virtual size_t ReadArrayBegin()=0
Start reading an array.
size_t(* VectorLengthFunc)(void *)
Definition: Vector.h:92
virtual void ReadArrayEnd()=0
End reading an array.
static Reader ElementReader()
Definition: Vector.h:98
ElementType
Definition: Property.h:299
virtual void ArrayBegin(size_t size)=0
Start exporting an array.
static NativeReader NativeElementReader()
Definition: Vector.h:124
static VectorLengthFunc VectorLength()
Definition: Vector.h:129
void CoherentVectorElementReader(Binder *binder, void *data, size_t position)
Definition: Vector.h:43
std::vector< T, A > VectorType
Definition: Vector.h:117
Provides binding between C++ and the UI.
Definition: Binder.h:138
static bool Invoke(Binder *binder, void *object, ArrayInfo *arrayInfo, const TypedProperty< T &> *prop)
Definition: Vector.h:321
void *(* NativeReader)(void *, size_t)
Definition: Vector.h:91
static NativeReader NativeElementReader()
Definition: Vector.h:102
T AllocatorType
Definition: Vector.h:268
Definition: TypeTraits.h:46
size_t CoherentVectorLength(void *data)
Definition: Vector.h:56
Definition: Property.h:580
Represents a array of a type exposed to the scripting.
Definition: Property.h:358
T ElementType
Definition: Vector.h:267
Definition: TypeTraits.h:352
Definition: Vector.h:95
virtual void BindNull()=0
Definition: TypeTraits.h:385
void CoherentVectorElementBinder(Binder *binder, void *arr, size_t index)
Definition: Vector.h:136
virtual void ReadArrayElement(size_t index)=0
Read element from an array.
static const ElementType value
Definition: Property.h:613
virtual bool TryBindArrayByRef(void *arr, size_t size, ArrayElementBinder elementBinder, ArrayElementReader elementReader)=0
static TypeInfo * Get(Binder *binder)
Definition: Property.h:730
Definition: Vector.h:265