This repository was archived by the owner on Nov 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathqtluaarrayproxy.hh
More file actions
144 lines (115 loc) · 3.94 KB
/
qtluaarrayproxy.hh
File metadata and controls
144 lines (115 loc) · 3.94 KB
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
/*
This file is part of LibQtLua.
LibQtLua is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
LibQtLua is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with LibQtLua. If not, see <http://www.gnu.org/licenses/>.
Copyright (C) 2012, Alexandre Becoulet <alexandre.becoulet@free.fr>
*/
#ifndef QTLUAARRAYPROXY_HH_
#define QTLUAARRAYPROXY_HH_
#include <QPointer>
#include "qtluauserdata.hh"
#include "qtluaiterator.hh"
namespace QtLua {
/**
* @short C array read only access wrapper for lua script
* @header QtLua/ArrayProxy
* @module {Container proxies}
*
* This template class may be used to expose a C array to lua script
* for read access. The @ref ArrayProxy class may be used for
* read/write access.
*
* See @ref ArrayProxy class documentation for details and examples.
*/
template <class T>
class ArrayProxyRo : public UserData
{
public:
QTLUA_REFTYPE(ArrayProxyRo);
/** Create a @ref ArrayProxy object with no attached array */
ArrayProxyRo();
/** Create a @ref ArrayProxy object and attach given array */
ArrayProxyRo(const T *array, unsigned int size);
/** Attach or detach container. argument may be NULL */
void set_container(const T *array, unsigned int size);
Value meta_operation(State &ls, Value::Operation op, const Value &a, const Value &b);
Value meta_index(State &ls, const Value &key);
bool meta_contains(State &ls, const Value &key);
Ref<Iterator> new_iterator(State &ls);
bool support(Value::Operation c) const;
private:
String get_type_name() const;
/**
* @short ArrayProxyRo iterator class
* @internal
*/
class ProxyIterator : public Iterator
{
public:
QTLUA_REFTYPE(ProxyIterator);
ProxyIterator(State *ls, const Ref<ArrayProxyRo> &proxy);
private:
bool more() const;
void next();
Value get_key() const;
Value get_value() const;
ValueRef get_value_ref();
QPointer<State> _ls;
typename ArrayProxyRo::ptr _proxy;
unsigned int _it;
};
protected:
const T *_array;
unsigned int _size;
};
/**
* @short C array access wrapper for lua script
* @header QtLua/ArrayProxy
* @module {Container proxies}
*
* This template class may be used to expose a C array to lua script
* for read and write access. The @ref ArrayProxyRo class may be
* used for read only access.
*
* Arrays may be attached and detached from the wrapper object
* to solve cases where we want to destroy the array when lua
* still holds references to the wrapper object. When no container
* is attached access will raise an exception.
*
* First entry has index 1. Lua @tt nil value is returned if index
* is above array size.
*
* Lua operator @tt # returns the array size. Lua
* operator @tt - returns a lua table copy of the container.
*
* The following example show how anarray can be
* accessed from both C++ and lua script directly:
*
* @example examples/cpp/proxy/arrayproxy_string.cc:1|2|3
*/
template <class T>
class ArrayProxy : public ArrayProxyRo<T>
{
using ArrayProxyRo<T>::_array;
using ArrayProxyRo<T>::_size;
public:
QTLUA_REFTYPE(ArrayProxy);
/** Create a @ref ArrayProxy object */
ArrayProxy();
/** Create a @ref ArrayProxy object */
ArrayProxy(T *array, unsigned int size);
/** Attach or detach associated array. argument may be NULL */
void set_container(T *array, unsigned int size);
void meta_newindex(State &ls, const Value &key, const Value &value);
bool support(enum Value::Operation c);
};
}
#endif