In case anyone is interested, I made a few changes to the User Guide
Gnogaboard example application to make *Users* a protected object for safe
concurrent access.

Attached is a diff showing the changes.

Regards,
David


-- 

David K. Trudgett
IT Systems and Software Engineer
Ph: 0467 312 782 (mobile)
Email: david.trudg...@eclecticse.com.au <dktrudg...@gmail.com>
Web: http://www.eclecticse.com.au/ (blog)
LinkedIn: https://au.linkedin.com/in/david-trudgett-6191ba103
ABN: 87 984 125 899
diff --git a/src/gnogaboard-controller.adb b/src/gnogaboard-controller.adb
index 5e7dd3c..8134a0f 100644
--- a/src/gnogaboard-controller.adb
+++ b/src/gnogaboard-controller.adb
@@ -4,7 +4,50 @@ with GnogaBoard.View;
 
 package body GnogaBoard.Controller is
    
-   Users : Gnoga.Gui.Base.Base_Type_Array;
+   protected Users is
+      procedure Add (View : in GnogaBoard.View.Default_View_Access);
+      procedure Remove (N : in Positive);
+      function Find_User (P : in out Gnoga.Gui.Base.Base_Type'Class) return Natural;
+      procedure Update_User_Views (X1, Y1, X2, Y2 : in Integer);
+   private
+      U : Gnoga.Gui.Base.Base_Type_Array;
+   end Users;
+   
+   protected body Users is
+      
+      procedure Add (View : in GnogaBoard.View.Default_View_Access) is
+      begin
+         U.Append (Gnoga.Gui.Base.Pointer_To_Base_Class (View));
+      end Add;
+      
+      procedure Remove (N : Positive) is
+      begin
+         U.Delete (N);
+      end Remove;
+      
+      function Find_User (P : in out Gnoga.Gui.Base.Base_Type'Class) return Natural is
+      begin
+         return U.Find_Index (P'Unchecked_Access);
+      end Find_User;
+      
+      procedure Update_User_Views (X1, Y1, X2, Y2 : in Integer) is
+      begin
+         for I in U.First_Index .. U.Last_Index loop
+            declare
+               User_View : GnogaBoard.View.Default_View_Access := 
+                             GnogaBoard.View.Default_View_Access (U.Element (I));
+            begin
+               User_View.Draw (X1, Y1, X2, Y2);
+            exception
+               -- Ignore error on attempting to draw to already-invalidated view.
+               when others =>
+                  null;
+            end;
+         end loop;
+      end Update_User_Views;
+
+   end Users;
+      
    
    
    procedure On_Change (Object : in out Gnoga.Gui.Base.Base_Type'Class);
@@ -20,22 +63,16 @@ package body GnogaBoard.Controller is
       use GnogaBoard.View;
       View : Default_View_Type renames Default_View_Type (Object);
    begin
-      for I in Users.First_Index .. Users.Last_Index loop
-         declare
-            User_View : GnogaBoard.View.Default_View_Access := GnogaBoard.View.Default_View_Access (Users.Element (I));
-         begin
-            User_View.Draw (View.X1, View.Y1, View.X2, View.Y2);
-         end;
-      end loop;
+      Users.Update_User_Views (View.X1, View.Y1, View.X2, View.Y2);
    end On_Change;
 
    
    procedure On_Destroy (Object : in out Gnoga.Gui.Base.Base_Type'Class) is
       use Gnoga.Gui.Base.Base_Type_Arrays;
-      N : Integer := Users.Find_Index (Object'Unchecked_Access);
+      N : Natural := Users.Find_User (Object);
    begin
       if N /= No_Index then
-         Users.Delete (N);
+         Users.Remove (N);
       end if;
    end On_Destroy;
    
@@ -50,7 +87,7 @@ package body GnogaBoard.Controller is
    begin
       View.Dynamic;
       View.Create (Main_Window);
-      Users.Append (Gnoga.Gui.Base.Pointer_To_Base_Class (View));
+      Users.Add (View);
       View.On_Change := On_Change'Access;
       View.On_Destroy_Handler (On_Destroy'Access);
    end Default;
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gnoga-list mailing list
Gnoga-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/gnoga-list

Reply via email to