summaryrefslogtreecommitdiff
path: root/core/pkg/tbox.pkg/inc/tbox/object/object.h
blob: f3853df2205534a1dc6eb2d0727471fc2f618883 (plain)
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
/*!The Treasure Box Library
 * 
 * TBox 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 2.1 of the License, or
 * (at your option) any later version.
 * 
 * TBox 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 Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with TBox; 
 * If not, see <a href="http://www.gnu.org/licenses/"> http://www.gnu.org/licenses/</a>
 * 
 * Copyright (C) 2009 - 2015, ruki All rights reserved.
 *
 * @author      ruki
 * @file        object.h
 * @defgroup    object
 *
 */
#ifndef TB_OBJECT_H
#define TB_OBJECT_H

/* //////////////////////////////////////////////////////////////////////////////////////
 * includes
 */
#include "prefix.h"
#include "null.h"
#include "data.h"
#include "date.h"
#include "array.h"
#include "string.h"
#include "number.h"
#include "boolean.h"
#include "dictionary.h"

/* //////////////////////////////////////////////////////////////////////////////////////
 * extern
 */
__tb_extern_c_enter__

/* //////////////////////////////////////////////////////////////////////////////////////
 * interfaces
 */

/*! init object context
 *
 * @return          tb_true or tb_false
 */
tb_bool_t           tb_object_context_init(tb_noarg_t);

/// exit object context
tb_void_t           tb_object_context_exit(tb_noarg_t);

/*! init object
 *
 * @param object    the object
 * @param flag      the object flag
 * @param type      the object type
 *
 * @return          tb_true or tb_false
 */
tb_bool_t           tb_object_init(tb_object_ref_t object, tb_size_t flag, tb_size_t type);

/*! decrease the object reference count, will exit it if --refn == 0
 *
 * @param object    the object
 *
 * @note the reference count must be one
 */
tb_void_t           tb_object_exit(tb_object_ref_t object);

/*! clear object
 *
 * @param object    the object
 */
tb_void_t           tb_object_clear(tb_object_ref_t object);

/*! set the object private data
 *
 * @param object    the object
 * @param priv      the private data
 *
 */
tb_void_t           tb_object_setp(tb_object_ref_t object, tb_cpointer_t priv);

/*! get the object private data
 *
 * @param object    the object
 *
 * @return          the private data
 */
tb_cpointer_t       tb_object_getp(tb_object_ref_t object);

/*! read object
 *
 * @param stream    the stream
 *
 * @return          the object
 */
tb_object_ref_t     tb_object_read(tb_stream_ref_t stream);

/*! read object from url
 *
 * @param url       the url
 *
 * @return          the object
 */
tb_object_ref_t     tb_object_read_from_url(tb_char_t const* url);

/*! read object from data
 *
 * @param data      the data
 * @param size      the size
 *
 * @return          the object
 */
tb_object_ref_t     tb_object_read_from_data(tb_byte_t const* data, tb_size_t size);

/*! writ object
 *
 * @param object    the object
 * @param stream    the stream
 * @param format    the object format
 *
 * @return          the writed size, failed: -1
 */
tb_long_t           tb_object_writ(tb_object_ref_t object, tb_stream_ref_t stream, tb_size_t format);

/*! writ object to url
 *
 * @param object    the object
 * @param url       the url
 * @param format    the format
 *
 * @return          the writed size, failed: -1
 */
tb_long_t           tb_object_writ_to_url(tb_object_ref_t object, tb_char_t const* url, tb_size_t format);

/*! writ object to data
 *
 * @param object    the object
 * @param data      the data
 * @param size      the size
 * @param format    the format
 *
 * @return          the writed size, failed: -1
 */
tb_long_t           tb_object_writ_to_data(tb_object_ref_t object, tb_byte_t* data, tb_size_t size, tb_size_t format);

/*! copy object
 *
 * @param object    the object
 *
 * @return          the object copy
 */
tb_object_ref_t     tb_object_copy(tb_object_ref_t object);

/*! the object type
 *
 * @param object    the object
 *
 * @return          the object type
 */
tb_size_t           tb_object_type(tb_object_ref_t object);

/*! the object data
 *
 * @param object    the object
 * @param format    the format
 *
 * @return          the data object
 */
tb_object_ref_t     tb_object_data(tb_object_ref_t object, tb_size_t format);

/*! seek to the object for the gived path
 *
 * <pre>
 *
    file:
    {
        "string":       "hello world!"
    ,   "com.xxx.xxx":  "hello world"
    ,   "integer":      31415926
    ,   "array":
        [
            "hello world!"
        ,   31415926
        ,   3.1415926
        ,   false
        ,   true
        ,   { "string": "hello world!" }
        ]
    ,   "macro":        "$.array[2]"
    ,   "macro2":       "$.com\\\\.xxx\\\\.xxx"
    ,   "macro3":       "$.macro"
    ,   "macro4":       "$.array"
    }

    path:
        1. ".string"               : hello world!
        2. ".array[1]"             : 31415926
        3. ".array[5].string"      : hello world!
        4. ".com\\.xxx\\.xxx"      : hello world
        5. ".macro"                : 3.1415926
        6. ".macro2"               : hello world
        7. ".macro3"               : 3.1415926
        8. ".macro4[0]"            : "hello world!"

 * 
 * </pre>
 *
 * @param object    the object
 * @param path      the object path
 * @param bmacro    enable macro(like "$.path")? 
 *
 * <code>
 * tb_object_ref_t object = tb_object_seek(root, ".array[5].string", tb_false);
 * if (object)
 * {
 *      tb_trace_d("%s", tb_object_string_cstr(object));
 * }
 * <endcode>
 *
 *
 * @return          the object
 */
tb_object_ref_t     tb_object_seek(tb_object_ref_t object, tb_char_t const* path, tb_bool_t bmacro);

/*! dump the object
 *
 * @param object    the object
 * @param format    the format, support: .xml, .xplist, .json
 *
 * @return          the object
 */
tb_object_ref_t     tb_object_dump(tb_object_ref_t object, tb_size_t format);

/*! the object reference count
 *
 * @param object    the object
 *
 * @return          the object reference count
 */
tb_size_t           tb_object_refn(tb_object_ref_t object);

/*! retain object and increase the object reference count
 *
 * @param object    the object
 */
tb_void_t           tb_object_retain(tb_object_ref_t object);

/* //////////////////////////////////////////////////////////////////////////////////////
 * extern
 */
__tb_extern_c_leave__

#endif