Based on the foregoing class definitions, the next example illustrates several important aspects of initializers. The following code is a completed implementation template and an example client program for the preceding classes. Code added to the original template is given in bold.
/*
* This file generated by the SOM Compiler and Emitter Framework.
* Generated using:
* SOM Emitter emitxtm.dll: 2.22
*/
#define SOM_Module_ctorfullexample_Source
#define VARIABLE_MACROS
#define METHOD_MACROS
#include <ctorFullExample.xih>
#include <stdio.h>
SOM_Scope void SOMLINK AsomDefaultInit(A *somSelf,
somInitCtrl* ctrl)
{
AData *somThis; /* set by BeginInitializer */
somInitCtrl globalCtrl;
somBooleanVector myMask;
AMethodDebug("A","somDefaultInit");
A_BeginInitializer_somDefaultInit;
A_Init_SOMObject_somDefaultInit(somSelf, ctrl);
/*
* local A initialization code added by programmer
*/
_a = 1;
}
SOM_Scope void SOMLINK AsomDestruct(A *somSelf, octet doFree,
somDestructCtrl* ctrl)
{
AData *somThis; /* set by BeginDestructor */
somDestructCtrl globalCtrl;
somBooleanVector myMask;
AMethodDebug("A","somDestruct");
A_BeginDestructor;
/*
* local A deinitialization code added by programmer
*/
A_EndDestructor;
}
SOM_Scope SOMObject* SOMLINK AsomPrintSelf(A *somSelf)
{
AData *somThis = AGetData(somSelf);
AMethodDebug("A","somPrintSelf");
somPrintf("{an instance of %s at location %X with (a=%d)}\n",
_somGetClassName(),somSelf,__get_a((Environment*)0));
return (SOMObject*)((void*)somSelf);
}
SOM_Scope void SOMLINK BBwithInitialValue(B *somSelf,
somInitCtrl* ctrl,
long initialValue)
{
BData *somThis; /* set by BeginInitializer */
somInitCtrl globalCtrl;
somBooleanVector myMask;
BMethodDebug("B","BwithInitialValue");
B_BeginInitializer_withInitialValue;
B_Init_SOMObject_somDefaultInit(somSelf, ctrl);
/*
* local B initialization code added by programmer
*/
_b = initialValue;
}
SOM_Scope void SOMLINK BsomDefaultInit(B *somSelf,
somInitCtrl* ctrl)
{
BData *somThis; /* set by BeginInitializer */
somInitCtrl globalCtrl;
somBooleanVector myMask;
BMethodDebug("B","somDefaultInit");
B_BeginInitializer_somDefaultInit;
B_Init_SOMObject_somDefaultInit(somSelf, ctrl);
/*
* local B initialization code added by programmer
*/
_b = 2;
}
SOM_Scope void SOMLINK BsomDestruct(B *somSelf, octet doFree,
somDestructCtrl* ctrl)
{
BData *somThis; /* set by BeginDestructor */
somDestructCtrl globalCtrl;
somBooleanVector myMask;
BMethodDebug("B","somDestruct");
B_BeginDestructor;
/*
* local B deinitialization code added by programmer
*/
B_EndDestructor;
}
SOM_Scope SOMObject* SOMLINK BsomPrintSelf(B *somSelf)
{
BData *somThis = BGetData(somSelf);
BMethodDebug("B","somPrintSelf");
printf("{an instance of %s at location %X with (b=%d)}\n",
_somGetClassName(),somSelf,__get_b());
return (SOMObject*)((void*)somSelf);
}
Note: The following initializer for a C object accepts a string as an argument, converts this to an integer, and uses this for ancestor initialization of "B." This illustrates how a default ancestor initializer call is replaced with a non-default ancestor initializer call.
SOM_Scope void SOMLINK CCwithInitialString(C *somSelf,
Environment *ev,
somInitCtrl* ctrl,
string initialString)
{
CData *somThis; /* set by BeginInitializer */
somInitCtrl globalCtrl;
somBooleanVector myMask;
CMethodDebug("C","CwithInitialString");
C_BeginInitializer_withInitialString;
C_Init_A_somDefaultInit(somSelf, ctrl);
C_Init_B_BwithInitialValue(somSelf, ctrl,
atoi(initialString)-11);
/*
* local C initialization code added by programmer
*/
_c = atoi(initialString);
}
SOM_Scope void SOMLINK CsomDefaultInit(C *somSelf,
somInitCtrl* ctrl)
{
CData *somThis; /* set by BeginInitializer */
somInitCtrl globalCtrl;
somBooleanVector myMask;
CMethodDebug("C","somDefaultInit");
C_BeginInitializer_somDefaultInit;
C_Init_A_somDefaultInit(somSelf, ctrl);
C_Init_B_somDefaultInit(somSelf, ctrl);
/*
* local C initialization code added by programmer
*/
_c = 3;
}
SOM_Scope void SOMLINK CsomDestruct(C *somSelf, octet doFree,
somDestructCtrl* ctrl)
{
CData *somThis; /* set by BeginDestructor */
somDestructCtrl globalCtrl;
somBooleanVector myMask;
CMethodDebug("C","somDestruct");
C_BeginDestructor;
/*
* local C deinitialization code added by programmer
*/
C_EndDestructor;
}
SOM_Scope SOMObject* SOMLINK CsomPrintSelf(C *somSelf)
{
CData *somThis = CGetData(somSelf);
CMethodDebug("C","somPrintSelf");
printf("{an instance of %s at location %X with"
" (a=%d, b=%d, c=%d)}\n",
_somGetClassName(),somSelf,
__get_a((Environment*)0),
__get_b(),
__get_c((Environment*)0));
return (SOMObject*)((void*)somSelf);
}
SOM_Scope void SOMLINK CCwithInitialValue( C *somSelf,
Environment *ev,
somInitCtrl* ctrl,
long initialValue)
{
CData *somThis; /* set by BeginInitializer */
somInitCtrl globalCtrl;
somBooleanVector myMask;
CMethodDebug("C","CwithInitialValue");
C_BeginInitializer_withInitialValue;
C_Init_A_somDefaultInit(somSelf, ctrl);
C_Init_B_BwithInitialValue(somSelf, ctrl, initialValue-11);
/*
* local C initialization code added by programmer
*/
_c = initialValue;
}
Here is a C++ program that creates instances of "A", "B", and "C" using the initializers defined above.
main()
{
SOM_TraceLevel = 1;
A *a = new A;
a->somPrintSelf();
delete a;
printf("\n");
B *b = new B();
b->somPrintSelf();
delete b;
printf("\n");
b = new B(22);
b->somPrintSelf();
delete b;
printf("\n");
C *c = new C;
c->somPrintSelf();
delete c;
printf("\n");
c = new C((Environment*)0, 44);
c->somPrintSelf();
delete c;
printf("\n");
c = new C((Environment*)0, "66");
c->somPrintSelf();
delete c;
}
The output from the preceding program is as follows:
"ctorFullExample.C": 18: In A:somDefaultInit
"ctorFullExample.C": 48: In A:somPrintSelf
"./ctorFullExample.xih": 292: In A:A_get_a
{an instance of A at location 20063C38 with (a=1)}
"ctorFullExample.C": 35: In A:somDestruct
"ctorFullExample.C": 79: In B:somDefaultInit
"ctorFullExample.C": 110: In B:somPrintSelf
"./ctorFullExample.xih": 655: In B:B_get_b
{an instance of B at location 20064578 with (b=2)}
"ctorFullExample.C": 97: In B:somDestruct
"ctorFullExample.C": 62: In B:BwithInitialValue
"ctorFullExample.C": 110: In B:somPrintSelf
"./ctorFullExample.xih": 655: In B:B_get_b
{an instance of B at location 20064578 with (b=22)}
"ctorFullExample.C": 97: In B:somDestruct
"ctorFullExample.C": 150: In C:somDefaultInit
"ctorFullExample.C": 18: In A:somDefaultInit
"ctorFullExample.C": 79: In B:somDefaultInit
"ctorFullExample.C": 182: In C:somPrintSelf
"./ctorFullExample.xih": 292: In A:A_get_a
"./ctorFullExample.xih": 655: In B:B_get_b
"./ctorFullExample.xih": 1104: In C:C_get_c
{an instance of C at location 20065448 with (a=1, b=2, c=3)}
"ctorFullExample.C": 169: In C:somDestruct
"ctorFullExample.C": 35: In A:somDestruct
"ctorFullExample.C": 97: In B:somDestruct
"ctorFullExample.C": 196: In C:CwithInitialValue
"ctorFullExample.C": 18: In A:somDefaultInit
"ctorFullExample.C": 62: In B:BwithInitialValue
"ctorFullExample.C": 182: In C:somPrintSelf
"./ctorFullExample.xih": 292: In A:A_get_a
"./ctorFullExample.xih": 655: In B:B_get_b
"./ctorFullExample.xih": 1104: In C:C_get_c
{an instance of C at location 20065448 with (a=1, b=33, c=44)}
"ctorFullExample.C": 169: In C:somDestruct
"ctorFullExample.C": 35: In A:somDestruct
"ctorFullExample.C": 97: In B:somDestruct
"ctorFullExample.C": 132: In C:CwithInitialString
"ctorFullExample.C": 18: In A:somDefaultInit
"ctorFullExample.C": 62: In B:BwithInitialValue
"ctorFullExample.C": 182: In C:somPrintSelf
"./ctorFullExample.xih": 292: In A:A_get_a
"./ctorFullExample.xih": 655: In B:B_get_b
"./ctorFullExample.xih": 1104: In C:C_get_c
{an instance of C at location 20065448 with (a=1, b=55, c=66)}
"ctorFullExample.C": 169: In C:somDestruct
"ctorFullExample.C": 35: In A:somDestruct
"ctorFullExample.C": 97: In B:somDestruct