Complete.Org: Mailing Lists: Archives: freeciv-dev: July 2005:
[Freeciv-Dev] (PR#13412) improvements to struct index fields
Home

[Freeciv-Dev] (PR#13412) improvements to struct index fields

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Subject: [Freeciv-Dev] (PR#13412) improvements to struct index fields
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 5 Jul 2005 09:28:37 -0700
Reply-to: bugs@xxxxxxxxxxx

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

This patch:

1.  Adds an index field to the terrain struct.

1.  Changes the index fields from const int to int.  Previously there
was a hack to initialize these const values, after which their
'const'-ness would protect them from further modification.  However
after doing some research I don't beileve this code was valid C.

-jason

Index: common/game.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/game.c,v
retrieving revision 1.224
diff -p -u -r1.224 game.c
--- common/game.c       2 Jul 2005 15:59:11 -0000       1.224
+++ common/game.c       5 Jul 2005 16:26:51 -0000
@@ -276,6 +276,7 @@ void game_init(void)
 
   init_our_capability();    
   map_init();
+  terrains_init();
   improvements_init();
   techs_init();
   unit_types_init();
Index: common/improvement.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/improvement.c,v
retrieving revision 1.65
diff -p -u -r1.65 improvement.c
--- common/improvement.c        23 Jun 2005 08:01:09 -0000      1.65
+++ common/improvement.c        5 Jul 2005 16:26:51 -0000
@@ -72,11 +72,7 @@ void improvements_init(void)
   for (i = 0; i < ARRAY_SIZE(improvement_types); i++) {
     struct impr_type *p = get_improvement_type(i);
 
-    /* HACK: this field is declared const to keep anyone from changing
-     * them.  But we have to set it somewhere!  This should be the only
-     * place. */
-    *(int *)&p->index = i;
-
+    p->index = i;
     requirement_vector_init(&p->reqs);
   }
 }
Index: common/improvement.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/improvement.h,v
retrieving revision 1.48
diff -p -u -r1.48 improvement.h
--- common/improvement.h        11 May 2005 14:11:21 -0000      1.48
+++ common/improvement.h        5 Jul 2005 16:26:51 -0000
@@ -62,7 +62,7 @@ BV_DEFINE(bv_imprs, B_LAST);
 
 /* Type of improvement. (Read from buildings.ruleset file.) */
 struct impr_type {
-  const int index;  /* Index in improvement_types array */
+  int index;  /* Index in improvement_types array */
   enum impr_genus_id genus;            /* genus; e.g. GreatWonder */
   const char *name; /* Translated string - doesn't need freeing. */
   char name_orig[MAX_LEN_NAME];                /* untranslated */
Index: common/map.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/map.c,v
retrieving revision 1.229
diff -p -u -r1.229 map.c
--- common/map.c        30 Jun 2005 15:43:00 -0000      1.229
+++ common/map.c        5 Jul 2005 16:26:52 -0000
@@ -406,11 +406,11 @@ void map_allocate(void)
     /* HACK: these fields are declared const to keep anyone from changing
      * them.  But we have to set them somewhere!  This should be the only
      * place. */
-    *(int *)&ptile->index = index;
-    *(int *)&ptile->x = map_x;
-    *(int *)&ptile->y = map_y;
-    *(int *)&ptile->nat_x = nat_x;
-    *(int *)&ptile->nat_y = nat_y;
+    ptile->index = index;
+    ptile->x = map_x;
+    ptile->y = map_y;
+    ptile->nat_x = nat_x;
+    ptile->nat_y = nat_y;
 
     tile_init(ptile);
   } whole_map_iterate_end;
Index: common/nation.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/nation.c,v
retrieving revision 1.52
diff -p -u -r1.52 nation.c
--- common/nation.c     14 Jun 2005 18:49:08 -0000      1.52
+++ common/nation.c     5 Jul 2005 16:26:52 -0000
@@ -211,10 +211,7 @@ void nations_alloc(int num)
   game.control.nation_count = num;
 
   for (i = 0; i < num; i++) {
-    /* HACK: this field is declared const to keep anyone from changing
-     * them.  But we have to set it somewhere!  This should be the only
-     * place. */
-    *(int *)&nations[i].index = i;
+    nations[i].index = i;
   }
 }
 
Index: common/nation.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/nation.h,v
retrieving revision 1.49
diff -p -u -r1.49 nation.h
--- common/nation.h     3 Jun 2005 16:46:18 -0000       1.49
+++ common/nation.h     5 Jul 2005 16:26:52 -0000
@@ -68,7 +68,7 @@ struct nation_group {
 };
 
 struct nation_type {
-  const int index;
+  int index;
   /* Pointer values are allocated on load then freed in free_nations(). */
   const char *name; /* Translated string - doesn't need freeing. */
   const char *name_plural; /* Translated string - doesn't need freeing. */
Index: common/tech.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/tech.c,v
retrieving revision 1.97
diff -p -u -r1.97 tech.c
--- common/tech.c       29 Jun 2005 08:21:24 -0000      1.97
+++ common/tech.c       5 Jul 2005 16:26:52 -0000
@@ -647,10 +647,7 @@ void techs_init(void)
   int i;
 
   for (i = 0; i < ARRAY_SIZE(advances); i++) {
-    /* HACK: this field is declared const to keep anyone from changing
-     * them.  But we have to set it somewhere!  This should be the only
-     * place. */
-    *(int *)&advances[i].index = i;
+    advances[i].index = i;
   }
 }
 
Index: common/tech.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/tech.h,v
retrieving revision 1.56
diff -p -u -r1.56 tech.h
--- common/tech.h       5 May 2005 18:32:52 -0000       1.56
+++ common/tech.h       5 Jul 2005 16:26:52 -0000
@@ -79,7 +79,7 @@ enum tech_state {
 };
 
 struct advance {
-  const int index; /* Tech index in tech array. */
+  int index; /* Tech index in tech array. */
   const char *name; /* Translated string - doesn't need freeing. */
   char name_orig[MAX_LEN_NAME];              /* untranslated */
   char graphic_str[MAX_LEN_NAME];      /* which named sprite to use */
Index: common/terrain.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/terrain.c,v
retrieving revision 1.23
diff -p -u -r1.23 terrain.c
--- common/terrain.c    7 Jun 2005 06:17:12 -0000       1.23
+++ common/terrain.c    5 Jul 2005 16:26:52 -0000
@@ -40,6 +40,20 @@ enum tile_special_type infrastructure_sp
   S_LAST
 };
 
+/****************************************************************************
+  Inialize terrain structures.
+****************************************************************************/
+void terrains_init(void)
+{
+  int i;
+
+  for (i = 0; i < ARRAY_SIZE(tile_types); i++) {
+    struct tile_type *t = get_tile_type(i);
+
+    t->index = i;
+  }
+}
+
 /***************************************************************
 ...
 ***************************************************************/
Index: common/terrain.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/terrain.h,v
retrieving revision 1.36
diff -p -u -r1.36 terrain.h
--- common/terrain.h    4 Jul 2005 18:42:27 -0000       1.36
+++ common/terrain.h    5 Jul 2005 16:26:52 -0000
@@ -100,6 +100,7 @@ enum mapgen_terrain_property {
  * it could be extended.
  */
 struct tile_type {
+  int index;
   const char *terrain_name; /* Translated string - doesn't need freeing. */
   char terrain_name_orig[MAX_LEN_NAME];        /* untranslated copy */
   char graphic_str[MAX_LEN_NAME];
@@ -161,6 +162,9 @@ struct tile_type {
 
 extern struct tile_type tile_types[MAX_NUM_TERRAINS];
 
+/* Terrain allocation functions. */
+void terrains_init(void);
+
 /* General terrain accessor functions. */
 struct tile_type *get_tile_type(Terrain_type_id type);
 Terrain_type_id get_terrain_by_name(const char * name);
Index: common/tile.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/tile.h,v
retrieving revision 1.9
diff -p -u -r1.9 tile.h
--- common/tile.h       7 Jun 2005 06:17:13 -0000       1.9
+++ common/tile.h       5 Jul 2005 16:26:52 -0000
@@ -25,9 +25,9 @@
                        ((ptile) ? (ptile)->y : -1)
 
 struct tile {
-  const int x, y; /* Cartesian (map) coordinates of the tile. */
-  const int nat_x, nat_y; /* Native coordinates of the tile. */
-  const int index; /* Index coordinate of the tile. */
+  int x, y; /* Cartesian (map) coordinates of the tile. */
+  int nat_x, nat_y; /* Native coordinates of the tile. */
+  int index; /* Index coordinate of the tile. */
   Terrain_type_id terrain;
   bv_special special;
   struct city *city;        /* city standing on the tile, NULL if none */
Index: common/unittype.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/unittype.c,v
retrieving revision 1.60
diff -p -u -r1.60 unittype.c
--- common/unittype.c   30 Jun 2005 15:43:01 -0000      1.60
+++ common/unittype.c   5 Jul 2005 16:26:53 -0000
@@ -650,10 +650,7 @@ void unit_types_init(void)
   int i;
 
   for (i = 0; i < ARRAY_SIZE(unit_types); i++) {
-    /* HACK: this field is declared const to keep anyone from changing
-     * them.  But we have to set it somewhere!  This should be the only
-     * place. */
-    *(int *)&unit_types[i].index = i;
+    unit_types[i].index = i;
   }
 }
 
Index: common/unittype.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/unittype.h,v
retrieving revision 1.51
diff -p -u -r1.51 unittype.h
--- common/unittype.h   30 Jun 2005 15:43:01 -0000      1.51
+++ common/unittype.h   5 Jul 2005 16:26:53 -0000
@@ -164,7 +164,7 @@ struct veteran_type {
 };
 
 struct unit_type {
-  const int index;
+  int index;
   const char *name; /* Translated string - doesn't need freeing. */
   char name_orig[MAX_LEN_NAME];              /* untranslated */
   char graphic_str[MAX_LEN_NAME];

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#13412) improvements to struct index fields, Jason Short <=