[htdig3-dev] enum-assigment fixed (was: Re: DocumentRef::Serialize)


Hans-Peter Nilsson (hans-peter.nilsson@axis.com)
Tue, 19 Jan 1999 23:53:08 +0100


* List: htdig3-dev@sob.htdig.org

> Date: Tue, 19 Jan 1999 18:09:29 +0100 (MEZ)
> From: Alexander Bergolth <leo@strike.wu-wien.ac.at>

> On Tue, 19 Jan 1999, Hans-Peter Nilsson wrote:

> > There's trickiness in that I cannot assign (as above) and cannot
> > do a straightforward memcpy since I cannot assume the size of
> > any specific enumerated type; it may vary with the enumerations.

Anyway, here's a patch that should fix this. It looks ugly, but
I could think of no better and portable way (thinking of endian,
compiler and type-size issues).

Tue Jan 19 23:44:49 1999 Hans-Peter Nilsson <hp@axis.se>

        * htcommon/DocumentRef.cc (MEMCPY_ASSIGN, NUM_ASSIGN): New macros
        for assigning portably to some possibly-enum numeric type.
        (getnum): Use them.

Index: DocumentRef.cc
===================================================================
RCS file: /opt/htdig/cvs/htdig3/htcommon/DocumentRef.cc,v
retrieving revision 1.18
diff -p -c -r1.18 DocumentRef.cc
*** DocumentRef.cc 1999/01/18 23:15:36 1.18
--- DocumentRef.cc 1999/01/19 22:44:14
*************** void DocumentRef::Deserialize(String &st
*** 390,405 ****
      int x;
      String *str;
  
  
  #define getnum(type, in, var) \
   if (type & CHARSIZE_MARKER_BIT) \
   { \
! var = (int) *(unsigned char *) in; \
     in += sizeof(unsigned char); \
   } \
   else if (type & SHORTSIZE_MARKER_BIT) \
   { \
! var = (int) *(unsigned short int *) in; \
     in += sizeof(unsigned short int); \
   } \
   else \
--- 384,422 ----
      int x;
      String *str;
  
+ // There is a problem with getting a numeric value into a
+ // numeric unknown type that may be an enum (the other way
+ // around is simply by casting (int)).
+ // Supposedly the enum incarnates as a simple type, so we can
+ // just check the size and copy the bits.
+ #define MEMCPY_ASSIGN(to, from, type) \
+ do { \
+ type _tmp = (type) (from); \
+ memcpy((char *) &(to), (char *) &_tmp, sizeof(to)); \
+ } while (0)
+
+ #define NUM_ASSIGN(to, from) \
+ do { \
+ if (sizeof(to) == sizeof(long int)) \
+ MEMCPY_ASSIGN(to, from, long int); \
+ else if (sizeof(to) == sizeof(int)) \
+ MEMCPY_ASSIGN(to, from, int); \
+ else if (sizeof(to) == sizeof(short int)) \
+ MEMCPY_ASSIGN(to, from, short int); \
+ else if (sizeof(to) == sizeof(char)) \
+ MEMCPY_ASSIGN(to, from, char); \
+ /* else fatal error here? */ \
+ } while (0)
  
  #define getnum(type, in, var) \
   if (type & CHARSIZE_MARKER_BIT) \
   { \
! NUM_ASSIGN(var, *(unsigned char *) in); \
     in += sizeof(unsigned char); \
   } \
   else if (type & SHORTSIZE_MARKER_BIT) \
   { \
! NUM_ASSIGN(var, *(unsigned short int *) in); \
     in += sizeof(unsigned short int); \
   } \
   else \

brgds, H-P



This archive was generated by hypermail 2.0b3 on Thu Feb 04 1999 - 22:24:19 PST