SlideShare uma empresa Scribd logo
1 de 10
Baixar para ler offline
CHAPTER
EIGHTYFOUR
CODE GENERATOR FOR WRAPPING C/C++ LIBRARIES
In this chapter we will learn how to use the code generator to wrap C/C++ Libraries to use it in our Ring applications.
84.1 Using the tool
The code generator program is parsec.ring that can be executed as any ring code using the ring language.
URL : https://github.com/ring-lang/ring/tree/master/extensions/codegen
for example to read a configuration file called test.cf to generate the source code file test.c run parsec.ring as in the
next command
ring parsec.ring test.cf test.c
84.2 Configuration file
The configuration file (*.cf) is the input file that we pass to the code generator. This file determine the functions
prototypes that we need to use from a C/C++ library.
Writing configuration files is simple according to the next rules
84.3 Using the function prototype
• To generate code that wraps a C function, we just write the C function prototype
Example:
ALLEGRO_DISPLAY *al_create_display(int w, int h)
void al_destroy_display(ALLEGRO_DISPLAY *display)
int al_get_new_display_flags(void)
void al_set_new_display_flags(int flags)
int al_get_new_display_option(int option, int *importance)
The previous example will guide the code generator to generate 5 functions that wraps the al_create_display(),
al_destroy_display(), al_get_new_display_flags(), al_set_new_diplay_flas() and al_get_new_display_option() func-
tions.
The generated code will be as in the next example
928
Ring Documentation, Release 1.10
RING_FUNC(ring_al_create_display)
{
if ( RING_API_PARACOUNT != 2 ) {
RING_API_ERROR(RING_API_MISS2PARA);
return ;
}
if ( ! RING_API_ISNUMBER(1) ) {
RING_API_ERROR(RING_API_BADPARATYPE);
return ;
}
if ( ! RING_API_ISNUMBER(2) ) {
RING_API_ERROR(RING_API_BADPARATYPE);
return ;
}
RING_API_RETCPOINTER(al_create_display( (int ) RING_API_GETNUMBER(1),
(int ) RING_API_GETNUMBER(2)),"ALLEGRO_DISPLAY");
}
RING_FUNC(ring_al_destroy_display)
{
if ( RING_API_PARACOUNT != 1 ) {
RING_API_ERROR(RING_API_MISS1PARA);
return ;
}
if ( ! RING_API_ISPOINTER(1) ) {
RING_API_ERROR(RING_API_BADPARATYPE);
return ;
}
al_destroy_display((ALLEGRO_DISPLAY *) RING_API_GETCPOINTER(1,"ALLEGRO_DISPLAY"));
}
RING_FUNC(ring_al_get_new_display_flags)
{
if ( RING_API_PARACOUNT != 0 ) {
RING_API_ERROR(RING_API_BADPARACOUNT);
return ;
}
RING_API_RETNUMBER(al_get_new_display_flags());
}
RING_FUNC(ring_al_set_new_display_flags)
{
if ( RING_API_PARACOUNT != 1 ) {
RING_API_ERROR(RING_API_MISS1PARA);
return ;
}
if ( ! RING_API_ISNUMBER(1) ) {
RING_API_ERROR(RING_API_BADPARATYPE);
return ;
}
al_set_new_display_flags( (int ) RING_API_GETNUMBER(1));
}
RING_FUNC(ring_al_get_new_display_option)
84.3. Using the function prototype 929
Ring Documentation, Release 1.10
{
if ( RING_API_PARACOUNT != 2 ) {
RING_API_ERROR(RING_API_MISS2PARA);
return ;
}
if ( ! RING_API_ISNUMBER(1) ) {
RING_API_ERROR(RING_API_BADPARATYPE);
return ;
}
if ( ! RING_API_ISSTRING(2) ) {
RING_API_ERROR(RING_API_BADPARATYPE);
return ;
}
RING_API_RETNUMBER(al_get_new_display_option( (int ) RING_API_GETNUMBER(1),
RING_API_GETINTPOINTER(2)));
RING_API_ACCEPTINTVALUE(2) ;
}
from the previous example we can see how much of time and effort is saved using the Code Generator.
84.4 Adding code to the generated code
• To generate code directly type it between <code> and </code>
Example :
<code>
/* some C code will be written here */
</code>
We use this feature when we need to do something without the help of the code generator. for example including
header files and defining constants using Macro.
84.5 Prefix for Functions Names
• To determine a prefix in all of the functions names type it between <funcstart> and </funcstart> for ex-
ample when we wrap the Allegro game programming library and we need all of the library functions
to start with “al” we type the next code in the configuration file
<funcstart>
al
</funcstart>
84.6 Generate function to wrap structures
• To generate functions that wrap structures (create/delete/get structure members)
just type the structures names between <struct> and </struct> also after the structure name you can type the structure
members between { } separated by comma.
Example
84.4. Adding code to the generated code 930
Ring Documentation, Release 1.10
<struct>
ALLEGRO_COLOR
ALLEGRO_EVENT { type , keyboard.keycode , mouse.x , mouse.y }
</struct>
from the previous example we will generate two function to create/delete the structure ALLEGRO_COLOR Also we
will generate two functions to create/delete the structure ALLEGRO_EVENT and four functions to get the structure
ALLEGRO_EVENT members (type, keyboard.keycode, mouse.x, mouse.y).
84.7 Determine Structure Members Types
You can determine the pointer name before the strucuture member name.
Example:
SDL_Surface {flags,SDL_PixelFormat *format,w,h,pitch,void *pixels}
84.8 Defining Constants
You can define constants using <constant> and </constant>
The generator will generate the required functions to get the constant values
And will define the constants to be used with the same name in Ring code using *.rh file that will be generated too.
rh = Ring Header
Example:
<constant>
MIX_DEFAULT_FORMAT
SDL_QUIT
SDL_BUTTON_LEFT
SDL_BUTTON_MIDDLE
SDL_BUTTON_RIGHT
</constant>
Note: You will need to pass the *.rh file name to parsec.ring after the generated source file name.
Example:
ring ..codegenparsec.ring libsdl.cf ring_libsdl.c ring_libsdl.rh
84.9 Register New Functions
We can register functions by typing the function prototype between <register> and </register> We need this feature
only when we don’t provide the function prototype as input directly where we need to write the code of this function.
Example:
<register>
void al_exit(void)
</register>
84.7. Determine Structure Members Types 931
Ring Documentation, Release 1.10
<code>
RING_FUNC(ring_al_exit)
{
if ( RING_API_PARACOUNT != 0 ) {
RING_API_ERROR(RING_API_BADPARACOUNT);
return ;
}
exit(0);
}
</code>
In the previous example we register the al_exit() function. This function is not part of the Allegro Library, it’s just an
extra function that we need to add. Then the code if this function is written inside <code> and </code>. This function
call the exit() function from the C language library.
84.10 Writing comments in the configuration file
• To type comments just type it between <comment> and </comment>
Example:
<comment>
configuration files
</comment>
84.11 Executing code during code generation
• To ask from the code generator to execute Ring code during reading the configuration file, just
write the code between <runcode> and </runcode>
Example:
<runcode>
aNumberTypes + "al_fixed"
</runcode>
The previoud line of code add the string “al_fixed” to the list aNumberTypes, This list contains types that can be
considered as numbers when the code generator find it in the function prototype.
84.12 Enum and Numbers
We have the list aEnumTypes to use for adding each Enumeration we uses in the functions prototype.
Example:
<runcode>
aNumberTypes + "qreal"
aNumberTypes + "qint64"
aEnumTypes + "Qt::GestureType"
aEnumTypes + "Qt::GestureFlag"
</runcode>
84.10. Writing comments in the configuration file 932
Ring Documentation, Release 1.10
84.13 Filtering using Expressions
using <filter> and </filter> we can include/exclude parts of the configuration file based on a condition, for example
<filter> iswindows()
... functions related to windows
</filter>
84.14 Constants Type
The default type for constant is Number But Some constants may be another type, for example (pointer : void *)
before using <constant> and </constant> we can use <runcode> and </runcode> to determine the constant type using
two global variables used by the code generator.
The first variable is $nDefaultConstantType which can be * C_CONSTANT_TYPE_NUMBER *
C_CONSTANT_TYPE_STRING * C_CONSTANT_TYPE_POINTER
if we are using C_CONSTANT_TYPE_POINTER then we will need the second global variable which is $cDefault-
ConstantPointerType to determine the pointer type.
Example :
The next example uses this feature to define constants in the FreeGLUT library
<runcode>
$nDefaultConstantType = C_CONSTANT_TYPE_POINTER
$cDefaultConstantPointerType = "void"
</runcode>
<constant>
GLUT_STROKE_ROMAN
GLUT_STROKE_MONO_ROMAN
GLUT_BITMAP_9_BY_15
GLUT_BITMAP_8_BY_13
GLUT_BITMAP_TIMES_ROMAN_10
GLUT_BITMAP_TIMES_ROMAN_24
GLUT_BITMAP_HELVETICA_10
GLUT_BITMAP_HELVETICA_12
GLUT_BITMAP_HELVETICA_18
</constant>
84.15 Configuration file for the Allegro Library
The next configuration file enable us to use the Allegro library functions. The configuration file size is less than 1000
lines. when the code generator take this file as input the generated source code file in the C language will be 12000
lines of code!
We can see this configuration file as a complete example about using the code generator Also we can use it to know
the functions that can be used from RingAllegro when you use it to create 2D games!
<code>
#define ALLEGRO_NO_MAGIC_MAIN
#include <allegro5/allegro.h>
#include "allegro5/allegro_image.h"
84.13. Filtering using Expressions 933
Ring Documentation, Release 1.10
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
#include <allegro5/allegro_audio.h>
#include <allegro5/allegro_acodec.h>
#include <allegro5/allegro_opengl.h>
#include <allegro5/allegro_direct3d.h>
#include <allegro5/allegro_color.h>
#include <allegro5/allegro_memfile.h>
#include "allegro5/allegro_native_dialog.h"
#include <allegro5/allegro_physfs.h>
#include <allegro5/allegro_primitives.h>
</code>
<funcstart>
al
</funcstart>
<struct>
ALLEGRO_EVENT { type , keyboard.keycode , mouse.x , mouse.y }
ALLEGRO_TIMEOUT
ALLEGRO_SAMPLE_ID
ALLEGRO_COLOR
</struct>
<register>
void al_exit(void)
</register>
<code>
RING_FUNC(ring_al_exit)
{
if ( RING_API_PARACOUNT != 0 ) {
RING_API_ERROR(RING_API_BADPARACOUNT);
return ;
}
exit(0);
}
</code>
int al_init(void)
<comment>
configuration files
</comment>
<runcode>
aNumberTypes + "al_fixed"
</runcode>
ALLEGRO_CONFIG *al_create_config(void)
void al_destroy_config(ALLEGRO_CONFIG *config)
ALLEGRO_CONFIG *al_load_config_file(const char *filename)
ALLEGRO_CONFIG *al_load_config_file_f(ALLEGRO_FILE *file)
bool al_save_config_file(const char *filename, const ALLEGRO_CONFIG *config)
bool al_save_config_file_f(ALLEGRO_FILE *file, const ALLEGRO_CONFIG *config)
void al_add_config_section(ALLEGRO_CONFIG *config, const char *name)
Note: we just provided part of the configuration file, for complete copy check the Ring source code distribution.
84.15. Configuration file for the Allegro Library 934
Ring Documentation, Release 1.10
84.16 Threads Support
Next, another part of the configutaiton file, it’s important because we can learn from it how to add threads to our Ring
applications by using a threads library.
The idea is using ring_vm_mutexfunctions() and ring_vm_runcodefromthread() to execute Ring code.
<comment>
Threads
</comment>
<code>
void *al_func_thread(ALLEGRO_THREAD *thread, void *pPointer)
{
List *pList;
VM *pVM;
const char *cStr;
pList = (List *) pPointer ;
pVM = (VM *) ring_list_getpointer(pList,2);
cStr = ring_list_getstring(pList,1);
ring_vm_runcodefromthread(pVM,cStr);
ring_list_delete(pList);
return NULL;
}
RING_FUNC(ring_al_create_thread)
{
ALLEGRO_THREAD *pThread;
List *pList;
if ( RING_API_PARACOUNT != 1 ) {
RING_API_ERROR(RING_API_MISS1PARA);
return ;
}
if ( ! RING_API_ISSTRING(1) ) {
RING_API_ERROR(RING_API_BADPARATYPE);
return ;
}
pList = ring_list_new(0);
ring_list_addstring(pList,RING_API_GETSTRING(1));
ring_list_addpointer(pList,pPointer);
ring_vm_mutexfunctions((VM *) pPointer,al_create_mutex,
al_lock_mutex,al_unlock_mutex,al_destroy_mutex);
pThread = al_create_thread(al_func_thread, pList);
al_start_thread(pThread);
RING_API_RETCPOINTER(pThread,"ALLEGRO_THREAD");
}
RING_FUNC(ring_al_run_detached_thread)
{
List *pList;
if ( RING_API_PARACOUNT != 1 ) {
RING_API_ERROR(RING_API_MISS1PARA);
return ;
}
if ( ! RING_API_ISSTRING(1) ) {
84.16. Threads Support 935
Ring Documentation, Release 1.10
RING_API_ERROR(RING_API_BADPARATYPE);
return ;
}
pList = ring_list_new(0);
ring_list_addstring(pList,RING_API_GETSTRING(1));
ring_list_addpointer(pList,pPointer);
ring_vm_mutexfunctions((VM *) pPointer,al_create_mutex,
al_lock_mutex,al_unlock_mutex,al_destroy_mutex);
al_run_detached_thread(al_func_thread, pList);
}
</code>
<register>
ALLEGRO_THREAD *al_create_thread(void)
void al_run_detached_thread(void)
</register>
void al_start_thread(ALLEGRO_THREAD *thread)
void al_join_thread(ALLEGRO_THREAD *thread, void **ret_value)
void al_set_thread_should_stop(ALLEGRO_THREAD *thread)
bool al_get_thread_should_stop(ALLEGRO_THREAD *thread)
void al_destroy_thread(ALLEGRO_THREAD *thread)
ALLEGRO_MUTEX *al_create_mutex(void)
ALLEGRO_MUTEX *al_create_mutex_recursive(void)
void al_lock_mutex(ALLEGRO_MUTEX *mutex)
void al_unlock_mutex(ALLEGRO_MUTEX *mutex)
void al_destroy_mutex(ALLEGRO_MUTEX *mutex)
ALLEGRO_COND *al_create_cond(void)
void al_destroy_cond(ALLEGRO_COND *cond)
void al_wait_cond(ALLEGRO_COND *cond, ALLEGRO_MUTEX *mutex)
84.17 Code Generator Rules for Wrapping C++ Classes
• We can define classes between <class> and </class>
• Between <class> and <class> we set attributes like “name, nonew, para, parent, codename, passvmpointer,
abstract and staticmethods”
• we set the attributes using the style attributename:value or attributename only if no values are required
• The “name” attribute determine the class name in C++ code and this name will be the default name in the Ring
code
• The nonew instruction means that we don’t need new/delete methods
• The parent attribute determine the parent class name
• The codename attribute determine another class name in C++ code
• The passvmpoint instruction means passing the Ring VM pointer to the class constructor when we create new
objects, this happens when we set the codename attribute to a class that we will define and this class need the
Virtual Machine pointer (for example to use it to execute Ring code from C++ code).
• The abstract instruction means that no new method is required for this class “no objects will be created”.
• The staticmethods instruction means that method doesn’t need an object to be called.
• Using <nodllstartup> we can avoid #include “ring.h”, We need this to write our startup code.
• Using <libinitfunc> we can change the function name that register the library functions
84.17. Code Generator Rules for Wrapping C++ Classes 936
Ring Documentation, Release 1.10
• Using <ignorecpointertype> we can ignore pointer type check
• Using the aStringTypes list when can defined new types that treated like const char *
• Using the aBeforeReturn list when can define code that is inserted after the variable name when we return that
variable from a function
• Using the aNewMethodName list we can define another method name to be used in Ring code when we call
the C++ method. this feature is required because some C++ method may be identical to Ring Keywords like
“load”,”next”,”end” and “done”.
• in method prototype - when we use @ in the method name, we mean that we have the same method with different
parameters (As in C++)
84.18 Using configuration file that wrap C++ Library
To run the code generator to generate code for using C++ library in the Ring application, we can do that as we did
with using C libraries but here we will generate .cpp file instead of *.c file. Also we will determine another file to be
generated (.ring). This file will contains classes in Ring code that wraps C++ functions for using C++ classes and
objects.
ring parsec.ring generatorqt.cf ring_qt.cpp ring_qt.ring
84.19 Configuration file for the Qt Framework
The next configuration file is used to wrap many Qt classes The configuration file is around 3500 lines and generate
C++ code around 56000 lines and generate also Ring code around 9000 lines.
<nodllstartup>
<libinitfunc> ring_qt_start
<ignorecpointertype>
<code>
extern "C" {
#include "ring.h"
}
#include "ring_qt.h"
#include "gpushbutton.h"
#include "gaction.h"
#include "glineedit.h"
#include "gtextedit.h"
#include "glistwidget.h"
#include "gtreeview.h"
#include "gtreewidget.h"
#include "gcombobox.h"
#include "gtabwidget.h"
#include "gtablewidget.h"
#include "gprogressbar.h"
#include "gspinbox.h"
#include "gslider.h"
#include "gdial.h"
84.18. Using configuration file that wrap C++ Library 937

