1.3.1.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 
39 namespace cohtml
40 {
41 class Binder;
42 
43 template <typename ArrayType>
44 void CoherentArrayElementReader(Binder* binder, void* data, size_t position)
45 {
46  auto arrayData = (ArrayType*)data;
47  CoherentReadInternal(binder, arrayData[position]);
48 }
49 
50 typedef void(*Reader)(Binder*, void*, size_t);
51 
52 template <typename T>
54 {
55 public:
57  {
58  return cohtml::CoherentArrayElementReader<T>;
59  }
60 };
61 
62 template <typename T>
64 {
65 public:
67  {
68  return nullptr;
69  }
70 };
71 
72 template <typename ArrayType>
73 void CoherentArrayElementBinder(Binder* binder, void* arr, size_t index)
74 {
75  auto castedArray = (ArrayType*)arr;
76  CoherentBindInternal(binder, castedArray[index]);
77 }
78 
79 template <typename ArrayType, size_t ArraySize>
80 void CoherentBindInternal(Binder* binder, ArrayType (&array)[ArraySize])
81 {
82  if (!binder->TryBindArrayByRef((void*)(array),
83  ArraySize,
84  CoherentArrayElementBinder<ArrayType>,
86  {
87  binder->ArrayBegin(ArraySize);
88 
89  for (size_t i = 0; i < ArraySize; ++i)
90  {
91  CoherentBindInternal(binder, array[i]);
92  }
93 
94  binder->ArrayEnd();
95  }
96 }
97 
98 template <typename ArrayType, size_t ArraySize>
99 void CoherentBindInternal(Binder* binder, const ArrayType(&array)[ArraySize])
100 {
101  if (!binder->TryBindArrayByRef((void*)(array),
102  ArraySize,
103  CoherentArrayElementBinder<const ArrayType>,
105  {
106  binder->ArrayBegin(ArraySize);
107 
108  for (size_t i = 0; i < ArraySize; ++i)
109  {
110  CoherentBindInternal(binder, array[i]);
111  }
112 
113  binder->ArrayEnd();
114  }
115 }
116 
117 template <typename ArrayType, size_t ArraySize>
118 void CoherentReadInternal(Binder* binder, ArrayType (&array)[ArraySize])
119 {
120  size_t arraySize = binder->ReadArrayBegin();
121  assert(arraySize == ArraySize);
122 
123  size_t index = 0;
124  for (size_t i = 0; i < arraySize; ++i)
125  {
126  binder->ReadArrayElement(index++);
127  CoherentReadInternal(binder, array[i]);
128  }
129 
130  binder->ReadArrayEnd();
131 }
132 
133 }
void CoherentReadInternal(Binder *binder, ArrayType(&array)[ArraySize])
Definition: Array.h:118
void(* Reader)(Binder *, void *, size_t)
Definition: Array.h:50
void CoherentBindInternal(Binder *binder, ArrayType(&array)[ArraySize])
Definition: Array.h:80
Contains almost all Coherent namespaces, classes and functions.
Definition: CursorTypes.h:37
void CoherentArrayElementReader(Binder *binder, void *data, size_t position)
Definition: Array.h:44
Definition: Array.h:53
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 ArrayBegin(size_t size)=0
Start exporting an array.
void CoherentArrayElementBinder(Binder *binder, void *arr, size_t index)
Definition: Array.h:73
Provides binding between C++ and the UI.
Definition: Binder.h:138
static Reader ElementReader()
Definition: Array.h:56
virtual void ReadArrayElement(size_t index)=0
Read element from an array.
static Reader ElementReader()
Definition: Array.h:66
virtual bool TryBindArrayByRef(void *arr, size_t size, ArrayElementBinder elementBinder, ArrayElementReader elementReader)=0