From b28cfcc332734e263efd4bf44438c5d433a3415e Mon Sep 17 00:00:00 2001 From: Christopher Lackner Date: Mon, 10 Dec 2018 14:37:27 +0100 Subject: [PATCH] fix casting, fix rest of core guidelines --- libsrc/core/.clang-tidy | 4 +- libsrc/core/archive.cpp | 6 ++ libsrc/core/archive.hpp | 134 ++++++++++++++++++++++------------------ libsrc/core/version.hpp | 3 +- 4 files changed, 85 insertions(+), 62 deletions(-) diff --git a/libsrc/core/.clang-tidy b/libsrc/core/.clang-tidy index 6d5e34da..65b4984d 100644 --- a/libsrc/core/.clang-tidy +++ b/libsrc/core/.clang-tidy @@ -1,4 +1,4 @@ -Checks: '*,-clang-analyzer-alpha.*,-*braces-around-statements,-fuchsia-*,-google-runtime-references,-readability-implicit-bool-conversion,-google-explicit-constructor,-hicpp-explicit-conversions' +Checks: '*,-clang-analyzer-alpha.*,-*braces-around-statements,-fuchsia-*,-google-runtime-references,-readability-implicit-bool-conversion,-google-explicit-constructor,-hicpp-explicit-conversions,-google-runtime-int,-llvm-header-guard' CheckOptions: - key: cppcoreguidelines-special-member-functions.AllowSoleDefaultDtor - value: 1 \ No newline at end of file + value: 1 diff --git a/libsrc/core/archive.cpp b/libsrc/core/archive.cpp index 9fe54704..1e45bfb4 100644 --- a/libsrc/core/archive.cpp +++ b/libsrc/core/archive.cpp @@ -1,4 +1,10 @@ +#include // for function +#include // for runtime_error +#include // for declval, enable_if, false_type, is_co... +#include // for type_info +#include // for move, swap, pair + #include "archive.hpp" #ifndef WIN32 diff --git a/libsrc/core/archive.hpp b/libsrc/core/archive.hpp index 0b87e843..45fb31f5 100644 --- a/libsrc/core/archive.hpp +++ b/libsrc/core/archive.hpp @@ -1,19 +1,22 @@ #ifndef NETGEN_CORE_ARCHIVE_HPP #define NETGEN_CORE_ARCHIVE_HPP -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include // for complex +#include // for size_t, strlen +#include // for operator<<, ifstream, ofstream, basic... +#include // for function +#include // for map, _Rb_tree_iterator +#include // for __shared_ptr_access, __shared_ptr_acc... +#include // for runtime_error +#include // for string, operator+ +#include // for declval, enable_if, false_type, is_co... +#include // for type_info +#include // for move, swap, pair +#include // for vector -#include "ngcore_api.hpp" -#include "type_traits.hpp" -#include "version.hpp" +#include "ngcore_api.hpp" // for NGCORE_API, unlikely +#include "type_traits.hpp" // for all_of_tmpl +#include "version.hpp" // for VersionInfo namespace ngcore { @@ -30,7 +33,7 @@ namespace ngcore { throw std::runtime_error(std::string(demangle(typeid(T).name())) + " is not default constructible!"); } template::value>::type> - T* constructIfPossible_impl(int /*unused*/) { return new T; } + T* constructIfPossible_impl(int /*unused*/) { return new T; } // NOLINT template T* constructIfPossible() { return constructIfPossible_impl(int{}); } @@ -45,7 +48,7 @@ namespace ngcore typename std::is_same().DoArchive(std::declval())),void>::type; template static constexpr std::false_type check(...); - using type = decltype(check(nullptr)); + using type = decltype(check(nullptr)); // NOLINT public: NGCORE_API static constexpr bool value = type::value; }; @@ -60,7 +63,7 @@ namespace ngcore typename std::is_same() & std::declval()),Archive&>::type; template static constexpr std::false_type check(...); - using type = decltype(check(nullptr)); + using type = decltype(check(nullptr)); // NOLINT public: NGCORE_API static constexpr bool value = type::value; }; @@ -107,11 +110,17 @@ namespace ngcore static void SetArchiveRegister(const std::string& classname, const ClassArchiveInfo& info); static bool IsRegistered(const std::string& classname); public: + Archive() = delete; + Archive(const Archive&) = delete; + Archive(Archive&&) = delete; Archive (bool ais_output) : is_output(ais_output), shared_ptr_count(0), ptr_count(0) { ; } virtual ~Archive() { ; } + Archive& operator=(const Archive&) = delete; + Archive& operator=(Archive&&) = delete; + bool Output () { return is_output; } bool Input () { return !is_output; } virtual const VersionInfo& getVersion(const std::string& library) = 0; @@ -183,28 +192,28 @@ namespace ngcore // this functions can be overloaded in Archive implementations for more efficiency template ::value>::type> Archive & Do (T * data, size_t n) - { for (size_t j = 0; j < n; j++) { (*this) & data[j]; }; return *this; }; + { for (size_t j = 0; j < n; j++) { (*this) & data[j]; }; return *this; }; // NOLINT virtual Archive & Do (double * d, size_t n) - { for (size_t j = 0; j < n; j++) { (*this) & d[j]; }; return *this; }; + { for (size_t j = 0; j < n; j++) { (*this) & d[j]; }; return *this; }; // NOLINT virtual Archive & Do (int * i, size_t n) - { for (size_t j = 0; j < n; j++) { (*this) & i[j]; }; return *this; }; + { for (size_t j = 0; j < n; j++) { (*this) & i[j]; }; return *this; }; // NOLINT virtual Archive & Do (long * i, size_t n) - { for (size_t j = 0; j < n; j++) { (*this) & i[j]; }; return *this; }; + { for (size_t j = 0; j < n; j++) { (*this) & i[j]; }; return *this; }; // NOLINT virtual Archive & Do (size_t * i, size_t n) - { for (size_t j = 0; j < n; j++) { (*this) & i[j]; }; return *this; }; + { for (size_t j = 0; j < n; j++) { (*this) & i[j]; }; return *this; }; // NOLINT virtual Archive & Do (short * i, size_t n) - { for (size_t j = 0; j < n; j++) { (*this) & i[j]; }; return *this; }; + { for (size_t j = 0; j < n; j++) { (*this) & i[j]; }; return *this; }; // NOLINT virtual Archive & Do (unsigned char * i, size_t n) - { for (size_t j = 0; j < n; j++) { (*this) & i[j]; }; return *this; }; + { for (size_t j = 0; j < n; j++) { (*this) & i[j]; }; return *this; }; // NOLINT virtual Archive & Do (bool * b, size_t n) - { for (size_t j = 0; j < n; j++) { (*this) & b[j]; }; return *this; }; + { for (size_t j = 0; j < n; j++) { (*this) & b[j]; }; return *this; }; // NOLINT // Archive a class implementing a (void DoArchive(Archive&)) method ======= template::value>> @@ -254,7 +263,6 @@ namespace ngcore (*this) << pos->second << neededDowncast; if(neededDowncast) (*this) << demangle(typeid(*ptr).name()); - return (*this); } else // Input { @@ -267,7 +275,7 @@ namespace ngcore return *this; } // -1 restores a new shared ptr by restoring the inner pointer and creating a shared_ptr to it - else if (nr == -1) + if (nr == -1) { T* p; bool neededDowncast; @@ -329,8 +337,7 @@ namespace ngcore throw std::runtime_error(std::string("Archive error: Polymorphic type ") + demangle(typeid(*p).name()) + " not registered for archive"); - else - reg_ptr = GetArchiveRegister(demangle(typeid(*p).name())).downcaster(typeid(T), static_cast(p)); + reg_ptr = GetArchiveRegister(demangle(typeid(*p).name())).downcaster(typeid(T), static_cast(p)); } auto pos = ptr2nr.find(reg_ptr); // if the pointer is not found in the map create a new entry @@ -355,8 +362,7 @@ namespace ngcore throw std::runtime_error(std::string("Archive error: Polymorphic type ") + demangle(typeid(*p).name()) + " not registered for archive"); - else - return (*this) << -3 << demangle(typeid(*p).name()) & (*p); + return (*this) << -3 << demangle(typeid(*p).name()) & (*p); } } else @@ -452,7 +458,7 @@ namespace ngcore { try { return GetArchiveRegister(demangle(typeid(B1).name())).upcaster(ti, static_cast(dynamic_cast(p))); } - catch(std::exception) + catch(std::exception&) { return Caster::tryUpcast(ti, p); } } @@ -461,8 +467,8 @@ namespace ngcore if(typeid(B1) == ti) return dynamic_cast(static_cast(p)); try - { return GetArchiveRegister(demangle(typeid(B1).name())).downcaster(ti, static_cast(dynamic_cast(p))); } - catch(std::exception) + { return GetArchiveRegister(demangle(typeid(B1).name())).downcaster(ti, static_cast(dynamic_cast(static_cast(p)))); } + catch(std::exception&) { return Caster::tryDowncast(ti, p); } } }; @@ -494,17 +500,24 @@ namespace ngcore class NGCORE_API BinaryOutArchive : public Archive { size_t ptr = 0; - enum { BUFFERSIZE = 1024 }; - char buffer[BUFFERSIZE] alignas(64); + static constexpr size_t BUFFERSIZE = 1024; + alignas(64) char buffer[BUFFERSIZE] = {}; std::shared_ptr fout; public: - BinaryOutArchive(std::shared_ptr afout) : Archive(true), fout(afout) + BinaryOutArchive() = delete; + BinaryOutArchive(const BinaryOutArchive&) = delete; + BinaryOutArchive(BinaryOutArchive&&) = delete; + BinaryOutArchive(std::shared_ptr&& afout) + : Archive(true), fout(std::move(afout)) { (*this) & GetLibraryVersions(); } - BinaryOutArchive(std::string filename) + BinaryOutArchive(const std::string& filename) : BinaryOutArchive(std::make_shared(filename)) {} - virtual ~BinaryOutArchive () { FlushBuffer(); } + ~BinaryOutArchive () override { FlushBuffer(); } + + BinaryOutArchive& operator=(const BinaryOutArchive&) = delete; + BinaryOutArchive& operator=(BinaryOutArchive&&) = delete; const VersionInfo& getVersion(const std::string& library) override { return GetLibraryVersions()[library]; } @@ -539,7 +552,7 @@ namespace ngcore (*this) & len; FlushBuffer(); if(len > 0) - fout->write (&str[0], len); + fout->write (&str[0], len); // NOLINT return *this; } void FlushBuffer() override @@ -571,14 +584,15 @@ namespace ngcore // BinaryInArchive ====================================================================== class NGCORE_API BinaryInArchive : public Archive { - std::map vinfo; + std::map vinfo{}; std::shared_ptr fin; public: - BinaryInArchive (std::shared_ptr afin) : Archive(false), fin(afin) + BinaryInArchive (std::shared_ptr&& afin) + : Archive(false), fin(std::move(afin)) { (*this) & vinfo; } - BinaryInArchive (std::string filename) + BinaryInArchive (const std::string& filename) : BinaryInArchive(std::make_shared(filename)) { ; } const VersionInfo& getVersion(const std::string& library) override @@ -605,7 +619,7 @@ namespace ngcore (*this) & len; str.resize(len); if(len) - fin->read(&str[0], len); + fin->read(&str[0], len); // NOLINT return *this; } Archive & operator & (char *& str) override @@ -616,24 +630,24 @@ namespace ngcore str = nullptr; else { - str = new char[len+1]; - fin->read(&str[0], len); - str[len] = '\0'; + str = new char[len+1]; // NOLINT + fin->read(&str[0], len); // NOLINT + str[len] = '\0'; // NOLINT } return *this; } Archive & Do (double * d, size_t n) override - { fin->read(reinterpret_cast(d), n*sizeof(double)); return *this; } + { fin->read(reinterpret_cast(d), n*sizeof(double)); return *this; } // NOLINT Archive & Do (int * i, size_t n) override - { fin->read(reinterpret_cast(i), n*sizeof(int)); return *this; } + { fin->read(reinterpret_cast(i), n*sizeof(int)); return *this; } // NOLINT Archive & Do (size_t * i, size_t n) override - { fin->read(reinterpret_cast(i), n*sizeof(size_t)); return *this; } + { fin->read(reinterpret_cast(i), n*sizeof(size_t)); return *this; } // NOLINT private: template inline void Read(T& val) - { fin->read(reinterpret_cast(&val), sizeof(T)); } + { fin->read(reinterpret_cast(&val), sizeof(T)); } // NOLINT }; // TextOutArchive ====================================================================== @@ -641,11 +655,12 @@ namespace ngcore { std::shared_ptr fout; public: - TextOutArchive (std::shared_ptr afout) : Archive(true), fout(afout) + TextOutArchive (std::shared_ptr&& afout) + : Archive(true), fout(std::move(afout)) { (*this) & GetLibraryVersions(); } - TextOutArchive (std::string filename) : + TextOutArchive (const std::string& filename) : TextOutArchive(std::make_shared(filename)) { } const VersionInfo& getVersion(const std::string& library) override @@ -672,7 +687,7 @@ namespace ngcore *fout << len << '\n'; if(len) { - fout->write(&str[0], len); + fout->write(&str[0], len); // NOLINT *fout << '\n'; } return *this; @@ -683,7 +698,7 @@ namespace ngcore *this & len; if(len > 0) { - fout->write (&str[0], len); + fout->write (&str[0], len); // NOLINT *fout << '\n'; } return *this; @@ -693,14 +708,15 @@ namespace ngcore // TextInArchive ====================================================================== class NGCORE_API TextInArchive : public Archive { - std::map vinfo; + std::map vinfo{}; std::shared_ptr fin; public: - TextInArchive (std::shared_ptr afin) : Archive(false), fin(afin) + TextInArchive (std::shared_ptr&& afin) : + Archive(false), fin(std::move(afin)) { (*this) & vinfo; } - TextInArchive (std::string filename) + TextInArchive (const std::string& filename) : TextInArchive(std::make_shared(filename)) {} const VersionInfo& getVersion(const std::string& library) override @@ -742,13 +758,13 @@ namespace ngcore str = nullptr; return (*this); } - str = new char[len+1]; + str = new char[len+1]; // NOLINT if(len) { fin->get(ch); // \n - fin->get(&str[0], len+1, '\0'); + fin->get(&str[0], len+1, '\0'); // NOLINT } - str[len] = '\0'; + str[len] = '\0'; // NOLINT return *this; } }; diff --git a/libsrc/core/version.hpp b/libsrc/core/version.hpp index 27828e6e..ab14d74a 100644 --- a/libsrc/core/version.hpp +++ b/libsrc/core/version.hpp @@ -1,10 +1,11 @@ #ifndef NETGEN_CORE_VERSION_HPP #define NETGEN_CORE_VERSION_HPP -#include "ngcore_api.hpp" #include #include +#include "ngcore_api.hpp" + namespace ngcore { class Archive;