1.9.9.2
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 size_t CoherentVectorLength(void* vectorPtr)
44 {
45  return ((VectorType*)vectorPtr)->size();
46 }
47 
48 template <typename VectorType, typename T>
49 typename EnableIf<!cohtml::PointerTrait<T>::Value, void>::Type CoherentVectorElementReader(Binder* binder, void* vectorPtr, size_t position)
50 {
51  auto castedVectorPtr = (VectorType*)vectorPtr;
52  CoherentReadInternal(binder, (*castedVectorPtr)[position]);
53 }
54 
55 template <typename VectorType, typename T>
56 typename EnableIf<cohtml::PointerTrait<T>::Value, void>::Type CoherentVectorElementReader(Binder* binder, void* vectorPtr, size_t position)
57 {
58  typedef typename UnwrapPointerType<T, PointerTrait<T>::Value>::Type UnwrappedType;
59 
60  auto castedVectorPtr = (VectorType*)vectorPtr;
61  if (auto unwrappedObjectPtr = (UnwrappedType*)UnwrapPointer<T>::Unwrap(&(*castedVectorPtr)[position]))
62  {
63  CoherentReadInternal(binder, *unwrappedObjectPtr);
64  }
65 }
66 
67 template <typename VectorType, typename T>
68 typename EnableIf<!cohtml::PointerTrait<T>::Value, void*>::Type CoherentVectorNativeElementReader(void* vectorPtr, size_t position)
69 {
70  auto castedVectorPtr = (VectorType*)vectorPtr;
71  return &(*castedVectorPtr)[position];
72 }
73 
74 template <typename VectorType, typename T>
75 typename EnableIf<cohtml::PointerTrait<T>::Value, void*>::Type CoherentVectorNativeElementReader(void* vectorPtr, size_t position)
76 {
77  auto castedVectorPtr = (VectorType*)vectorPtr;
78  return UnwrapPointer<T>::Unwrap(&((*castedVectorPtr)[position]));
79 }
80 
81 typedef void(*Reader)(Binder*, void*, size_t);
82 typedef void*(*NativeReader)(void*, size_t);
83 typedef size_t(*VectorLengthFunc)(void*);
84 
85 template <typename T>
87 
88 template <typename T, typename A>
89 class VectorReaderFactory<std::vector<T, A>>
90 {
91 public:
92  typedef std::vector<T, A> VectorType;
93 
95  {
96  return cohtml::CoherentVectorElementReader<VectorType, T>;
97  }
98 
100  {
101  return cohtml::CoherentVectorNativeElementReader<VectorType, T>;
102  }
103 
105  {
106  return CoherentVectorLength<VectorType>;
107  }
108 };
109 
110 template <typename VectorType>
111 void CoherentVectorElementBinder(Binder* binder, void* vectorPtr, size_t index)
112 {
113  if (vectorPtr)
114  {
115  auto castedVectorPtr = (VectorType*)vectorPtr;
116  CoherentBindInternal(binder, (*castedVectorPtr)[index]);
117  }
118  else
119  {
120  binder->BindNull();
121  }
122 }
123 
124 template <typename T, typename A>
125 void CoherentBindInternal(Binder* binder, std::vector<T, A>& value)
126 {
127  typedef std::vector<T, A> VectorType;
128 
129  if (!binder->TryBindArrayByRef(&value,
130  value.size(),
131  CoherentVectorElementBinder<VectorType>,
133  {
134  binder->ArrayBegin(value.size());
135  typedef typename VectorType::const_iterator Iterator;
136 
137  Iterator end = value.end();
138  for (Iterator i = value.begin(); i != end; ++i)
139  {
140  CoherentBindInternal(binder, *i);
141  }
142 
143  binder->ArrayEnd();
144  }
145 }
146 
147 template <typename T, typename A>
148 void CoherentBindInternal(Binder* binder, const std::vector<T, A>& value)
149 {
150  typedef std::vector<T, A> VectorType;
151 
152  if (!binder->TryBindArrayByRef(const_cast<VectorType*>(&value),
153  value.size(),
154  CoherentVectorElementBinder<VectorType>,
156  {
157  binder->ArrayBegin(value.size());
158  typedef typename VectorType::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 A>
171 void CoherentBindInternal(Binder* binder, std::vector<float, A>& value)
172 {
173  typedef std::vector<float, A> VectorType;
174 
175  if (!binder->TryBindArrayByRef(&value,
176  value.size(),
177  CoherentVectorElementBinder<VectorType>,
179  {
180  binder->BindArray(value.data(), value.size());
181  }
182 }
183 
184 template <typename A>
185 void CoherentBindInternal(Binder* binder, const std::vector<float, A>& value)
186 {
187  typedef std::vector<float, A> VectorType;
188 
189  if (!binder->TryBindArrayByRef(const_cast<VectorType*>(&value),
190  value.size(),
191  CoherentVectorElementBinder<VectorType>,
193  {
194  binder->BindArray(value.data(), value.size());
195  }
196 }
197 
198 template <typename A>
199 void CoherentBindInternal(Binder* binder, std::vector<int, A>& value)
200 {
201  typedef std::vector<int, A> VectorType;
202 
203  if (!binder->TryBindArrayByRef(&value,
204  value.size(),
205  CoherentVectorElementBinder<VectorType>,
207  {
208  binder->BindArray(value.data(), value.size());
209  }
210 }
211 
212 template <typename A>
213 void CoherentBindInternal(Binder* binder, const std::vector<int, A>& value)
214 {
215  typedef std::vector<int, A> VectorType;
216 
217  if (!binder->TryBindArrayByRef(const_cast<VectorType*>(&value),
218  value.size(),
219  CoherentVectorElementBinder<VectorType>,
221  {
222  binder->BindArray(value.data(), value.size());
223  }
224 }
225 
226 template <typename T, typename A>
227 void CoherentReadInternal(Binder* binder, std::vector<T, A>& value)
228 {
229  typedef typename std::vector<T, A>::iterator Iterator;
230 
231  size_t size = binder->ReadArrayBegin();
232 
233  value.resize(size);
234 
235  Iterator end = value.end();
236  size_t index = 0;
237  for (Iterator i = value.begin(); i != end; ++i)
238  {
239  binder->ReadArrayElement(index++);
240  CoherentReadInternal(binder, *i);
241  }
242 
243  binder->ReadArrayEnd();
244 }
245 template<typename T, typename A>
246 struct TypeToElementType<std::vector<T, A>>
247 {
248  static constexpr ElementType value = ElementType::ET_Array;
249 };
250 
251 template <typename T>
253 {
254  typedef T ElementType;
255  typedef T AllocatorType;
256 };
257 
258 template <typename T, typename A>
259 struct IsVector<std::vector<T, A>> : TrueType
260 {
261  typedef T ElementType;
262  typedef A AllocatorType;
263 };
264 
265 template <typename T>
266 inline typename EnableIf<!IsVector<typename UnwrapPointerType<T, PointerTrait<T>::Value>::Type>::Value, bool>::Type
267  GetArrayValueInvoke(Binder* binder, void* object, ArrayInfo* arrayInfo, const TypedProperty<T&>* prop)
268 {
269  UNUSED_PARAM(binder);
270  UNUSED_PARAM(object);
271  UNUSED_PARAM(arrayInfo);
272  UNUSED_PARAM(prop);
273  return false;
274 }
275 
276 template <typename T, typename A>
277 inline bool GetArrayValueInvoke(Binder* binder, ArrayInfo* arrayInfo, std::vector<T, A>* pVector)
278 {
279  if (!pVector)
280  {
281  return false;
282  }
283 
284  *arrayInfo = ArrayInfo{
286  VectorReaderFactory<std::vector<T, A>>::VectorLength(),
287  VectorReaderFactory<std::vector<T, A>>::NativeElementReader(),
288  CoherentTypeInfo<T>::Get(binder),
289  pVector
290  };
291 
292  return true;
293 }
294 
295 template <typename T>
296 typename EnableIf<IsVector<typename UnwrapPointerType<T, PointerTrait<T>::Value>::Type>::Value, bool>::Type
297  GetArrayValueInvoke(Binder* binder, void* object, ArrayInfo* arrayInfo, const TypedProperty<T&>* prop)
298 {
299  typedef typename UnwrapPointerType<T, PointerTrait<T>::Value>::Type UnwrappedType;
300  auto pVector = (UnwrappedType*)UnwrapPointer<T>::Unwrap(const_cast<T*>(&prop->GetValue(object)));
301 
302  return GetArrayValueInvoke(binder, arrayInfo, pVector);
303 }
304 
305 template<typename T>
306 struct GetArrayValue<T&>
307 {
308  static bool Invoke(Binder* binder, void* object, ArrayInfo* arrayInfo, const TypedProperty<T&>* prop)
309  {
310  return GetArrayValueInvoke(binder, object, arrayInfo, prop);
311  }
312 };
313 
314 }
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:267
static Reader ElementReader()
Definition: Vector.h:94
virtual ValuePassType GetValue(void *object) const =0
Definition: Property.h:591
EnableIf<!cohtml::PointerTrait< T >::Value, void * >::Type CoherentVectorNativeElementReader(void *vectorPtr, size_t position)
Definition: Vector.h:68
Definition: TypeTraits.h:334
A AllocatorType
Definition: Vector.h:262
EnableIf<!IsChar< ArrayType >::Value||IsSame< ArrayType, unsigned char >::Value >::Type CoherentBindInternal(Binder *binder, ArrayType(&array)[ArraySize])
Definition: Array.h:83
size_t CoherentVectorLength(void *vectorPtr)
Definition: Vector.h:43
void(* Reader)(Binder *, void *, size_t)
Definition: Array.h:53
virtual void BindArray(const int *values, size_t count)=0
EnableIf<!cohtml::PointerTrait< T >::Value, void >::Type CoherentVectorElementReader(Binder *binder, void *vectorPtr, size_t position)
Definition: Vector.h:49
Definition: Property.h:562
void CoherentVectorElementBinder(Binder *binder, void *vectorPtr, size_t index)
Definition: Vector.h:111
static void * Unwrap(T *ptr)
Definition: TypeTraits.h:336
Contains almost all Coherent namespaces, classes and functions.
Definition: CursorTypes.h:37
T ElementType
Definition: Vector.h:261
virtual void ArrayEnd()=0
End exporting an array.
virtual size_t ReadArrayBegin()=0
Start reading an array.
size_t(* VectorLengthFunc)(void *)
Definition: Vector.h:83
virtual void ReadArrayEnd()=0
End reading an array.
ElementType
Definition: Property.h:336
virtual void ArrayBegin(size_t size)=0
Start exporting an array.
static NativeReader NativeElementReader()
Definition: Vector.h:99
static VectorLengthFunc VectorLength()
Definition: Vector.h:104
std::vector< T, A > VectorType
Definition: Vector.h:92
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:308
void *(* NativeReader)(void *, size_t)
Definition: Vector.h:82
T AllocatorType
Definition: Vector.h:255
Definition: TypeTraits.h:46
Definition: Property.h:617
Represents a array of a type exposed to the scripting.
Definition: Property.h:395
T ElementType
Definition: Vector.h:254
Definition: TypeTraits.h:363
Definition: Vector.h:86
virtual void BindNull()=0
Definition: TypeTraits.h:396
virtual void ReadArrayElement(size_t index)=0
Read element from an array.
EnableIf<!IsChar< ArrayType >::Value||IsSame< ArrayType, unsigned char >::Value >::Type CoherentReadInternal(Binder *binder, ArrayType(&array)[ArraySize])
Definition: Array.h:108
static const ElementType value
Definition: Property.h:650
virtual bool TryBindArrayByRef(void *arr, size_t size, ArrayElementBinder elementBinder, ArrayElementReader elementReader)=0
static TypeInfo * Get(Binder *binder)
Definition: Property.h:767
Definition: Vector.h:252