Mais conteúdo relacionado

Mais procurados

Basic calculator tutorial
Basic calculator tutorialBasic calculator tutorial
Basic calculator tutorial
Gilkye
 

Mais procurados (20)

The Ring programming language version 1.5.1 book - Part 78 of 180
The Ring programming language version 1.5.1 book - Part 78 of 180The Ring programming language version 1.5.1 book - Part 78 of 180
The Ring programming language version 1.5.1 book - Part 78 of 180
 
The Ring programming language version 1.5.3 book - Part 91 of 184
The Ring programming language version 1.5.3 book - Part 91 of 184The Ring programming language version 1.5.3 book - Part 91 of 184
The Ring programming language version 1.5.3 book - Part 91 of 184
 
The Ring programming language version 1.7 book - Part 87 of 196
The Ring programming language version 1.7 book - Part 87 of 196The Ring programming language version 1.7 book - Part 87 of 196
The Ring programming language version 1.7 book - Part 87 of 196
 
The Ring programming language version 1.7 book - Part 86 of 196
The Ring programming language version 1.7 book - Part 86 of 196The Ring programming language version 1.7 book - Part 86 of 196
The Ring programming language version 1.7 book - Part 86 of 196
 
Calculator
CalculatorCalculator
Calculator
 
Constructor,destructors cpp
Constructor,destructors cppConstructor,destructors cpp
Constructor,destructors cpp
 
