Complete.Org: Mailing Lists: Archives: freeciv-dev: October 2005:
[Freeciv-Dev] (PR#14166) const qualifiers for ai log functions
Home

[Freeciv-Dev] (PR#14166) const qualifiers for ai log functions

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Subject: [Freeciv-Dev] (PR#14166) const qualifiers for ai log functions
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 2 Oct 2005 22:19:01 -0700
Reply-to: bugs@xxxxxxxxxxx

<URL: http://bugs.freeciv.org/Ticket/Display.html?id=14166 >

The AI log functions should take const structures.  Currently this isn't 
the case and it means some callers have to use casts.  This is actually 
an important issue since the log functions MUST not have side effects.

It's not enough to just change the prototypes.  DIPLO_LOG needs some 
changes to the internals: ai_data_get cannot take a const player 
function.  The reason is that ai_data_get may have side effects; 
sometimes it rebuilds the AI data.  So this is a bug!  This patch fixes it.

-jason

Index: ai/aidata.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aidata.c,v
retrieving revision 1.76
diff -p -u -r1.76 aidata.c
--- ai/aidata.c 4 Aug 2005 16:26:11 -0000       1.76
+++ ai/aidata.c 3 Oct 2005 05:13:13 -0000
@@ -558,6 +558,17 @@ struct ai_data *ai_data_get(struct playe
 }
 
 /**************************************************************************
+  Like ai_data_get, but no side effects.
+**************************************************************************/
+const struct ai_dip_intel *ai_diplomacy_get(const struct player *pplayer,
+                                           const struct player *aplayer)
+{
+  const struct ai_data *ai = &aidata[pplayer->player_no];
+
+  return &ai->diplomacy.player_intel[aplayer->player_no];
+}
+
+/**************************************************************************
   Initialize with sane values.
 **************************************************************************/
 void ai_data_init(struct player *pplayer)
Index: ai/aidata.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aidata.h,v
retrieving revision 1.34
diff -p -u -r1.34 aidata.h
--- ai/aidata.h 18 Aug 2005 06:44:26 -0000      1.34
+++ ai/aidata.h 3 Oct 2005 05:13:14 -0000
@@ -168,5 +168,7 @@ void ai_data_init(struct player *pplayer
 void ai_data_analyze_rulesets(struct player *pplayer);
 
 struct ai_data *ai_data_get(struct player *pplayer);
+const struct ai_dip_intel *ai_diplomacy_get(const struct player *pplayer,
+                                           const struct player *aplayer);
 
 #endif
Index: ai/aiguard.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aiguard.c,v
retrieving revision 1.3
diff -p -u -r1.3 aiguard.c
--- ai/aiguard.c        13 Jul 2005 16:49:39 -0000      1.3
+++ ai/aiguard.c        3 Oct 2005 05:13:14 -0000
@@ -83,11 +83,11 @@ void aiguard_check_charge_unit(const str
  
  if (guard && guard->ai.charge != charge->id) {
     /* FIXME: UNIT_LOG should take a const struct * */
-    UNIT_LOG(LOG_DEBUG, (struct unit *)charge,
+    UNIT_LOG(LOG_DEBUG, charge,
              "inconsistent guard references");
   } else if (guard && unit_owner(guard) != charge_owner) {
     /* FIXME: UNIT_LOG should take a const struct * */
-    UNIT_LOG(LOG_DEBUG, (struct unit *)charge, "foreign guard");
+    UNIT_LOG(LOG_DEBUG, charge, "foreign guard");
   }
 }
 
Index: ai/ailog.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/ailog.c,v
retrieving revision 1.31
diff -p -u -r1.31 ailog.c
--- ai/ailog.c  4 Sep 2005 03:03:43 -0000       1.31
+++ ai/ailog.c  3 Oct 2005 05:13:14 -0000
@@ -40,7 +40,7 @@ static int recursion[AIT_LAST];
 /**************************************************************************
   Log player tech messages.
 **************************************************************************/
-void TECH_LOG(int level, struct player *pplayer, Tech_type_id id,
+void TECH_LOG(int level, const struct player *pplayer, Tech_type_id id,
               const char *msg, ...)
 {
   char buffer[500];
@@ -79,23 +79,23 @@ void TECH_LOG(int level, struct player *
     
   where ti is timer, co countdown and lo love for target, who is e.
 **************************************************************************/
-void DIPLO_LOG(int level, struct player *pplayer, struct player *aplayer,
-               const char *msg, ...)
+void DIPLO_LOG(int level, const struct player *pplayer,
+              const struct player *aplayer, const char *msg, ...)
 {
   char buffer[500];
   char buffer2[500];
   va_list ap;
   int minlevel = MIN(LOGLEVEL_PLAYER, level);
-  struct ai_data *ai;
-  struct ai_dip_intel *adip;
+  const struct ai_dip_intel *adip;
 
   if (BV_ISSET(pplayer->debug, PLAYER_DEBUG_DIPLOMACY)) {
     minlevel = LOG_NORMAL;
   } else if (minlevel > fc_log_level) {
     return;
   }
-  ai = ai_data_get(pplayer);
-  adip = &ai->diplomacy.player_intel[aplayer->player_no];
+
+  /* Don't use ai_data_get since it can have side effects. */
+  adip = ai_diplomacy_get(pplayer, aplayer);
 
   my_snprintf(buffer, sizeof(buffer), "%s->%s(l%d,c%d,d%d%s): ", 
               pplayer->name, aplayer->name, 
@@ -118,7 +118,7 @@ void DIPLO_LOG(int level, struct player 
   Log city messages, they will appear like this
     2: c's Romenna(5,35) [s1 d106 u11 g1] must have Archers ...
 **************************************************************************/
-void CITY_LOG(int level, struct city *pcity, const char *msg, ...)
+void CITY_LOG(int level, const struct city *pcity, const char *msg, ...)
 {
   char buffer[500];
   char buffer2[500];
@@ -154,7 +154,7 @@ void CITY_LOG(int level, struct city *pc
   where [] is unit id, ()->() are coordinates present and goto, and
   {,} contains bodyguard and ferryboat ids.
 **************************************************************************/
-void UNIT_LOG(int level, struct unit *punit, const char *msg, ...)
+void UNIT_LOG(int level, const struct unit *punit, const char *msg, ...)
 {
   char buffer[500];
   char buffer2[500];
Index: ai/ailog.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/ailog.h,v
retrieving revision 1.15
diff -p -u -r1.15 ailog.h
--- ai/ailog.h  4 Aug 2005 16:26:12 -0000       1.15
+++ ai/ailog.h  3 Oct 2005 05:13:14 -0000
@@ -71,15 +71,15 @@ enum ai_timer_activity  {
   TIMER_START, TIMER_STOP
 };
 
-void TECH_LOG(int level, struct player *pplayer, Tech_type_id id,
+void TECH_LOG(int level, const struct player *pplayer, Tech_type_id id,
               const char *msg, ...)
      fc__attribute((format (printf, 4, 5)));
-void DIPLO_LOG(int level, struct player *pplayer, struct player *aplayer,
-               const char *msg, ...)
+void DIPLO_LOG(int level, const struct player *pplayer,
+              const struct player *aplayer, const char *msg, ...)
      fc__attribute((format (printf, 4, 5)));
-void CITY_LOG(int level, struct city *pcity, const char *msg, ...)
+void CITY_LOG(int level, const struct city *pcity, const char *msg, ...)
      fc__attribute((format (printf, 3, 4)));
-void UNIT_LOG(int level, struct unit *punit, const char *msg, ...)
+void UNIT_LOG(int level, const struct unit *punit, const char *msg, ...)
      fc__attribute((format (printf, 3, 4)));
 void BODYGUARD_LOG(int level, const struct unit *punit, const char *msg);
 void TIMING_LOG(enum ai_timer timer, enum ai_timer_activity activity);

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#14166) const qualifiers for ai log functions, Jason Short <=