min/max and types

Francois Gouget fgouget at free.fr
Sat Sep 15 02:03:37 CDT 2001


On Fri, 14 Sep 2001, eric pouech wrote:

> just for fun, I tried compiling Wine with Linux latest min/max macros
> definitions

   I think it's an excellent idea. But there are some problems with
their macros: we have duplicate consts :-(
   Example in controls/edit.c:1575

   *x = min(max(*x, es->format_rect.left), es->format_rect.right-1);
   *y = min(max(*y, es->format_rect.top), es->format_rect.bottom-1);

edit.c: In function `EDIT_ConfinePoint':
edit.c:1575: warning: duplicate `const'
edit.c:1576: warning: duplicate `const'


   Still I think it would be a good idea to put them in windef.h on a
more permanent basis. What about the attached patch?

 * removed the consts
 * added a WINE_MINMAX for WineLibe applications that would want to use
this feature
 * removed the undefs and using the same #ifndef for both

   How does all this sound?

PS:
   Alternately we may be able to keep the consts if we cast the result
to typeof(x) (or y). Because in the above case it is because max returns
a 'const int' instead of an int that we have a problem. 'const ints' are
pretty rare otherwise (I think).


--
Francois Gouget         fgouget at free.fr        http://fgouget.free.fr/
           Cahn's Axiom: When all else fails, read the instructions.

-------------- next part --------------
Index: include/windef.h
===================================================================
RCS file: /home/wine/wine/include/windef.h,v
retrieving revision 1.58
diff -u -r1.58 windef.h
--- include/windef.h	2001/07/30 18:57:18	1.58
+++ include/windef.h	2001/09/15 04:27:51
@@ -126,12 +126,37 @@
 
 /* min and max macros */
 #ifndef NOMINMAX
+
+#if (defined(WINE_MINMAX) || defined(__WINE__)) && defined(__GNUC__)
 #ifndef max
+#define max(x,y)                   \
+    ({ typeof(x) _x = x;     \
+       typeof(y) _y = y;     \
+                                   \
+       (void) (&_x == &_y);        \
+                                   \
+       _x > _y ? _x : _y;          \
+    })
+#endif
+#ifndef min
+#define min(x,y)                   \
+    ({ typeof(x) _x = x;     \
+       typeof(y) _y = y;     \
+                                   \
+       (void) (&_x == &_y);        \
+                                   \
+       _x < _y ? _x : _y;          \
+    })
+#endif
+#else
+#ifndef max
 #define max(a,b)   (((a) > (b)) ? (a) : (b))
 #endif
 #ifndef min
 #define min(a,b)   (((a) < (b)) ? (a) : (b))
 #endif
+#endif
+
 #endif  /* NOMINMAX */
 
 #ifndef _MAX_PATH


More information about the wine-devel mailing list