forked from livecode/livecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfoundation-handler.cpp
More file actions
120 lines (88 loc) · 3.64 KB
/
Copy pathfoundation-handler.cpp
File metadata and controls
120 lines (88 loc) · 3.64 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
/* Copyright (C) 2003-2013 Runtime Revolution Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode 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 General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include <foundation.h>
#include <foundation-auto.h>
#include "foundation-private.h"
////////////////////////////////////////////////////////////////////////////////
bool MCHandlerCreate(MCTypeInfoRef p_typeinfo, const MCHandlerCallbacks *p_callbacks, void *p_context, MCHandlerRef& r_handler)
{
// The context data for an MCHandler is stored after the common elements. The
// start of this is a field 'context' in the struct so we must adjust for its
// length.
__MCHandler *self;
if (!__MCValueCreate(kMCValueTypeCodeHandler, (sizeof(__MCHandler) - sizeof(self -> context)) + p_callbacks -> size, (__MCValue*&)self))
return false;
MCMemoryCopy(MCHandlerGetContext(self), p_context, p_callbacks -> size);
self -> typeinfo = MCValueRetain(p_typeinfo);
self -> callbacks = p_callbacks;
r_handler = self;
return true;
}
bool MCHandlerInvoke(MCHandlerRef self, MCValueRef *p_arguments, uindex_t p_argument_count, MCValueRef& r_value)
{
return self -> callbacks -> invoke(MCHandlerGetContext(self), p_arguments, p_argument_count, r_value);
}
MCErrorRef MCHandlerTryToInvokeWithList(MCHandlerRef self, MCProperListRef& x_arguments, MCValueRef& r_value)
{
MCAutoValueRefArray t_args;
MCAutoProperListRef t_out_args;
if (!t_args . New(MCProperListGetLength(x_arguments)))
goto error_exit;
for(uindex_t i = 0; i < MCProperListGetLength(x_arguments); i++)
t_args[i] = MCValueRetain(MCProperListFetchElementAtIndex(x_arguments, i));
if (!MCHandlerInvoke(self, t_args . Ptr(), t_args . Size(), r_value))
goto error_exit;
if (!t_args . TakeAsProperList(Out(t_out_args)))
goto error_exit;
MCValueAssign(x_arguments, t_out_args . Take());
return nil;
error_exit:
MCValueRelease(x_arguments);
x_arguments = nil;
r_value = nil;
MCErrorRef t_error;
if (!MCErrorCatch(t_error))
return nil;
return t_error;
}
void *MCHandlerGetContext(MCHandlerRef self)
{
return (void *)self -> context;
}
const MCHandlerCallbacks *MCHandlerGetCallbacks(MCHandlerRef self)
{
return self -> callbacks;
}
////////////////////////////////////////////////////////////////////////////////
void __MCHandlerDestroy(__MCHandler *self)
{
if (self -> callbacks -> release != nil)
self -> callbacks -> release(MCHandlerGetContext(self));
}
hash_t __MCHandlerHash(__MCHandler *self)
{
return MCHashPointer(self);
}
bool __MCHandlerIsEqualTo(__MCHandler *self, __MCHandler *other_self)
{
return self == other_self;
}
bool __MCHandlerCopyDescription(__MCHandler *self, MCStringRef& r_desc)
{
if (NULL != self->callbacks->describe)
return self->callbacks->describe(MCHandlerGetContext (self), r_desc);
/* Default implementation. */
/* FIXME Should include information about arguments and return
* values, extracted from the handler's typeinfo. */
return MCStringCopy(MCSTR("<handler>"), r_desc);
}
////////////////////////////////////////////////////////////////////////////////