Skip to content

Commit 5ca109b

Browse files
committed
Factor complex data_stream into one method
And let's support that regardless of what LIBMESH_ENABLE_COMPLEX has done with Number. We already accidentally made std::complex a dependency of xdr_cxx.h, though we hadn't yet removed the ifdefs for <complex>.
1 parent 23a311d commit 5ca109b

2 files changed

Lines changed: 23 additions & 127 deletions

File tree

include/utils/xdr_cxx.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,10 @@
3636
#include "libmesh/restore_warnings.h"
3737
#endif
3838

39+
#include <complex>
3940
#include <iosfwd>
4041
#include <vector>
4142
#include <string>
42-
#ifdef LIBMESH_USE_COMPLEX_NUMBERS
43-
# include <complex>
44-
#endif
4543

4644
const unsigned int xdr_MAX_STRING_LENGTH=256;
4745

@@ -197,6 +195,13 @@ class Xdr
197195
template <typename T>
198196
void do_write(std::vector<std::complex<T>> & a);
199197

198+
/**
199+
* Helper method for complex types
200+
*/
201+
template <typename T>
202+
void _complex_data_stream (std::complex<T> * val, const unsigned int len,
203+
const unsigned int line_break);
204+
200205
/**
201206
* Helper method for extended FP types
202207
*/

src/utils/xdr_cxx.C

Lines changed: 15 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,134 +1103,24 @@ void Xdr::_xfp_data_stream (XFP * val, const unsigned int len,
11031103

11041104

11051105

1106-
#ifdef LIBMESH_USE_COMPLEX_NUMBERS
11071106
template <>
11081107
void Xdr::data_stream (std::complex<double> * val, const unsigned int len, const unsigned int line_break)
11091108
{
1110-
switch (mode)
1111-
{
1112-
case ENCODE:
1113-
case DECODE:
1114-
{
1115-
#ifdef LIBMESH_HAVE_XDR
1116-
1117-
libmesh_assert (this->is_open());
1118-
1119-
1120-
if (len > 0)
1121-
{
1122-
std::vector<double> io_buffer (2*len);
1123-
1124-
// Fill io_buffer if we are writing.
1125-
if (mode == ENCODE)
1126-
for (unsigned int i=0, cnt=0; i<len; i++)
1127-
{
1128-
io_buffer[cnt++] = val[i].real();
1129-
io_buffer[cnt++] = val[i].imag();
1130-
}
1131-
1132-
xdr_vector(xdrs.get(),
1133-
reinterpret_cast<char *>(io_buffer.data()),
1134-
2*len,
1135-
sizeof(double),
1136-
(xdrproc_t) xdr_double);
1137-
1138-
// Fill val array if we are reading.
1139-
if (mode == DECODE)
1140-
for (unsigned int i=0, cnt=0; i<len; i++)
1141-
{
1142-
double re = io_buffer[cnt++];
1143-
double im = io_buffer[cnt++];
1144-
val[i] = std::complex<double>(re,im);
1145-
}
1146-
}
1147-
#else
1148-
1149-
libmesh_error_msg("ERROR: Functionality is not available.\n" \
1150-
<< "Make sure LIBMESH_HAVE_XDR is defined at build time\n" \
1151-
<< "The XDR interface is not available in this installation");
1152-
1153-
#endif
1154-
return;
1155-
}
1156-
1157-
case READ:
1158-
{
1159-
libmesh_assert(in.get());
1160-
libmesh_assert (in->good());
1161-
1162-
for (unsigned int i=0; i<len; i++)
1163-
{
1164-
libmesh_assert(in.get());
1165-
libmesh_assert (in->good());
1166-
double re, im;
1167-
*in >> re >> im;
1168-
val[i] = std::complex<double>(re,im);
1169-
}
1170-
1171-
return;
1172-
}
1109+
this->_complex_data_stream(val, len, line_break);
1110+
}
11731111

1174-
case WRITE:
1175-
{
1176-
libmesh_assert(out.get());
1177-
libmesh_assert (out->good());
11781112

1179-
// Save stream flags
1180-
std::ios_base::fmtflags out_flags = out->flags();
11811113

1182-
// We will use scientific notation sufficient to exactly
1183-
// represent our floating point precision in the following
1184-
// output. The desired precision and format will
1185-
// automatically determine the width.
1186-
*out << std::scientific
1187-
<< std::setprecision(std::numeric_limits<double>::max_digits10);
1188-
1189-
if (line_break == libMesh::invalid_uint)
1190-
for (unsigned int i=0; i<len; i++)
1191-
{
1192-
libmesh_assert(out.get());
1193-
libmesh_assert (out->good());
1194-
*out << val[i].real() << ' ';
1195-
*out << val[i].imag() << ' ';
1196-
}
1197-
else
1198-
{
1199-
const unsigned imax = std::min(line_break, len);
1200-
unsigned int cnt=0;
1201-
while (cnt < len)
1202-
{
1203-
for (unsigned int i=0; (i<imax && cnt<len); i++)
1204-
{
1205-
libmesh_assert(out.get());
1206-
libmesh_assert (out->good());
1207-
*out << val[cnt].real() << ' ';
1208-
*out << val[cnt].imag();
1209-
cnt++;
1210-
1211-
// Write a space unless this is the last character on the current line.
1212-
if (i+1 != imax)
1213-
*out << " ";
1214-
}
1215-
libmesh_assert(out.get());
1216-
libmesh_assert (out->good());
1217-
*out << '\n';
1218-
}
1219-
}
1220-
1221-
// Restore stream flags
1222-
out->flags(out_flags);
1114+
template <>
1115+
void Xdr::data_stream (std::complex<long double> * val, const unsigned int len, const unsigned int line_break)
1116+
{
1117+
this->_complex_data_stream(val, len, line_break);
1118+
}
12231119

1224-
return;
1225-
}
12261120

1227-
default:
1228-
libmesh_error_msg("Invalid mode = " << mode);
1229-
}
1230-
}
12311121

