![]() |
![]() |
![]() |
![]() |
![]() |
Using Cadvanced/Cbasic SDL to C Compiler to Generate C++
General
The C code in the Master Library and the C code generated by the Cadvanced/Cbasic SDL to C Compiler is in the common subset of C and C++, and will thus compile both as a C program and as a C++ program. There is one special feature in the code generator concerning C++ when it comes to abstract data types and the possibility to match a C++ class and an SDL data type. Otherwise all the features for including C code, directive #ADT, directive #CODE (see Abstract Data Types and Accessing SDL Names in C Code - Directive #SDL), and so on, are directly applicable for C++ as well. The #CODE directives make it possible to include class definitions as C++ code and the utilization of the classes as C++ code in SDL tasks.
Example 417 : Using C++ Classes
CODE directive containing declarations (see Including C Declarations - Directive #CODE) which should be placed among the SDL declarations:
/*#CODE#HEADINGclass TEST {public:void putvar(int avar, int bvar){a = avar; b = bvar;}int geta(){return a;}int getb(){return b;}private:int a,b;} TESTvar;*/Example of usage of the class in a CODE directive in a TASK (see Including C Code in Task - Directive #CODE).
TASK '' /*#CODETESTvar.putvar(#(I), #(I)+10); */;Example of usage of the class in a CODE operators in expressions (see Including C Code in SDL Expressions - Operator #CODE).
OUTPUT Score(#CODE('TESTvar.geta()'),#CODE('TESTvar.getb()'),#CODE('#(TClass)->getVar()'));Connection Between C++ Classes and SDL
To obtain a close connection between a C++ class and SDL, an abstract data type in SDL can be used; see below.
If you have a C++ class (from a class library or developed specifically for the project), then the following correspondence rules can be used to map the class on an abstract data type.
Example 418 : SDL and C++ Class
Suppose we have a C++ class with the following interface (.h file):
class TestClass {public :TestClass (int);TestClass ();~TestClass ();int updateVar(int);int getVar();private :int v;};then the following abstract data type can be used to represent the class (in the example the NAME directive, see Specifying Names in Generated Code - Directive #NAME, is used to instruct the Cadvanced/Cbasic SDL to C Compiler which name to use in C for particular SDL objects):
NEWTYPE TestClass /*#NAME 'TestClassPtr' */LITERALSnewTestClass /*#NAME 'new1TestClass' */;OPERATORSnewTestClass /*#NAME 'new2TestClass' */: integer -> TestClass;deleteTestClass /*#NAME 'deleteTestClass' */: TestClass -> TestClass;updateVar /*#NAME 'updateVar' */: TestClass, integer -> integer; /*#OP(HC) */getVar /*#NAME 'getVar' */: TestClass -> integer; /*#OP(HC) *//*#ADT(T A(S) E(S) D(H) H P)#TYPE#include "TestClass.h"typedef TestClass * TestClassPtr;COMMENT((NOTE! SDL data type TestClass ispointer to C++ class TestClass))#HEADING#define yDef_TestClassPtr(p) *(p) = 0#define new1TestClass() new TestClass()#define new2TestClass(P) new TestClass(P)extern TestClassPtr deleteTestClass(TestClassPtr);#BODYextern TestClassPtr deleteTestClass(TestClassPtr P){delete P;return (TestClassPtr)0;}*/ENDNEWTYPE;Note that the #ADT specification means that no code will be generated for the abstract data type. The abstract data type can utilized in the following way:
DCLTClass TestClass,I Integer;TASK TClass := newTestClass;TASK TClass := newTestClass(2);TASK I := updateVar(TClass, I);TASK I := getVar(TClass);TASK TClass := deleteTestClass(TClass);The only feature that is not described before is the C option in the #OP directive. C (class) is an alternative to I (infix) and P (prefix), and specifies that the operator call should be translated to a member function call of a C++ member function. #OP(C) means that an operator call
- F(a, b, c)
- a->F(b, c)
This means that each operator mapped on class member function should have the class instance pointer as first parameter.
http://www.ibm.com/rational |
![]() |
![]() |
![]() |
![]() |