The Ring programming language version 1.5.4 book - Part 80 of 185
The Ring programming language version 1.5.4 book - Part 80 of 185The Ring programming language version 1.5.4 book - Part 80 of 185
The Ring programming language version 1.5.4 book - Part 80 of 185
 
The Ring programming language version 1.8 book - Part 90 of 202
The Ring programming language version 1.8 book - Part 90 of 202The Ring programming language version 1.8 book - Part 90 of 202
The Ring programming language version 1.8 book - Part 90 of 202
 
Java calculator
Java calculatorJava calculator
Java calculator
 
JavaScript Functions
JavaScript FunctionsJavaScript Functions
JavaScript Functions
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4
 
Tdd.eng.ver
Tdd.eng.verTdd.eng.ver
Tdd.eng.ver
 
Alexey Tsoy Meta Programming in C++ 16.11.17
Alexey Tsoy Meta Programming in C++ 16.11.17Alexey Tsoy Meta Programming in C++ 16.11.17
Alexey Tsoy Meta Programming in C++ 16.11.17
 
Basic calculator tutorial
Basic calculator tutorialBasic calculator tutorial
Basic calculator tutorial
 
The Ring programming language version 1.5.4 book - Part 81 of 185
The Ring programming language version 1.5.4 book - Part 81 of 185The Ring programming language version 1.5.4 book - Part 81 of 185
The Ring programming language version 1.5.4 book - Part 81 of 185
 