1232-
template <>
1233-
void Xdr::data_stream (std::complex<long double> * val, const unsigned int len, const unsigned int line_break)
1122+
template <typename T>
1123+
void Xdr::_complex_data_stream (std::complex<T> * val, const unsigned int len, const unsigned int line_break)
12341124
{
12351125
switch (mode)
12361126
{
@@ -1271,7 +1161,7 @@ void Xdr::data_stream (std::complex<long double> * val, const unsigned int len,
12711161
{
12721162
double re = io_buffer[cnt++];
12731163
double im = io_buffer[cnt++];
1274-
val[i] = std::complex<long double>(re, im);
1164+
val[i] = std::complex<T>(re, im);
12751165
}
12761166
}
12771167
#else
@@ -1293,9 +1183,9 @@ void Xdr::data_stream (std::complex<long double> * val, const unsigned int len,
12931183
{
12941184
libmesh_assert(in.get());
12951185
libmesh_assert (in->good());
1296-
long double re, im;
1186+
T re, im;
12971187
*in >> re >> im;
1298-
val[i] = std::complex<long double>(re,im);
1188+
val[i] = std::complex<T>(re,im);
12991189
}
13001190

13011191
return;
@@ -1317,7 +1207,7 @@ void Xdr::data_stream (std::complex<long double> * val, const unsigned int len,
13171207
// base) that can be represented without change. Equivalent
13181208
// to FLT_DIG, DBL_DIG or LDBL_DIG for floating types.
13191209
*out << std::scientific
1320-
<< std::setprecision(std::numeric_limits<long double>::max_digits10);
1210+
<< std::setprecision(std::numeric_limits<T>::max_digits10);
13211211

13221212
if (line_break == libMesh::invalid_uint)
13231213
for (unsigned int i=0; i<len; i++)
@@ -1359,7 +1249,8 @@ void Xdr::data_stream (std::complex<long double> * val, const unsigned int len,
13591249
libmesh_error_msg("Invalid mode = " << mode);
13601250
}
13611251
}
1362-
#endif // # LIBMESH_USE_COMPLEX_NUMBERS
1252+
1253+
13631254

13641255
void Xdr::comment (std::string & comment_in)
13651256
{

0 commit comments

Comments
 (0)