summaryrefslogtreecommitdiff
path: root/ext/satellite/struct.c
blob: 9e37e5314ac8f89fc68b1618f2571ee1c1f75a60 (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
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
/*
   +----------------------------------------------------------------------+
   | PHP version 4.0                                                      |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997-2001 The PHP Group                                |
   +----------------------------------------------------------------------+
   | This source file is subject to version 2.02 of the PHP license,      |
   | that is bundled with this package in the file LICENSE, and is        |
   | available at through the world-wide-web at                           |
   | http://www.php.net/license/2_02.txt.                                 |
   | If you did not receive a copy of the PHP license and are unable to   |
   | obtain it through the world-wide-web, please send a note to          |
   | license@php.net so we can mail you a copy immediately.               |
   +----------------------------------------------------------------------+
   | Author: David Eriksson <david@2good.com>                            |
   +----------------------------------------------------------------------+
 */

/*
 * $Id$
 * vim: syntax=c tabstop=2 shiftwidth=2
 */

/* -----------------------------------------------------------------------
 * 
 * OrbitStruct class
 * 
 * There are three ways to create a structure
 *
 * (1) OrbitStruct_Constructor, for new OrbitStruct(...) in PHP
 * (2) OrbitStruct_Create, used when a CORBA method returns a struct
 * (3) OrbitStruct_Wakeup, used on "unserialization"
 * -----------------------------------------------------------------------
 */
#include "struct.h"
#include "common.h"
#include "typemanager.h"
#include "hashtable.h"

/*
 * enable/disable incomplete serialization support
 */
#define SERIALIZABLE_STRUCT		0

struct _OrbitStruct
{
	HashTable mMembers;
	zend_bool mIsException;	/* otherwise normal struct */
	union
	{
		StructType * mpStructType;
		ExceptionType * mpExceptionType;
	} u;
};

#if SERIALIZABLE_STRUCT

static void OrbitStruct_Wakeup(INTERNAL_FUNCTION_PARAMETERS);
static void OrbitStruct_Sleep(INTERNAL_FUNCTION_PARAMETERS);

static zend_function_entry OrbitStruct_class_functions[] =
{
	{"__sleep", OrbitStruct_Sleep},
	{"__wakeup", OrbitStruct_Wakeup},
	{NULL, NULL}
};

#define MY_IMPLEMENT_CLASS(name, flags)\
IMPLEMENT_DECLARATIONS(name, flags)		\
IMPLEMENT_FUNCTION_CALL(name, flags)	\
IMPLEMENT_PUT_PROPERTY(name, flags)		\
IMPLEMENT_GET_PROPERTY(name, flags)		\
IMPLEMENT_INIT_EX(name, flags, ##name##_class_functions, _##name##_FunctionCall, _##name##_GetProperty, _##name##_PutProperty)\
IMPLEMENT_DATA_HELPERS(name, flags)
	
MY_IMPLEMENT_CLASS(OrbitStruct, NO_FUNCTIONS);

#else	/* !SERIALIZABLE_STRUCT */

IMPLEMENT_CLASS(OrbitStruct, NO_FUNCTIONS);

#endif	/* SERIALIZABLE_STRUCT */

#if SERIALIZABLE_STRUCT

/*
 * prepare for serialization
 */
static void OrbitStruct_Sleep(INTERNAL_FUNCTION_PARAMETERS)
{
	/* get struct data */
	OrbitStruct * p_struct = OrbitStruct_RetrieveData(this_ptr);
	
	/* validate data */
	if (p_struct == NULL)
	{
		goto error;
	}

#if 0
	/* add property to zval */
	add_property_string(this_ptr, IOR_PROPERTY_KEY, p_ior, TRUE);
	
	/* create array */
	array_init(return_value);

	/* add name of property IOR to array */
	add_next_index_string(return_value, IOR_PROPERTY_KEY, TRUE);
#endif
	
	return;

error:
	RETURN_NULL();
}

static void OrbitStruct_Wakeup(INTERNAL_FUNCTION_PARAMETERS)
{
}

#endif	/* SERIALIZABLE_STRUCT */

char * OrbitStruct_GetRepositoryId(OrbitStruct * pStruct)
{
	if (pStruct->mIsException)
		return ExceptionType_GetRepositoryId(pStruct->u.mpExceptionType);
	else
		return StructType_GetRepositoryId(pStruct->u.mpStructType);
}

static zend_bool OrbitStruct_InitializeMembers(OrbitStruct * pStruct)
{
	MemberType * p_member = NULL;
 
	if (pStruct->mIsException)
		p_member = ExceptionType_GetFirstMember(pStruct->u.mpExceptionType);
	else
		p_member = StructType_GetFirstMember(pStruct->u.mpStructType);
	
	zend_hash_init(											
			&pStruct->mMembers,					/* hash table */
			0,													/* size */
			NULL,												/* hash function */
			ZVAL_DESTRUCTOR,						/* destructor */
			0);													/* persistent */

	if (!MemberType_IsValid(p_member))
		return TRUE;	/* no members */

	do
	{
		zval * p_value = NULL;
		char * p_name = MemberType_GetName(p_member);

		ALLOC_ZVAL(p_value);
		ZVAL_NULL(p_value);
		
		zend_hash_add(
				&pStruct->mMembers,
				p_name,
				strlen(p_name)+1,	/* important: include terminating zero byte */
				p_value,
				sizeof(zval),
				NULL
				);
	} while (MemberType_GetNext(p_member));
	
	return TRUE;
}

static zend_bool OrbitStruct_Initialize(const char * pId, OrbitStruct * pStruct)
{
	/* find type info */
	pStruct->u.mpStructType = TypeManager_FindStruct(pId);

	if (pStruct->u.mpStructType == NULL)
	{
		/* not a struct -- maybe an exception? */
		pStruct->u.mpExceptionType = TypeManager_FindException(pId);
		if (pStruct->u.mpExceptionType == NULL)
		{
			zend_error(E_WARNING, "(Satellite) unknown struct or exception '%s'", pId);
		
			goto error;	
		}

		pStruct->mIsException = TRUE;
	}

	/* initialize members */
	if (!OrbitStruct_InitializeMembers(pStruct))
		goto error;	

	return TRUE;

error:
	return FALSE;
}

/*
 * used by orbit_namedvalue_to_zval_struct(...)
 */
zend_bool OrbitStruct_Create(const char * pId, zval * pDestination)
{
	/* allocate buffer */
	OrbitStruct * p_struct = orbit_new(OrbitStruct);
	
	/* initialize struct */
	if (!OrbitStruct_Initialize(pId, p_struct))
		goto error;

	/* set zval members */
	pDestination->type = IS_OBJECT;
	pDestination->is_ref = 1;
	pDestination->refcount = 1;

	pDestination->value.obj.ce = &OrbitStruct_class_entry;
	pDestination->value.obj.properties = orbit_new(HashTable);

	zend_hash_init(
			pDestination->value.obj.properties,	/* hash table */
			0, 																	/* size */
			NULL, 															/* hash function */
			ZVAL_PTR_DTOR, 											/* destructor */
			0);																	/* persistent */

	/* save orbit data */
	OrbitStruct_SaveData(pDestination, p_struct);

	return TRUE;

error:
	orbit_delete(p_struct);
	return FALSE;
}

/*
 * Constructor
 *
 * Parameters: Repository ID of struct
 */
zend_bool  OrbitStruct_Constructor(OrbitStruct  ** ppStruct, 
		int parameterCount, const zval ** ppParameters)
{
	/* allocate buffer */
	OrbitStruct * p_struct = orbit_new(OrbitStruct);

	/* check parameter count */
	if (parameterCount != 1)
	{
		wrong_param_count();
		goto error;
	}

	/* validate parameter types */
	if (ppParameters[0]->type != IS_STRING)
		goto error;
	
	/* initialize struct */
	if (!OrbitStruct_Initialize(ppParameters[0]->value.str.val, p_struct))
		goto error;
	
	*ppStruct = p_struct;
	return TRUE;

error:
	OrbitStruct_Destructor(p_struct);
	*ppStruct = NULL;
	return FALSE;
}

zend_bool  OrbitStruct_Destructor(OrbitStruct  * pStruct)
{
	if (pStruct->mIsException)
		ExceptionType_release(pStruct->u.mpExceptionType);
	else
		StructType_release(pStruct->u.mpStructType);
	
	/* will crash on uninitialized members structure :-( */
	zend_hash_destroy(&pStruct->mMembers);
	orbit_delete(pStruct);

	return TRUE;
}

/* not used */
zend_bool  OrbitStruct_CallFunction(OrbitStruct  * pStruct, 
		const char * pFunctionName, int parameterCount, const zval ** ppParameters, zval * pResult)
{
	return FALSE;
}

zend_bool  OrbitStruct_PutProperty(OrbitStruct  * pStruct, 
		const char * pPropertyName, const zval * pValue)
{
	zend_bool result;
	
	result = orbit_store_by_key(&pStruct->mMembers, pPropertyName, pValue);

	if (!result)
	{
		zend_error(E_WARNING, "(Satellite) unknown member '%s' in struct '%s'",
				pPropertyName, OrbitStruct_GetRepositoryId(pStruct));
	}

	return result;
}

zend_bool  OrbitStruct_GetProperty(OrbitStruct  * pStruct, 
		const char * pPropertyName, zval * pReturnValue)
{
	zval * p_value = orbit_find_by_key(&pStruct->mMembers, pPropertyName);

	if (p_value == NULL)
	{
		zend_error(E_WARNING, "(Satellite) unknown member '%s' in struct '%s'",
				pPropertyName, OrbitStruct_GetRepositoryId(pStruct));

		ZVAL_NULL(pReturnValue);
		return FALSE;
	}
	else
	{
		memcpy(pReturnValue, p_value, sizeof(zval));		/* raw data copy */
		zval_copy_ctor(pReturnValue);									/* smart data copy */
		INIT_PZVAL(pReturnValue);											/* set reference count */
		return TRUE;
	}
}