Complete.Org: Mailing Lists: Archives: freeciv-dev: August 2005:
[Freeciv-Dev] (PR#13815) change unit_class values to pointers
Home

[Freeciv-Dev] (PR#13815) change unit_class values to pointers

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Subject: [Freeciv-Dev] (PR#13815) change unit_class values to pointers
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 29 Aug 2005 11:09:34 -0700
Reply-to: bugs@xxxxxxxxxxx

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

This patch changes punittype->class, and the unit_class functions, to 
use struct unit_class * pointers rather than Unit_class_id integer 
values.  This makes dereferences easier and makes things typesafe.

-jason

Index: common/unittype.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/unittype.c,v
retrieving revision 1.67
diff -p -u -r1.67 unittype.c
--- common/unittype.c   28 Aug 2005 11:49:33 -0000      1.67
+++ common/unittype.c   29 Aug 2005 18:05:55 -0000
@@ -227,15 +227,12 @@ const char *get_unit_name(const struct u
 }
 
 /**************************************************************************
-...
+  Returns the name of the unit class.
 **************************************************************************/
-const char *unit_class_name(Unit_Class_id id)
+const char *unit_class_name(const struct unit_class *pclass)
 {
-  if ((id >= 0) && (id < UCL_LAST)) {
-    return unit_class_names[id];
-  } else {
-    return "";
-  }
+  assert(pclass != NULL && &unit_classes[pclass->id] == pclass);
+  return unit_class_names[pclass->id];
 }
 
 /**************************************************************************
@@ -360,7 +357,7 @@ struct unit_type *find_unit_type_by_name
   Convert Unit_Class_id names to enum; case insensitive;
   returns UCL_LAST if can't match.
 **************************************************************************/
-Unit_Class_id unit_class_from_str(const char *s)
+struct unit_class *unit_class_from_str(const char *s)
 {
   Unit_Class_id i;
 
@@ -368,10 +365,10 @@ Unit_Class_id unit_class_from_str(const 
 
   for (i = 0; i < UCL_LAST; i++) {
     if (mystrcasecmp(unit_class_names[i], s)==0) {
-      return i;
+      return &unit_classes[i];
     }
   }
-  return UCL_LAST;
+  return NULL;
 }
 
 /**************************************************************************
@@ -694,5 +691,5 @@ void unit_types_free(void)
 ***************************************************************/
 struct unit_class *get_unit_class(const struct unit_type *punittype)
 {
-  return &unit_classes[punittype->class];
+  return punittype->class;
 }
Index: common/unittype.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/unittype.h,v
retrieving revision 1.57
diff -p -u -r1.57 unittype.h
--- common/unittype.h   28 Aug 2005 11:49:33 -0000      1.57
+++ common/unittype.h   29 Aug 2005 18:05:55 -0000
@@ -187,7 +187,7 @@ struct unit_type {
   /* Values for bombardment */
   int bombard_rate;
 
-  Unit_Class_id class;
+  struct unit_class *class;
   
   char *helptext;
 };
@@ -212,7 +212,7 @@ int unit_pop_value(const struct unit_typ
 struct unit_class *get_unit_class(const struct unit_type *punittype);
 const char *unit_name(const struct unit_type *punittype);
 const char *unit_name_orig(const struct unit_type *punittype);
-const char *unit_class_name(Unit_Class_id id);
+const char *unit_class_name(const struct unit_class *pclass);
 
 const char *get_unit_name(const struct unit_type *punittype);
 const char *get_units_with_flag_string(int flag);
@@ -230,7 +230,7 @@ int unit_upgrade_price(const struct play
 struct unit_type *find_unit_type_by_name(const char *name);
 struct unit_type *find_unit_type_by_name_orig(const char *name_orig);
 
-Unit_Class_id unit_class_from_str(const char *s);
+struct unit_class *unit_class_from_str(const char *s);
 enum unit_flag_id unit_flag_from_str(const char *s);
 enum unit_role_id unit_role_from_str(const char *s);
 
Index: server/ruleset.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/ruleset.c,v
retrieving revision 1.280
diff -p -u -r1.280 ruleset.c
--- server/ruleset.c    18 Aug 2005 06:53:31 -0000      1.280
+++ server/ruleset.c    29 Aug 2005 18:05:56 -0000
@@ -1064,26 +1064,27 @@ if (vet_levels_default > MAX_VET_LEVELS 
   /* main stats: */
   unit_type_iterate(u) {
     const int i = u->index;
+    struct unit_class *pclass;
 
     u->impr_requirement = lookup_building(file, sec[i], "impr_req",
                                          FALSE, filename, u->name);
 
     sval = secfile_lookup_str(file, "%s.class", sec[i]);
-    ival = unit_class_from_str(sval);
-    if (ival == UCL_LAST) {
+    pclass = unit_class_from_str(sval);
+    if (!pclass) {
       freelog(LOG_FATAL, "for unit_type \"%s\": bad class %s (%s)",
               u->name, sval, filename);
       exit(EXIT_FAILURE);
     }
-    u->class = ival;
-    switch (ival)
+    u->class = pclass;
+    switch (pclass->id)
     {
     case UCL_MISSILE:
     case UCL_NUCLEAR:
       u->move_type = AIR_MOVING;
       break;
     default:
-      u->move_type = ival;
+      u->move_type = pclass->id;
       break;
     }
     

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#13815) change unit_class values to pointers, Jason Short <=