Skip to content

Commit 789ba98

Browse files
committed
Refactor if statement
1 parent 3ced4e3 commit 789ba98

File tree

1 file changed

+52
-52
lines changed

1 file changed

+52
-52
lines changed

src/IO/JSON/JSONIOHandlerImpl.cpp

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,54 +1346,55 @@ auto JSONIOHandlerImpl::putJsonContents(
13461346
filename.valid(),
13471347
"[JSON] File has been overwritten/deleted before writing");
13481348
auto it = m_jsonVals.find(filename);
1349-
if (it != m_jsonVals.end())
1349+
if (it == m_jsonVals.end())
13501350
{
1351-
(*it->second)["platform_byte_widths"] = platformSpecifics();
1351+
return it;
1352+
}
13521353

1353-
auto writeSingleFile = [this, &it](std::string const &writeThisFile) {
1354-
auto [fh, _, fh_with_precision] =
1355-
getFilehandle(File(writeThisFile), Access::CREATE);
1356-
(void)_;
1354+
(*it->second)["platform_byte_widths"] = platformSpecifics();
13571355

1358-
switch (m_fileFormat)
1359-
{
1360-
case FileFormat::Json:
1361-
*fh_with_precision << *it->second << std::endl;
1362-
break;
1363-
case FileFormat::Toml:
1364-
*fh_with_precision << openPMD::json::jsonToToml(*it->second)
1365-
<< std::endl;
1366-
break;
1367-
}
1356+
auto writeSingleFile = [this, &it](std::string const &writeThisFile) {
1357+
auto [fh, _, fh_with_precision] =
1358+
getFilehandle(File(writeThisFile), Access::CREATE);
1359+
(void)_;
13681360

1369-
VERIFY(fh->good(), "[JSON] Failed writing data to disk.")
1370-
};
1361+
switch (m_fileFormat)
1362+
{
1363+
case FileFormat::Json:
1364+
*fh_with_precision << *it->second << std::endl;
1365+
break;
1366+
case FileFormat::Toml:
1367+
*fh_with_precision << openPMD::json::jsonToToml(*it->second)
1368+
<< std::endl;
1369+
break;
1370+
}
13711371

1372-
auto serialImplementation = [&filename, &writeSingleFile]() {
1373-
writeSingleFile(*filename);
1374-
};
1372+
VERIFY(fh->good(), "[JSON] Failed writing data to disk.")
1373+
};
1374+
1375+
auto serialImplementation = [&filename, &writeSingleFile]() {
1376+
writeSingleFile(*filename);
1377+
};
13751378

13761379
#if openPMD_HAVE_MPI
1377-
auto num_digits = [](unsigned n) -> unsigned {
1378-
constexpr auto max = std::numeric_limits<unsigned>::max();
1379-
unsigned base_10 = 1;
1380-
unsigned res = 1;
1381-
while (base_10 < max)
1380+
auto num_digits = [](unsigned n) -> unsigned {
1381+
constexpr auto max = std::numeric_limits<unsigned>::max();
1382+
unsigned base_10 = 1;
1383+
unsigned res = 1;
1384+
while (base_10 < max)
1385+
{
1386+
base_10 *= 10;
1387+
if (n / base_10 == 0)
13821388
{
1383-
base_10 *= 10;
1384-
if (n / base_10 == 0)
1385-
{
1386-
return res;
1387-
}
1388-
++res;
1389+
return res;
13891390
}
1390-
return res;
1391-
};
1391+
++res;
1392+
}
1393+
return res;
1394+
};
13921395

1393-
auto parallelImplementation = [this,
1394-
&filename,
1395-
&writeSingleFile,
1396-
&num_digits](MPI_Comm comm) {
1396+
auto parallelImplementation =
1397+
[this, &filename, &writeSingleFile, &num_digits](MPI_Comm comm) {
13971398
auto path = fullPath(*filename);
13981399
auto dirpath = path + ".parallel";
13991400
if (!auxiliary::create_directories(dirpath))
@@ -1453,23 +1454,22 @@ merge the .json files somehow (no tooling provided for this (yet)).
14531454
}
14541455
};
14551456

1456-
std::shared_ptr<nlohmann::json> res;
1457-
if (m_communicator.has_value())
1458-
{
1459-
parallelImplementation(m_communicator.value());
1460-
}
1461-
else
1462-
{
1463-
serialImplementation();
1464-
}
1457+
std::shared_ptr<nlohmann::json> res;
1458+
if (m_communicator.has_value())
1459+
{
1460+
parallelImplementation(m_communicator.value());
1461+
}
1462+
else
1463+
{
1464+
serialImplementation();
1465+
}
14651466

14661467
#else
1467-
serialImplementation();
1468+
serialImplementation();
14681469
#endif
1469-
if (unsetDirty)
1470-
{
1471-
m_dirty.erase(filename);
1472-
}
1470+
if (unsetDirty)
1471+
{
1472+
m_dirty.erase(filename);
14731473
}
14741474
return it;
14751475
}

0 commit comments

Comments
 (0)