summaryrefslogtreecommitdiffhomepage
path: root/Tobii-EyeX/include/eyex/EyeXUtils.h
blob: 7adedff53cbac90f39f0fd50bf84a22e77da9485 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*********************************************************************************************************************
* Copyright 2013-2014 Tobii Technology AB. All rights reserved.
* EyeXUtils.h
*********************************************************************************************************************/

#if !defined(__TOBII_TX_UTILS__H__)
#define __TOBII_TX_UTILS__H__

/*********************************************************************************************************************/

#if defined(__cplusplus)

#include <vector>
#include <string>
#include <functional>

namespace Tx
{    
    namespace Utils
    {
		template <typename THandle>
        class ScopedHandleTemplate
        {
        public:
            ScopedHandleTemplate(THandle handle = TX_EMPTY_HANDLE)
                : _handle(handle)
            {}

            ~ScopedHandleTemplate()
            {
                SafeRelease();
            }

            THandle* operator &() 
            {
                SafeRelease();
                return &_handle;
            }            

            operator const THandle () const
            {
                return _handle;
            }

            THandle Detach()
            {
                auto handle = _handle;
                _handle = TX_EMPTY_HANDLE;
                return handle;
            }

            bool IsAttached() const
            {
                return _handle != TX_EMPTY_HANDLE;
            }
				
        private:
            void SafeRelease()
            {
                if(!IsAttached())
                    return;

                txReleaseObject(&_handle);
                Detach();
            }

        private:
            THandle _handle;
        };

		typedef ScopedHandleTemplate<TX_HANDLE> ScopedHandle;

        template <typename TElement>
        inline TX_RESULT GetBufferData(std::vector<TElement>& targetBuffer, std::function<TX_RESULT (TElement*, TX_SIZE*)> fnGetBuf)
        {
		    TX_RESULT resultCode;
		    TX_SIZE size;

		    if(targetBuffer.empty())
		    {
			    size = 0;
			    resultCode = fnGetBuf(nullptr, &size);

			    if(resultCode == TX_RESULT_OK && size == 0)
				    return resultCode;
		    }
		    else
		    {
			    size = (TX_SIZE)targetBuffer.size();
			    resultCode = fnGetBuf((TElement*)&targetBuffer[0], &size);

			    if(resultCode == TX_RESULT_OK)
				    return resultCode;
		    }
		        
            targetBuffer.resize(size);
            resultCode = fnGetBuf((TElement*)&targetBuffer[0], &size);
        
            return resultCode;
	    }

        template <typename TElement1, typename TElement2, typename THandle1, typename THandle2>
        inline TX_RESULT GetBufferData(std::vector<TElement1>& targetBuffer, TX_RESULT (*pFn)(THandle1, TElement2*, TX_SIZE*), THandle2 handle)
        {
            std::function<TX_RESULT (TElement1*, TX_SIZE*)> fnGetBuf = [handle, pFn](TElement1* pBuf, TX_SIZE* pSize) 
            {
                return pFn(handle, (TElement2*)pBuf, pSize); 
            };

            return GetBufferData<TElement1>(targetBuffer, fnGetBuf);
        }
    
        inline TX_RESULT GetString(std::string* pTargetString, std::function<TX_RESULT (TX_STRING, TX_SIZE*)> fnGetString, TX_SIZE estimatedLength = 0)
        {
            std::vector<TX_CHAR> buf(estimatedLength);
            auto result = GetBufferData(buf, fnGetString);
            if(result != TX_RESULT_OK)
                return result;

            *pTargetString = &buf[0];
            return TX_RESULT_OK;
        }

        template <typename THandle1, typename THandle2>
        inline TX_RESULT GetString(std::string* pTargetString, TX_RESULT (*pFn)(THandle1, TX_STRING, TX_SIZE*), THandle2 handle, TX_SIZE estimatedLength = 0)
        {
            auto fnGetString = [handle, pFn](TX_STRING pStr, TX_SIZE* pSize)
            {
                return pFn(handle, pStr, pSize); 
            };

            return GetString(pTargetString, fnGetString);
        }        
    }   
}

#endif /* defined(__cplusplus) */

#if !defined(TX_NODEBUGOBJECT)

static const char* __txDbgObject(TX_CONSTHANDLE hObject)
{
    static char buf[65536];
    TX_SIZE bufSize;

    bufSize = sizeof(buf);
    txFormatObjectAsText(hObject, buf, &bufSize);
    return buf;
}

typedef const char* (*TX_DEBUGOBJECT)(TX_CONSTHANDLE hObject);
static TX_DEBUGOBJECT txDebugObject = __txDbgObject;

#endif /* !defined(TX_NODEBUGOBJECT) */

#if defined(__cplusplus)

#if !defined(TX_NODEBUGOBJECT)

// This variation is needed to make ScopedHandles debuggable in Visual Studios watch window
static const char* __txDbgScoped(const Tx::Utils::ScopedHandle& hObject)
{
    return txDebugObject(hObject);
}

typedef const char* (*TX_DEBUGSCOPEDHANDLE)(const Tx::Utils::ScopedHandle& hObject);
static TX_DEBUGSCOPEDHANDLE txDebugScopedHandle = __txDbgScoped;

#endif /* !defined(TX_NODEBUGOBJECT) */


/*********************************************************************************************************************/

/**
  Tx::CommitSnapshotAsync. This is a functional-enabled version of txCommitSnapshotAsync defined in EyeXSnapshot.h
 */

/*********************************************************************************************************************/


TX_API_FUNCTION_CPP(CommitSnapshotAsync, (
	TX_HANDLE hSnapshot,
	const Tx::AsyncDataCallback& completionHandler
	));

/*********************************************************************************************************************/

