Studio:Scl brew class: Difference between revisions
No edit summary |
m (Text replace - 'Category: SCL' to 'Category:Studio:SCL') |
||
(12 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
= The scl_brew_class pragma= | = The scl_brew_class pragma= | ||
The scl_brew_class pragma is for Brew API developers who simulate C++ classes and virtual functions within ANSI C by following a fixed set of conventions. The conventions are used to create classes referred to as | The scl_brew_class pragma is for Brew API developers who simulate C++ classes and virtual functions within ANSI C by following a fixed set of conventions. The conventions are used to create classes referred to as "Brew Classes." A Brew class is formed from a C structure type that is used to represent the C++ virtual function table. The structure must contain members which are of the type pointer to function. Furthermore, these members must point to a function type whose first parameter is a pointer to this same structure type. | ||
The C-based class must adhere to the following rules: | The C-based class must adhere to the following rules: | ||
* The class is structured using a C structure typedef | * The class is structured using a C structure typedef | ||
* | * Function pointers are used to define the class methods | ||
* Each class method is required to have a parameter, which is used to pass in a reference to the object upon which the method will act | * Each class method is required to have a parameter, which is used to pass in a reference to the object upon which the method will act | ||
* There must be at least one SCL-compliant function that references the class typedef. | * There must be at least one SCL-compliant function that references the class typedef. | ||
Line 32: | Line 32: | ||
<class-name>_<field-name> | <class-name>_<field-name> | ||
For additional information on scl_brew_class, including constraints, refer to the section on scl_brew_class in the SCL Reference Guide. | For additional information on scl_brew_class, including constraints, refer to the section on scl_brew_class in the [[Media:s2sSCLReferenceGuide.pdf|SCL Reference Guide]]. | ||
== Example == | == Example == | ||
<source lang=c> | |||
// Const defining a maximum string length // | |||
#define MAX_STR_SIZE 64 | |||
// Structure type used to define a C-based class. Each // | |||
// method is defined as a pointer to a function whose // | |||
// first parameter is a pointer to the structure type. // | |||
typedef struct _myObj MyObj; | |||
struct _myObj | |||
{ | |||
int ( *method1 )(MyObj * t, int x, int y); | |||
void ( *method2 )(MyObj * t, char * str); | |||
char* ( *method3 )(MyObj * t); | |||
}; | |||
// createMyObj - Interface used to instantiate the C-based object. // | |||
// *ppObj will be declared as opaque to obtain the object's address. // | |||
// The object's address can then be passed in as the first // | |||
// parameter for each of the method calls. // | |||
void createMyObj (MyObj ** ppObj); | |||
static MyObj * bindMyObj(void * pObj) | |||
{ | |||
return (MyObj *) pObj; | |||
}; | |||
#ifdef _SCL | |||
// Use the scl_function pragma to associate an object with the // | |||
// interface. // | |||
// Use the scl_ptr pragma to define the parameter ppObj as an OUT // | |||
// pointer. // | |||
// Use the scl_ptr_opaque pragma to opaque the child pointer to the // | |||
// object. // | |||
#pragma scl_function(createMyObj) | |||
#pragma scl_ptr(createMyObj, ppObj, "OUT", "PRIVATE") | |||
#pragma scl_ptr_opaque(createMyObj, *ppObj) | |||
// Use the scl_function pragma to associate an object with the interface // | |||
#pragma scl_function(bindMyObj) | |||
#pragma scl_brew_class(MyObj) | |||
// Use the scl_string pragma to declare the char pointers as ASCII strings. // | |||
// The method names are derived from <class-name>, <field-name> // | |||
#pragma scl_string(MyObj_method2, str, MAX_STR_SIZE) | |||
#pragma scl_string(MyObj_method3(), MAX_STR_SIZE) | |||
#endif //_SCL// | |||
</source> | |||
[[Category:Studio:SCL]] |
Latest revision as of 00:10, 21 August 2009
The scl_brew_class pragma
The scl_brew_class pragma is for Brew API developers who simulate C++ classes and virtual functions within ANSI C by following a fixed set of conventions. The conventions are used to create classes referred to as "Brew Classes." A Brew class is formed from a C structure type that is used to represent the C++ virtual function table. The structure must contain members which are of the type pointer to function. Furthermore, these members must point to a function type whose first parameter is a pointer to this same structure type.
The C-based class must adhere to the following rules:
- The class is structured using a C structure typedef
- Function pointers are used to define the class methods
- Each class method is required to have a parameter, which is used to pass in a reference to the object upon which the method will act
- There must be at least one SCL-compliant function that references the class typedef.
Syntax
#pragma scl_brew_class(structure-name)
#pragma scl_brew_class(structure-name, field-name 1 .. n)
Parameters | Type | Description |
structure-name | Type | Name of the structure that defines the C-based class |
field-name 1 .. n | Member | Name of the field that defines one or more methods |
Notes
The method names are automatically generated based on the class name and field name as follows:
<class-name>_<field-name>
For additional information on scl_brew_class, including constraints, refer to the section on scl_brew_class in the SCL Reference Guide.
Example
// Const defining a maximum string length //
#define MAX_STR_SIZE 64
// Structure type used to define a C-based class. Each //
// method is defined as a pointer to a function whose //
// first parameter is a pointer to the structure type. //
typedef struct _myObj MyObj;
struct _myObj
{
int ( *method1 )(MyObj * t, int x, int y);
void ( *method2 )(MyObj * t, char * str);
char* ( *method3 )(MyObj * t);
};
// createMyObj - Interface used to instantiate the C-based object. //
// *ppObj will be declared as opaque to obtain the object's address. //
// The object's address can then be passed in as the first //
// parameter for each of the method calls. //
void createMyObj (MyObj ** ppObj);
static MyObj * bindMyObj(void * pObj)
{
return (MyObj *) pObj;
};
#ifdef _SCL
// Use the scl_function pragma to associate an object with the //
// interface. //
// Use the scl_ptr pragma to define the parameter ppObj as an OUT //
// pointer. //
// Use the scl_ptr_opaque pragma to opaque the child pointer to the //
// object. //
#pragma scl_function(createMyObj)
#pragma scl_ptr(createMyObj, ppObj, "OUT", "PRIVATE")
#pragma scl_ptr_opaque(createMyObj, *ppObj)
// Use the scl_function pragma to associate an object with the interface //
#pragma scl_function(bindMyObj)
#pragma scl_brew_class(MyObj)
// Use the scl_string pragma to declare the char pointers as ASCII strings. //
// The method names are derived from <class-name>, <field-name> //
#pragma scl_string(MyObj_method2, str, MAX_STR_SIZE)
#pragma scl_string(MyObj_method3(), MAX_STR_SIZE)
#endif //_SCL//