forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp3dObject.cxx
More file actions
459 lines (405 loc) · 11.5 KB
/
p3dObject.cxx
File metadata and controls
459 lines (405 loc) · 11.5 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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/**
* PANDA 3D SOFTWARE
* Copyright (c) Carnegie Mellon University. All rights reserved.
*
* All use of this software is subject to the terms of the revised BSD
* license. You should have received a copy of this license along
* with this source code in a file named "LICENSE."
*
* @file p3dObject.cxx
* @author drose
* @date 2009-06-30
*/
#include "p3dObject.h"
#include "p3dBoolObject.h"
#include "p3dIntObject.h"
#include "p3dFloatObject.h"
#include "p3dStringObject.h"
#include "p3dInstanceManager.h"
#include <string.h> // strncpy
using std::string;
// The following functions are C-style wrappers around the below P3DObject
// virtual methods; they are defined to allow us to create the C-style
// P3D_class_definition method table to store in the P3D_object structure.
static void
object_finish(P3D_object *object) {
delete (P3DObject *)object;
}
static P3D_object_type
object_get_type(P3D_object *object) {
return ((P3DObject *)object)->get_type();
}
static bool
object_get_bool(P3D_object *object) {
return ((P3DObject *)object)->get_bool();
}
static int
object_get_int(P3D_object *object) {
return ((P3DObject *)object)->get_int();
}
static double
object_get_float(P3D_object *object) {
return ((P3DObject *)object)->get_float();
}
static int
object_get_string(P3D_object *object, char *buffer, int buffer_length) {
return ((P3DObject *)object)->get_string(buffer, buffer_length);
}
static int
object_get_repr(P3D_object *object, char *buffer, int buffer_length) {
return ((P3DObject *)object)->get_repr(buffer, buffer_length);
}
static P3D_object *
object_get_property(P3D_object *object, const char *property) {
return ((P3DObject *)object)->get_property(property);
}
static bool
object_set_property(P3D_object *object, const char *property,
bool needs_response, P3D_object *value) {
return ((P3DObject *)object)->set_property(property, needs_response, value);
}
static bool
object_has_method(P3D_object *object, const char *method_name) {
return ((P3DObject *)object)->has_method(method_name);
}
static P3D_object *
object_call(P3D_object *object, const char *method_name,
bool needs_response,
P3D_object *params[], int num_params) {
if (method_name == nullptr) {
method_name = "";
}
return ((P3DObject *)object)->call(method_name, needs_response, params, num_params);
}
static P3D_object *
object_eval(P3D_object *object, const char *expression) {
return ((P3DObject *)object)->eval(expression);
}
P3D_class_definition P3DObject::_object_class = {
&object_finish,
&object_get_type,
&object_get_bool,
&object_get_int,
&object_get_float,
&object_get_string,
&object_get_repr,
&object_get_property,
&object_set_property,
&object_has_method,
&object_call,
&object_eval,
};
// The next functions are used to construct the generic P3D_class_definition
// class returned by P3D_make_class_definition(). These are pointers to no-op
// functions, which the host may or may not choose to override.
static void
generic_finish(P3D_object *object) {
// You must override finish(), though, otherwise it's a leak. The core API
// has no idea how to delete your object.
nout << "Warning! default object_finish() method does nothing; object will leak.\n";
}
static P3D_object_type
generic_get_type(P3D_object *object) {
// We assume anyone going through the trouble of subclassing this will want
// to return an object, not one of the other fundamental types.
return P3D_OT_object;
}
static bool
generic_get_bool(P3D_object *object) {
return false;
}
static int
generic_get_int(P3D_object *object) {
return 0;
}
static double
generic_get_float(P3D_object *object) {
return 0.0;
}
static int
generic_get_string(P3D_object *object, char *buffer, int buffer_length) {
return 0;
}
static int
generic_get_repr(P3D_object *object, char *buffer, int buffer_length) {
return 0;
}
static P3D_object *
generic_get_property(P3D_object *object, const char *property) {
return nullptr;
}
static bool
generic_set_property(P3D_object *object, const char *property,
bool needs_response, P3D_object *value) {
return false;
}
static bool
generic_has_method(P3D_object *object, const char *method_name) {
return false;
}
static P3D_object *
generic_call(P3D_object *object, const char *method_name,
bool needs_response, P3D_object *params[], int num_params) {
return nullptr;
}
static P3D_object *
generic_eval(P3D_object *object, const char *expression) {
return nullptr;
}
P3D_class_definition P3DObject::_generic_class = {
&generic_finish,
&generic_get_type,
&generic_get_bool,
&generic_get_int,
&generic_get_float,
&generic_get_string,
&generic_get_repr,
&generic_get_property,
&generic_set_property,
&generic_has_method,
&generic_call,
&generic_eval,
};
/**
*
*/
P3DObject::
~P3DObject() {
assert(_ref_count == 0);
}
/**
* Returns the object value coerced to an integer, if possible.
*/
int P3DObject::
get_int() {
return 0;
}
/**
* Returns the object value coerced to a floating-point value, if possible.
*/
double P3DObject::
get_float() {
return get_int();
}
/**
* Stores a string that represents the object value in the indicated buffer; a
* null character is included if there is space. Returns the number of
* characters needed in the output (which might be more than the actual number
* of characters stored if buffer_length was too small).
*/
int P3DObject::
get_string(char *buffer, int buffer_length) {
string result;
make_string(result);
strncpy(buffer, result.c_str(), buffer_length);
return (int)result.size();
}
/**
* Returns a user-friendly representation of the object, similar to
* get_string(), above.
*/
int P3DObject::
get_repr(char *buffer, int buffer_length) {
std::ostringstream strm;
output(strm);
string result = strm.str();
strncpy(buffer, result.c_str(), buffer_length);
return (int)result.size();
}
/**
* Returns the named property element in the object. The return value is a
* new-reference P3D_object, or NULL on error.
*/
P3D_object *P3DObject::
get_property(const string &property) {
return nullptr;
}
/**
* Modifies (or deletes, if value is NULL) the named property element in the
* object. Returns true on success, false on failure.
*/
bool P3DObject::
set_property(const string &property, bool needs_response, P3D_object *value) {
return false;
}
/**
* Returns true if the named method exists on this object, false otherwise.
*/
bool P3DObject::
has_method(const string &method_name) {
return false;
}
/**
* Invokes the named method on the object, passing the indicated parameters.
* If the method name is empty, invokes the object itself.
*
* If needs_response is true, the return value is a new-reference P3D_object
* on success, or NULL on failure. If needs_response is false, the return
* value is always NULL, and there is no way to determine success or failure.
*/
P3D_object *P3DObject::
call(const string &method_name, bool needs_response,
P3D_object *params[], int num_params) {
return nullptr;
}
/**
* Evaluates an arbitrary JavaScript expression. None of the P3DObject
* classes implement this.
*/
P3D_object *P3DObject::
eval(const string &expression) {
return nullptr;
}
/**
* Writes a formatted representation of the value to the indicated string.
* This is intended for developer assistance.
*/
void P3DObject::
output(std::ostream &out) {
string value;
make_string(value);
out << value;
}
/**
* If this object has a valid XML representation for the indicated session
* (that hasn't already been implemented by the generic code in P3DSession),
* this method will apply it to the indicated "value" element and return true.
* Otherwise, this method will leave the element unchanged and return false.
*/
bool P3DObject::
fill_xml(TiXmlElement *xvalue, P3DSession *session) {
return false;
}
/**
* Returns a pointer to the array of objects represented by this object, if
* any, or NULL if the object does not represent an array of objects. This
* may also return NULL for a zero-length array; use get_object_array_size()
* to differentiate.
*/
P3D_object **P3DObject::
get_object_array() {
return nullptr;
}
/**
* Returns the number of elements in the array returned by get_object_array(),
* or -1 if this object does not representan array of objects.
*/
int P3DObject::
get_object_array_size() {
return -1;
}
/**
* Returns this object, downcast to a P3DPythonObject, if it is in fact an
* object of that type; or NULL if it is not.
*/
P3DPythonObject *P3DObject::
as_python_object() {
return nullptr;
}
/**
* Returns the value of the named property, as a boolean. Returns 0 if the
* property does not exist.
*/
bool P3DObject::
get_bool_property(const string &property) {
P3D_object *result = get_property(property);
if (result == nullptr) {
return 0;
}
bool bresult = P3D_OBJECT_GET_BOOL(result);
P3D_OBJECT_DECREF(result);
return bresult;
}
/**
* Changes the value of the named property to the indicated boolean value.
*/
void P3DObject::
set_bool_property(const string &property, bool value) {
P3D_object *bvalue = new P3DBoolObject(value);
set_property(property, false, bvalue);
P3D_OBJECT_DECREF(bvalue);
}
/**
* Returns the value of the named property, as an integer. Returns 0 if the
* property does not exist.
*/
int P3DObject::
get_int_property(const string &property) {
P3D_object *result = get_property(property);
if (result == nullptr) {
return 0;
}
int iresult = P3D_OBJECT_GET_INT(result);
P3D_OBJECT_DECREF(result);
return iresult;
}
/**
* Changes the value of the named property to the indicated integer value.
*/
void P3DObject::
set_int_property(const string &property, int value) {
P3D_object *ivalue = new P3DIntObject(value);
set_property(property, false, ivalue);
P3D_OBJECT_DECREF(ivalue);
}
/**
* Returns the value of the named property, as a floating-point number.
* Returns 0.0 if the property does not exist.
*/
double P3DObject::
get_float_property(const string &property) {
P3D_object *result = get_property(property);
if (result == nullptr) {
return 0.0;
}
double fresult = P3D_OBJECT_GET_FLOAT(result);
P3D_OBJECT_DECREF(result);
return fresult;
}
/**
* Changes the value of the named property to the indicated floating-point
* value.
*/
void P3DObject::
set_float_property(const string &property, double value) {
P3D_object *fvalue = new P3DFloatObject(value);
set_property(property, false, fvalue);
P3D_OBJECT_DECREF(fvalue);
}
/**
* Returns the value of the named property, as a string. Returns empty string
* if the property does not exist.
*/
string P3DObject::
get_string_property(const string &property) {
P3D_object *result = get_property(property);
if (result == nullptr) {
return string();
}
int size = P3D_OBJECT_GET_STRING(result, nullptr, 0);
char *buffer = new char[size];
P3D_OBJECT_GET_STRING(result, buffer, size);
string sresult(buffer, size);
delete[] buffer;
P3D_OBJECT_DECREF(result);
return sresult;
}
/**
* Changes the value of the named property to the indicated string value.
*/
void P3DObject::
set_string_property(const string &property, const string &value) {
P3D_object *svalue = new P3DStringObject(value);
set_property(property, false, svalue);
P3D_OBJECT_DECREF(svalue);
}
/**
* Changes the value of the named property to the undefined value.
*/
void P3DObject::
set_undefined_property(const string &property) {
P3DInstanceManager *inst_mgr = P3DInstanceManager::get_global_ptr();
P3D_object *uvalue = inst_mgr->new_undefined_object();
set_property(property, false, uvalue);
P3D_OBJECT_DECREF(uvalue);
}