perl course-in-mumbai
 perl course-in-mumbai perl course-in-mumbai
perl course-in-mumbai
 
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
 
The Ring programming language version 1.5.1 book - Part 76 of 180
The Ring programming language version 1.5.1 book - Part 76 of 180The Ring programming language version 1.5.1 book - Part 76 of 180
The Ring programming language version 1.5.1 book - Part 76 of 180
 
Qe Reference
Qe ReferenceQe Reference
Qe Reference
 
soscon2018 - Tracing for fun and profit
soscon2018 - Tracing for fun and profitsoscon2018 - Tracing for fun and profit
soscon2018 - Tracing for fun and profit
 

Semelhante a The Ring programming language version 1.10 book - Part 97 of 212

20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
eugeniadean34240
 
Gift-VT Tools Development Overview
Gift-VT Tools Development OverviewGift-VT Tools Development Overview
Gift-VT Tools Development Overview
stn_tkiller
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
Deepak Singh
 
Compiler Design and Construction COSC 5353Project Instructions -
Compiler Design and Construction COSC 5353Project Instructions -Compiler Design and Construction COSC 5353Project Instructions -
Compiler Design and Construction COSC 5353Project Instructions -
LynellBull52
 

Semelhante a The Ring programming language version 1.10 book - Part 97 of 212 (20)

The Ring programming language version 1.8 book - Part 91 of 202
The Ring programming language version 1.8 book - Part 91 of 202The Ring programming language version 1.8 book - Part 91 of 202
The Ring programming language version 1.8 book - Part 91 of 202
 
The Ring programming language version 1.5.3 book - Part 90 of 184
The Ring programming language version 1.5.3 book - Part 90 of 184The Ring programming language version 1.5.3 book - Part 90 of 184
The Ring programming language version 1.5.3 book - Part 90 of 184
 
The Ring programming language version 1.8 book - Part 89 of 202
The Ring programming language version 1.8 book - Part 89 of 202The Ring programming language version 1.8 book - Part 89 of 202
The Ring programming language version 1.8 book - Part 89 of 202
 
The Ring programming language version 1.5.2 book - Part 77 of 181
The Ring programming language version 1.5.2 book - Part 77 of 181The Ring programming language version 1.5.2 book - Part 77 of 181
The Ring programming language version 1.5.2 book - Part 77 of 181
 
The Ring programming language version 1.6 book - Part 83 of 189
The Ring programming language version 1.6 book - Part 83 of 189The Ring programming language version 1.6 book - Part 83 of 189
The Ring programming language version 1.6 book - Part 83 of 189
 
The Ring programming language version 1.5.2 book - Part 78 of 181
The Ring programming language version 1.5.2 book - Part 78 of 181The Ring programming language version 1.5.2 book - Part 78 of 181
The Ring programming language version 1.5.2 book - Part 78 of 181
 
Ppt of c vs c#
Ppt of c vs c#Ppt of c vs c#
Ppt of c vs c#
 
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
 
