2 * Copyright (c) 2012, Intel Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of Intel Corporation nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #ifndef __MURPHY_CORE_METHOD_H__
31 #define __MURPHY_CORE_METHOD_H__
33 typedef struct mrp_method_descr_s mrp_method_descr_t;
35 #include <murphy/core/plugin.h>
36 #include <murphy/core/scripting.h>
43 #define __MRP_METHOD_FIELDS(...) \
44 __VA_ARGS__ char *name; /* method name */ \
45 __VA_ARGS__ char *signature; /* method signature */ \
46 /* pointers to exported function, native and boilerplate for scripts */ \
48 int (*script_ptr)(mrp_plugin_t *plugin, const char *name, \
49 mrp_script_env_t *env); \
50 mrp_plugin_t *plugin /* exporting plugin (or NULL) */ \
52 struct mrp_method_descr_s {
53 __MRP_METHOD_FIELDS(const);
58 * convenience macros for declaring exported methods
61 #define MRP_EXPORTABLE(_return_type, _func, _arglist) \
62 static const char _func##_method_signature[] = \
63 #_return_type" __ "#_arglist; \
65 static _return_type _func _arglist
67 /** Declare a method along with a boilerplate to call from scripts. */
68 #define MRP_GENERIC_METHOD(_name, _func, _boilerplate) { \
70 .signature = _func##_method_signature, \
71 .native_ptr = _func, \
72 .script_ptr = _boilerplate, \
76 /** Declare a method that cannot be called from scripts. */
77 #define MRP_NATIVE_METHOD(_name, _func) { \
79 .signature = _func##_method_signature, \
80 .native_ptr = _func, \
85 /** Declare a method that can only be called via a boilerplate for scripts. */
86 #define MRP_SCRIPT_METHOD(_name, _boilerplate) { \
90 .script_ptr = _boilerplate, \
96 * convenience macros for declaring imported methods
99 #define MRP_IMPORTABLE(_return_type, _funcptr, _arglist) \
100 static const char _funcptr##_method_signature[] = \
101 #_return_type" __ "#_arglist; \
103 static _return_type (*_funcptr) _arglist
105 #define MRP_IMPORT_METHOD(_name, _funcptr) { \
107 .signature = _funcptr##_method_signature, \
108 .native_ptr = (void *)&_funcptr, \
111 /** Export a method for plugins and/or scripts. */
112 int mrp_export_method(mrp_method_descr_t *method);
114 /** Remove an exported method. */
115 int mrp_remove_method(mrp_method_descr_t *method);
117 /** Import an exported method. */
118 int mrp_import_method(const char *name, const char *signature,
120 int (**script_ptr)(mrp_plugin_t *plugin, const char *name,
121 mrp_script_env_t *env),
122 mrp_plugin_t **plugin);
124 /** Release an imported method. */
125 int mrp_release_method(const char *name, const char *signature,
127 int (**script_ptr)(mrp_plugin_t *plugin, const char *name,
128 mrp_script_env_t *env));
132 #endif /* __MURPHY_CORE_METHOD_H__ */