Skip to content

Commit 4631180

Browse files
author
Mats Kindahl
committed
WL#5825: Using C++ Standard Library with MySQL code
This patch will make it possible to use the C++ Standard Library by removing the min/max macros from the code. The patch makes the following changes: - Define macros MY_MIN and MY_MAX instead of min and max. - Use MY_MIN/MY_MAX in C code - Use MY_MIN/MY_MAX in array declarations - Include <algorithm> to get min/max from std - Add using-declarations in source files - Use std:: prefix for min/max in header files - Turning some constant declarations unsigned - Removing an overloaded use of 'min' as both macro name and variable. - It enables C++ builds for all files
1 parent b2677a5 commit 4631180

File tree

119 files changed

+557
-323
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

119 files changed

+557
-323
lines changed

BUILD/SETUP.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ if test -z "$CC" ; then
207207
fi
208208

209209
if test -z "$CXX" ; then
210-
CXX=gcc
210+
CXX=g++
211211
fi
212212

213213
# If ccache (a compiler cache which reduces build time)

client/mysql.cc

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
#include <signal.h>
4040
#include <violite.h>
4141

42+
#include <algorithm>
43+
44+
using std::min;
45+
using std::max;
46+
4247
#if defined(USE_LIBEDIT_INTERFACE) && defined(HAVE_LOCALE_H)
4348
#include <locale.h>
4449
#endif
@@ -3324,9 +3329,9 @@ print_table_data(MYSQL_RES *result)
33243329
{
33253330
uint length= column_names ? field->name_length : 0;
33263331
if (quick)
3327-
length=max(length,field->length);
3332+
length= max<size_t>(length, field->length);
33283333
else
3329-
length=max(length,field->max_length);
3334+
length= max<size_t>(length, field->max_length);
33303335
if (length < 4 && !IS_NOT_NULL(field->flags))
33313336
length=4; // Room for "NULL"
33323337
field->max_length=length;
@@ -3346,8 +3351,8 @@ print_table_data(MYSQL_RES *result)
33463351
field->name,
33473352
field->name + name_length);
33483353
uint display_length= field->max_length + name_length - numcells;
3349-
tee_fprintf(PAGER, " %-*s |",(int) min(display_length,
3350-
MAX_COLUMN_LENGTH),
3354+
tee_fprintf(PAGER, " %-*s |",
3355+
min<int>(display_length, MAX_COLUMN_LENGTH),
33513356
field->name);
33523357
num_flag[off]= IS_NUM(field->type);
33533358
}
@@ -3436,9 +3441,9 @@ static int get_field_disp_length(MYSQL_FIELD *field)
34363441
uint length= column_names ? field->name_length : 0;
34373442

34383443
if (quick)
3439-
length= max(length, field->length);
3444+
length= max<uint>(length, field->length);
34403445
else
3441-
length= max(length, field->max_length);
3446+
length= max<uint>(length, field->max_length);
34423447

34433448
if (length < 4 && !IS_NOT_NULL(field->flags))
34443449
length= 4; /* Room for "NULL" */

client/mysql_upgrade.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ static int extract_variable_from_show(DYNAMIC_STRING* ds, char* value)
546546
if ((value_end= strchr(value_start, '\n')) == NULL)
547547
return 1; /* Unexpected result */
548548

549-
strncpy(value, value_start, min(FN_REFLEN, value_end-value_start));
549+
strncpy(value, value_start, MY_MIN(FN_REFLEN, value_end - value_start));
550550
return 0;
551551
}
552552

client/mysqlbinlog.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@
3939
#include "sql_common.h"
4040
#include <welcome_copyright_notice.h> // ORACLE_WELCOME_COPYRIGHT_NOTICE
4141

42-
#define BIN_LOG_HEADER_SIZE 4
42+
#include <algorithm>
43+
44+
using std::min;
45+
using std::max;
46+
47+
#define BIN_LOG_HEADER_SIZE 4U
4348
#define PROBE_HEADER_LEN (EVENT_LEN_OFFSET+4)
4449

4550