The Ring programming language version 1.9 book - Part 93 of 210
The Ring programming language version 1.9 book - Part 93 of 210The Ring programming language version 1.9 book - Part 93 of 210
The Ring programming language version 1.9 book - Part 93 of 210
 
The Ring programming language version 1.5.3 book - Part 92 of 184
The Ring programming language version 1.5.3 book - Part 92 of 184The Ring programming language version 1.5.3 book - Part 92 of 184
The Ring programming language version 1.5.3 book - Part 92 of 184
 
The Ring programming language version 1.7 book - Part 88 of 196
The Ring programming language version 1.7 book - Part 88 of 196The Ring programming language version 1.7 book - Part 88 of 196
The Ring programming language version 1.7 book - Part 88 of 196
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Hooking signals and dumping the callstack
Hooking signals and dumping the callstackHooking signals and dumping the callstack
Hooking signals and dumping the callstack
 
Gift-VT Tools Development Overview
Gift-VT Tools Development OverviewGift-VT Tools Development Overview
Gift-VT Tools Development Overview
 
Typescript language extension of java script
Typescript language extension of java scriptTypescript language extension of java script
Typescript language extension of java script
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
 
Compiler Design and Construction COSC 5353Project Instructions -
Compiler Design and Construction COSC 5353Project Instructions -Compiler Design and Construction COSC 5353Project Instructions -
Compiler Design and Construction COSC 5353Project Instructions -
 
08 -functions
08  -functions08  -functions
08 -functions
 
Function C++
Function C++ Function C++
Function C++
 

Mais de Mahmoud Samir Fayed

Mais de Mahmoud Samir Fayed (20)

The Ring programming language version 1.10 book - Part 212 of 212
The Ring programming language version 1.10 book - Part 212 of 212The Ring programming language version 1.10 book - Part 212 of 212
The Ring programming language version 1.10 book - Part 212 of 212
 
The Ring programming language version 1.10 book - Part 211 of 212
The Ring programming language version 1.10 book - Part 211 of 212The Ring programming language version 1.10 book - Part 211 of 212
The Ring programming language version 1.10 book - Part 211 of 212
 
The Ring programming language version 1.10 book - Part 210 of 212
The Ring programming language version 1.10 book - Part 210 of 212The Ring programming language version 1.10 book - Part 210 of 212
The Ring programming language version 1.10 book - Part 210 of 212
 
The Ring programming language version 1.10 book - Part 208 of 212
The Ring programming language version 1.10 book - Part 208 of 212The Ring programming language version 1.10 book - Part 208 of 212
The Ring programming language version 1.10 book - Part 208 of 212
 
The Ring programming language version 1.10 book - Part 207 of 212
The Ring programming language version 1.10 book - Part 207 of 212The Ring programming language version 1.10 book - Part 207 of 212
The Ring programming language version 1.10 book - Part 207 of 212
 
The Ring programming language version 1.10 book - Part 205 of 212
The Ring programming language version 1.10 book - Part 205 of 212The Ring programming language version 1.10 book - Part 205 of 212
The Ring programming language version 1.10 book - Part 205 of 212
 
The Ring programming language version 1.10 book - Part 206 of 212
The Ring programming language version 1.10 book - Part 206 of 212The Ring programming language version 1.10 book - Part 206 of 212
The Ring programming language version 1.10 book - Part 206 of 212
 
The Ring programming language version 1.10 book - Part 204 of 212
The Ring programming language version 1.10 book - Part 204 of 212The Ring programming language version 1.10 book - Part 204 of 212
The Ring programming language version 1.10 book - Part 204 of 212
 
The Ring programming language version 1.10 book - Part 203 of 212
The Ring programming language version 1.10 book - Part 203 of 212The Ring programming language version 1.10 book - Part 203 of 212
The Ring programming language version 1.10 book - Part 203 of 212
 
The Ring programming language version 1.10 book - Part 202 of 212
The Ring programming language version 1.10 book - Part 202 of 212The Ring programming language version 1.10 book - Part 202 of 212
The Ring programming language version 1.10 book - Part 202 of 212
 
The Ring programming language version 1.10 book - Part 201 of 212
The Ring programming language version 1.10 book - Part 201 of 212The Ring programming language version 1.10 book - Part 201 of 212
The Ring programming language version 1.10 book - Part 201 of 212
 
The Ring programming language version 1.10 book - Part 200 of 212
The Ring programming language version 1.10 book - Part 200 of 212The Ring programming language version 1.10 book - Part 200 of 212
The Ring programming language version 1.10 book - Part 200 of 212
 
The Ring programming language version 1.10 book - Part 199 of 212
The Ring programming language version 1.10 book - Part 199 of 212The Ring programming language version 1.10 book - Part 199 of 212
The Ring programming language version 1.10 book - Part 199 of 212
 
The Ring programming language version 1.10 book - Part 198 of 212
The Ring programming language version 1.10 book - Part 198 of 212The Ring programming language version 1.10 book - Part 198 of 212
The Ring programming language version 1.10 book - Part 198 of 212
 
The Ring programming language version 1.10 book - Part 197 of 212
The Ring programming language version 1.10 book - Part 197 of 212The Ring programming language version 1.10 book - Part 197 of 212
The Ring programming language version 1.10 book - Part 197 of 212
 
The Ring programming language version 1.10 book - Part 196 of 212
The Ring programming language version 1.10 book - Part 196 of 212The Ring programming language version 1.10 book - Part 196 of 212
The Ring programming language version 1.10 book - Part 196 of 212
 
The Ring programming language version 1.10 book - Part 195 of 212
The Ring programming language version 1.10 book - Part 195 of 212The Ring programming language version 1.10 book - Part 195 of 212
The Ring programming language version 1.10 book - Part 195 of 212
 
The Ring programming language version 1.10 book - Part 194 of 212
The Ring programming language version 1.10 book - Part 194 of 212The Ring programming language version 1.10 book - Part 194 of 212
The Ring programming language version 1.10 book - Part 194 of 212
 
