Skip to content

Commit dacb4aa

Browse files
committed
change deleter scope
1 parent ad30730 commit dacb4aa

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

src/nwp_emulator/grib_file_reader.cc

+21-9
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
*/
1111
#include <algorithm>
1212
#include <cstring>
13-
#include <sstream>
1413
#include <iostream>
14+
#include <sstream>
1515
#include <typeinfo>
1616

1717
#include "eckit/exception/Exceptions.h"
@@ -22,6 +22,18 @@
2222
#include "nwp_definitions.h"
2323

2424
namespace nwp_emulator {
25+
void codesHandleDeleter::operator()(codes_handle* h) {
26+
if (h) {
27+
codes_handle_delete(h);
28+
}
29+
}
30+
31+
void fileDeleter::operator()(FILE* f) {
32+
if (f) {
33+
fclose(f);
34+
}
35+
}
36+
2537
GRIBFileReader::GRIBFileReader(const eckit::PathName& inputPath, size_t rank, size_t root, int stepCountLimit) :
2638
rank_(rank), root_(root), DataReader(stepCountLimit) {
2739
if (rank_ != root_) {
@@ -44,10 +56,10 @@ GRIBFileReader::GRIBFileReader(const eckit::PathName& inputPath, size_t rank, si
4456
// 2. Field names & metadata (parameters)
4557
std::string paramBuffer;
4658
if (rank_ == root_) {
47-
for (const auto& param: params_) {
59+
for (const auto& param : params_) {
4860
paramBuffer += param + ";";
4961
}
50-
paramBuffer.pop_back(); // remove last ';' separator
62+
paramBuffer.pop_back(); // remove last ';' separator
5163
}
5264
size_t paramSize = paramBuffer.size();
5365
eckit::mpi::comm().broadcast(paramSize, root_);
@@ -169,8 +181,8 @@ void GRIBFileReader::validateSrcFiles(bool isRoot) {
169181
for (int i = 0; i < count_ - 1; ++i) {
170182
readMsgMetadata(gridName, paramMd);
171183
if (gridName != gridName_) {
172-
eckit::Log::error() << "Grid in " << filename << " is different from setup ("
173-
<< gridName_ << "), exit..." << std::endl;
184+
eckit::Log::error() << "Grid in " << filename << " is different from setup (" << gridName_
185+
<< "), exit..." << std::endl;
174186
eckit::mpi::comm().abort(1);
175187
}
176188
fileParams.push_back(paramMd);
@@ -179,10 +191,10 @@ void GRIBFileReader::validateSrcFiles(bool isRoot) {
179191
eckit::mpi::comm().abort(1);
180192
}
181193
}
182-
readMsgMetadata(gridName, paramMd); // read the last message in the file
194+
readMsgMetadata(gridName, paramMd); // read the last message in the file
183195
if (gridName != gridName_) {
184-
eckit::Log::error() << "Grid in " << filename << " is different from setup ("
185-
<< gridName_ << "), exit..." << std::endl;
196+
eckit::Log::error() << "Grid in " << filename << " is different from setup (" << gridName_ << "), exit..."
197+
<< std::endl;
186198
eckit::mpi::comm().abort(1);
187199
}
188200
fileParams.push_back(paramMd);
@@ -314,7 +326,7 @@ bool GRIBFileReader::nextMessage(std::string& shortName, std::string& levtype, s
314326
++index_;
315327
eckit::mpi::comm().broadcast(count_, root_);
316328
if (rank_ == root_ && index_ < count_) {
317-
return resetGribHandle(); // Load the next message
329+
return resetGribHandle(); // Load the next message
318330
}
319331
return true;
320332
}

src/nwp_emulator/grib_file_reader.h

+11-12
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,36 @@
1010
*/
1111
#pragma once
1212

13+
#include <memory>
1314
#include <string>
1415
#include <vector>
15-
#include <cstdio>
16-
#include <memory>
1716

1817
#include "eccodes.h"
1918
#include "eckit/filesystem/PathName.h"
2019

2120
#include "data_reader.h"
2221

22+
namespace nwp_emulator {
23+
2324
/**
2425
* @brief A structure for a custom deleter to manage codes handles.
25-
*
26+
*
2627
* This deleter allows to use codes handles pointers as unique pointers for safer memory usage.
2728
*/
2829
struct codesHandleDeleter {
29-
void operator()(codes_handle* h) { if (h) {codes_handle_delete(h);} }
30+
void operator()(codes_handle* h);
3031
};
3132

3233
/**
3334
* @brief A structure for a custom deleter to manage files.
34-
*
35+
*
3536
* This deleter allows to use C-style file pointers as unique pointers for safer memory usage.
3637
* `std::fstream` is not compatible with eccodes, hence the need to stick to `FILE`.
3738
*/
3839
struct fileDeleter {
39-
void operator()(FILE* f) { if (f) {fclose(f);} }
40+
void operator()(FILE* f);
4041
};
4142

42-
namespace nwp_emulator {
43-
4443
/**
4544
* @class GRIBFileReader
4645
* @brief Handles reading of GRIB data and serves them as Atlas fields. Supports parallel environments.
@@ -55,8 +54,8 @@ namespace nwp_emulator {
5554
class GRIBFileReader final : public DataReader {
5655
private:
5756
std::vector<eckit::PathName> srcFilenames_;
58-
std::unique_ptr<FILE, fileDeleter> currentFile_{nullptr, fileDeleter()};
59-
std::unique_ptr<codes_handle, codesHandleDeleter> grib_{nullptr, codesHandleDeleter()};
57+
std::unique_ptr<FILE, fileDeleter> currentFile_ = nullptr;
58+
std::unique_ptr<codes_handle, codesHandleDeleter> grib_ = nullptr;
6059

6160
size_t rank_;
6261
size_t root_;
@@ -84,9 +83,9 @@ class GRIBFileReader final : public DataReader {
8483

8584
/**
8685
* @brief Resets the GRIB handle. Decodes the next message in the current file.
87-
*
86+
*
8887
* This method first resets the handle pointer using the deleter, making sure memory is properly deallocated.
89-
*
88+
*
9089
* @return True if successful, false if there was an error loading the next message or if the current file
9190
* has had all its messages decoded already.
9291
*/

0 commit comments

Comments
 (0)