Seite 4 von 6

Re: Modul BaseClass (Modul als Objekt)

Verfasst: 16.09.2016 22:24
von Kurzer
Vielen Dank für das Beispiel. Jetzt hab ich es verstanden. :D :allright:

Re: Modul BaseClass (Modul als Objekt)

Verfasst: 17.09.2016 12:36
von mk-soft
Update v1.23
- RefCounter über Mutex geschützt

Bei Verwendung in Thread muss der RefCounter auch über Mutex geschützt werden. Hatte ich vergessen, auch wenn es bei Test bis jetzt ging.
:wink:

Re: Modul BaseClass (Modul als Objekt)

Verfasst: 25.02.2017 13:55
von mk-soft
Update v1.24
Hilfs-Macro CloneObject hinzugefügt da bei kopieren eines Objektes einiges zu beachten ist

Auszug...

Code: Alles auswählen

...
Procedure Clone(*this.sUser)
    Protected *clone.sUser
    CloneObject(*this, *clone, sUser) 
    ProcedureReturn *clone
EndProcedure : AsMethode(Clone)
...  

Re: Modul BaseClass (Modul als Objekt)

Verfasst: 25.02.2017 14:46
von mk-soft
Update v1.25
- Mutex Überprüfung hingefügt
- Macro LockObject und UnlockObject hinzugefügt

Example 9 Update

Code: Alles auswählen

;-TOP

; Example 9

CompilerIf #PB_Compiler_Thread = 0
  CompilerError "Use compiler option theadsafe"
CompilerEndIf

IncludeFile "Modul_BaseClassSmall.pb"

DeclareModule Work

  UseModule BaseClass
  
  Structure sWork Extends sBaseClass
    Value.i
  EndStructure
  
  Interface iWork Extends iBaseClass
    Add(Value)
    Sub(Value)
  EndInterface
  
  Declare New()
  
EndDeclareModule

Module Work
  
  UseModule BaseClass
  
  NewClass(iWork)
  
  ; ---------------------------------------------------------------------------
  
  Procedure Init(*this.sWork)
    Debug "Initialize Work"
  EndProcedure : AsInitializeObject(Init)
  
  ; ---------------------------------------------------------------------------
  
  Procedure Destroy(*this.sWork)
    Debug "Dispose Work"
    Debug "Result: " + *this\Value
  EndProcedure : AsDisposeObject(Destroy)
  
  ; ---------------------------------------------------------------------------
  
  Procedure Add(*this.sWork, Value)
    Protected result
    LockObject(*this)
    *this\Value + Value
    result = *this\Value
    UnlockObject(*this)
    ProcedureReturn result
  EndProcedure : AsMethode(Add)
  
  ; ---------------------------------------------------------------------------
  
  Procedure Sub(*this.sWork, Value = 0)
    Protected result
    LockObject(*this)
    *this\Value - Value
    result = *this\Value
    UnlockObject(*this)
    ProcedureReturn result
  EndProcedure : AsMethode(Sub)
  
  ; ---------------------------------------------------------------------------
  
  Procedure New()
    InitObject(sWork) ; Mehr kommt hier nicht rein!
  EndProcedure
  
  ; ---------------------------------------------------------------------------
  
  CheckInterface()
  
EndModule

; ***************************************************************************************

;-Test AddRef

Procedure thAdd(*Object.Work::iWork)
  Protected time
  *Object\AddRef()
  Delay(1000)
  ;Debug "Start"
  For i = 1 To 10
    time = Random(200)
    *Object\Add(1)
    Delay(time)
  Next
  ;Debug "Ready."
  *Object\Release()
EndProcedure

Debug "Mainscope Create Object"
Define *Object.Work::iWork
*Object = Work::New()

mutex = CreateMutex()
Debug "Start Threads"
For i = 1 To 1000
  th = CreateThread(@thAdd(), *Object)
  Delay(5)
  If th = 0
    Debug "No Thread " + i
  EndIf
Next

Debug "Mainscope Wait..."
Repeat
  Delay(200)
  ref = *Object\AddRef()
  ref = *Object\Release()
  Debug ref
  If ref = 0
    Break
  EndIf
ForEver
Debug "Mainscope Release Object"
*Object\Release()

Debug "Ready."

Re: Modul BaseClass (Modul als Objekt)

Verfasst: 26.02.2017 14:54
von Kurzer
Besten Dank für die Erweiterungen, mk-soft. :allright:
Habe meine lokale Kopie Deines Moduls aktualisiert.

