1 #ifndef __R_OBJECT_H
2 #define __R_OBJECT_H
3 
4 /*
5 Copyright 2011 Jared Krinke.
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 THE SOFTWARE.
24 */
25 
26 #include <lua.h>
27 
28 #include "r_defs.h"
29 #include "r_state.h"
30 
31 /* Custom wrapped type (available to scripts) support */
32 #define R_OBJECT_ID_INVALID     0
33 
34 typedef enum
35 {
36     R_OBJECT_TYPE_STRING_BUFFER = 1,
37     R_OBJECT_TYPE_COLOR,
38     R_OBJECT_TYPE_ANIMATION,
39     R_OBJECT_TYPE_ELEMENT,
40     R_OBJECT_TYPE_ELEMENT_LIST,
41     R_OBJECT_TYPE_MESH,
42     R_OBJECT_TYPE_ENTITY,
43     R_OBJECT_TYPE_ENTITY_LIST,
44     R_OBJECT_TYPE_LAYER,
45     R_OBJECT_TYPE_LAYER_STACK,
46     R_OBJECT_TYPE_FILE,
47     R_OBJECT_TYPE_IMAGE,
48     R_OBJECT_TYPE_AUDIO_CLIP,
49     R_OBJECT_TYPE_COLLISION_DETECTOR,
50     R_OBJECT_TYPE_MAX
51 } r_object_type_t;
52 
53 typedef enum
54 {
55     R_OBJECT_INIT_REQUIRED = 1,
56     R_OBJECT_INIT_OPTIONAL,
57     R_OBJECT_INIT_EXCLUDED,
58     R_OBJECT_INIT_MAX
59 } r_object_init_type_t;
60 
61 struct _r_object;
62 struct _r_object_field;
63 
64 typedef r_status_t (*r_object_field_init_function_t)(r_state_t *rs, struct _r_object *object, const struct _r_object_field *field, void *value);
65 typedef r_status_t (*r_object_field_read_function_t)(r_state_t *rs, struct _r_object *object, const struct _r_object_field *field, void *value);
66 typedef r_status_t (*r_object_field_write_function_t)(r_state_t *rs, struct _r_object *object, const struct _r_object_field *field, void *value, int value_index);
67 
68 typedef struct _r_object_field
69 {
70     const char                          *name;
71     int                                 script_type;
72     r_object_type_t                     object_ref_type;
73     int                                 offset;
74     r_boolean_t                         writeable;
75     r_object_init_type_t                init_type;
76     r_object_field_init_function_t      init;
77     r_object_field_read_function_t      read;
78     void                                *read_value_override;
79     r_object_field_write_function_t     write;
80 } r_object_field_t;
81 
82 typedef r_status_t (*r_object_init_function_t)(r_state_t *rs, struct _r_object *object);
83 typedef r_status_t (*r_object_process_arguments_function_t)(r_state_t *rs, struct _r_object *object, int argument_count);
84 typedef r_status_t (*r_object_cleanup_function_t)(r_state_t *rs, struct _r_object *object);
85 
86 typedef struct
87 {
88     r_object_type_t                         type;
89     size_t                                  size;
90     r_boolean_t                             extensible;
91     r_object_field_t                        *fields;
92     r_object_init_function_t                init;
93     r_object_process_arguments_function_t   process_arguments;
94     r_object_cleanup_function_t             cleanup;
95 } r_object_header_t;
96 
97 typedef struct _r_object
98 {
99     const r_object_header_t *header;
100     unsigned int            id;
101 } r_object_t;
102 
103 extern r_status_t r_object_field_read_unsigned_int(r_state_t *rs, r_object_t *object, const r_object_field_t *field, void *value);
104 extern r_status_t r_object_field_write_unsigned_int(r_state_t *rs, r_object_t *object, const r_object_field_t *field, void *value, int value_index);
105 extern r_status_t r_object_field_write_default(r_state_t *rs, r_object_t *object, const r_object_field_t *field, void *value, int value_index);
106 
107 extern r_status_t r_object_push_by_id(r_state_t *rs, unsigned int id);
108 extern r_status_t r_object_push(r_state_t *rs, r_object_t *object);
109 
110 extern r_status_t r_object_setup(r_state_t *rs);
111 
112 extern r_status_t r_object_push_new(r_state_t *rs, const r_object_header_t *header, int argument_count, int *result_count_out, r_object_t **object_out);
113 extern int l_Object_new(lua_State *ls, const r_object_header_t *header);
114 
115 #endif
116 
117