The Ring programming language version 1.10 book - Part 193 of 212
The Ring programming language version 1.10 book - Part 193 of 212The Ring programming language version 1.10 book - Part 193 of 212
The Ring programming language version 1.10 book - Part 193 of 212
 
The Ring programming language version 1.10 book - Part 192 of 212
The Ring programming language version 1.10 book - Part 192 of 212The Ring programming language version 1.10 book - Part 192 of 212
The Ring programming language version 1.10 book - Part 192 of 212
 

Último

%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 

Último (20)

10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 

The Ring programming language version 1.10 book - Part 97 of 212

  • 1. CHAPTER EIGHTYFOUR CODE GENERATOR FOR WRAPPING C/C++ LIBRARIES In this chapter we will learn how to use the code generator to wrap C/C++ Libraries to use it in our Ring applications. 84.1 Using the tool The code generator program is parsec.ring that can be executed as any ring code using the ring language. URL : https://github.com/ring-lang/ring/tree/master/extensions/codegen for example to read a configuration file called test.cf to generate the source code file test.c run parsec.ring as in the next command ring parsec.ring test.cf test.c 84.2 Configuration file The configuration file (*.cf) is the input file that we pass to the code generator. This file determine the functions prototypes that we need to use from a C/C++ library. Writing configuration files is simple according to the next rules 84.3 Using the function prototype • To generate code that wraps a C function, we just write the C function prototype Example: ALLEGRO_DISPLAY *al_create_display(int w, int h) void al_destroy_display(ALLEGRO_DISPLAY *display) int al_get_new_display_flags(void) void al_set_new_display_flags(int flags) int al_get_new_display_option(int option, int *importance) The previous example will guide the code generator to generate 5 functions that wraps the al_create_display(), al_destroy_display(), al_get_new_display_flags(), al_set_new_diplay_flas() and al_get_new_display_option() func- tions. The generated code will be as in the next example 928
  • 2. Ring Documentation, Release 1.10 RING_FUNC(ring_al_create_display) { if ( RING_API_PARACOUNT != 2 ) { RING_API_ERROR(RING_API_MISS2PARA); return ; } if ( ! RING_API_ISNUMBER(1) ) { RING_API_ERROR(RING_API_BADPARATYPE); return ; } if ( ! RING_API_ISNUMBER(2) ) { RING_API_ERROR(RING_API_BADPARATYPE); return ; } RING_API_RETCPOINTER(al_create_display( (int ) RING_API_GETNUMBER(1), (int ) RING_API_GETNUMBER(2)),"ALLEGRO_DISPLAY"); } RING_FUNC(ring_al_destroy_display) { if ( RING_API_PARACOUNT != 1 ) { RING_API_ERROR(RING_API_MISS1PARA); return ; } if ( ! RING_API_ISPOINTER(1) ) { RING_API_ERROR(RING_API_BADPARATYPE); return ; } al_destroy_display((ALLEGRO_DISPLAY *) RING_API_GETCPOINTER(1,"ALLEGRO_DISPLAY")); } RING_FUNC(ring_al_get_new_display_flags) { if ( RING_API_PARACOUNT != 0 ) { RING_API_ERROR(RING_API_BADPARACOUNT); return ; } RING_API_RETNUMBER(al_get_new_display_flags()); } RING_FUNC(ring_al_set_new_display_flags) { if ( RING_API_PARACOUNT != 1 ) { RING_API_ERROR(RING_API_MISS1PARA); return ; } if ( ! RING_API_ISNUMBER(1) ) { RING_API_ERROR(RING_API_BADPARATYPE); return ; } al_set_new_display_flags( (int ) RING_API_GETNUMBER(1)); } RING_FUNC(ring_al_get_new_display_option) 84.3. Using the function prototype 929
  • 3. Ring Documentation, Release 1.10 { if ( RING_API_PARACOUNT != 2 ) { RING_API_ERROR(RING_API_MISS2PARA); return ; } if ( ! RING_API_ISNUMBER(1) ) { RING_API_ERROR(RING_API_BADPARATYPE); return ; } if ( ! RING_API_ISSTRING(2) ) { RING_API_ERROR(RING_API_BADPARATYPE); return ; } RING_API_RETNUMBER(al_get_new_display_option( (int ) RING_API_GETNUMBER(1), RING_API_GETINTPOINTER(2))); RING_API_ACCEPTINTVALUE(2) ; } from the previous example we can see how much of time and effort is saved using the Code Generator. 84.4 Adding code to the generated code • To generate code directly type it between <code> and </code> Example : <code> /* some C code will be written here */ </code> We use this feature when we need to do something without the help of the code generator. for example including header files and defining constants using Macro. 84.5 Prefix for Functions Names • To determine a prefix in all of the functions names type it between <funcstart> and </funcstart> for ex- ample when we wrap the Allegro game programming library and we need all of the library functions to start with “al” we type the next code in the configuration file <funcstart> al </funcstart> 84.6 Generate function to wrap structures • To generate functions that wrap structures (create/delete/get structure members) just type the structures names between <struct> and </struct> also after the structure name you can type the structure members between { } separated by comma. Example 84.4. Adding code to the generated code 930
  • 4. Ring Documentation, Release 1.10 <struct> ALLEGRO_COLOR ALLEGRO_EVENT { type , keyboard.keycode , mouse.x , mouse.y } </struct> from the previous example we will generate two function to create/delete the structure ALLEGRO_COLOR Also we will generate two functions to create/delete the structure ALLEGRO_EVENT and four functions to get the structure ALLEGRO_EVENT members (type, keyboard.keycode, mouse.x, mouse.y). 84.7 Determine Structure Members Types You can determine the pointer name before the strucuture member name. Example: SDL_Surface {flags,SDL_PixelFormat *format,w,h,pitch,void *pixels} 84.8 Defining Constants You can define constants using <constant> and </constant> The generator will generate the required functions to get the constant values And will define the constants to be used with the same name in Ring code using *.rh file that will be generated too. rh = Ring Header Example: <constant> MIX_DEFAULT_FORMAT SDL_QUIT SDL_BUTTON_LEFT SDL_BUTTON_MIDDLE SDL_BUTTON_RIGHT </constant> Note: You will need to pass the *.rh file name to parsec.ring after the generated source file name. Example: ring ..codegenparsec.ring libsdl.cf ring_libsdl.c ring_libsdl.rh 84.9 Register New Functions We can register functions by typing the function prototype between <register> and </register> We need this feature only when we don’t provide the function prototype as input directly where we need to write the code of this function. Example: <register> void al_exit(void) </register> 84.7. Determine Structure Members Types 931
  • 5. Ring Documentation, Release 1.10 <code> RING_FUNC(ring_al_exit) { if ( RING_API_PARACOUNT != 0 ) { RING_API_ERROR(RING_API_BADPARACOUNT); return ; } exit(0); } </code> In the previous example we register the al_exit() function. This function is not part of the Allegro Library, it’s just an extra function that we need to add. Then the code if this function is written inside <code> and </code>. This function call the exit() function from the C language library. 84.10 Writing comments in the configuration file • To type comments just type it between <comment> and </comment> Example: <comment> configuration files </comment> 84.11 Executing code during code generation • To ask from the code generator to execute Ring code during reading the configuration file, just write the code between <runcode> and </runcode> Example: <runcode> aNumberTypes + "al_fixed" </runcode> The previoud line of code add the string “al_fixed” to the list aNumberTypes, This list contains types that can be considered as numbers when the code generator find it in the function prototype. 84.12 Enum and Numbers We have the list aEnumTypes to use for adding each Enumeration we uses in the functions prototype. Example: <runcode> aNumberTypes + "qreal" aNumberTypes + "qint64" aEnumTypes + "Qt::GestureType" aEnumTypes + "Qt::GestureFlag" </runcode> 84.10. Writing comments in the configuration file 932
  • 6. Ring Documentation, Release 1.10 84.13 Filtering using Expressions using <filter> and </filter> we can include/exclude parts of the configuration file based on a condition, for example <filter> iswindows() ... functions related to windows </filter> 84.14 Constants Type The default type for constant is Number But Some constants may be another type, for example (pointer : void *) before using <constant> and </constant> we can use <runcode> and </runcode> to determine the constant type using two global variables used by the code generator. The first variable is $nDefaultConstantType which can be * C_CONSTANT_TYPE_NUMBER * C_CONSTANT_TYPE_STRING * C_CONSTANT_TYPE_POINTER if we are using C_CONSTANT_TYPE_POINTER then we will need the second global variable which is $cDefault- ConstantPointerType to determine the pointer type. Example : The next example uses this feature to define constants in the FreeGLUT library <runcode> $nDefaultConstantType = C_CONSTANT_TYPE_POINTER $cDefaultConstantPointerType = "void" </runcode> <constant> GLUT_STROKE_ROMAN GLUT_STROKE_MONO_ROMAN GLUT_BITMAP_9_BY_15 GLUT_BITMAP_8_BY_13 GLUT_BITMAP_TIMES_ROMAN_10 GLUT_BITMAP_TIMES_ROMAN_24 GLUT_BITMAP_HELVETICA_10 GLUT_BITMAP_HELVETICA_12 GLUT_BITMAP_HELVETICA_18 </constant> 84.15 Configuration file for the Allegro Library The next configuration file enable us to use the Allegro library functions. The configuration file size is less than 1000 lines. when the code generator take this file as input the generated source code file in the C language will be 12000 lines of code! We can see this configuration file as a complete example about using the code generator Also we can use it to know the functions that can be used from RingAllegro when you use it to create 2D games! <code> #define ALLEGRO_NO_MAGIC_MAIN #include <allegro5/allegro.h> #include "allegro5/allegro_image.h" 84.13. Filtering using Expressions 933
  • 7. Ring Documentation, Release 1.10 #include <allegro5/allegro_font.h> #include <allegro5/allegro_ttf.h> #include <allegro5/allegro_audio.h> #include <allegro5/allegro_acodec.h> #include <allegro5/allegro_opengl.h> #include <allegro5/allegro_direct3d.h> #include <allegro5/allegro_color.h> #include <allegro5/allegro_memfile.h> #include "allegro5/allegro_native_dialog.h" #include <allegro5/allegro_physfs.h> #include <allegro5/allegro_primitives.h> </code> <funcstart> al </funcstart> <struct> ALLEGRO_EVENT { type , keyboard.keycode , mouse.x , mouse.y } ALLEGRO_TIMEOUT ALLEGRO_SAMPLE_ID ALLEGRO_COLOR </struct> <register> void al_exit(void) </register> <code> RING_FUNC(ring_al_exit) { if ( RING_API_PARACOUNT != 0 ) { RING_API_ERROR(RING_API_BADPARACOUNT); return ; } exit(0); } </code> int al_init(void) <comment> configuration files </comment> <runcode> aNumberTypes + "al_fixed" </runcode> ALLEGRO_CONFIG *al_create_config(void) void al_destroy_config(ALLEGRO_CONFIG *config) ALLEGRO_CONFIG *al_load_config_file(const char *filename) ALLEGRO_CONFIG *al_load_config_file_f(ALLEGRO_FILE *file) bool al_save_config_file(const char *filename, const ALLEGRO_CONFIG *config) bool al_save_config_file_f(ALLEGRO_FILE *file, const ALLEGRO_CONFIG *config) void al_add_config_section(ALLEGRO_CONFIG *config, const char *name) Note: we just provided part of the configuration file, for complete copy check the Ring source code distribution. 84.15. Configuration file for the Allegro Library 934
  • 8. Ring Documentation, Release 1.10 84.16 Threads Support Next, another part of the configutaiton file, it’s important because we can learn from it how to add threads to our Ring applications by using a threads library. The idea is using ring_vm_mutexfunctions() and ring_vm_runcodefromthread() to execute Ring code. <comment> Threads </comment> <code> void *al_func_thread(ALLEGRO_THREAD *thread, void *pPointer) { List *pList; VM *pVM; const char *cStr; pList = (List *) pPointer ; pVM = (VM *) ring_list_getpointer(pList,2); cStr = ring_list_getstring(pList,1); ring_vm_runcodefromthread(pVM,cStr); ring_list_delete(pList); return NULL; } RING_FUNC(ring_al_create_thread) { ALLEGRO_THREAD *pThread; List *pList; if ( RING_API_PARACOUNT != 1 ) { RING_API_ERROR(RING_API_MISS1PARA); return ; } if ( ! RING_API_ISSTRING(1) ) { RING_API_ERROR(RING_API_BADPARATYPE); return ; } pList = ring_list_new(0); ring_list_addstring(pList,RING_API_GETSTRING(1)); ring_list_addpointer(pList,pPointer); ring_vm_mutexfunctions((VM *) pPointer,al_create_mutex, al_lock_mutex,al_unlock_mutex,al_destroy_mutex); pThread = al_create_thread(al_func_thread, pList); al_start_thread(pThread); RING_API_RETCPOINTER(pThread,"ALLEGRO_THREAD"); } RING_FUNC(ring_al_run_detached_thread) { List *pList; if ( RING_API_PARACOUNT != 1 ) { RING_API_ERROR(RING_API_MISS1PARA); return ; } if ( ! RING_API_ISSTRING(1) ) { 84.16. Threads Support 935
  • 9. Ring Documentation, Release 1.10 RING_API_ERROR(RING_API_BADPARATYPE); return ; } pList = ring_list_new(0); ring_list_addstring(pList,RING_API_GETSTRING(1)); ring_list_addpointer(pList,pPointer); ring_vm_mutexfunctions((VM *) pPointer,al_create_mutex, al_lock_mutex,al_unlock_mutex,al_destroy_mutex); al_run_detached_thread(al_func_thread, pList); } </code> <register> ALLEGRO_THREAD *al_create_thread(void) void al_run_detached_thread(void) </register> void al_start_thread(ALLEGRO_THREAD *thread) void al_join_thread(ALLEGRO_THREAD *thread, void **ret_value) void al_set_thread_should_stop(ALLEGRO_THREAD *thread) bool al_get_thread_should_stop(ALLEGRO_THREAD *thread) void al_destroy_thread(ALLEGRO_THREAD *thread) ALLEGRO_MUTEX *al_create_mutex(void) ALLEGRO_MUTEX *al_create_mutex_recursive(void) void al_lock_mutex(ALLEGRO_MUTEX *mutex) void al_unlock_mutex(ALLEGRO_MUTEX *mutex) void al_destroy_mutex(ALLEGRO_MUTEX *mutex) ALLEGRO_COND *al_create_cond(void) void al_destroy_cond(ALLEGRO_COND *cond) void al_wait_cond(ALLEGRO_COND *cond, ALLEGRO_MUTEX *mutex) 84.17 Code Generator Rules for Wrapping C++ Classes • We can define classes between <class> and </class> • Between <class> and <class> we set attributes like “name, nonew, para, parent, codename, passvmpointer, abstract and staticmethods” • we set the attributes using the style attributename:value or attributename only if no values are required • The “name” attribute determine the class name in C++ code and this name will be the default name in the Ring code • The nonew instruction means that we don’t need new/delete methods • The parent attribute determine the parent class name • The codename attribute determine another class name in C++ code • The passvmpoint instruction means passing the Ring VM pointer to the class constructor when we create new objects, this happens when we set the codename attribute to a class that we will define and this class need the Virtual Machine pointer (for example to use it to execute Ring code from C++ code). • The abstract instruction means that no new method is required for this class “no objects will be created”. • The staticmethods instruction means that method doesn’t need an object to be called. • Using <nodllstartup> we can avoid #include “ring.h”, We need this to write our startup code. • Using <libinitfunc> we can change the function name that register the library functions 84.17. Code Generator Rules for Wrapping C++ Classes 936
  • 10. Ring Documentation, Release 1.10 • Using <ignorecpointertype> we can ignore pointer type check • Using the aStringTypes list when can defined new types that treated like const char * • Using the aBeforeReturn list when can define code that is inserted after the variable name when we return that variable from a function • Using the aNewMethodName list we can define another method name to be used in Ring code when we call the C++ method. this feature is required because some C++ method may be identical to Ring Keywords like “load”,”next”,”end” and “done”. • in method prototype - when we use @ in the method name, we mean that we have the same method with different parameters (As in C++) 84.18 Using configuration file that wrap C++ Library To run the code generator to generate code for using C++ library in the Ring application, we can do that as we did with using C libraries but here we will generate .cpp file instead of *.c file. Also we will determine another file to be generated (.ring). This file will contains classes in Ring code that wraps C++ functions for using C++ classes and objects. ring parsec.ring generatorqt.cf ring_qt.cpp ring_qt.ring 84.19 Configuration file for the Qt Framework The next configuration file is used to wrap many Qt classes The configuration file is around 3500 lines and generate C++ code around 56000 lines and generate also Ring code around 9000 lines. <nodllstartup> <libinitfunc> ring_qt_start <ignorecpointertype> <code> extern "C" { #include "ring.h" } #include "ring_qt.h" #include "gpushbutton.h" #include "gaction.h" #include "glineedit.h" #include "gtextedit.h" #include "glistwidget.h" #include "gtreeview.h" #include "gtreewidget.h" #include "gcombobox.h" #include "gtabwidget.h" #include "gtablewidget.h" #include "gprogressbar.h" #include "gspinbox.h" #include "gslider.h" #include "gdial.h" 84.18. Using configuration file that wrap C++ Library 937