Browse Source

* Do not allow same instance to be attached twice as observer

Michaël Van Canneyt 1 week ago
parent
commit
940a86f869
1 changed files with 7 additions and 4 deletions
  1. 7 4
      packages/fcl-base/src/fpobserver.pp

+ 7 - 4
packages/fcl-base/src/fpobserver.pp

@@ -390,9 +390,10 @@ implementation
 
 
 
 
 Resourcestring
 Resourcestring
-  SErrNotObserver = 'Instance of class %s is not an observer.';
-  SErrInvalidPropertyName = '%s is not a valid published property of class %s';
-  SErrObjectCannotBeObserved = 'Cannot observe an instance of class %d';
+  SErrNotObserver           = 'Instance of class %s is not an observer.';
+  SErrDuplicateObserver     = 'Cannot add an instance of class %s twice as observer';
+  SErrInvalidPropertyName   = '%s is not a valid published property of class %s';
+  SErrObjectCannotBeObserved = 'Cannot observe an instance of class %s';
   sErrInvalidFieldName      = 'No fieldname specified for column %d';
   sErrInvalidFieldName      = 'No fieldname specified for column %d';
   sErrInvalidAlignmentChar  = 'Invalid alignment character "%s" specified for column %d';
   sErrInvalidAlignmentChar  = 'Invalid alignment character "%s" specified for column %d';
   sErrInvalidWidthSpecifier = 'Invalid with "%s" specified for column %d';
   sErrInvalidWidthSpecifier = 'Invalid with "%s" specified for column %d';
@@ -492,7 +493,9 @@ begin
   If Not AObserver.GetInterface(SGUIDObserver,I) then
   If Not AObserver.GetInterface(SGUIDObserver,I) then
     Raise EObserver.CreateFmt(SErrNotObserver,[AObserver.ClassName]);
     Raise EObserver.CreateFmt(SErrNotObserver,[AObserver.ClassName]);
   If not Assigned(FObservers) then
   If not Assigned(FObservers) then
-    FObservers:=TFPList.Create;
+    FObservers:=TFPList.Create
+  else if FObservers.IndexOf(aObserver)<>-1 then
+    Raise EObserver.CreateFmt(SErrDuplicateObserver,[AObserver.ClassName]);
   FObservers.Add(I);
   FObservers.Add(I);
 end;
 end;