@@ -2073,7 +2078,7 @@ static Exit_status dump_local_log_entries(PRINT_EVENT_INFO *print_event_info,
20732078
my_off_t length,tmp;
20742079
for (length= start_position_mot ; length > 0 ; length-=tmp)
20752080
{
2076-
tmp=min(length,sizeof(buff));
2081+
tmp= min<size_t>(length, sizeof(buff));
20772082
if (my_b_read(file, buff, (uint) tmp))
20782083
{
20792084
error("Failed reading from file.");

client/mysqldump.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,7 @@ get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
845845
&err_ptr, &err_len);
846846
if (err_len)
847847
{
848-
strmake(buff, err_ptr, min(sizeof(buff) - 1, err_len));
848+
strmake(buff, err_ptr, MY_MIN(sizeof(buff) - 1, err_len));
849849
fprintf(stderr, "Invalid mode to --compatible: %s\n", buff);
850850
exit(1);
851851
}
@@ -4668,7 +4668,7 @@ static ulong find_set(TYPELIB *lib, const char *x, uint length,
46684668

46694669
for (; pos != end && *pos != ','; pos++) ;
46704670
var_len= (uint) (pos - start);
4671-
strmake(buff, start, min(sizeof(buff) - 1, var_len));
4671+
strmake(buff, start, MY_MIN(sizeof(buff) - 1, var_len));
46724672
find= find_type(buff, lib, FIND_TYPE_BASIC);
46734673
if (!find)
46744674
{

client/mysqltest.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@
5454

5555
#include <welcome_copyright_notice.h> // ORACLE_WELCOME_COPYRIGHT_NOTICE
5656

57+
#include <algorithm>
58+
59+
using std::min;
60+
using std::max;
61+
5762
#ifdef __WIN__
5863
#include <crtdbg.h>
5964
#define SIGNAL_FMT "exception 0x%x"
@@ -5920,9 +5925,9 @@ int read_line(char *buf, int size)
59205925
}
59215926
else if ((c == '{' &&
59225927
(!my_strnncoll_simple(charset_info, (const uchar*) "while", 5,
5923-
(uchar*) buf, min(5, p - buf), 0) ||
5928+
(uchar*) buf, min<my_ptrdiff_t>(5, p - buf), 0) ||
59245929
!my_strnncoll_simple(charset_info, (const uchar*) "if", 2,
5925-
(uchar*) buf, min(2, p - buf), 0))))
5930+
(uchar*) buf, min<my_ptrdiff_t>(2, p - buf), 0))))
59265931
{
59275932
/* Only if and while commands can be terminated by { */
59285933
*p++= c;

client/sql_string.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323

2424
#include "sql_string.h"
2525

26+
#include <algorithm>
27+
28+
using std::min;
29+
using std::max;
30+
2631
/*****************************************************************************
2732
** String functions
2833
*****************************************************************************/

config.h.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@
381381

382382
#cmakedefine HAVE_MBSTATE_T
383383

384-
#define MAX_INDEXES 64
384+
#define MAX_INDEXES 64U
385385

386386
#cmakedefine QSORT_TYPE_IS_VOID 1
387387
#cmakedefine RETQSORTTYPE @RETQSORTTYPE@

dbug/dbug.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,7 +1323,7 @@ void _db_dump_(uint _line_, const char *keyword,
13231323
if (TRACING)
13241324
{
13251325
Indent(cs, cs->level + 1);
1326-
pos= min(max(cs->level-cs->stack->sub_level,0)*INDENT,80);
1326+
pos= MY_MIN(MY_MAX(cs->level-cs->stack->sub_level,0)*INDENT,80);
13271327
}
13281328
else
13291329
{
@@ -1743,7 +1743,7 @@ static void Indent(CODE_STATE *cs, int indent)
17431743
{
17441744
REGISTER int count;
17451745

1746-
indent= max(indent-1-cs->stack->sub_level,0)*INDENT;
1746+
indent= MY_MAX(indent-1-cs->stack->sub_level,0)*INDENT;
17471747
for (count= 0; count < indent ; count++)
17481748
{
17491749
if ((count % INDENT) == 0)

include/m_string.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ size_t my_gcvt(double x, my_gcvt_arg_type type, int width, char *to,
156156
(DBL_DIG + 2) significant digits + sign + "." + ("e-NNN" or
157157
MAX_DECPT_FOR_F_FORMAT zeros for cases when |x|<1 and the 'f' format is used).
158158
*/
159-
#define MY_GCVT_MAX_FIELD_WIDTH (DBL_DIG + 4 + max(5, MAX_DECPT_FOR_F_FORMAT)) \
159+
#define MY_GCVT_MAX_FIELD_WIDTH (DBL_DIG + 4 + MY_MAX(5, MAX_DECPT_FOR_F_FORMAT)) \
160160

161161
extern char *llstr(longlong value,char *buff);
162162
extern char *ullstr(longlong value,char *buff);

0 commit comments

Comments
 (0)