/xbmc/visualizations/Vortex/angelscript/docs/doxygen/source/doc_adv_template.h

http://github.com/xbmc/xbmc · C++ Header · 122 lines · 0 code · 0 blank · 122 comment · 0 complexity · 93500294ea0d655e4054f40d00a92ac7 MD5 · raw file

  1. /**
  2. \page doc_adv_template Template types
  3. A template type in AngelScript works similarly to how templates work in C++. The scripts
  4. will be able to instanciate different forms of the template type by specifying which subtype
  5. that should be used. The methods for the instance will then be adapted to this subtype, so
  6. that the correct handling of parameters and return types will be applied.
  7. The implementation of the template type is not a C++ template though, instead it must
  8. be implemented as a generic class that can determine what to do dynamically at runtime based
  9. on the subtype for which it was instanciated. This is obviously a lot less efficient than
  10. having specific implementations for each type, and for that reason AngelScript permits the
  11. application to register a template specialization where the extra performance is needed.
  12. This gives the best of both worlds, performance where the subtype is known before hand, and
  13. support for all other types that cannot be pre-determined.
  14. \section doc_adv_template_1 Registering the template type
  15. The template registration is registered similarly to normal \ref doc_reg_basicref "reference types",
  16. with a few differences. The name of the type is formed by the name of the template type plus
  17. the name of the subtype with angle brackets. The type flag asOBJ_TEMPLATE must also be used.
  18. \code
  19. // Register the template type
  20. r = engine->RegisterObjectType("myTemplate<class T>", 0, asOBJ_REF | asOBJ_GC | asOBJ_TEMPLATE); assert( r >= 0 );
  21. \endcode
  22. The template type doesn't have to be \ref doc_gc_object "garbage collected", but since you may not know
  23. which subtypes it will be instanciated for, it is usually best to implement that support.
  24. When registering the behaviours, methods, and properties for the template type the type is identified
  25. with the name and subtype within angle brackets, but without the class token, e.g. <tt>myTemplate&lt;T&gt;</tt>.
  26. The sub type is identified by just the name of the subtype as it was declared in the call to RegisterObjectType.
  27. The factory behaviour for the template type is also different. In order for the implementation to know
  28. which subtype it is instanciated for the factory receives the \ref asIObjectType of the template instance
  29. as a hidden first parameter. When registering the factory this hidden parameter is reflected in the declaration,
  30. for example as <tt>int &amp;in</tt>.
  31. \code
  32. // Register the factory behaviour
  33. r = engine->RegisterObjectBehaviour("myTemplate<T>", asBEHAVE_FACTORY, "myTemplate<T>@ f(int&in)", asFUNCTIONPR(myTemplateFactory, (asIObjectType*), myTemplate*), asCALL_CDECL); assert( r >= 0 );
  34. \endcode
  35. Remember that since the subtype must be determined dynamically at runtime, it is not possible to declare
  36. functions to receive the subtype by value, nor to return it by value. Instead you'll have to design the
  37. methods and behaviours to take the type by reference. It is possible to use object handles, but then
  38. the script engine won't be able to instanciate the template type for primitives and other values types.
  39. \see \ref doc_addon_array
  40. \section doc_adv_template_4 Validating template instantiations at compile time
  41. In order to avoid unnecessary runtime validations of invalid template instantiations, the application
  42. should preferably register the \ref asBEHAVE_TEMPLATE_CALLBACK behaviour. This is a special behaviour function
  43. that the script engine will invoke everytime a new template instance type is generated. The callback
  44. function can then perform necessary validations to verify if the type can be handled, and if not tell
  45. the engine that the instance isn't supported.
  46. The callback function must be a global function that receives an asIObjectType pointer, and should return
  47. a boolean. If the template instance is valid the return value should be true.
  48. \code
  49. // Register the template callback
  50. r = engine->RegisterObjectBehaviour("myTemplate<T>", asBEHAVE_TEMPLATE_CALLBACK, "bool f(int &in)", asFUNCTION(myTemplateCallback), asCALL_CDECL); assert( r >= 0 );
  51. \endcode
  52. Here's an example callback function:
  53. \code
  54. bool myTemplateCallback(asIObjectType *ot)
  55. {
  56. // This template will only support primitive types
  57. int typeId = ot->GetSubTypeId();
  58. if( typeId & asTYPEID_MASK_OBJECT )
  59. {
  60. // The script is attempting to instantiate the
  61. // template with an object type, this is not allowed.
  62. return false;
  63. }
  64. // Primitive types are allowed
  65. return true;
  66. }
  67. \endcode
  68. \section doc_adv_template_2 Template specializations
  69. When registering a template specialization you override the template instance that AngelScript would normally
  70. do when compiling a declaration with the template type. This allow the application to register a completely
  71. different object with its own implementation for template specializations. Obviously it is recommended that
  72. the template specialization is registered so that to the script writer it is transparent, i.e. try to avoid
  73. having different method names or behaviours for the template type and template specializations.
  74. With the exception of the type name, a template specialization is registered exactly like a \ref doc_register_type "normal type".
  75. \code
  76. // Register a template specialization for the float subtype
  77. r = engine->RegisterObjectType("myTemplate<float>", 0, asOBJ_REF); assert( r >= 0 );
  78. // Register the factory (there are no hidden parameters for specializations)
  79. r = engine->RegisterObjectBehaviour("myTemplate<float>", asBEHAVE_FACTORY, "myTemplate<float>@ f()", asFUNCTION(myTemplateFloatFactory, (), myTemplateFloat*), asCALL_CDECL); assert( r >= 0 );
  80. \endcode
  81. \section doc_adv_template_3 Current limitations
  82. - Template types are currently limited to reference types only, i.e. it must be registered
  83. with a factory and addref and release behaviours.
  84. - Only one template subtype can be used at the moment.
  85. */