obj.h
Go to the documentation of this file.
1 /* vim: set ts=2 expandtab: */
2 /**
3  * @file obj.h
4  * @brief Commotion object model
5  *
6  * @author Josh King (jheretic), jking@chambana.net
7  *
8  * @internal
9  * Created 11/07/2013 02:00:15 PM
10  * Compiler gcc/g++
11  * Organization The Open Technology Institute
12  * Copyright Copyright (c) 2013, Josh King
13  *
14  * This file is part of Commotion, Copyright (c) 2013, Josh King
15  *
16  * Commotion is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU Affero General Public License as
18  * published by the Free Software Foundation, either version 3 of the
19  * License, or (at your option) any later version.
20  *
21  * Commotion is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  * GNU Affero General Public License for more details.
25  *
26  * You should have received a copy of the GNU Affero General Public License
27  * along with Commotion. If not, see <http://www.gnu.org/licenses/>.
28  *
29  * =====================================================================================
30  */
31 #ifndef _OBJ_H
32 #define _OBJ_H
33 
34 #define CO_OBJ_INTERNAL
35 #include <stdlib.h>
36 #include <stddef.h>
37 #include <stdint.h>
38 #include <stdbool.h>
39 #include "debug.h"
40 #include "extern/halloc.h"
41 
42 /* Types */
43 #define _nil 0xc0
44 #define _false 0xc2
45 #define _true 0xc3
46 #define _bin8 0xc4
47 #define _bin16 0xc5
48 #define _bin32 0xc6
49 #define _ext8 0xc7
50 #define _ext16 0xc8
51 #define _ext32 0xc9
52 #define _fixext1 0xd4
53 #define _fixext2 0xd5
54 #define _fixext4 0xd6
55 #define _fixext8 0xd7
56 #define _fixext16 0xd8
57 #define _float32 0xca
58 #define _float64 0xcb
59 #define _uint8 0xcc
60 #define _uint16 0xcd
61 #define _uint32 0xce
62 #define _uint64 0xcf
63 #define _int8 0xd0
64 #define _int16 0xd1
65 #define _int32 0xd2
66 #define _int64 0xd3
67 #define _str8 0xd9
68 #define _str16 0xda
69 #define _str32 0xdb
70 #define _list16 0xdc
71 #define _list32 0xdd
72 #define _tree16 0xde
73 #define _tree32 0xdf
74 
75 /* Extension types (1-127) */
76 #define _ptr 1
77 #define _cmd 2
78 #define _plug 3
79 #define _profile 4
80 #define _cbptr 4
81 #define _fd 5
82 #define _sock 6
83 #define _co_timer 7
84 #define _process 8
85 #define _iface 9
86 
87 /* Convenience */
88 #define CO_TYPE(J) (((co_obj_t *)J)->_type)
89 
90 /* Lists */
91 #define _LIST_ELEMENT(J,I) co_obj_data_ptr(co_list_element(J,I))
92 
93 /* Type checking */
94 #define IS_NIL(J) (CO_TYPE(J) == _nil)
95 #define IS_FIXINT(J) (CO_TYPE(J) < 128)
96 #define IS_BOOL(J) (IS_FALSE(J) || IS_TRUE(J))
97 #define IS_TRUE(J) (CO_TYPE(J) == _true)
98 #define IS_FALSE(J) (CO_TYPE(J) == _false)
99 #define IS_BIN(J) ((CO_TYPE(J) == _bin8) || (CO_TYPE(J) == _bin16) || (CO_TYPE(J) == _bin32))
100 #define IS_EXT(J) ((CO_TYPE(J) == _ext8) || (CO_TYPE(J) == _ext16) || (CO_TYPE(J) == _ext32))
101 #define IS_FIXEXT(J) ((CO_TYPE(J) == _fixext1) || (CO_TYPE(J) == _fixext2) || (CO_TYPE(J) == _fixext4) || (CO_TYPE(J) == _fixext8) || (CO_TYPE(J) == _fixext16))
102 #define IS_FLOAT(J) ((CO_TYPE(J) == _float32) || (CO_TYPE(J) == _float64))
103 #define IS_UINT(J) ((CO_TYPE(J) == _uint8) || (CO_TYPE(J) == _uint16) || (CO_TYPE(J) == _uint32) || (CO_TYPE(J) == _uint64))
104 #define IS_INT(J) ((CO_TYPE(J) == _int8) || (CO_TYPE(J) == _int16) || (CO_TYPE(J) == _int32) || (CO_TYPE(J) == _int64))
105 #define IS_STR(J) ((CO_TYPE(J) == _str8) || (CO_TYPE(J) == _str16) || (CO_TYPE(J) == _str32))
106 #define IS_LIST(J) ((CO_TYPE(J) == _list16) || (CO_TYPE(J) == _list32))
107 #define IS_TREE(J) ((CO_TYPE(J) == _tree16) || (CO_TYPE(J) == _tree32))
108 #define IS_CHAR(J) (IS_BIN(J) || IS_STR(J))
109 #define IS_EXTENSION(J) (IS_EXT(J) || IS_FIXEXT(J))
110 #define IS_INTEGER(J) (IS_INT(J) || IS_UINT(J) || IS_FIXINT(J))
111 #define IS_COMPLEX(J) (IS_LIST(J) || IS_TREE(J))
112 
113 /* Extension type checking */
114 #define IS_CMD(J) (IS_EXT(J) && ((co_cmd_t *)J)->_exttype == _cmd)
115 #define IS_PLUG(J) (IS_EXT(J) && ((co_plugin_t *)J)->_exttype == _plug)
116 #define IS_PROFILE(J) (IS_EXT(J) && ((co_profile_t *)J)->_exttype == _profile)
117 #define IS_CBPTR(J) (IS_EXT(J) && ((co_cbptr_t *)J)->_exttype == _cbptr)
118 #define IS_FD(J) (IS_EXT(J) && ((co_fd_t *)J)->_exttype == _fd)
119 #define IS_SOCK(J) (IS_EXT(J) && ((co_socket_t *)J)->_exttype == _sock)
120 #define IS_TIMER(J) (IS_EXT(J) && ((co_timer_t *)J)->_exttype == _co_timer)
121 #define IS_PROCESS(J) (IS_EXT(J) && ((co_process_t *)J)->_exttype == _process)
122 #define IS_IFACE(J) (IS_EXT(J) && ((co_iface_t *)J)->_exttype == _iface)
123 
124 /*-----------------------------------------------------------------------------
125  * Object Declaration
126  *-----------------------------------------------------------------------------*/
127 typedef uint8_t _type_t;
128 
129 typedef struct co_obj_t co_obj_t;
130 
131 struct co_obj_t
132 {
133  uint8_t _flags;
134  /* For "list" types. */
135  co_obj_t *_prev;
136  co_obj_t *_next;
137  uint16_t _ref;
138  _type_t _type;
139 } __attribute__ ((packed));
140 
141 /* Function pointers */
142 typedef co_obj_t *(*co_iter_t)(co_obj_t *data, co_obj_t *current, void *context);
143 
144 typedef int (*co_cb_t)(co_obj_t *self, co_obj_t **output, co_obj_t *params);
145 
146 /*-----------------------------------------------------------------------------
147  * Character-array-types Declaration Macros
148  *-----------------------------------------------------------------------------*/
149 #define _DECLARE_CHAR(T, L) typedef struct __attribute__((packed)) { co_obj_t _header; uint##L##_t _len; \
150  char data[1]; } co_##T##L##_t; int co_##T##L##_alloc(co_obj_t *output, \
151  const size_t out_size, const char *input, const size_t in_size, \
152  const uint8_t flags ); co_obj_t *co_##T##L##_create(const char *input, \
153  const size_t input_size, const uint8_t flags);
154 
155 _DECLARE_CHAR(bin, 8);
156 _DECLARE_CHAR(bin, 16);
157 _DECLARE_CHAR(bin, 32);
158 _DECLARE_CHAR(str, 8);
159 _DECLARE_CHAR(str, 16);
160 _DECLARE_CHAR(str, 32);
161 
162 /*-----------------------------------------------------------------------------
163  * Extension-types Declaration Macros
164  *-----------------------------------------------------------------------------*/
165 /*
166 #define _DECLARE_FIXEXT(L) typedef struct { co_obj_t _header; \
167  uint8_t type; char data[L]; } co_fixext##L##_t; int co_fixext##L##_alloc(co_obj_t *output, \
168  const char *input, const size_t in_size, \
169  const uint8_t flags, const uint8_t type ); co_obj_t *co_fixext##L##_create(const char *input, \
170  const size_t input_size, const uint8_t flags, const uint8_t type);
171 
172 _DECLARE_FIXEXT(1);
173 _DECLARE_FIXEXT(2);
174 _DECLARE_FIXEXT(4);
175 _DECLARE_FIXEXT(8);
176 _DECLARE_FIXEXT(16);
177 
178 #define _DECLARE_EXT(L) typedef struct { co_obj_t _header; uint##L##_t _len; \
179  uint8_t type; char data[1]; } co_ext##L##_t; int co_ext##L##_alloc(co_obj_t *output, \
180  const size_t out_size, const char *input, const size_t in_size, \
181  const uint8_t flags, const uint8_t type ); co_obj_t *co_ext##L##_create(const char *input, \
182  const size_t input_size, const uint8_t flags, const uint8_t type);
183 
184 _DECLARE_EXT(8);
185 _DECLARE_EXT(16);
186 _DECLARE_EXT(32);
187 */
188 
189 /*-----------------------------------------------------------------------------
190  * Integer-types Declaration Macros
191  *-----------------------------------------------------------------------------*/
192 #define _DECLARE_INTEGER(T, L) typedef struct __attribute__((packed)) { co_obj_t _header; T##L##_t data; }\
193  co_##T##L##_t; int co_##T##L##_alloc(co_obj_t *output, \
194  const T##L##_t input, const uint8_t flags); co_obj_t *co_##T##L##_create(\
195  const T##L##_t input, const uint8_t flags);
196 
197 _DECLARE_INTEGER(int, 8);
198 _DECLARE_INTEGER(int, 16);
199 _DECLARE_INTEGER(int, 32);
200 _DECLARE_INTEGER(int, 64);
201 _DECLARE_INTEGER(uint, 8);
202 _DECLARE_INTEGER(uint, 16);
203 _DECLARE_INTEGER(uint, 32);
204 _DECLARE_INTEGER(uint, 64);
205 
206 /*-----------------------------------------------------------------------------
207  * Declarations of other simple types
208  *-----------------------------------------------------------------------------*/
209 
210 /* Type "Nil" declaration */
211 typedef struct
212 {
213  co_obj_t _header;
214 } co_nil_t;
215 
216 int co_nil_alloc(co_obj_t *output, const uint8_t flags);
217 co_obj_t * co_nil_create(const uint8_t flags);
218 
219 /* Type "Bool" declaration */
220 typedef struct
221 {
222  co_obj_t _header;
223 } co_bool_t;
224 
225 int co_bool_alloc(co_obj_t *output, const bool input, const uint8_t flags);
226 co_obj_t * co_bool_create(const bool input, const uint8_t flags);
227 
228 /* Type "fixint" declaration */
229 typedef struct
230 {
231  co_obj_t _header;
232 } co_fixint_t;
233 
234 int co_fixint_alloc(co_obj_t *output, const int input, const uint8_t flags);
235 co_obj_t * co_fixint_create(const int input, const uint8_t flags);
236 
237 /* Type "float32" declaration */
238 typedef struct
239 {
240  co_obj_t _header;
241  float data;
242 } co_float32_t;
243 
244 int co_float32_alloc(co_obj_t *output, const float input, const uint8_t flags);
245 co_obj_t * co_float32_create(const double input, const uint8_t flags);
246 
247 /* Type "float64" declaration */
248 typedef struct
249 {
250  co_obj_t _header;
251  double data;
252 } co_float64_t;
253 
254 int co_float64_alloc(co_obj_t *output, const double input, const uint8_t flags);
255 co_obj_t * co_float64_create(const double input, const uint8_t flags);
256 
257 
258 /*-----------------------------------------------------------------------------
259  * Deconstructors
260  *-----------------------------------------------------------------------------*/
261 void co_obj_free(co_obj_t *object);
262 
263 /*-----------------------------------------------------------------------------
264  * Accessors
265  *-----------------------------------------------------------------------------*/
266 size_t co_obj_raw(char **data, const co_obj_t *object);
267 
268 size_t co_obj_data(char **data, const co_obj_t *object);
269 
270 #define co_obj_data_ptr(J) ({ char *d = NULL; co_obj_data(&d,J); d; })
271 
272 size_t co_obj_import(co_obj_t **output, const char *input, const size_t in_size, const uint8_t flags);
273 
274 int co_obj_getflags(const co_obj_t *object);
275 
276 void co_obj_setflags(co_obj_t *object, const int flags);
277 
278 /*-----------------------------------------------------------------------------
279  * Strings
280  *-----------------------------------------------------------------------------*/
281 
282 int co_str_copy(co_obj_t *dst, const co_obj_t *src, const size_t size);
283 
284 int co_str_cat(co_obj_t *dst, const co_obj_t *src, const size_t size);
285 
286 int co_str_cmp(const co_obj_t *a, const co_obj_t *b);
287 
288 #define co_str_cmp_str(J,S) ({ co_obj_t *s = co_str8_create(S,sizeof(S),0); \
289  int i = co_str_cmp(J,s); co_obj_free(s); i; })
290 
291 #define co_str_len(J) ({ char *v = NULL; co_obj_data(&v,J); })
292 
293 #endif
debug macros for the Commotion daemon
Definition: obj.h:238
Definition: obj.h:229
Definition: obj.h:211
Definition: obj.h:131
Definition: obj.h:220
Definition: obj.h:248