Don't dispatch virtual methods for builtin classes (reduces overhead).
Annotate for file src/libsmoke/smoke.cpp
2009-04-17 tobias 1 #include "cl_smoke.h"
2010-01-10 tobias 2 #include "smokebinding.h"
2009-04-05 tobias 3
15:36:29 ' 4 #include <smoke.h>
' 5 #include <QtGlobal>
' 6
' 7 /** @file
2009-04-17 tobias 8 * @brief C wrapper the Smoke bindings.
2009-04-05 tobias 9 */
15:36:29 ' 10
' 11 using namespace cl_smoke;
' 12
' 13 extern "C" {
' 14
2010-02-19 tobias 15 /** Returns the Smoke module of a Smoke binding.
21:10:24 ' 16 * @related cl_smoke::Binding
' 17 * @param binding the Binding
' 18 *
' 19 * @return the Smoke module
' 20 */
' 21 CL_SMOKE_EXPORT void*
' 22 cl_smoke_get_smoke(smoke_binding binding)
' 23 {
2010-02-18 tobias 24 return static_cast<NoDispatchBinding*>(binding)->get_smoke();
2010-02-19 tobias 25 }
21:10:24 ' 26
2009-04-05 tobias 27 /** Creates a new Smoke binding.
15:36:29 ' 28 * The binding is allocated on the heap an can be freed with smoke_destruct().
2010-02-18 tobias 29 * When method dispatching is not needed, a null pointer can be passed for @a dispatch.
2009-04-05 tobias 30 * @related cl_smoke::Binding
2010-02-18 tobias 31 * @related cl_smoke::NoDispatchBinding
19:57:00 ' 32 * @related cl_smoke_destruct_binding
2009-04-05 tobias 33 * @param smoke pointer to a Smoke module instance
15:36:29 ' 34 * @param destruct callback for object destruction
' 35 * @param dispatch method dispatch callback
' 36 *
' 37 * @return a pointer to a new Smoke binding.
' 38 */
2009-05-11 tobias 39 CL_SMOKE_EXPORT smoke_binding
2010-02-18 tobias 40 cl_smoke_construct_binding(void* smoke, void* destruct, void* dispatch)
2009-04-05 tobias 41 {
2010-02-18 tobias 42 if (NULL == dispatch)
19:57:00 ' 43 return new NoDispatchBinding(static_cast<Smoke*>(smoke),
' 44 reinterpret_cast<NoDispatchBinding::destructed>(destruct));
' 45 else
' 46 return new Binding(static_cast<Smoke*>(smoke),
' 47 reinterpret_cast<NoDispatchBinding::destructed>(destruct),
' 48 reinterpret_cast<Binding::dispatch_method>(dispatch));
2009-04-05 tobias 49 }
15:36:29 ' 50
2010-02-18 tobias 51 /** Deletes the Smoke binding.
19:57:00 ' 52 * @related cl_smoke_construct_binding
2009-04-05 tobias 53 */
2009-05-11 tobias 54 CL_SMOKE_EXPORT void
2010-02-18 tobias 55 cl_smoke_destruct_binding(smoke_binding binding)
2009-04-05 tobias 56 {
2010-02-18 tobias 57 // Destructor is virtual; thus we can do this.
19:57:00 ' 58 delete static_cast<SmokeBinding*>(binding);
2009-04-05 tobias 59 }
15:36:29 ' 60
2010-02-18 tobias 61 /** Gets a Smoke module name.
2009-04-05 tobias 62 * @param smoke the Smoke module
15:36:29 ' 63 *
' 64 * @return the module name
' 65 */
2009-05-11 tobias 66 CL_SMOKE_EXPORT const char*
2010-01-10 tobias 67 cl_smoke_get_module_name(void* smoke)
2009-04-05 tobias 68 {
15:36:29 ' 69 return get_smoke(smoke)->moduleName();
' 70 }
' 71
2009-06-22 tobias 72
12:18:08 ' 73 /** Returns the pointer to the array @a array of @a smoke.
' 74 * @param smoke the Smoke module
' 75 * @param array the array type
' 76 *
' 77 * @return a pointer to the array
' 78 */
' 79 CL_SMOKE_EXPORT void*
' 80 cl_smoke_array(void* smoke, cl_smoke_module_array array)
' 81 {
' 82 switch (array)
' 83 {
' 84 case classes:
' 85 return get_smoke(smoke)->classes;
' 86 case methods:
' 87 return get_smoke(smoke)->methods;
' 88 case method_maps:
' 89 return get_smoke(smoke)->methodMaps;
' 90 case method_names:
' 91 return get_smoke(smoke)->methodNames;
' 92 case types:
' 93 return get_smoke(smoke)->types;
' 94 case inheritance_list:
' 95 return get_smoke(smoke)->inheritanceList;
' 96 case argument_list:
' 97 return get_smoke(smoke)->argumentList;
' 98 case ambiguous_method_list:
' 99 return get_smoke(smoke)->ambiguousMethodList;
' 100 }
' 101 qFatal("cl_smoke_array(): Unknown smoke_array %d", array);
' 102 }
' 103
' 104 /** Returns the size of the array @a array of @a smoke.
' 105 * The size if inclusive the bound.
' 106 * @param smoke the Smoke module
' 107 * @param array the array type
' 108 *
' 109 * @return the size
' 110 */
' 111 CL_SMOKE_EXPORT Smoke::Index
' 112 cl_smoke_array_size(void* smoke, cl_smoke_module_array array)
' 113 {
' 114 switch (array)
' 115 {
' 116 case classes:
' 117 return get_smoke(smoke)->numClasses;
' 118 case methods:
' 119 return get_smoke(smoke)->numMethods;
' 120 case method_maps:
' 121 return get_smoke(smoke)->numMethodMaps;
' 122 case method_names:
' 123 return get_smoke(smoke)->numMethodNames;
' 124 case types:
' 125 return get_smoke(smoke)->numTypes;
' 126 case inheritance_list:
' 127 case argument_list:
' 128 case ambiguous_method_list:
' 129 qFatal("cl_smoke_array_size(): size of %d not known.", array);
' 130 }
' 131 qFatal("cl_smoke_array_size(): Unknown smoke_array %d.", array);
' 132 }
' 133
2009-04-05 tobias 134 ///////////////////////////
15:36:29 ' 135 /// Class
' 136 ///////////////////////////
' 137
' 138 /** Finds a class.
' 139 * @param c pointer to write the result to
' 140 * @param name the name of the class
' 141 */
2009-05-11 tobias 142 CL_SMOKE_EXPORT void
2010-01-17 tobias 143 cl_smoke_find_class(Smoke::ModuleIndex* c, const char* name)
2009-04-05 tobias 144 {
2010-01-17 tobias 145 *c = Smoke::findClass(name);
2009-04-05 tobias 146 }
15:36:29 ' 147
' 148 /** Gets the class ID for a Smoke module.
' 149 * @param smoke the Smoke module
' 150 * @param name the class name
' 151 *
' 152 * @return the class ID in the supplied Smoke module
' 153 */
2009-05-11 tobias 154 CL_SMOKE_EXPORT Smoke::Index
2010-01-10 tobias 155 cl_smoke_class_id(void* smoke, const char* name)
2009-04-05 tobias 156 {
15:36:29 ' 157 Smoke::ModuleIndex m = get_smoke(smoke)->idClass(name, true);
' 158 Q_ASSERT(m.smoke == smoke);
' 159
' 160 return m.index;
' 161 }
' 162
' 163 /** Gets a class
' 164 * @param smoke the smoke binding
' 165 * @param class_index the index of the class
' 166 *
' 167 * @return A pointer to the class into the array of class structs
' 168 */
2009-05-11 tobias 169 CL_SMOKE_EXPORT const struct Smoke::Class*
2010-01-10 tobias 170 cl_smoke_get_class(void* smoke, Smoke::Index class_index)
2009-04-05 tobias 171 {
2009-06-22 tobias 172 Q_ASSERT(class_index >= 0 && class_index <= get_smoke(smoke)->numClasses);
2009-04-05 tobias 173 return &get_smoke(smoke)->classes[class_index];
15:36:29 ' 174 }
' 175
' 176 /** Determines werter a class is from a base class.
' 177 * @param smoke the Smoke module of @a class_index
' 178 * @param class_index the class index
' 179 * @param smoke_base the Smoke module of the base class @a base_index
' 180 * @param base_index the index of the base class
' 181 *
' 182 * @return Returns 0 when the class is not derived from the base class and nonzero value otherwise.
' 183 */
2009-05-11 tobias 184 CL_SMOKE_EXPORT int
2010-01-10 tobias 185 cl_smoke_is_derived_from(void* smoke, Smoke::Index class_index, void* smoke_base,
08:49:36 ' 186 Smoke::Index base_index)
2009-04-05 tobias 187 {
2010-02-03 tobias 188 Q_ASSERT(!cl_smoke_get_class(smoke, class_index)->external);
16:20:56 ' 189 Q_ASSERT(!cl_smoke_get_class(smoke_base, base_index)->external);
2009-04-05 tobias 190
2010-01-17 tobias 191 return Smoke::isDerivedFrom(get_smoke(smoke), class_index,
2010-01-10 tobias 192 get_smoke(smoke_base), base_index);
2009-04-05 tobias 193 }
15:36:29 ' 194
' 195 //////////////////////////////
' 196 /// Method
' 197 //////////////////////////////
' 198
' 199 /** Finds a method of a class.
' 200 * @param m pointer to write the result to
2010-02-19 tobias 201 * @param smoke the smoke binding
2009-04-05 tobias 202 * @param class_index index of the class
15:36:29 ' 203 * @param method_name method name
' 204 */
2009-05-11 tobias 205 CL_SMOKE_EXPORT void
2010-01-10 tobias 206 cl_smoke_find_method(Smoke::ModuleIndex* m, void* smoke,
2009-04-05 tobias 207 Smoke::Index class_index, const char* method_name)
15:36:29 ' 208 {
2010-02-18 tobias 209 Q_ASSERT(class_index >= 0 && class_index <= get_smoke(smoke)->numClasses);
19:57:00 ' 210
' 211 const char* class_name = get_smoke(smoke)->className(class_index);
' 212 *m = get_smoke(smoke)->findMethod(class_name, method_name);
' 213
2009-04-05 tobias 214 if(m->index > 0)
2009-06-22 tobias 215 m->index = m->smoke->methodMaps[m->index].method;
2009-04-05 tobias 216 }
15:36:29 ' 217
' 218 ///////////////////////////
' 219 /// Type
' 220 //////////////////////////
' 221
' 222 /** Gets the index of a type.
' 223 * @param smoke the Smoke module
' 224 * @param name the types name
' 225 *
' 226 * @return the index of the type
' 227 */
2009-05-11 tobias 228 CL_SMOKE_EXPORT Smoke::Index
2010-01-10 tobias 229 cl_smoke_find_type(void* smoke, const char* name)
2009-04-05 tobias 230 {
15:36:29 ' 231 return get_smoke(smoke)->idType(name);
' 232 }
' 233
' 234 /** Casts an object.
' 235 * @param smoke the Smoke module
2010-02-18 tobias 236 * @param object the object
2009-04-05 tobias 237 * @param from the class index of @a object
15:36:29 ' 238 * @param to the class index to cast to
' 239 *
' 240 * @return the casted object
' 241 */
2009-05-11 tobias 242 CL_SMOKE_EXPORT void*
2010-01-10 tobias 243 cl_smoke_cast(void* smoke, void* object, Smoke::Index from, Smoke::Index to)
2009-04-05 tobias 244 {
2009-06-22 tobias 245 Q_ASSERT(from > 0 && from <= get_smoke(smoke)->numClasses);
12:18:08 ' 246 Q_ASSERT(to > 0 && to <= get_smoke(smoke)->numClasses);
2009-04-05 tobias 247
15:36:29 ' 248 return get_smoke(smoke)->cast(object, from, to);
' 249 }
' 250
' 251 } // extern "C"