INIT

 (Available in 01 TS COM - TS_COM_IUnknown)

Purpose

Create a 'TS_IUnknown' instance

Syntax

oRoot0:=TS_IUnknown{ [oIOwner], [symNameSym], [lCountThisComponent] }

Arguments

uoIOwner
NIL, OBJECT(TS_IUnknown)
usymNameSym
NIL, SYMBOL
ulCountThisComponent
NIL, LOGIC

Description

This class serves no purpose on its own, the class should be used as a derived from class when defining you own application classes.

Example

CLASS SomeIClass INHERIT TS_IUnknown

METHOD INIT( ..., ...  ; ...
...) CLASS SomeIClass
// Less secure ( and simple ) form of an '[less]safe' init method
// It is better not to call any object method/access/assign from
// inside the init method, if needed do this form the 'New()' method.
// From here on insert you own initialization code
// Process the parameters
   ..., ... 
   IF lParameterError
      TSTrace Warning "... parameter error ..."
      SELF:InvalidateObject( )
   END
// At this point all initialization code should be done
   IF SELF:IsValidObject
//    The 'New()' method will be called by the SUPER class
      SUPER:INIT( ... ; ...
     ... , ... )
   ELSE
      TSTrace Warning TS_TEXT_GEN_CAN_NOT_INITIALIZE
      SELF:InvalidateObject( )
   END
RETURN SELF

METHOD INIT( ..., ...  ; ...
...) CLASS SomeIClass
// Most secure ( and complex ) form of an 'safe' init method
// It is better not to call any object method/access/assign from
// inside the init method, if needed do this form the 'New()' method.
   DO CASE
   CASE SELF:IsInited
      TSTrace Warning "Can not re-init"
   CASE SELF:IsInInit
      SELF:CountInInit( TRUE )
//    From here on insert you own initialization code
//    Process the parameters
      ..., ... 
      IF lParameterError
         TSTrace Warning "... parameter error ..."
         SELF:InvalidateObject( )
      END
//    At this point all initialization code should be done
      SELF:CountInInit( FALSE )
      IF SELF:IsValidObject
         SUPER:INIT( ... ; ...
        ... , ... )
         IF SELF:NeedNew
            SELF:NeedNew:=FALSE
//          Put your 'PostInit()' code in the 'New()' method
            SELF:NEW( ... ; ...
           ... , ... )
         END
      ELSE
         TSTrace Warning TS_TEXT_GEN_CAN_NOT_INITIALIZE
         SELF:InvalidateObject( )
      END
   OTHERWISE
      TSTrace Warning "This can not happen"
   END
RETURN SELF

Source

METHOD INIT( uoIOwner, usymNameSym, ulCountThisComponent ) CLASS TS_IUnknown
LOCAL lCountThisComponent AS LOGIC
LOCAL oServerInfoGuard AS TS_ServerInfoGuard
LOCAL cAccessTypeList AS STRING
LOCAL oCOMGlobalData AS _TS_COMGlobalData
LOCAL oServerInfo AS TS_ServerInfo
LOCAL oIOwner AS TS_IUnknown
LOCAL hResult AS LONG
LOCAL lInit AS LOGIC
   TSTrace Enter CREATE
   oCOMGlobalData:=_TS_ModuleGetCOMGlobalData( )
   #IFDEF TS_COMGLOBALDATA_CRITICALSECTION
      EnterCriticalSection( oCOMGlobalData:_COMGGeneralPurposeSection )
   #ENDIF
   IF TS_IsValidObject( uoIOwner, _OR( TS_ISVALID_NULL_OBJECT, TS_ISVALID_STATIC_IUNKNOWN  ; ...
  ... ) )
      oIOwner:=uoIOwner
   ELSE
      oIOwner:=NULL_OBJECT
   END
   IF LOGIC( _CAST, _AND( SELF:_ObjectStat1, TS_OBJSTAT1_ININIT  ; ...
  ... ) )
      SELF:_ObjectStat1:=_AND( SELF:_ObjectStat1 ; ...
     ... +TS_OBJSTAT1_COUNTVALUE, _NOT( TS_OBJSTAT1_ININIT ) )
      cAccessTypeList:=TS_AccessTypeList( uoIOwner, usymNameSym, ulCountThisComponent  ; ...
     ... )
      lInit:=TRUE
      DO CASE
      CASE CharPos( cAccessTypeList, 1 )=="U"