/**
  Tx::GetStateAsync. This is a functional-enabled version of txGetStateAsync defined in EyeXStates.h
 */

/*********************************************************************************************************************/

TX_API_FUNCTION_CPP(GetStateAsync,(
    TX_CONTEXTHANDLE hContext,
    TX_CONSTSTRING statePath,
    const Tx::AsyncDataCallback& completionHandler));

/*********************************************************************************************************************/

/**
  Tx::SetStateAsync. This is a functional-enabled version of txESetStateAsync defined in EyeXStates.h
 */

/*********************************************************************************************************************/

TX_API_FUNCTION_CPP(SetStateAsync, (
    TX_HANDLE hStateBag,        
    const Tx::AsyncDataCallback& completionHandler));

/*********************************************************************************************************************/

/**
  Tx::RegisterStateChangedHandler. This is a functional-enabled version of txRegisterStateChangedHandler defined in EyeXStates.h
 */

/*********************************************************************************************************************/

TX_API_FUNCTION_CPP(RegisterStateChangedHandler, (
    TX_CONTEXTHANDLE hContext,
    TX_TICKET* pTicket,
    TX_CONSTSTRING statePath,
    const Tx::AsyncDataCallback& handler));

/*********************************************************************************************************************/

/**
  Tx::ExecuteCommandAsync. This is a functional-enabled version of txExecuteCommandAsync defined in EyeXCommand.h
 */

/*********************************************************************************************************************/

TX_API_FUNCTION_CPP(ExecuteCommandAsync, (
	TX_HANDLE hCommand, 
	const Tx::AsyncDataCallback& completionHandler
	));

/*********************************************************************************************************************/

/**
  Tx::RegisterMessageHandler. This is a functional-enabled version of txRegisterMessageHandler defined in EyeXContext.h
 */

/*********************************************************************************************************************/

TX_API_FUNCTION_CPP(RegisterMessageHandler,(
    TX_CONTEXTHANDLE hContext,
    TX_TICKET* pTicket,
    TX_MESSAGETYPE messageType,
    TX_HANDLE hOptions,
    const Tx::AsyncDataCallback& completionHandler));

/*********************************************************************************************************************/

/**
  Tx::DisableBuiltinKeys. This is a functional-enabled version of txDisableBuiltinKeys defined in EyeXActions.h
 */

/*********************************************************************************************************************/

TX_API_FUNCTION_CPP(DisableBuiltinKeys,(
    TX_CONTEXTHANDLE hContext,
    TX_CONSTSTRING windowId,
    const Tx::AsyncDataCallback& completionHandler));

/*********************************************************************************************************************/

/**
  Tx::EnableBuiltinKeys. This is a functional-enabled version of txEnableBuiltinKeys defined in EyeXActions.h
 */

/*********************************************************************************************************************/

TX_API_FUNCTION_CPP(EnableBuiltinKeys,(
    TX_CONTEXTHANDLE hContext,
    TX_CONSTSTRING windowId,    
    const Tx::AsyncDataCallback& completionHandler));

/*********************************************************************************************************************/

/**
  Tx::LaunchConfigurationTool. This is a functional-enabled version of txLaunchConfigurationTool defined in EyeXActions.h
*/

/*********************************************************************************************************************/

TX_API_FUNCTION_CPP(LaunchConfigurationTool, (
	TX_CONTEXTHANDLE hContext,
	TX_CONFIGURATIONTOOL configurationTool,
	const Tx::AsyncDataCallback& completionHandler));

/*********************************************************************************************************************/

/**
  Tx::SetCurrentProfile. This is a functional-enabled version of txSetCurrentProfile defined in EyeXActions.h
*/

/*********************************************************************************************************************/

TX_API_FUNCTION_CPP(SetCurrentProfile, (
	TX_CONTEXTHANDLE hContext,
	TX_CONSTSTRING profileName,
	const Tx::AsyncDataCallback& completionHandler));

/*********************************************************************************************************************/

/**
  Tx::DeleteProfile. This is a functional-enabled version of txDeleteProfile defined in EyeXActions.h
*/

TX_API_FUNCTION_CPP(DeleteProfile, (
	TX_CONTEXTHANDLE hContext,
	TX_CONSTSTRING profileName,
	const Tx::AsyncDataCallback& completionHandler));

/*********************************************************************************************************************/

/**
* Tx::GetStateValueAsArrayOfUnicodeStrings. This is a utility function to get state values as an array of strings. 

  \Since 1.3.0

  @param hStateBag [in]:
    A TX_CONSTHANDLE to the state bag from which the value should be retrieved.
    Must not be TX_EMPTY_HANDLE.
    
  @param valuePath [in]:
    The path to the value.

  @param valuePath [out]:
    A vector of strings to which the string values will be copied.

  @return
    TX_RESULT_OK: The state value was successfully retrieved.
    TX_RESULT_EYEXNOTINITIALIZED: The EyeX client environment is not initialized.
    TX_RESULT_INVALIDARGUMENT: An invalid argument was passed to the function.
    TX_RESULT_NOTFOUND: The value was not found.
    TX_RESULT_INVALIDPROPERTYTYPE: The value type was not TX_PROPERTYVALUETYPE_STRING or array of TX_PROPERTYVALUETYPE_STRING.
*/
TX_API_FUNCTION_CPP(GetStateValueAsArrayOfStrings, (
    TX_CONSTHANDLE hStateBag,
    TX_CONSTSTRING valuePath,
    TX_REF_PARAM(std::vector<std::string>) arrayOfStrings));

/*********************************************************************************************************************/

#endif /* defined(__cplusplus) */

#endif /* !defined(__TOBII_TX_UTILS__H__) */

/*********************************************************************************************************************/