Edit:
Ich habe Dein aktuelles Beispiel nun mal ausprobiert.
Ist die Aktivierung der Compileroption "Threadsichere Exe erzeugen" Pflicht - oder sollte Dein Beispiel auch ohne diese Option laufen?

Ohne die Option schmiert das Beispiel nämlich ab (Win7 x64, PB 5.51 64 Bit Compiler):

Code: Alles auswählen

[14:37:44] Warte auf den Start des Executable...
[14:37:44] Executable-Typ: Windows - x64  (64bit, Unicode)
[14:37:44] Executable gestartet.
[14:37:44] [Debug] Mainscope Create Object
[14:37:44] [Debug] Initialize Work
[14:37:44] [Debug] Start Threads
[14:37:47] Das mit dem Debugger getestete Executable endete unerwartet.

Re: Modul BaseClass (Modul als Objekt)

Verfasst: 03.03.2017 20:12
von mk-soft
Ohne schmiert es bei mir auch ab. Muss am Stringmanager liegen oder am Debugger.

Re: Modul BaseClass (Modul als Objekt)

Verfasst: 03.03.2017 20:14
von mk-soft
Update v1.26
- Hinzugefügt: QuerInterface für Kompatibilität zu Interface 'IUnknown'
-- Das Standardergebnis von QuerInterface ist die Konstante '#E_NoInterface' um für Linux und Mac kompatibel zu bleiben
-- Um die Methode 'QuerInterface' mit 'AsNewMethode' zu managen siehe das Beispiel 10 an. Link: http://www.purebasic.fr/english/viewtop ... 05#p503757
- Geändert: RefCounter beginnt jetzt mit Null

Hilfscode zum erzeugen einer eigenen Uuid

Code: Alles auswählen

Procedure.s CreateUuid()
  Protected Uuid.iid, result.s, i
  UuidCreate_(Uuid.iid)
  result = "  DataSection" + #LF$
  result + "    Uuid:" + #LF$
  result + "    Data.l $" + RSet(Hex(Uuid\Data1), 8, "0") + #LF$
  result + "    Data.w $" + RSet(Hex(Uuid\Data2), 4, "0") + ", $" + RSet(Hex(Uuid\Data3), 4, "0") + #LF$
  result + "    Data.b $" + RSet(Hex(Uuid\Data4[0]), 2, "0")
  For i = 1 To 7
    result + ", $" + RSet(Hex(Uuid\Data4[i]), 2, "0")
  Next
  result + #LF$
  result + "  EndDataSection" + #LF$
  ProcedureReturn result
EndProcedure

; ***************************************************************************************

Uuid.s = CreateUuid()
SetClipboardText(Uuid)
Debug Uuid
Debug "Copied into clipboard"
:wink:

Re: Modul BaseClass (Modul als Objekt)

Verfasst: 14.04.2017 15:48
von mk-soft
Update v1.27
- Bugfix BaseClass als DLL: Über Macros erstelle Codes in Prozeduren gekapselt, da auf machen Windows 7 (x64) Maschinen es zum Absturz führte
- Code bereinigt und interne Namen geändert

Re: Modul BaseClass (Modul als Objekt)

Verfasst: 16.04.2017 14:06
von mk-soft
Update v1.28
- Interne BaseClass Management geändert wegen den regeln zur Erstellung von DLL´s

Re: Modul BaseClass (Modul als Objekt)

Verfasst: 01.05.2017 22:00
von GPI
Hiho, ich hab mir gerade mal den Quellcode angeschaut und hätte zwei Fragen:

a) Seh ich das richtig, das es keine Copy-Konstruktoren gibt?
Können praktisch sein, wenn man bspw. ein Image in der Klasse gespeichert hat. Mit einen Copy-Konstruktor kann man dafür sorgen, dass das neue Objekt nicht auf das Original-Image verweist, sondern davon auch eine Kopie erstellt.

b) Reihenfolge der Konstruktoren/Destruktoren
Wenn ich richtig sehe, werden beide in der gleichen Reihenfolge aufgerufen.
Wenn meine Informationen nicht falsch sind, sollte ein Konstruktor in der Reihenfolge ParentClass,ChildClass aufgerufen werden und die Destruktoren umgekehrter Reihenfolge, also ChildClass, dann ParentClass. Mag zwar in 99% der Fälle keinen Unterschied machen, könnte aber eventuell mal praktisch sein.