//       Do nothing
      CASE CharPos( cAccessTypeList, 1 )=="O"
         DO CASE
         CASE Empty( uoIOwner )
//          Already set above
         CASE TS_IsInstanceOf( uoIOwner, #TS_IUnknown )
//          Already set above
         OTHERWISE
            TSTrace Warning "Invalid parameter, 'uoIOwner' must be derived from 'TS_IUnknown'"
            SELF:InvalidateObject( )
            lInit:=FALSE
         END
      OTHERWISE
         TSTrace Warning "Invalid parameter, 'uoIOwner' must be 'NIL' or derived " ; ...
        ... +"from 'TS_IUnknown'"
         SELF:InvalidateObject( )
         lInit:=FALSE
      END
      DO CASE
      CASE CharPos( cAccessTypeList, 2 )=="U"
//       Do nothing
      CASE CharPos( cAccessTypeList, 2 )=="#"
         IF usymNameSym<>NULL_SYMBOL
            SELF:_NameSym:=usymNameSym
         END
      OTHERWISE
         TSTrace Warning "Invalid parameter, 'usymNameSym' must be 'NIL' or 'SYMBOL'"
         SELF:InvalidateObject( )
         lInit:=FALSE
      END
      DO CASE
      CASE CharPos( cAccessTypeList, 3 )=="U"
//       Do nothing
      CASE CharPos( cAccessTypeList, 3 )=="L"
//       Do nothing
      OTHERWISE
         TSTrace Warning "Invalid parameter, 'ulCountThisComponent' must be 'NIL' " ; ...
        ... +"or 'LOGIC'"
         SELF:InvalidateObject( )
         lInit:=FALSE
      END
      DO CASE
      CASE IsDynPtr( PTR( _CAST, SELF ) )
         TSTrace Warning "IsDynPtr(PTR(_CAST,SELF))"
         SELF:InvalidateObject( )
      CASE LOGIC( _CAST, SELF:_SORID )
         TSTrace Warning "LOGIC(_CAST,SELF:_SORID)"
         SELF:InvalidateObject( )
      CASE lInit
         oServerInfoGuard:=TS_ServerInfoGuard{ }
         oServerInfo:=oServerInfoGuard:Get( COINIT_DEFAULT, NULL_PTR )
         IF !SELF:IsInvalidated
            IF oIOwner<>NULL_OBJECT
               IF !TS_IsValidObject( oIOwner, TS_ISVALID_STATIC_IUNKNOWN )
                  TSTrace Warning "!TS_IsValidObject(oIOwner,TS_ISVALID_STATIC_IUNKNOWN)"
                  SELF:InvalidateObject( )
               END
            END
         END
         IF SELF:_OLE32LibraryInited==0U
            IF SELF:IsInvalidated
               SELF:_OLE32LibraryInited:=2U
            ELSE
               IF SELF:_CoInitMode==COINIT_NONE
                  SELF:_CoInitMode:=oServerInfo:CoInit
               END
               hResult:=S_OK
               IF oServerInfo:OLE32LibraryLoad( SELF:_CoInitMode ; ...
              ... , @hResult )
                  SELF:_OLE32LibraryInited:=1U
               ELSE
                  SELF:_OLE32LibraryInited:=2U
                  TSTrace Warning "!oServerInfo:OLE32LibraryLoad(SELF:_CoInitMode" ; ...
                 ... +",@hResult)"
                  SELF:InvalidateObject( )
               END
            END
         END
         IF !SELF:IsInvalidated
            SELF:_SORID:=oServerInfo:StaticObjectsRepo:Add ; ...
           ... ( SELF )
            IF LOGIC( _CAST, SELF:_SORID )
               IF SELF:_IOwnedsArrayPP<>NULL_PTR .OR. SELF:_IOwnedsCount ; ...
              ... <>0U
                  TSTrace Warning "SELF:_IOwnedsArrayPP<>NULL_PTR .OR. SELF:_IOwnedsCount<>0U"
                  SELF:InvalidateObject( )
               ELSE
                  SELF:_IOwnedsArrayPP:=TS_CoTaskMemAlloc( DWORD ; ...
                 ... ( _CAST, _SIZEOF( PTR ) )*SELF:_IOwnedsMax  ; ...
                 ... )
                  IF SELF:_IOwnedsArrayPP==NULL_PTR
                     TSTrace Fatal "SELF:_IOwnedsArrayPP==NULL_PTR"
                     SELF:_IOwnedsMax:=0U
                     SELF:InvalidateObject( )
                  ELSE
                     IF oIOwner<>NULL_OBJECT
                        SELF:_ObjectStat2:=_OR( SELF:_ObjectStat2 ; ...
                       ... , TS_OBJSTAT2_REGISTER_ENABLED )
                        IF SELF:_InternalRegisterIOwner( oIOwner  ; ...
                       ... )
                           oIOwner:=NULL_OBJECT
                        END
                        SELF:_ObjectStat2:=_OR( SELF:_ObjectStat2 ; ...
                       ... , TS_OBJSTAT2_REGISTER_DISABLED )
                     END
                     IF SELF:_CountThisComponent==0U
                        IF SELF:IsInvalidated
                           SELF:_CountThisComponent:=2U
                        ELSE
                           IF IsLogic( ulCountThisComponent )
                              lCountThisComponent:=ulCountThisComponent
                           ELSE
                              lCountThisComponent:=TRUE
                           END
                           IF lCountThisComponent
                              oServerInfo:ActiveComponentsIncrement( )
                              SELF:_CountThisComponent:=1U
                           ELSE
                              SELF:_CountThisComponent:=2U
                           END
                        END
                     END
                  END
               END
            ELSE
               TSTrace Warning "!LOGIC(_CAST,SELF:_SORID:=oServerInfo:StaticObjectsRepo" ; ...
              ... +":Add(SELF))"
               SELF:InvalidateObject( )
            END
         END
         oServerInfo:=oServerInfoGuard:FREEREF( )
         oServerInfoGuard:=NULL_OBJECT
      END
      SELF:_ObjectStat1-=TS_OBJSTAT1_COUNTVALUE
      IF _AND( SELF:_ObjectStat1, TS_OBJSTAT1_NEWTEST )==TS_OBJSTAT1_NEEDNEW
         SELF:_ObjectStat1:=_AND( SELF:_ObjectStat1 ; ...
        ... , _NOT( TS_OBJSTAT1_NEEDNEW ) )
         IF SELF:IsValidObject
            SELF:NEW( uoIOwner, usymNameSym, ulCountThisComponent  ; ...
           ... )
         ELSE
            TSTrace Warning TS_TEXT_GEN_CAN_NOT_INITIALIZE
            SELF:InvalidateObject( )
         END
      END
   ELSE
      TSTrace Warning "!LOGIC(_CAST,_And(SELF:_ObjectStat1,TS_OBJECT_STATUS_ININIT))"
   END
   IF oIOwner<>NULL_OBJECT
      oIOwner:Release( )
      oIOwner:=NULL_OBJECT
   END
   #IFDEF TS_COMGLOBALDATA_CRITICALSECTION
      LeaveCriticalSection( oCOMGlobalData:_COMGGeneralPurposeSection )
   #ENDIF
   TSTrace Leave
RETURN SELF