summaryrefslogtreecommitdiffhomepage
path: root/Tobii EyeX/include/eyex-cpp/InteractionObject.inl
blob: 5722ce69240b97e5d9b088fa4c7b868052fcb1a9 (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
/*********************************************************************************************************************
 * Copyright 2013-2014 Tobii Technology AB. All rights reserved.
 * InteractionObject.inl
 *********************************************************************************************************************/

#if !defined(__TOBII_TX_CLIENT_CPPBINDINGS_INTERACTIONOBJECT__INL__)
#define __TOBII_TX_CLIENT_CPPBINDINGS_INTERACTIONOBJECT__INL__

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

TX_NAMESPACE_BEGIN

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

template <typename THandle>
inline InteractionObjectBase<THandle>::InteractionObjectBase(const std::shared_ptr<const Context>& spContext, THandle hObject)
: HandleWrapper(spContext, hObject)
{ }

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

template <typename THandle>
inline TX_INTERACTIONOBJECTTYPE InteractionObjectBase<THandle>::GetObjectType() const
{
	TX_INTERACTIONOBJECTTYPE objectType;
	TX_VALIDATE(txGetObjectType(_hObject, &objectType));
	return objectType;
}

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

template <typename THandle>
inline bool InteractionObjectBase<THandle>::TryGetProperty(std::shared_ptr<Property>* pspProperty, const std::string& propertyName) const
{
	TX_PROPERTYHANDLE hProperty;
	auto result = txGetProperty(_hObject, &hProperty, propertyName.c_str());
	if(result == TX_RESULT_NOTFOUND)
		return false;

	*pspProperty = _spContext->CreateProperty(hProperty);
	return true;
}

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

template <typename THandle>
inline std::shared_ptr<Property> InteractionObjectBase<THandle>::GetProperty(const std::string& propertyName) const
{
	std::shared_ptr<Property> spProperty;
	if(TryGetProperty(&spProperty, propertyName))
		return spProperty;
	
	throw APIException(TX_RESULT_NOTFOUND, "Property not found");
}

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

template <typename THandle>
inline std::vector<std::shared_ptr<Property>> InteractionObjectBase<THandle>::GetProperties() const 
{
	std::vector<TX_PROPERTYHANDLE> propertyHandles;
    TX_VALIDATE(Tx::Utils::GetBufferData(propertyHandles, txGetProperties, _hObject));
    
	std::vector<std::shared_ptr<Property>> properties;	
	for(auto& hProperty : propertyHandles)
	{
		auto spProperty = _spContext->CreateProperty(hProperty);
		properties.push_back(spProperty);
	}

	return properties;
}

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

template <typename THandle>
inline void InteractionObjectBase<THandle>::CopyPropertiesTo(const std::shared_ptr<InteractionObject>& spObject) const
{
	TX_VALIDATE(txCopyProperties(_hObject, spObject->GetHandle()));
}

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

template <typename THandle>
inline std::string InteractionObjectBase<THandle>::FormatAsText() const
{
    return GetString(txFormatObjectAsText, _hObject, 512);
}

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

template <typename THandle>
template <typename TValue>
inline typename PropertyValueResolver<TValue>::ValueType InteractionObjectBase<THandle>::GetPropertyValue(const std::string& propertyName) const
{
	auto spProperty = GetProperty(propertyName);
	return spProperty->GetValue<TValue>();
}

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

template <typename THandle>
template <typename TValue>
inline bool InteractionObjectBase<THandle>::TryGetPropertyValue(TValue* pValue, const std::string& propertyName) const
{
    std::shared_ptr<Property> spProperty;
	if(!TryGetProperty(&spProperty, propertyName))
		return false;

    if(!spProperty->TryGetValue(pValue))
        return false;

    return true;
}

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


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

inline InteractionObject::InteractionObject(const std::shared_ptr<const Context>& spContext, TX_HANDLE hObject)
: InteractionObjectBase<TX_HANDLE>(spContext, hObject)
{ }

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

inline InteractionObject::~InteractionObject()
{
	txReleaseObject(&_hObject);
}

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

inline std::shared_ptr<Property> InteractionObject::CreateProperty(const std::string& propertyName)
{
	TX_PROPERTYHANDLE hProperty;
	TX_VALIDATE(txCreateProperty(_hObject, &hProperty, propertyName.c_str()));
	auto spProperty = _spContext->CreateProperty(hProperty);
	return spProperty;
}

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

template <typename TValue>
inline void InteractionObject::SetPropertyValue(const std::string& propertyName, const TValue& value)
{
	std::shared_ptr<Property> spProperty;
	if(!TryGetProperty(&spProperty, propertyName))
		spProperty = CreateProperty(propertyName);

	spProperty->SetValue(value);
}

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

TX_NAMESPACE_END

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

#endif // !defined(__TOBII_TX_CLIENT_CPPBINDINGS_INTERACTIONOBJECT__INL__)

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