#### /xbmc/visualizations/Vortex/angelscript/docs/doxygen/source/doc_as_vs_cpp_types.h

http://github.com/xbmc/xbmc
C++ Header | 131 lines | 0 code | 0 blank | 131 comment | 0 complexity | fe950b68c78b6f4dbc8f84c3d9d487e5 MD5 | raw file
  1/**
2
3\page doc_as_vs_cpp_types Datatypes in AngelScript and C++
4
5
6\section doc_as_vs_cpp_types_1 Primitives
7
8Primitives in AngelScript have direct matches in C++.
9
11<tr><td width=100><b>AngelScript</b></td><td width=150><b>C++</b></td><td width=100><b>Size (bits)</b></tr>
12<tr><td>void  </td><td>void              </td><td>0     </td></tr>
13<tr><td>int8  </td><td>signed char       </td><td>8     </td></tr>
14<tr><td>int16 </td><td>signed short      </td><td>16    </td></tr>
15<tr><td>int   </td><td>signed long (*)   </td><td>32    </td></tr>
16<tr><td>int64 </td><td>signed long long  </td><td>64    </td></tr>
17<tr><td>uint8 </td><td>unsigned char     </td><td>8     </td></tr>
18<tr><td>uint16</td><td>unsigned short    </td><td>16    </td></tr>
19<tr><td>uint  </td><td>unsigned long (*) </td><td>32    </td></tr>
20<tr><td>uint64</td><td>unsigned long long</td><td>64    </td></tr>
21<tr><td>float </td><td>float             </td><td>32    </td></tr>
22<tr><td>double</td><td>double            </td><td>64    </td></tr>
23<tr><td>bool  </td><td>bool              </td><td>8 (**)</td></tr>
24</table>
25
26
27%*) If the target CPU is 32 bit, the C++ type long is 32 bit, but on 64 bit CPUs
28   it is commonly 64 bit, but in AngelScript an int is always 32 bits.
29
30%**) On 32 bit PowerPC platforms the bool type commonly have the size of 32 bit,
31   when compiled on such platforms AngelScript also uses 32 bits for the bool type
32
33\section doc_as_vs_cpp_types_5 Strings
34
35AngelScript expects the application to register its own \ref doc_strings "string type",
36so the string types should match perfectly.
37
38The char* string type that is so convenient in C++ is however very difficult to use
39in a scripted environment where you do not have full control over how it is used. For that
40reason it is recommended that you wrap any functions that use the char* string type so
41that the string is properly converted to an object that can be safely handled by both
42the application and the script engine, e.g. std::string or another class of your preference.
43
44\section doc_as_vs_cpp_types_2 Arrays
45
46The AngelScript arrays are not directly matched by C++ arrays. The arrays
47are stored in an special object, accessed through the \ref asIScriptArray interface.
48Thus you can normally not directly exchange a script with a C++ function expecting
49a C++ array, or vice versa. Nor can the application register C++ arrays as properties
50and expect AngelScript to be able to understand them.
51
52It is however possible to override AngelScript's built-in array objects
53with application specified objects, on a per array type basis.
54
55\section doc_as_vs_cpp_types_3 Object handles
56
57The AngelScript object handles are reference counted pointers to objects.
58This means that for object handles to work, the object must have some way of
59counting references, for example an AddRef/Release method pair.
60
61When AngelScript passes an object handle by value to a function it
62increases the reference count to count for the argument instance, thus the
63function is responsible for releasing the reference once it is finished with
64it. In the same manner AngelScript expects any handle returned from a function
65to already have the reference accounted for.
66
67However, when registering functions/methods with AngelScript the
68application can tell the library that it should automatically take care of
69releasing the handle references once a function return, likewise for returned
70handles. This is done by adding a + sign to the \@ type modifier. When doing
71this an object handle can be safely passed to a C++ function that expects a
72normal pointer, but don't release it afterwards.
73
74\see \ref doc_obj_handle
75
76\section doc_as_vs_cpp_types_4 Parameter references
77
78Because AngelScript needs to guarantee validity of pointers at all times,
79it doesn't always pass references to the true object to the function
80parameter. Instead it creates a copy of the object, whose reference is passed
81to the function, and if the reference is marked to return a value, the clone
82is copied back to the original object (if it still exists) once the function
83returns.
84
85Because of this, AngelScript's parameter references are mostly compatible
86with C++ references, or pointers, except that the address normally shouldn't be
87stored for later use, since the object may be destroyed once the function returns.
88
89If it is necessary to store the address of the object, then object handles
91
93<tr><td width=100 valign=top><b>Reference</b></td><td valign=top><b>Description</b></td></tr>
94<tr><td valign=top>&in</td><td>A copy of the value is always taken and the
95reference to the copy is passed to the function. For script functions this is not
96useful, but it is maintained for compatibility with application registered functions.</td></tr>
97<tr><td valign=top>const &in</td><td>If the life time of the value can be
98guaranteed to be valid during the execution of the function, the reference to the
99true object is passed to the function, otherwise a copy is made.</td></tr>
100<tr><td valign=top>&out</td><td>A reference to an unitialized value is passed
101to the function. When the function returns the value is copied to the true reference.
102The argument expression is evaluated only after the function call. This is the best
103way to have functions return multiple values.</td></tr>
104<tr><td valign=top>const &out</td><td>Useless as the function wouldn't be able
105to modify the value.</td></tr>
106<tr><td valign=top>&inout</td><td>The true reference is always passed to the
107function. If the life time of the value cannot be guaranteed, a compile time error
108is generated and the script writer will have to copy the value to a local variable
109first. Objects that support object handles are best suitable for this type as they
110can always be guaranteed.</td></tr>
111<tr><td valign=top>const &inout</td><td>The reference cannot be changed by the
112function.</td></tr>
113</table>
114
115If the application wants parameter references that work like they do in C++,
116then the engine property asEP_ALLOW_UNSAFE_REFERENCES can be set. When this is done,
117the parameter references can be declared without the <code>in</code>, <code>out</code>,
118or <code>inout</code>. The parameter references declared without any of the keywords
119will always pass the address to the original value, and will not have any restrictions
120to the expressions that can be used. The parameter references with the <code>in</code>
121and <code>out</code> keywords will still work like before, but the references with the
122<code>inout</code> keyword will have the restrictions removed so that they work just
123like normal C++ references.
124
125The application writer and script writer has to be aware that it is possible to
126write scripts that access invalid references when the library is compiled in this
127mode, just like it is possible to do so in C++.
128
129
130
131*/