1.9.9.2
Gameface
Array.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 
37 #include <assert.h>
38 #include <algorithm>
39 #include "TypeTraits.h"
40 
41 
42 namespace cohtml
43 {
44 class Binder;
45 
46 template <typename ArrayType>
47 void CoherentArrayElementReader(Binder* binder, void* data, size_t position)
48 {
49  auto arrayData = (ArrayType*)data;
50  CoherentReadInternal(binder, arrayData[position]);
51 }
52 
53 typedef void(*Reader)(Binder*, void*, size_t);
54 
55 template <typename T>
57 {
58 public:
60  {
61  return cohtml::CoherentArrayElementReader<T>;
62  }
63 };
64 
65 template <typename T>
67 {
68 public:
70  {
71  return nullptr;
72  }
73 };
74 
75 template <typename ArrayType>
76 void CoherentArrayElementBinder(Binder* binder, void* arr, size_t index)
77 {
78  auto castedArray = (ArrayType*)arr;
79  CoherentBindInternal(binder, castedArray[index]);
80 }
81 
82 template <typename ArrayType, size_t ArraySize>
83 inline typename EnableIf<!IsChar<ArrayType>::Value || IsSame<ArrayType, unsigned char>::Value>::Type CoherentBindInternal(Binder* binder, ArrayType(&array)[ArraySize])
84 {
85  if (!binder->TryBindArrayByRef((void*)(array),
86  ArraySize,
87  CoherentArrayElementBinder<ArrayType>,
88  ArrayReaderFactory<typename RemoveConst<ArrayType>::Type>::ElementReader()))
89  {
90  binder->ArrayBegin(ArraySize);
91 
92  for (size_t i = 0; i < ArraySize; ++i)
93  {
94  CoherentBindInternal(binder, array[i]);
95  }
96 
97  binder->ArrayEnd();
98  }
99 }
100 
101 template <typename ArrayType, size_t ArraySize>
102 inline typename EnableIf<IsChar<ArrayType>::Value && !IsSame<ArrayType, unsigned char>::Value>::Type CoherentBindInternal(Binder* binder, ArrayType array[ArraySize])
103 {
104  binder->Bind(array);
105 }
106 
107 template <typename ArrayType, size_t ArraySize>
108 inline typename EnableIf<!IsChar<ArrayType>::Value || IsSame<ArrayType, unsigned char>::Value>::Type CoherentReadInternal(Binder* binder, ArrayType (&array)[ArraySize])
109 {
110  size_t arraySize = binder->ReadArrayBegin();
111  assert(arraySize == ArraySize);
112 
113  size_t index = 0;
114  for (size_t i = 0; i < arraySize; ++i)
115  {
116  binder->ReadArrayElement(index++);
117  CoherentReadInternal(binder, array[i]);
118  }
119 
120  binder->ReadArrayEnd();
121 }
122 
123 template <typename ArrayType, size_t ArraySize>
124 inline typename EnableIf<IsChar<ArrayType>::Value && !IsSame<ArrayType, unsigned char>::Value>::Type CoherentReadInternal(Binder* binder, ArrayType array[ArraySize])
125 {
126  static_assert(IsConst<ArrayType>::Value == false, "You can't write in constant memory. Maybe you have to use PropertyReadOnly");
127  size_t size = 0;
128  const ArrayType* buffer = nullptr;
129  binder->Read(buffer, size);
130  if (size != 0)
131  {
132  memcpy((void*)array, (void*)buffer, std::min(size, ArraySize) * sizeof(ArrayType));
133  array[std::min(size, ArraySize - 1)] = 0;
134  }
135  else if (ArraySize > 0)
136  {
137  array[0] = 0;
138  }
139 }
140 
141 }
Definition: TypeTraits.h:407
EnableIf<!IsChar< ArrayType >::Value||IsSame< ArrayType, unsigned char >::Value >::Type CoherentBindInternal(Binder *binder, ArrayType(&array)[ArraySize])
Definition: Array.h:83
void(* Reader)(Binder *, void *, size_t)
Definition: Array.h:53
Contains almost all Coherent namespaces, classes and functions.
Definition: CursorTypes.h:37
void CoherentArrayElementReader(Binder *binder, void *data, size_t position)
Definition: Array.h:47
Definition: Array.h:56
virtual void ArrayEnd()=0
End exporting an array.
virtual size_t ReadArrayBegin()=0
Start reading an array.
virtual void ReadArrayEnd()=0
End reading an array.
virtual void Read(bool &value)=0
virtual void Bind(bool value)=0
virtual void ArrayBegin(size_t size)=0
Start exporting an array.
void CoherentArrayElementBinder(Binder *binder, void *arr, size_t index)
Definition: Array.h:76
Definition: TypeTraits.h:55
Provides binding between C++ and the UI.
Definition: Binder.h:138
T Type
Definition: TypeTraits.h:274
Definition: TypeTraits.h:396
static Reader ElementReader()
Definition: Array.h:59
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 Reader ElementReader()
Definition: Array.h:69
virtual bool TryBindArrayByRef(void *arr, size_t size, ArrayElementBinder elementBinder, ArrayElementReader elementReader)=0