diff --git a/src/eckit/codec/Data.cc b/src/eckit/codec/Data.cc index 622e25957..be5ac0cdf 100644 --- a/src/eckit/codec/Data.cc +++ b/src/eckit/codec/Data.cc @@ -27,7 +27,7 @@ Data::Data(void* p, size_t size) : buffer_(p, size), size_(size) {} std::uint64_t Data::write(Stream& out) const { - if (size()) { + if (size() > 0) { ASSERT(buffer_.size() >= size()); return out.write(buffer_.data(), size()); } @@ -44,12 +44,13 @@ std::uint64_t Data::read(Stream& in, size_t size) { void Data::compress(const std::string& compression) { - if (size_) { + if (size_ > 0) { auto compressor = std::unique_ptr(CompressorFactory::instance().build(compression)); - if (dynamic_cast(compressor.get())) { + if (dynamic_cast(compressor.get()) != nullptr) { return; } - Buffer compressed(size_t(1.2 * size_)); + + Buffer compressed(static_cast(1.2 * static_cast(size_))); size_ = compressor->compress(buffer_, size_, compressed); buffer_ = std::move(compressed); } @@ -57,11 +58,11 @@ void Data::compress(const std::string& compression) { void Data::decompress(const std::string& compression, size_t uncompressed_size) { auto compressor = std::unique_ptr(CompressorFactory::instance().build(compression)); - if (dynamic_cast(compressor.get())) { + if (dynamic_cast(compressor.get()) != nullptr) { return; } - Buffer uncompressed(size_t(1.2 * uncompressed_size)); + Buffer uncompressed(static_cast(1.2 * static_cast(uncompressed_size))); compressor->uncompress(buffer_, size_, uncompressed, uncompressed_size); size_ = uncompressed_size; buffer_ = std::move(uncompressed); diff --git a/src/eckit/codec/FileStream.cc b/src/eckit/codec/FileStream.cc index 26be65690..92be18ced 100644 --- a/src/eckit/codec/FileStream.cc +++ b/src/eckit/codec/FileStream.cc @@ -61,8 +61,14 @@ class FileHandle : public eckit::FileHandle { FileHandle(const PathName& path, const std::string& openmode) : FileHandle(path, openmode[0]) {} + FileHandle(const FileHandle&) = delete; + FileHandle(FileHandle&&) = delete; + ~FileHandle() override { close(); } + void operator=(const FileHandle&) = delete; + void operator=(FileHandle&&) = delete; + private: bool closed_{false}; }; @@ -79,11 +85,19 @@ class FileHandle : public eckit::FileHandle { /// - Automatic opening and closing of file class PooledHandle : public eckit::PooledHandle { public: - PooledHandle(const PathName& path) : + explicit PooledHandle(const PathName& path) : eckit::PooledHandle(path), path_(path) { openForRead(); } + + PooledHandle(const PooledHandle&) = delete; + PooledHandle(PooledHandle&&) = delete; + ~PooledHandle() override { close(); } + + void operator=(const PooledHandle&) = delete; + void operator=(PooledHandle&&) = delete; + PathName path_; }; @@ -92,16 +106,7 @@ class PooledHandle : public eckit::PooledHandle { //--------------------------------------------------------------------------------------------------------------------- FileStream::FileStream(const PathName& path, char openmode) : - Stream([&path, &openmode]() -> DataHandle* { - DataHandle* datahandle; - if (openmode == 'r') { - datahandle = new PooledHandle(path); - } - else { - datahandle = new FileHandle(path, openmode); - } - return datahandle; - }()) { + Stream(openmode == 'r' ? static_cast(new PooledHandle(path)) : new FileHandle(path, openmode)) { if (openmode == 'r') { // Keep the PooledHandle alive until the end of active session Session::store(*this); diff --git a/src/eckit/codec/FileStream.h b/src/eckit/codec/FileStream.h index 0052d87b4..13f9f7e4f 100644 --- a/src/eckit/codec/FileStream.h +++ b/src/eckit/codec/FileStream.h @@ -36,25 +36,25 @@ enum class Mode class FileStream : public Stream { public: - FileStream(const PathName& path, Mode openmode); - FileStream(const PathName& path, char openmode); - FileStream(const PathName& path, const std::string& openmode); + FileStream(const PathName&, Mode openmode); + FileStream(const PathName&, char openmode); + FileStream(const PathName&, const std::string& openmode); }; //--------------------------------------------------------------------------------------------------------------------- class InputFileStream : public FileStream { public: - InputFileStream(const PathName& path); + explicit InputFileStream(const PathName&); }; //--------------------------------------------------------------------------------------------------------------------- class OutputFileStream : public FileStream { public: - OutputFileStream(const PathName& path, Mode openmode = Mode::write); - OutputFileStream(const PathName& path, const std::string& openmode); - OutputFileStream(const PathName& path, char openmode); + explicit OutputFileStream(const PathName&, Mode openmode = Mode::write); + OutputFileStream(const PathName&, const std::string& openmode); + OutputFileStream(const PathName&, char openmode); void close(); }; diff --git a/src/eckit/codec/Metadata.cc b/src/eckit/codec/Metadata.cc index 93f12f6cb..7b4e6d930 100644 --- a/src/eckit/codec/Metadata.cc +++ b/src/eckit/codec/Metadata.cc @@ -28,12 +28,12 @@ size_t uncompressed_size(const Metadata& m) { if (m.has("data.size")) { return m.getUnsigned("data.size"); } - else if (m.has("type")) { - if (m.getString("type") == "array") { - ArrayMetadata array(m); - return array.bytes(); - } + + if (m.has("type") && m.getString("type") == "array") { + ArrayMetadata array(m); + return array.bytes(); } + std::stringstream err; err << "Could not compute uncompressed data size from metadata \n"; write(m, err); @@ -58,7 +58,7 @@ void write(const Metadata& metadata, Stream& out) { void Metadata::link(Metadata&& linked) { std::string initial_link = link(); - ASSERT(initial_link.size()); + ASSERT(!initial_link.empty()); data = std::move(linked.data); record = std::move(linked.record); diff --git a/src/eckit/codec/Metadata.h b/src/eckit/codec/Metadata.h index d98b7adba..30fd713ff 100644 --- a/src/eckit/codec/Metadata.h +++ b/src/eckit/codec/Metadata.h @@ -41,8 +41,7 @@ class Metadata : public LocalConfiguration { public: using LocalConfiguration::LocalConfiguration; - Metadata() : - LocalConfiguration() {} + Metadata() = default; Link link() const { return Link{getString("link", "")}; } @@ -59,8 +58,8 @@ class Metadata : public LocalConfiguration { // extended LocalConfiguration: using LocalConfiguration::set; Metadata& set(const LocalConfiguration& other) { - Value& root = const_cast(get()); - auto& other_root = other.get(); + auto& root = const_cast(get()); + const auto& other_root = other.get(); std::vector other_keys; fromValue(other_keys, other_root.keys()); for (auto& key : other_keys) { @@ -81,7 +80,7 @@ class Metadata : public LocalConfiguration { Metadata& remove(const std::string& name) { - Value& root = const_cast(get()); + auto& root = const_cast(get()); root.remove(name); return *this; } diff --git a/src/eckit/codec/ReadRequest.cc b/src/eckit/codec/ReadRequest.cc index da20deab4..788ad8aee 100644 --- a/src/eckit/codec/ReadRequest.cc +++ b/src/eckit/codec/ReadRequest.cc @@ -31,7 +31,7 @@ static std::string stream_path(Stream stream) { ReadRequest::ReadRequest(const std::string& URI, Decoder* decoder) : uri_(URI), decoder_(decoder), item_(new RecordItem()) { do_checksum_ = defaults::checksum_read(); - ASSERT(uri_.size()); + ASSERT(!uri_.empty()); } ReadRequest::ReadRequest(Stream stream, size_t offset, const std::string& key, Decoder* decoder) : diff --git a/src/eckit/codec/ReadRequest.h b/src/eckit/codec/ReadRequest.h index 8a1bfa2bc..5cf53f8d8 100644 --- a/src/eckit/codec/ReadRequest.h +++ b/src/eckit/codec/ReadRequest.h @@ -39,6 +39,9 @@ class ReadRequest { ReadRequest(const RecordItem::URI& URI, T& value) : ReadRequest{URI.str(), value} {} + ReadRequest() = delete; + ReadRequest(const ReadRequest&) = delete; + ~ReadRequest(); void read(); @@ -57,9 +60,6 @@ class ReadRequest { ReadRequest(const std::string& URI, Decoder* decoder); ReadRequest(Stream, size_t offset, const std::string& key, Decoder*); - ReadRequest() = delete; - ReadRequest(const ReadRequest&) = delete; - Stream stream_; size_t offset_; std::string key_; diff --git a/src/eckit/codec/Record.cc b/src/eckit/codec/Record.cc index edee8fc6a..c99d75a86 100644 --- a/src/eckit/codec/Record.cc +++ b/src/eckit/codec/Record.cc @@ -26,7 +26,7 @@ namespace { template inline size_t read_struct(IStream& in, Struct& s) { - static_assert(Struct::bytes == sizeof(Struct), ""); + static_assert(Struct::bytes == sizeof(Struct)); return in.read(reinterpret_cast(&s), sizeof(Struct)); } @@ -48,13 +48,9 @@ inline Struct read_struct(IStream& in) { //--------------------------------------------------------------------------------------------------------------------- Endian RecordHead::endian() const { - if (magic_number == 1234) { - return Endian::native; - } - else if (magic_number == 3523477504) { - return Endian::swapped; - } - throw Exception("Mixed endianness is not supported", Here()); + return magic_number == 1234 ? Endian::native + : magic_number == 3523477504 ? Endian::swapped + : throw Exception("Mixed endianness is not supported", Here()); } //--------------------------------------------------------------------------------------------------------------------- @@ -196,7 +192,7 @@ Record& Record::read(Stream& in, bool read_to_end) { "]"); } std::string metadata_str; - metadata_str.resize(size_t(r.metadata_length) - sizeof(RecordMetadataSection::Begin) - + metadata_str.resize(static_cast(r.metadata_length) - sizeof(RecordMetadataSection::Begin) - sizeof(RecordMetadataSection::End)); if (in.read(const_cast(metadata_str.data()), metadata_str.size()) != metadata_str.size()) { throw InvalidRecord("Unexpected EOF reached"); @@ -232,10 +228,10 @@ Record& Record::read(Stream& in, bool read_to_end) { throw InvalidRecord("Data index section is not valid. Invalid section begin marker: [" + index_begin.str() + "]"); } - const auto index_length = - (size_t(r.index_length) - sizeof(RecordDataIndexSection::Begin) - sizeof(RecordDataIndexSection::End)); - const auto index_size = index_length / sizeof(RecordDataIndexSection::Entry); - auto& data_sections = record_->data_sections; + const auto index_length = (static_cast(r.index_length) - sizeof(RecordDataIndexSection::Begin) - + sizeof(RecordDataIndexSection::End)); + const auto index_size = index_length / sizeof(RecordDataIndexSection::Entry); + auto& data_sections = record_->data_sections; data_sections.resize(index_size); if (in.read(data_sections.data(), index_length) != index_length) { throw InvalidRecord("Unexpected EOF reached"); @@ -278,8 +274,8 @@ void ParsedRecord::parse() { item.data.section(item.getInt("data.section", 0)); item.data.endian(head.endian()); item.data.compression(item.getString("data.compression.type", "none")); - if (item.data.section()) { - auto& data_section = data_sections.at(size_t(item.data.section() - 1)); + if (item.data.section() != 0) { + auto& data_section = data_sections.at(static_cast(item.data.section() - 1)); item.data.checksum(data_section.checksum); item.data.compressed_size(data_section.length - sizeof(RecordDataSection::Begin) - sizeof(RecordDataSection::End)); diff --git a/src/eckit/codec/Record.h b/src/eckit/codec/Record.h index dfa17ec98..6686ee94e 100644 --- a/src/eckit/codec/Record.h +++ b/src/eckit/codec/Record.h @@ -35,11 +35,12 @@ class Record { std::string str() const; std::string path; std::uint64_t offset; - URI() = default; - URI(const std::string& _path, std::uint64_t _offset = 0) : + + URI() = default; + URI(const URI&) = default; + + explicit URI(const std::string& _path, std::uint64_t _offset = 0) : path(_path), offset(_offset) {} - URI(const URI& other) : - path(other.path), offset(other.offset) {} }; private: @@ -52,7 +53,7 @@ class Record { bool empty() const; - Record& read(Stream& in, bool verify_end = false); + Record& read(Stream&, bool read_to_end = false); const Metadata& metadata(const std::string& key) const; @@ -68,7 +69,7 @@ class Record { bool has(const std::string& key); - operator const ParsedRecord&() const; + explicit operator const ParsedRecord&() const; }; //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/eckit/codec/RecordItem.h b/src/eckit/codec/RecordItem.h index a4b564bbe..7cb2d12b2 100644 --- a/src/eckit/codec/RecordItem.h +++ b/src/eckit/codec/RecordItem.h @@ -27,10 +27,10 @@ namespace eckit::codec { struct RecordItem { public: struct URI { - URI(const std::string& uri); + explicit URI(const std::string& uri); URI(const std::string& _path, std::uint64_t _offset, const std::string& _key); std::string str() const; - operator std::string() { return str(); } + operator std::string() const { return str(); } std::string path; std::uint64_t offset; std::string key; diff --git a/src/eckit/codec/RecordItemReader.cc b/src/eckit/codec/RecordItemReader.cc index 882e3785d..b856486f1 100644 --- a/src/eckit/codec/RecordItemReader.cc +++ b/src/eckit/codec/RecordItemReader.cc @@ -28,7 +28,7 @@ namespace { template inline size_t read_struct(IStream& in, Struct& s) { - static_assert(Struct::bytes == sizeof(Struct), ""); + static_assert(Struct::bytes == sizeof(Struct)); return in.read(reinterpret_cast(&s), sizeof(Struct)); } @@ -45,13 +45,13 @@ inline Struct read_struct(IStream& in) { //--------------------------------------------------------------------------------------------------------------------- -static Data read_data(const Record& record, int data_section_index, Stream in) { +Data read_data(const Record& record, int data_section_index, Stream in) { if (data_section_index == 0) { - return Data(); + return {}; } const auto& parsed = static_cast(record); - const auto& data_section = parsed.data_sections.at(size_t(data_section_index) - 1); + const auto& data_section = parsed.data_sections.at(static_cast(data_section_index) - 1); Data data; auto offset = data_section.offset; @@ -61,8 +61,9 @@ static Data read_data(const Record& record, int data_section_index, Stream in) { if (not data_begin.valid()) { throw InvalidRecord("Data section is not valid"); } - auto data_size = size_t(data_section.length) - sizeof(RecordDataSection::Begin) - sizeof(RecordDataSection::End); - if (data_size) { + auto data_size = + static_cast(data_section.length) - sizeof(RecordDataSection::Begin) - sizeof(RecordDataSection::End); + if (data_size > 0) { if (data.read(in, data_size) != data_size) { throw InvalidRecord("Data section is not valid"); } @@ -77,9 +78,9 @@ static Data read_data(const Record& record, int data_section_index, Stream in) { //--------------------------------------------------------------------------------------------------------------------- -static PathName make_absolute_path(const std::string& reference_path, RecordItem::URI& uri) { +PathName make_absolute_path(const std::string& reference_path, RecordItem::URI& uri) { PathName absolute_path = uri.path; - if (reference_path.size() && uri.path[0] != '/' && uri.path[0] != '~') { + if (!reference_path.empty() && uri.path[0] != '/' && uri.path[0] != '~') { absolute_path = PathName{reference_path} / absolute_path; } return absolute_path.fullName(); @@ -87,7 +88,7 @@ static PathName make_absolute_path(const std::string& reference_path, RecordItem //--------------------------------------------------------------------------------------------------------------------- -static Record read_record(const std::string& path, size_t offset) { +Record read_record(const std::string& path, size_t offset) { auto record = Session::record(path, offset); if (record.empty()) { auto in = InputFileStream(path); @@ -99,7 +100,7 @@ static Record read_record(const std::string& path, size_t offset) { //--------------------------------------------------------------------------------------------------------------------- -static Record read_record(Stream in, size_t offset) { +Record read_record(Stream in, size_t offset) { auto record = Session::record(in, offset); if (record.empty()) { in.seek(offset); @@ -188,10 +189,9 @@ static void read_from_stream(Record record, Stream in, const std::string& key, M if (metadata.link()) { throw Exception("Cannot follow links in records that are not file based"); } - else { - if (metadata.data.section()) { - data = read_data(record, metadata.data.section(), in); - } + + if (metadata.data.section() != 0) { + data = read_data(record, metadata.data.section(), in); } } @@ -211,10 +211,8 @@ void RecordItemReader::read(Metadata& metadata, Data& data) { RecordItemReader{absolute_path.dirName(), metadata.link()}.read(linked, data); metadata.link(std::move(linked)); } - else { - if (metadata.data.section()) { - data = read_data(record_, metadata.data.section(), InputFileStream(absolute_path)); - } + else if (metadata.data.section() != 0) { + data = read_data(record_, metadata.data.section(), InputFileStream(absolute_path)); } }; diff --git a/src/eckit/codec/RecordItemReader.h b/src/eckit/codec/RecordItemReader.h index 662d81db0..116bd23dd 100644 --- a/src/eckit/codec/RecordItemReader.h +++ b/src/eckit/codec/RecordItemReader.h @@ -28,7 +28,7 @@ class RecordItemReader { RecordItemReader(Stream, const std::string& key); - RecordItemReader(const std::string& uri); + explicit RecordItemReader(const std::string& uri); void read(RecordItem& item); diff --git a/src/eckit/codec/RecordPrinter.h b/src/eckit/codec/RecordPrinter.h index 67cfb79d0..655b8a456 100644 --- a/src/eckit/codec/RecordPrinter.h +++ b/src/eckit/codec/RecordPrinter.h @@ -28,9 +28,9 @@ namespace eckit::codec { class RecordPrinter { public: - RecordPrinter(const Record::URI&, const Configuration& = NoConfig()); + explicit RecordPrinter(const Record::URI&, const Configuration& = NoConfig()); - RecordPrinter(const PathName&, const Configuration& = NoConfig()); + explicit RecordPrinter(const PathName&, const Configuration& = NoConfig()); RecordPrinter(const PathName&, std::uint64_t offset, const Configuration& = NoConfig()); diff --git a/src/eckit/codec/RecordReader.cc b/src/eckit/codec/RecordReader.cc index ce785c73d..7629c7752 100644 --- a/src/eckit/codec/RecordReader.cc +++ b/src/eckit/codec/RecordReader.cc @@ -25,10 +25,10 @@ RecordReader::RecordReader(const Record::URI& ref) : //--------------------------------------------------------------------------------------------------------------------- RecordReader::RecordReader(const std::string& path, uint64_t offset) : - session_{}, path_{path}, offset_{offset} {} + path_{path}, offset_{offset} {} RecordReader::RecordReader(Stream stream, uint64_t offset) : - session_{}, stream_{stream}, path_{}, offset_{offset} {} + stream_{stream}, offset_{offset} {} //--------------------------------------------------------------------------------------------------------------------- @@ -42,7 +42,7 @@ Record::URI RecordReader::uri() const { //--------------------------------------------------------------------------------------------------------------------- RecordItem::URI RecordReader::uri(const std::string& key) const { - return RecordItem::URI(path_, offset_, key); + return {path_, offset_, key}; } //--------------------------------------------------------------------------------------------------------------------- @@ -80,7 +80,7 @@ Metadata RecordReader::metadata(const std::string& key) { } void RecordReader::checksum(bool b) { - do_checksum_ = b; + do_checksum_ = b ? 1 : 0; } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/eckit/codec/RecordReader.h b/src/eckit/codec/RecordReader.h index 9c67dff11..7fe89b664 100644 --- a/src/eckit/codec/RecordReader.h +++ b/src/eckit/codec/RecordReader.h @@ -26,11 +26,9 @@ namespace eckit::codec { class RecordReader { public: - RecordReader(const Record::URI& ref); - - RecordReader(const std::string& path, std::uint64_t offset = 0); - - RecordReader(Stream stream, std::uint64_t offset = 0); + explicit RecordReader(const Record::URI& ref); + explicit RecordReader(const std::string& path, std::uint64_t offset = 0); + explicit RecordReader(Stream stream, std::uint64_t offset = 0); template ReadRequest& read(const std::string& key, T& value) { diff --git a/src/eckit/codec/RecordWriter.cc b/src/eckit/codec/RecordWriter.cc index 6cb781540..9b809150e 100644 --- a/src/eckit/codec/RecordWriter.cc +++ b/src/eckit/codec/RecordWriter.cc @@ -12,7 +12,6 @@ #include "eckit/codec/RecordWriter.h" -#include "eckit/codec/RecordWriter.h" #include "eckit/codec/detail/Checksum.h" #include "eckit/codec/detail/Defaults.h" #include "eckit/codec/detail/Encoder.h" @@ -25,7 +24,7 @@ namespace eckit::codec { template inline void write_struct(OStream& out, const Struct& s) { - static_assert(Struct::bytes == sizeof(Struct), ""); + static_assert(Struct::bytes == sizeof(Struct)); if (out.write(reinterpret_cast(&s), sizeof(s)) != sizeof(s)) { throw WriteError("Could not write struct to stream"); } @@ -65,12 +64,12 @@ size_t RecordWriter::write(Stream out) const { write_struct(out, RecordMetadataSection::End()); r.metadata_length = position() - r.metadata_offset; r.metadata_checksum = - do_checksum_ ? codec::checksum(metadata_str.data(), metadata_str.size()) : std::string("none:"); + do_checksum_ != 0 ? codec::checksum(metadata_str.data(), metadata_str.size()) : std::string("none:"); // Index section // ------------- - size_t nb_data_sections = static_cast(nb_data_sections_); - r.index_offset = position(); + auto nb_data_sections = static_cast(nb_data_sections_); + r.index_offset = position(); write_struct(out, RecordDataIndexSection::Begin()); index.resize(nb_data_sections); for (size_t i = 0; i < nb_data_sections; ++i) { @@ -84,9 +83,9 @@ size_t RecordWriter::write(Stream out) const { // ------------- { size_t i{0}; - for (auto& key : keys_) { - auto& encoder = encoders_.at(key); - auto& info = info_.at(key); + for (const auto& key : keys_) { + const auto& encoder = encoders_.at(key); + const auto& info = info_.at(key); if (info.section() == 0) { continue; } @@ -101,7 +100,7 @@ size_t RecordWriter::write(Stream out) const { } write_struct(out, RecordDataSection::End()); data_section.length = position() - data_section.offset; - data_section.checksum = do_checksum_ ? data.checksum() : std::string("none:"); + data_section.checksum = do_checksum_ != 0 ? data.checksum() : std::string("none:"); ++i; } } @@ -145,19 +144,14 @@ void RecordWriter::compression(bool on) { //--------------------------------------------------------------------------------------------------------------------- void RecordWriter::checksum(bool on) { - if (on) { - do_checksum_ = defaults::checksum_write(); // still possible to be off via environment - } - else { - do_checksum_ = false; - } + do_checksum_ = on && defaults::checksum_write() ? 1 : 0; // possible to be off via environment } //--------------------------------------------------------------------------------------------------------------------- void RecordWriter::set(const RecordWriter::Key& key, Link&& link, const Configuration&) { keys_.emplace_back(key); - encoders_[key] = std::move(Encoder{link}); + encoders_[key] = Encoder{link}; info_.emplace(key, DataInfo{}); } @@ -199,12 +193,12 @@ size_t RecordWriter::estimateMaximumSize() const { size += sizeof(RecordMetadataSection::End); size += sizeof(RecordDataIndexSection::Begin); - size += size_t(nb_data_sections_) * sizeof(RecordDataIndexSection::Entry); + size += static_cast(nb_data_sections_) * sizeof(RecordDataIndexSection::Entry); size += sizeof(RecordDataIndexSection::End); - for (auto& key : keys_) { - auto& encoder = encoders_.at(key); - auto& info = info_.at(key); + for (const auto& key : keys_) { + const auto& encoder = encoders_.at(key); + const auto& info = info_.at(key); if (info.section() == 0) { continue; } @@ -213,7 +207,7 @@ size_t RecordWriter::estimateMaximumSize() const { Metadata m; size_t max_data_size = encode_metadata(encoder, m); if (info.compression() != "none") { - max_data_size = size_t(1.2 * max_data_size); + max_data_size = static_cast(1.2 * static_cast(max_data_size)); max_data_size = std::max(max_data_size, 10 * 1024); // minimum 10KB } size += max_data_size; @@ -230,12 +224,12 @@ size_t RecordWriter::estimateMaximumSize() const { std::string RecordWriter::metadata() const { Metadata metadata; - for (auto& key : keys_) { - auto& encoder = encoders_.at(key); - auto& info = info_.at(key); + for (const auto& key : keys_) { + const auto& encoder = encoders_.at(key); + const auto& info = info_.at(key); Metadata m; encode_metadata(encoder, m); - if (info.section()) { + if (info.section() != 0) { m.set("data.section", info.section()); if (info.compression() != "none") { m.set("data.compression.type", info.compression()); diff --git a/src/eckit/codec/RecordWriter.h b/src/eckit/codec/RecordWriter.h index 9c6aa3f38..65a8f4f47 100644 --- a/src/eckit/codec/RecordWriter.h +++ b/src/eckit/codec/RecordWriter.h @@ -122,7 +122,7 @@ class RecordWriter { std::map info_; std::string compression_{defaults::compression_algorithm()}; - int do_checksum_{defaults::checksum_write()}; + int do_checksum_{defaults::checksum_write() ? 1 : 0}; int nb_data_sections_{0}; std::string metadata() const; diff --git a/src/eckit/codec/Session.cc b/src/eckit/codec/Session.cc index 3de6793e3..f67e34ba6 100644 --- a/src/eckit/codec/Session.cc +++ b/src/eckit/codec/Session.cc @@ -70,18 +70,17 @@ ActiveSession& ActiveSession::instance() { //--------------------------------------------------------------------------------------------------------------------- Record ActiveSession::record(const std::string& path, size_t offset) { - if (count_) { + if (count_ != 0) { return current().record(path, offset); } - else { - return Record(); - } + + return {}; } //--------------------------------------------------------------------------------------------------------------------- void ActiveSession::store(Stream stream) { - if (count_) { + if (count_ != 0) { return current().store(stream); } } @@ -102,7 +101,7 @@ void ActiveSession::push() { lock_guard lock(mutex_); if (count_ == 0) { ASSERT(session_ == nullptr); - session_.reset(new SessionImpl()); + session_ = std::make_unique(); } ++count_; } @@ -131,7 +130,7 @@ void SessionImpl::store(Stream stream) { Record SessionImpl::record(const std::string& path, size_t offset) { lock_guard lock(mutex_); - auto key = Record::URI{eckit::PathName(path).fullName(), offset}.str(); + auto key = Record::URI{PathName(path).fullName(), offset}.str(); if (records_.find(key) == records_.end()) { records_.emplace(key, Record{}); } diff --git a/src/eckit/codec/Stream.cc b/src/eckit/codec/Stream.cc index e4f8de3d3..00c352a1d 100644 --- a/src/eckit/codec/Stream.cc +++ b/src/eckit/codec/Stream.cc @@ -19,40 +19,40 @@ namespace eckit::codec { //--------------------------------------------------------------------------------------------------------------------- -Stream::Stream(eckit::DataHandle& datahandle) : +Stream::Stream(DataHandle& datahandle) : ptr_(&datahandle) {} -Stream::Stream(eckit::DataHandle* datahandle) : +Stream::Stream(DataHandle* datahandle) : shared_(datahandle), ptr_(shared_.get()) {} -Stream::Stream(std::shared_ptr datahandle) : +Stream::Stream(std::shared_ptr datahandle) : shared_(datahandle), ptr_(shared_.get()) {} Stream::Stream(const Stream& other) = default; -eckit::DataHandle& Stream::datahandle() { +DataHandle& Stream::datahandle() { ASSERT(ptr_ != nullptr); return *ptr_; } uint64_t Stream::seek(uint64_t offset) { ASSERT(ptr_ != nullptr); - return std::uint64_t(ptr_->seek(static_cast(offset))); + return static_cast(ptr_->seek(static_cast(offset))); } uint64_t Stream::position() { ASSERT(ptr_ != nullptr); - return std::uint64_t(ptr_->position()); + return static_cast(ptr_->position()); } uint64_t Stream::write(const void* data, size_t length) { ASSERT(ptr_ != nullptr); - return std::uint64_t(ptr_->write(data, static_cast(length))); + return static_cast(ptr_->write(data, static_cast(length))); } uint64_t Stream::read(void* data, size_t length) { ASSERT(ptr_ != nullptr); - return std::uint64_t(ptr_->read(data, static_cast(length))); + return static_cast(ptr_->read(data, static_cast(length))); } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/eckit/codec/Stream.h b/src/eckit/codec/Stream.h index 6c73c8a10..3c5ecc258 100644 --- a/src/eckit/codec/Stream.h +++ b/src/eckit/codec/Stream.h @@ -36,10 +36,10 @@ class Stream { Stream() = default; /// Constructor taking ownership of datahandle - Stream(DataHandle*); + explicit Stream(DataHandle*); /// Constructor to share datahandle with a shared_ptr - Stream(std::shared_ptr); + explicit Stream(std::shared_ptr); /// Constructor referencing datahandle, no ownership is taken /// @note The usability depends on the usable lifetime of @@ -69,7 +69,7 @@ class Stream { std::uint64_t read(void* data, size_t length); /// Return true if pointer is valid; - operator bool() const { return ptr_; } + explicit operator bool() const { return ptr_ != nullptr; } private: std::shared_ptr shared_; diff --git a/src/eckit/codec/detail/Base64.cc b/src/eckit/codec/detail/Base64.cc index 1cc2606c5..dfe695bb9 100644 --- a/src/eckit/codec/detail/Base64.cc +++ b/src/eckit/codec/detail/Base64.cc @@ -20,7 +20,7 @@ namespace eckit::codec { namespace { -static std::array b64_decode_table{ +const std::array b64_decode_table{ /* ASCII table */ 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, @@ -32,7 +32,7 @@ static std::array b64_decode_table{ 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64}; -static std::array b64_encode_table{ +const std::array b64_encode_table{ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'}; @@ -42,24 +42,24 @@ static std::array b64_encode_table{ //--------------------------------------------------------------------------------------------------------------------- std::string Base64::encode(const void* data, size_t len) { - const auto& table = b64_encode_table; - const unsigned char* src = reinterpret_cast(data); - unsigned char *out, *pos; - const unsigned char *end, *in; + const auto& table = b64_encode_table; + const auto* src = reinterpret_cast(data); size_t out_len = 4 * ((len + 2) / 3); /* 3-byte blocks to 4-byte */ if (out_len < len) { - return std::string(); /* integer overflow */ + return {}; /* integer overflow */ } std::string str; str.resize(out_len); - out = reinterpret_cast(const_cast(str.data())); - end = src + len; - in = src; - pos = out; + auto* out = reinterpret_cast(const_cast(str.data())); + auto* pos = out; + + const auto* end = src + len; + const auto* in = src; + while (end - in >= 3) { *pos++ = table[in[0] >> 2]; *pos++ = table[((in[0] & 0x03) << 4) | (in[1] >> 4)]; @@ -88,9 +88,9 @@ std::string Base64::encode(const void* data, size_t len) { std::string Base64::decode(const void* data, size_t len) { const auto& table = b64_decode_table; - const unsigned char* p = reinterpret_cast(data); - int pad = len > 0 && (len % 4 || p[len - 1] == '='); - const size_t L = ((len + 3) / 4 - pad) * 4; + const auto* p = reinterpret_cast(data); + int pad = len > 0 && (len % 4 || p[len - 1] == '='); + const size_t L = ((len + 3) / 4 - pad) * 4; std::string str(L / 4 * 3 + pad, '\0'); for (size_t i = 0, j = 0; i < L; i += 4) { @@ -100,7 +100,7 @@ std::string Base64::decode(const void* data, size_t len) { str[j++] = n & 0xFF; } - if (pad) { + if (pad != 0) { int n = table[p[L]] << 18 | table[p[L + 1]] << 12; str[str.size() - 1] = n >> 16; diff --git a/src/eckit/codec/detail/Checksum.cc b/src/eckit/codec/detail/Checksum.cc index d034b6043..5072c363a 100644 --- a/src/eckit/codec/detail/Checksum.cc +++ b/src/eckit/codec/detail/Checksum.cc @@ -35,7 +35,7 @@ Checksum::Checksum(const std::string& checksum) { } bool Checksum::available() const { - return checksum_.size() && algorithm_ != "none"; + return !checksum_.empty() && algorithm_ != "none"; } std::string Checksum::str() const { @@ -57,17 +57,12 @@ std::string checksum(const void* buffer, size_t size, const std::string& algorit auto hash = [&](const std::string& alg) -> std::string { std::unique_ptr hasher(HashFactory::instance().build(alg)); - return alg + ":" + hasher->compute(buffer, long(size)); + return alg + ":" + hasher->compute(buffer, static_cast(size)); }; - std::string alg = algorithm.empty() ? defaults::checksum_algorithm() : algorithm; + auto alg = algorithm.empty() ? defaults::checksum_algorithm() : algorithm; - if (is_available(alg)) { - return hash(alg); - } - else { - return hash("none"); - } + return hash(is_available(alg) ? alg : "none"); } diff --git a/src/eckit/codec/detail/Checksum.h b/src/eckit/codec/detail/Checksum.h index 11b24b5d7..5b91c3e03 100644 --- a/src/eckit/codec/detail/Checksum.h +++ b/src/eckit/codec/detail/Checksum.h @@ -20,7 +20,7 @@ namespace eckit::codec { class Checksum { public: Checksum() = default; - Checksum(const std::string& checksum); + explicit Checksum(const std::string& checksum); bool available() const; std::string str() const; std::string str(size_t size) const; diff --git a/src/eckit/codec/detail/DataInfo.h b/src/eckit/codec/detail/DataInfo.h index 2e05e8100..6223a3033 100644 --- a/src/eckit/codec/detail/DataInfo.h +++ b/src/eckit/codec/detail/DataInfo.h @@ -41,7 +41,7 @@ class DataInfo { bool compressed() const { return compression_ != "none"; } - operator bool() const { return section_ > 0; } + explicit operator bool() const { return section_ > 0; } const Checksum& checksum() const { return checksum_; } void checksum(const std::string& c) { checksum_ = Checksum(c); } diff --git a/src/eckit/codec/detail/DataType.h b/src/eckit/codec/detail/DataType.h index dbaa63451..4fab8e3d4 100644 --- a/src/eckit/codec/detail/DataType.h +++ b/src/eckit/codec/detail/DataType.h @@ -21,7 +21,8 @@ namespace eckit::codec { class DataType { public: - typedef long kind_t; + using kind_t = long; + static const kind_t KIND_BYTE = 1; static const kind_t KIND_INT32 = -4; static const kind_t KIND_INT64 = -8; @@ -32,12 +33,12 @@ class DataType { template static DataType create(); - static DataType byte() { return DataType(KIND_BYTE); } - static DataType int32() { return DataType(KIND_INT32); } - static DataType int64() { return DataType(KIND_INT64); } - static DataType real32() { return DataType(KIND_REAL32); } - static DataType real64() { return DataType(KIND_REAL64); } - static DataType uint64() { return DataType(KIND_UINT64); } + static DataType byte() { return {KIND_BYTE}; } + static DataType int32() { return {KIND_INT32}; } + static DataType int64() { return {KIND_INT64}; } + static DataType real32() { return {KIND_REAL32}; } + static DataType real64() { return {KIND_REAL64}; } + static DataType uint64() { return {KIND_UINT64}; } template static kind_t kind(); @@ -65,10 +66,11 @@ class DataType { [[noreturn]] static void throw_not_recognised(std::string datatype); public: - DataType(const std::string&); + explicit DataType(const std::string&); DataType(long); - DataType(const DataType&); - DataType& operator=(const DataType&); + DataType(const DataType&) = default; + DataType& operator=(const DataType&) = default; + std::string str() const { return kind_to_str(kind_); } kind_t kind() const { return kind_; } size_t size() const { return (kind_ == KIND_UINT64) ? 8 : std::abs(kind_); } @@ -94,73 +96,73 @@ inline std::string DataType::str() { } template <> inline std::string DataType::str() { - static_assert(sizeof(int) == 4, ""); + static_assert(sizeof(int) == 4); return int32_str(); } template <> inline std::string DataType::str() { - static_assert(sizeof(int) == 4, ""); + static_assert(sizeof(int) == 4); return int32_str(); } template <> inline std::string DataType::str() { - static_assert(sizeof(long) == 8, ""); + static_assert(sizeof(long) == 8); return int64_str(); } template <> inline std::string DataType::str() { - static_assert(sizeof(long) == 8, ""); + static_assert(sizeof(long) == 8); return int64_str(); } template <> inline std::string DataType::str() { - static_assert(sizeof(long long) == 8, ""); + static_assert(sizeof(long long) == 8); return int64_str(); } template <> inline std::string DataType::str() { - static_assert(sizeof(long long) == 8, ""); + static_assert(sizeof(long long) == 8); return int64_str(); } template <> inline std::string DataType::str() { - static_assert(sizeof(float) == 4, ""); + static_assert(sizeof(float) == 4); return real32_str(); } template <> inline std::string DataType::str() { - static_assert(sizeof(float) == 4, ""); + static_assert(sizeof(float) == 4); return real32_str(); } template <> inline std::string DataType::str() { - static_assert(sizeof(double) == 8, ""); + static_assert(sizeof(double) == 8); return real64_str(); } template <> inline std::string DataType::str() { - static_assert(sizeof(double) == 8, ""); + static_assert(sizeof(double) == 8); return real64_str(); } template <> inline std::string DataType::str() { - static_assert(sizeof(unsigned long) == 8, ""); + static_assert(sizeof(unsigned long) == 8); return uint64_str(); } template <> inline std::string DataType::str() { - static_assert(sizeof(unsigned long) == 8, ""); + static_assert(sizeof(unsigned long) == 8); return uint64_str(); } template <> inline std::string DataType::str() { - static_assert(sizeof(unsigned long long) == 8, ""); + static_assert(sizeof(unsigned long long) == 8); return uint64_str(); } template <> inline std::string DataType::str() { - static_assert(sizeof(unsigned long long) == 8, ""); + static_assert(sizeof(unsigned long long) == 8); return uint64_str(); } template <> @@ -193,82 +195,82 @@ inline std::string DataType::str(const double&) { } template <> inline DataType::kind_t DataType::kind() { - static_assert(sizeof(std::byte) == 1, ""); + static_assert(sizeof(std::byte) == 1); return KIND_BYTE; } template <> inline DataType::kind_t DataType::kind() { - static_assert(sizeof(std::byte) == 1, ""); + static_assert(sizeof(std::byte) == 1); return KIND_BYTE; } template <> inline DataType::kind_t DataType::kind() { - static_assert(sizeof(int) == 4, ""); + static_assert(sizeof(int) == 4); return KIND_INT32; } template <> inline DataType::kind_t DataType::kind() { - static_assert(sizeof(int) == 4, ""); + static_assert(sizeof(int) == 4); return KIND_INT32; } template <> inline DataType::kind_t DataType::kind() { - static_assert(sizeof(long) == 8, ""); + static_assert(sizeof(long) == 8); return KIND_INT64; } template <> inline DataType::kind_t DataType::kind() { - static_assert(sizeof(long) == 8, ""); + static_assert(sizeof(long) == 8); return KIND_INT64; } template <> inline DataType::kind_t DataType::kind() { - static_assert(sizeof(long long) == 8, ""); + static_assert(sizeof(long long) == 8); return KIND_INT64; } template <> inline DataType::kind_t DataType::kind() { - static_assert(sizeof(long long) == 8, ""); + static_assert(sizeof(long long) == 8); return KIND_INT64; } template <> inline DataType::kind_t DataType::kind() { - static_assert(sizeof(unsigned long) == 8, ""); + static_assert(sizeof(unsigned long) == 8); return KIND_UINT64; } template <> inline DataType::kind_t DataType::kind() { - static_assert(sizeof(unsigned long) == 8, ""); + static_assert(sizeof(unsigned long) == 8); return KIND_UINT64; } template <> inline DataType::kind_t DataType::kind() { - static_assert(sizeof(unsigned long long) == 8, ""); + static_assert(sizeof(unsigned long long) == 8); return KIND_UINT64; } template <> inline DataType::kind_t DataType::kind() { - static_assert(sizeof(unsigned long long) == 8, ""); + static_assert(sizeof(unsigned long long) == 8); return KIND_UINT64; } template <> inline DataType::kind_t DataType::kind() { - static_assert(sizeof(float) == 4, ""); + static_assert(sizeof(float) == 4); return KIND_REAL32; } template <> inline DataType::kind_t DataType::kind() { - static_assert(sizeof(float) == 4, ""); + static_assert(sizeof(float) == 4); return KIND_REAL32; } template <> inline DataType::kind_t DataType::kind() { - static_assert(sizeof(double) == 8, ""); + static_assert(sizeof(double) == 8); return KIND_REAL64; } template <> inline DataType::kind_t DataType::kind() { - static_assert(sizeof(double) == 8, ""); + static_assert(sizeof(double) == 8); return KIND_REAL64; } template <> @@ -293,22 +295,26 @@ inline DataType::kind_t DataType::kind(const double&) { } inline DataType::kind_t DataType::str_to_kind(const std::string& datatype) { - if (datatype == "int32") + if (datatype == "int32") { return KIND_INT32; - else if (datatype == "int64") + } + if (datatype == "int64") { return KIND_INT64; - else if (datatype == "uint64") + } + if (datatype == "uint64") { return KIND_UINT64; - else if (datatype == "real32") + } + if (datatype == "real32") { return KIND_REAL32; - else if (datatype == "real64") + } + if (datatype == "real64") { return KIND_REAL64; - else if (datatype == "byte") { - return KIND_BYTE; } - else { - throw_not_recognised(datatype); + if (datatype == "byte") { + return KIND_BYTE; } + + throw_not_recognised(datatype); } inline std::string DataType::kind_to_str(kind_t kind) { switch (kind) { @@ -342,14 +348,6 @@ inline bool DataType::kind_valid(kind_t kind) { } } -inline DataType::DataType(const DataType& other) : - kind_(other.kind_) {} - -inline DataType& DataType::operator=(const DataType& other) { - kind_ = other.kind_; - return *this; -} - inline DataType::DataType(const std::string& datatype) : kind_(str_to_kind(datatype)) {} diff --git a/src/eckit/codec/detail/Encoder.h b/src/eckit/codec/detail/Encoder.h index f24faf967..c7e7d8b35 100644 --- a/src/eckit/codec/detail/Encoder.h +++ b/src/eckit/codec/detail/Encoder.h @@ -30,13 +30,13 @@ class Encoder { public: Encoder() = default; - operator bool() const { return bool(self_); } + operator bool() const { return static_cast(self_); } template = 0> explicit Encoder(T&& x) : self_(new EncodableValue(std::move(x))) {} - Encoder(const Link& link) : + explicit Encoder(const Link& link) : self_(new EncodableLink(link)) {} Encoder(Encoder&& other) : @@ -68,13 +68,13 @@ class Encoder { template struct EncodableValue : Encodable { - EncodableValue(Value&& v) : + explicit EncodableValue(Value&& v) : value_{std::move(v)} { sfinae::encode_metadata(value_, metadata_, data_size_); } template = 0> - EncodableValue(const Value& v) : + explicit EncodableValue(const Value& v) : value_{v} { sfinae::encode_metadata(value_, metadata_, data_size_); } @@ -93,7 +93,7 @@ class Encoder { }; struct EncodableLink : Encodable { - EncodableLink(const Link& link) : + explicit EncodableLink(const Link& link) : link_(link) {} size_t encode_metadata_(Metadata& metadata) const override { diff --git a/src/eckit/codec/detail/Link.h b/src/eckit/codec/detail/Link.h index 12a3599ed..c223fe732 100644 --- a/src/eckit/codec/detail/Link.h +++ b/src/eckit/codec/detail/Link.h @@ -24,7 +24,7 @@ struct Link { std::string uri; const std::string& str() const { return uri; } - operator bool() const { return uri.size(); } + operator bool() const { return !uri.empty(); } operator const std::string&() const { return str(); } // bool relative() const; diff --git a/src/eckit/codec/detail/Reference.h b/src/eckit/codec/detail/Reference.h index a81d13087..a7bbde6de 100644 --- a/src/eckit/codec/detail/Reference.h +++ b/src/eckit/codec/detail/Reference.h @@ -23,7 +23,7 @@ namespace eckit::codec { template struct Reference { const T* ref; - Reference(const T& r) : + explicit Reference(const T& r) : ref(&r) {} friend size_t encode_metadata(const Reference& in, Metadata& metadata) { diff --git a/src/eckit/codec/detail/Time.cc b/src/eckit/codec/detail/Time.cc index efff49dbb..3346f87f3 100644 --- a/src/eckit/codec/detail/Time.cc +++ b/src/eckit/codec/detail/Time.cc @@ -28,16 +28,15 @@ namespace eckit::codec { namespace { -static std::time_t to_time_t(Time time) { - return std::time_t(time.tv_sec); +std::time_t to_time_t(Time time) { + return static_cast(time.tv_sec); } -static Time from_time_point(std::chrono::time_point t) { - using namespace std::chrono; +Time from_time_point(std::chrono::time_point t) { auto since_epoch = t.time_since_epoch(); - auto sec_since_epoch = duration_cast(since_epoch); - auto nsec_since_epoch = duration_cast(since_epoch); - auto extra_nsec = duration_cast(nsec_since_epoch - sec_since_epoch); + auto sec_since_epoch = std::chrono::duration_cast(since_epoch); + auto nsec_since_epoch = std::chrono::duration_cast(since_epoch); + auto extra_nsec = std::chrono::duration_cast(nsec_since_epoch - sec_since_epoch); Time time; time.tv_sec = static_cast(sec_since_epoch.count()); @@ -63,7 +62,7 @@ std::ostream& operator<<(std::ostream& out, const Time& time) { return out; } -eckit::JSON& operator<<(eckit::JSON& out, const Time& time) { +JSON& operator<<(JSON& out, const Time& time) { std::stringstream s; s << time; out << s.str(); diff --git a/src/eckit/codec/detail/Type.h b/src/eckit/codec/detail/Type.h index 84ec758fa..2c1321519 100644 --- a/src/eckit/codec/detail/Type.h +++ b/src/eckit/codec/detail/Type.h @@ -18,11 +18,11 @@ namespace eckit::codec { struct Type { const std::string name_; - operator const std::string&() { return name_; } - operator bool() const { return name_.size(); } + explicit operator const std::string&() { return name_; } + operator bool() const { return !name_.empty(); } Type(const char* name) : name_(name) {} - Type(const std::string& name) : + explicit Type(const std::string& name) : name_(name) {} bool operator==(const Type& other) const { return name_ == other.name_; } }; diff --git a/src/eckit/codec/print/Bytes.cc b/src/eckit/codec/print/Bytes.cc index e5e2333e2..18be1254b 100644 --- a/src/eckit/codec/print/Bytes.cc +++ b/src/eckit/codec/print/Bytes.cc @@ -59,7 +59,7 @@ class FixedFormat { }; inline FixedFormat fixed_format(double x, long precision) { - return FixedFormat(x, precision); + return {x, precision}; } /* @@ -75,7 +75,7 @@ static std::pair reduce_to_10( size_t bytes ) { } */ -static std::pair reduce_to_1000(size_t bytes) { +std::pair reduce_to_1000(size_t bytes) { static const std::vector magnitudes{"B", "K", "M", "G", "T", "P", "E", "Z", "Y"}; double x = bytes; size_t n = 0; diff --git a/src/eckit/codec/print/Bytes.h b/src/eckit/codec/print/Bytes.h index 5613089bf..f7f20bf87 100644 --- a/src/eckit/codec/print/Bytes.h +++ b/src/eckit/codec/print/Bytes.h @@ -20,10 +20,10 @@ namespace eckit::codec { struct Bytes { public: - Bytes(size_t bytes) : + explicit Bytes(size_t bytes) : bytes_(bytes) {} - operator size_t() const { return bytes_; } + explicit operator size_t() const { return bytes_; } std::string str(int decimals = 2, int width = 7) const; diff --git a/src/eckit/codec/print/JSONFormat.cc b/src/eckit/codec/print/JSONFormat.cc index 77c00d594..f3120efa4 100644 --- a/src/eckit/codec/print/JSONFormat.cc +++ b/src/eckit/codec/print/JSONFormat.cc @@ -52,7 +52,7 @@ void JSONFormat::print(std::ostream& out) const { } if (print_details_) { - if (item.data.size()) { + if (item.data.size() > 0) { m.set("data.compression.type", item.data.compression()); m.set("data.compression.size", item.data.compressed_size()); m.set("data.size", item.data.size()); diff --git a/src/eckit/codec/print/TableFormat.cc b/src/eckit/codec/print/TableFormat.cc index 27935ac52..c72907938 100644 --- a/src/eckit/codec/print/TableFormat.cc +++ b/src/eckit/codec/print/TableFormat.cc @@ -39,7 +39,7 @@ std::string MetadataPrettyPrintBase::str() const { class DefaultMetadataPrettyPrint : public MetadataPrettyPrintBase { public: - DefaultMetadataPrettyPrint(const Metadata&) {} + explicit DefaultMetadataPrettyPrint(const Metadata&) {} void print(std::ostream&) const override {} }; @@ -59,7 +59,7 @@ class ArrayMetadataPrettyPrint : public MetadataPrettyPrintBase { } public: - ArrayMetadataPrettyPrint(const Metadata& m) : + explicit ArrayMetadataPrettyPrint(const Metadata& m) : metadata_(m) {} void print(std::ostream& out) const override { std::string type = metadata_.getString("type"); @@ -103,7 +103,7 @@ class ArrayMetadataPrettyPrint : public MetadataPrettyPrintBase { class StringMetadataPrettyPrint : public MetadataPrettyPrintBase { public: - StringMetadataPrettyPrint(const Metadata& m) : + explicit StringMetadataPrettyPrint(const Metadata& m) : metadata_(m) {} void print(std::ostream& out) const override { std::string type = metadata_.getString("type"); @@ -123,7 +123,7 @@ class StringMetadataPrettyPrint : public MetadataPrettyPrintBase { class ScalarMetadataPrettyPrint : public MetadataPrettyPrintBase { public: - ScalarMetadataPrettyPrint(const Metadata& m) : + explicit ScalarMetadataPrettyPrint(const Metadata& m) : metadata_(m) {} template T decode() const { @@ -286,8 +286,8 @@ void TableFormat::print(std::ostream& out) const { } table.column(); - for (auto& key : record_.keys()) { - auto& item = items_.at(key); + for (const auto& key : record_.keys()) { + const auto& item = items_.at(key); size_t cbytes = item.data.compressed_size(); size_t ubytes = item.data.size(); std::string endian = (item.data.endian() == Endian::little) ? "little" : "big"; @@ -298,7 +298,7 @@ void TableFormat::print(std::ostream& out) const { table << MetadataPrettyPrint{item}; if (print_details_) { table << item.record.version(); - if (item.data.section()) { + if (item.data.section() != 0) { table << item.data.compression(); table << Bytes{cbytes}.str() + (item.data.compressed() ? " < " + Bytes{ubytes}.str() : ""); table << Checksum{item.data.checksum()}.str(8); diff --git a/src/eckit/codec/print/TableFormat.h b/src/eckit/codec/print/TableFormat.h index 2921a374b..d233985c3 100644 --- a/src/eckit/codec/print/TableFormat.h +++ b/src/eckit/codec/print/TableFormat.h @@ -35,7 +35,7 @@ class MetadataPrettyPrintBase { class MetadataPrettyPrint { public: - MetadataPrettyPrint(const Metadata&); + explicit MetadataPrettyPrint(const Metadata&); friend std::ostream& operator<<(std::ostream& out, const MetadataPrettyPrint& p); std::string str() const; diff --git a/src/eckit/codec/types/ArrayMetadata.cc b/src/eckit/codec/types/ArrayMetadata.cc index 14bdc6d91..3bf59262b 100644 --- a/src/eckit/codec/types/ArrayMetadata.cc +++ b/src/eckit/codec/types/ArrayMetadata.cc @@ -23,7 +23,7 @@ namespace eckit::codec { //--------------------------------------------------------------------------------------------------------------------- size_t encode_metadata(const ArrayMetadata& value, Metadata& out) { - out.set("type", value.type()); + out.set("type", ArrayMetadata::type()); out.set("shape", value.shape_); out.set("datatype", value.datatype_.str()); return value.bytes(); @@ -37,7 +37,7 @@ int ArrayMetadata::shape(int i) const { "ArrayMetadata::shape(i=" + std::to_string(i) + ") goes out of bounds. rank=" + std::to_string(rank()), Here()); } - return shape_[size_t(i)]; + return static_cast(shape_[static_cast(i)]); } //--------------------------------------------------------------------------------------------------------------------- @@ -56,13 +56,13 @@ ArrayMetadata::ArrayMetadata(const Metadata& metadata) : //--------------------------------------------------------------------------------------------------------------------- size_t ArrayMetadata::size() const { - return size_t(std::accumulate(shape_.begin(), shape_.end(), 1, std::multiplies())); + return size_t(std::accumulate(shape_.begin(), shape_.end(), 1, std::multiplies<>())); } //--------------------------------------------------------------------------------------------------------------------- ArrayMetadata::ArrayMetadata() : - shape_(), datatype_(DataType::KIND_REAL64) /* circumvent absense of default constructor */ {} + datatype_(DataType::KIND_REAL64) /* circumvent absense of default constructor */ {} //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/eckit/codec/types/ArrayMetadata.h b/src/eckit/codec/types/ArrayMetadata.h index 556b059fb..41f486f86 100644 --- a/src/eckit/codec/types/ArrayMetadata.h +++ b/src/eckit/codec/types/ArrayMetadata.h @@ -28,9 +28,9 @@ class ArrayShape : public std::vector { using Base = std::vector; public: - ArrayShape() {} + ArrayShape() = default; - ArrayShape(Base&& base) : + explicit ArrayShape(Base&& base) : Base(std::forward(base)) {} template @@ -42,15 +42,15 @@ class ArrayShape : public std::vector { Base(data, data + size) {} template - ArrayShape(const std::array& list) : + explicit ArrayShape(const std::array& list) : Base(list.begin(), list.end()) {} template - ArrayShape(const std::vector& list) : + explicit ArrayShape(const std::vector& list) : Base(list.begin(), list.end()) {} template >> - ArrayShape(Int1 i) { + explicit ArrayShape(Int1 i) { resize(1); operator[](0) = i; } @@ -107,13 +107,13 @@ class ArrayMetadata { explicit ArrayMetadata(const DataType&, const ArrayShape&); - explicit ArrayMetadata(const ArrayMetadata&); + ArrayMetadata(const ArrayMetadata&); ArrayMetadata(ArrayMetadata&&); ArrayMetadata& operator=(ArrayMetadata&&); - int rank() const { return int(shape_.size()); } + int rank() const { return static_cast(shape_.size()); } int shape(int i) const; diff --git a/src/eckit/codec/types/scalar.cc b/src/eckit/codec/types/scalar.cc index bdfcd5a48..e67966cf9 100644 --- a/src/eckit/codec/types/scalar.cc +++ b/src/eckit/codec/types/scalar.cc @@ -70,13 +70,13 @@ void encode_scalar_metadata(const T& value, Metadata& out) { inline void encode_scalar_metadata(const unsigned long& value, Metadata& out) { out.set("type", "scalar"); out.set("datatype", DataType::str()); - out.set("value", size_t(value)); + out.set("value", static_cast(value)); } inline void encode_scalar_metadata(const unsigned long long& value, Metadata& out) { out.set("type", "scalar"); out.set("datatype", DataType::str()); - out.set("value", size_t(value)); + out.set("value", static_cast(value)); } template diff --git a/tests/codec/test_codec_encoding.cc b/tests/codec/test_codec_encoding.cc index 5941552a7..4977acfe2 100644 --- a/tests/codec/test_codec_encoding.cc +++ b/tests/codec/test_codec_encoding.cc @@ -34,25 +34,23 @@ class EncodableType { public: using Operations = std::vector; - EncodableType(std::string s, std::shared_ptr operations = std::make_shared()) : + explicit EncodableType(std::string s, std::shared_ptr operations = std::make_shared()) : str(s), ops(operations) { - ops->push_back("constructor"); + ops->emplace_back("constructor"); } EncodableType() : ops(std::make_shared()) {} - EncodableType(const EncodableType& other) { + EncodableType(const EncodableType& other) : + str(other.str), ops(other.ops) { // This constructor should not be called. - str = other.str; - ops = other.ops; - ops->push_back("copy constructor"); + ops->emplace_back("copy constructor"); } - EncodableType(EncodableType&& other) { - str = std::move(other.str); - ops = other.ops; - ops->push_back("move constructor"); + EncodableType(EncodableType&& other) : + str(std::move(other.str)), ops(other.ops) { + ops->emplace_back("move constructor"); } @@ -60,34 +58,34 @@ class EncodableType { // This assignment should not be called. str = rhs.str; ops = rhs.ops; - ops->push_back("assignment"); + ops->emplace_back("assignment"); return *this; } - EncodableType& operator=(const EncodableType&& rhs) { + EncodableType& operator=(EncodableType&& rhs) { // This assignment should not be called. str = std::move(rhs.str); ops = rhs.ops; - ops->push_back("move"); + ops->emplace_back("move"); return *this; } friend void encode_data(const EncodableType& in, codec::Data& out) { - in.ops->push_back("encode_data"); + in.ops->emplace_back("encode_data"); out.assign(in.str.data(), in.str.size()); } friend size_t encode_metadata(const EncodableType& in, codec::Metadata& metadata) { - in.ops->push_back("encode_metadata"); + in.ops->emplace_back("encode_metadata"); metadata.set("type", "EncodableType"); metadata.set("bytes", in.str.size()); return in.str.size(); } friend void decode(const codec::Metadata&, const codec::Data& b, EncodableType& out) { - out.ops->push_back("decode"); - const char* data = static_cast(b.data()); + out.ops->emplace_back("decode"); + const auto* data = static_cast(b.data()); out.str = std::string(data, data + b.size()); } @@ -118,9 +116,9 @@ CASE("test exceptions") { // ------------------------------------------------------------------------------------------------------- CASE("encoding test::EncodableType") { - static_assert(not codec::is_interpretable(), ""); - static_assert(codec::is_encodable(), ""); - static_assert(codec::is_decodable(), ""); + static_assert(not codec::is_interpretable()); + static_assert(codec::is_encodable()); + static_assert(codec::is_decodable()); const std::string encoded_string{"encoded string"}; EncodableType in(encoded_string); @@ -137,20 +135,20 @@ CASE("encoding test::EncodableType") { // ------------------------------------------------------------------------------------------------------- CASE("encoding codec::types::ArrayView") { - static_assert(not codec::is_interpretable(), ""); - static_assert(codec::can_encode_data(), ""); - static_assert(codec::can_encode_metadata(), ""); - static_assert(codec::is_encodable(), ""); + static_assert(not codec::is_interpretable()); + static_assert(codec::can_encode_data()); + static_assert(codec::can_encode_metadata()); + static_assert(codec::is_encodable()); } // ------------------------------------------------------------------------------------------------------- template void assert_StdVector() { - static_assert(codec::is_interpretable, codec::ArrayReference>(), ""); - static_assert(not codec::can_encode_data>(), ""); - static_assert(not codec::can_encode_metadata>(), ""); - static_assert(not codec::is_encodable>(), ""); + static_assert(codec::is_interpretable, codec::ArrayReference>()); + static_assert(not codec::can_encode_data>()); + static_assert(not codec::can_encode_metadata>()); + static_assert(not codec::is_encodable>()); } template @@ -213,10 +211,10 @@ CASE("encoding std::vector") { template void assert_StdArray() { - static_assert(codec::is_interpretable, codec::ArrayReference>(), ""); - static_assert(not codec::can_encode_data>(), ""); - static_assert(not codec::can_encode_metadata>(), ""); - static_assert(not codec::is_encodable>(), ""); + static_assert(codec::is_interpretable, codec::ArrayReference>()); + static_assert(not codec::can_encode_data>()); + static_assert(not codec::can_encode_metadata>()); + static_assert(not codec::is_encodable>()); } template @@ -617,7 +615,8 @@ CASE("Encode/Decode string") { template void test_encode_decode_scalar() { - T in{std::numeric_limits::max()}, out; + T in{std::numeric_limits::max()}; + T out; codec::Metadata metadata; codec::Data data; encode(in, metadata, data); diff --git a/tests/codec/test_codec_record.cc b/tests/codec/test_codec_record.cc index 68ed5b25d..66a7372c4 100644 --- a/tests/codec/test_codec_record.cc +++ b/tests/codec/test_codec_record.cc @@ -85,7 +85,7 @@ namespace globals { struct TestRecord { Arrays data; TestRecord() = default; - TestRecord(const std::function& initializer) { initializer(data); } + explicit TestRecord(const std::function& initializer) { initializer(data); } }; static TestRecord record1{[](Arrays& data) { @@ -164,7 +164,7 @@ CASE("Write records to same file using record.write(path,codec::Mode)") { record.set("v2", codec::ref(data.v2), no_compression); record.set("v3", codec::ref(data.v3)); - globals::records.emplace_back(codec::Record::URI{path, offsets.back()}); + globals::records.emplace_back(path, offsets.back()); lengths.emplace_back(record.write(path, mode)); offsets.emplace_back(offsets.back() + lengths.back()); }; @@ -241,7 +241,7 @@ CASE("Write records in nested subdirectories") { record.set("v2", codec::ref(globals::record1.data.v2)); record.set("v3", codec::ref(globals::record1.data.v3)); record.set("s1", std::string("short string")); - record.set("s2", double(1. / 3.)); + record.set("s2", (1. / 3.)); record.write(reference_path / "links" / "1" / "record.atlas" + suffix()); } { @@ -251,7 +251,7 @@ CASE("Write records in nested subdirectories") { record.set("v1", codec::ref(globals::record2.data.v1)); record.set("v2", codec::ref(globals::record2.data.v2)); record.set("v3", codec::ref(globals::record2.data.v3)); - record.set("s1", size_t(10000000000)); + record.set("s1", static_cast(10000000000)); record.write(reference_path / "links" / "2" / "record.atlas" + suffix()); } { @@ -341,7 +341,9 @@ CASE("Test RecordItemReader") { //----------------------------------------------------------------------------- CASE("Read records from different files") { - Arrays data1, data2, data3; + Arrays data1; + Arrays data2; + Arrays data3; auto read_record = [&](const eckit::PathName& path, Arrays& data) { codec::RecordReader record(path); @@ -362,7 +364,8 @@ CASE("Read records from different files") { //----------------------------------------------------------------------------- CASE("Read multiple records from same file") { - Arrays data1, data2; + Arrays data1; + Arrays data2; codec::RecordReader record1(globals::records[0]); codec::RecordReader record2(globals::records[1]); @@ -395,7 +398,8 @@ CASE("Write master record referencing record1 and record2") { //----------------------------------------------------------------------------- CASE("Read master record") { - Arrays data1, data2; + Arrays data1; + Arrays data2; codec::RecordReader record("record.atlas" + suffix()); eckit::Log::info() << "record.metadata(\"v1\"): " << record.metadata("v1") << std::endl; @@ -415,7 +419,8 @@ CASE("Read master record") { //----------------------------------------------------------------------------- CASE("Async read") { - Arrays data1, data2; + Arrays data1; + Arrays data2; codec::RecordReader record("record.atlas" + suffix()); // Request reads @@ -451,7 +456,8 @@ CASE("Recursive Write/read records in nested subdirectories") { // Read - Arrays data1, data2; + Arrays data1; + Arrays data2; codec::RecordReader record(reference_path / "record.atlas" + suffix()); record.read("l1", data1.v1).wait(); @@ -473,7 +479,7 @@ CASE("Recursive Write/read records in nested subdirectories") { EXPECT(data2 == globals::record2.data); EXPECT_EQUAL(l7, "short string"); EXPECT_EQUAL(l8, 1. / 3.); - EXPECT_EQUAL(l9, 10000000000ul); + EXPECT_EQUAL(l9, 10000000000UL); } //----------------------------------------------------------------------------- diff --git a/tests/codec/test_codec_stream.cc b/tests/codec/test_codec_stream.cc index 8f07ff710..bd57ae354 100644 --- a/tests/codec/test_codec_stream.cc +++ b/tests/codec/test_codec_stream.cc @@ -87,7 +87,9 @@ CASE("Opening same file in same scope") { out.write("line3\n", 6); } - std::string l1(5, ' '), l2(5, ' '), l3(5, ' '); + std::string l1(5, ' '); + std::string l2(5, ' '); + std::string l3(5, ' '); codec::Stream f1 = codec::InputFileStream{"test_io_session.data"}; f1.seek(0 * 6); @@ -122,7 +124,9 @@ CASE("Opening same file in parallel scopes") { out.write("line3\n", 6); } - std::string l1(5, ' '), l2(5, ' '), l3(5, ' '); + std::string l1(5, ' '); + std::string l2(5, ' '); + std::string l3(5, ' '); { codec::Stream f1 = codec::InputFileStream{"test_io_session.data"}; @@ -168,7 +172,9 @@ CASE("Opening same file in parallel scopes with Session") { } - std::string l1(5, ' '), l2(5, ' '), l3(5, ' '); + std::string l1(5, ' '); + std::string l2(5, ' '); + std::string l3(5, ' '); { codec::Stream f1 = codec::InputFileStream{"test_io_session.data"};