summaryrefslogtreecommitdiffstats
path: root/squirrel_3_0_1_stable/sqrat/sqrat/sqratArray.h
blob: 67bf4f4b60fb4193155d2db3d18d72d8766ae6b1 (plain) (blame)
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

//
// SqratArray: Array Binding
//

//
// Copyright 2011 Alston Chen
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
//  1. The origin of this software must not be misrepresented; you must not
//  claim that you wrote the original software. If you use this software
//  in a product, an acknowledgment in the product documentation would be
//  appreciated but is not required.
//
//  2. Altered source versions must be plainly marked as such, and must not be
//  misrepresented as being the original software.
//
//  3. This notice may not be removed or altered from any source
//  distribution.
//


#if !defined(_SCRAT_ARRAY_H_)
#define _SCRAT_ARRAY_H_

#include <squirrel.h>
#include <string.h>

#include "sqratObject.h"
#include "sqratFunction.h"
#include "sqratGlobalMethods.h"

namespace Sqrat {

    class ArrayBase : public Object {
    public:
        ArrayBase(HSQUIRRELVM v = DefaultVM::Get()) : Object(v, true) {
        }

        ArrayBase(const Object& obj) : Object(obj) {
        }

        // Bind a Table or Class to the Array (Can be used to facilitate Namespaces)
        // Note: Bind cannot be called "inline" like other functions because it introduces order-of-initialization bugs.
        void Bind(const SQInteger index, Object& obj) {
            sq_pushobject(vm, GetObject());
            sq_pushinteger(vm, index);
            sq_pushobject(vm, obj.GetObject());
            sq_set(vm, -3);
            sq_pop(vm,1); // pop array
        }
        
        // Bind a raw Squirrel closure to the Array
        ArrayBase& SquirrelFunc(const SQInteger index, SQFUNCTION func) {
            sq_pushobject(vm, GetObject());
            sq_pushinteger(vm, index);
            sq_newclosure(vm, func, 0);
            sq_set(vm, -3);
            sq_pop(vm,1); // pop array
            return *this;
        }

        //
        // Variable Binding
        //
        
        template<class V>
        ArrayBase& SetValue(const SQInteger index, const V& val) {
            sq_pushobject(vm, GetObject());
            sq_pushinteger(vm, index);
            PushVar(vm, val);
            sq_set(vm, -3);
            sq_pop(vm,1); // pop array
            return *this;
        }

        template<class V>
        ArrayBase& SetInstance(const SQInteger index, V* val) {
            BindInstance<V>(index, false);
            return *this;
        }

        template<class F>
        ArrayBase& Func(const SQInteger index, F method) {
            BindFunc(index, &method, sizeof(method), SqGlobalFunc(method));
            return *this;
        }

        //template<class F>
        //ArrayBase& Overload(const SQChar* name, F method) {
        //    BindOverload(name, &method, sizeof(method), SqGlobalFunc(method), SqOverloadFunc(method), SqGetArgCount(method));
        //    return *this;
        //}

        //
        // Function Calls
        //

        Function GetFunction(const SQInteger index) {
            HSQOBJECT funcObj;
            sq_pushobject(vm, GetObject());
            sq_pushinteger(vm, index);
            if(SQ_FAILED(sq_get(vm, -2))) {
                sq_pushnull(vm);
            }
            sq_getstackobj(vm, -1, &funcObj);
            Function ret(vm, GetObject(), funcObj);
            sq_pop(vm, 2);

            return ret;
        }

        //
        // Array manipulation
        //

        template<class V>
        ArrayBase& Append(const V& val) {
            sq_pushobject(vm, GetObject());
            PushVar(vm, val);
            sq_arrayappend(vm, -2);
            sq_pop(vm,1); // pop array
            return *this;
        }

        template<class V>
        ArrayBase& Append(V* val) {
            sq_pushobject(vm, GetObject());
            PushVar(vm, val);
            sq_arrayappend(vm, -2);
            sq_pop(vm,1); // pop array
            return *this;
        }

        template<class V>
        ArrayBase& Insert(const SQInteger destpos, const V& val) {
            sq_pushobject(vm, GetObject());
            PushVar(vm, val);
            sq_arrayinsert(vm, -2, destpos);
            sq_pop(vm,1); // pop array
            return *this;
        }

        template<class V>
        ArrayBase& Insert(const SQInteger destpos, V* val) {
            sq_pushobject(vm, GetObject());
            PushVar(vm, val);
            sq_arrayinsert(vm, -2, destpos);
            sq_pop(vm,1); // pop array
            return *this;
        }

        Object Pop() {
            HSQOBJECT slotObj;
            sq_pushobject(vm, GetObject());
            if(SQ_FAILED(sq_arraypop(vm, -1, true))) {
                sq_pop(vm, 1);
                return Object(); // Return a NULL object
            } else {
                sq_getstackobj(vm, -1, &slotObj);
                Object ret(slotObj, vm);
                sq_pop(vm, 2);
                return ret;
            }
        }

        ArrayBase& Remove(const SQInteger itemidx) {
            sq_pushobject(vm, GetObject());
            sq_arrayremove(vm, -1, itemidx);
            sq_pop(vm,1); // pop array
            return *this;
        }

        ArrayBase& Resize(const SQInteger newsize) {
            sq_pushobject(vm, GetObject());
            sq_arrayresize(vm, -1, newsize);
            sq_pop(vm,1); // pop array
            return *this;
        }

        ArrayBase& Reverse() {
            sq_pushobject(vm, GetObject());
            sq_arrayreverse(vm, -1);
            sq_pop(vm,1); // pop array
            return *this;
        }
    };

    class Array : public ArrayBase {
    public:
        Array(HSQUIRRELVM v = DefaultVM::Get(), const SQInteger size = 0) : ArrayBase(v) {
            sq_newarray(vm, size);
            sq_getstackobj(vm,-1,&obj);
            sq_addref(vm, &obj);
            sq_pop(vm,1);
        }

        Array(const Object& obj) : ArrayBase(obj) {
        }
    };
}

#endif