From 8bc450f5ac7c4a9ae15b9a9e8085a79cbeb7182d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org> Date: Thu, 10 Jan 2019 18:24:04 +0100 Subject: [PATCH] Various modernizations --- dynare++/parser/cc/formula_parser.cc | 1 + dynare++/sylv/cc/Vector.cc | 3 +- dynare++/sylv/cc/Vector.hh | 5 +-- dynare++/utils/cc/exception.hh | 47 ++++++++++------------------ dynare++/utils/cc/pascal_triangle.cc | 9 +++--- dynare++/utils/cc/pascal_triangle.hh | 11 ++----- 6 files changed, 30 insertions(+), 46 deletions(-) diff --git a/dynare++/parser/cc/formula_parser.cc b/dynare++/parser/cc/formula_parser.cc index 39724a130..6e8dbec31 100644 --- a/dynare++/parser/cc/formula_parser.cc +++ b/dynare++/parser/cc/formula_parser.cc @@ -11,6 +11,7 @@ #include "formula_tab.hh" #include <cmath> +#include <cstring> using namespace ogp; diff --git a/dynare++/sylv/cc/Vector.cc b/dynare++/sylv/cc/Vector.cc index 47ac74408..1c529113b 100644 --- a/dynare++/sylv/cc/Vector.cc +++ b/dynare++/sylv/cc/Vector.cc @@ -413,6 +413,5 @@ ConstVector::print() const ZeroPad::ZeroPad() { - for (double & i : pad) - i = 0.0; + pad.fill(0.0); } diff --git a/dynare++/sylv/cc/Vector.hh b/dynare++/sylv/cc/Vector.hh index 80bb0def5..5873a0a12 100644 --- a/dynare++/sylv/cc/Vector.hh +++ b/dynare++/sylv/cc/Vector.hh @@ -9,6 +9,7 @@ * to avoid running virtual method invokation mechanism. Some * members, and methods are thus duplicated */ +#include <array> #include <cstdio> class GeneralMatrix; @@ -235,13 +236,13 @@ class ZeroPad public: static const int length = 16; private: - double pad[16]; + std::array<double, length> pad; public: ZeroPad(); const double * getBase() const { - return pad; + return pad.data(); } }; diff --git a/dynare++/utils/cc/exception.hh b/dynare++/utils/cc/exception.hh index dc579d2c6..5ddd446ff 100644 --- a/dynare++/utils/cc/exception.hh +++ b/dynare++/utils/cc/exception.hh @@ -5,54 +5,41 @@ #ifndef OGU_EXCEPTION_H #define OGU_EXCEPTION_H -#include <cstdio> -#include <cstring> - #include <string> -#include <algorithm> +#include <iostream> +#include <utility> namespace ogu { - /** A primitive exception. */ class Exception { - static const int file_length = 100; - static const int mes_length = 500; protected: - char file[file_length]; - int line; - char mes[mes_length]; + const std::string file; + const int line; + const std::string mes; public: - Exception(const char *f, int l, const char *m) - { - strncpy(file, f, file_length-1); - file[file_length-1] = '\0'; - line = l; - strncpy(mes, m, std::min(mes_length-1, (int) strlen(m))); - mes[mes_length-1] = '\0'; - } - Exception(const char *f, int l, const std::string &m) + Exception(std::string file_arg, int line_arg, std::string mes_arg) + : file{std::move(file_arg)}, + line{line_arg}, + mes{std::move(mes_arg)} { - strncpy(file, f, file_length-1); - file[file_length-1] = '\0'; - line = l; - strncpy(mes, m.c_str(), std::min(mes_length-1, (int) m.length())); - mes[mes_length-1] = '\0'; } - virtual ~Exception() - = default; + virtual ~Exception() = default; + void - print(FILE *fd) const + print(std::ostream &out) const { - fprintf(fd, "%s:%d: %s\n", file, line, mes); + out << file << ':' << line << ": " << mes << std::endl; } + void print() const { - print(stdout); + print(std::cout); } - const char * + + std::string message() const { return mes; diff --git a/dynare++/utils/cc/pascal_triangle.cc b/dynare++/utils/cc/pascal_triangle.cc index 21e1d36dd..093f48246 100644 --- a/dynare++/utils/cc/pascal_triangle.cc +++ b/dynare++/utils/cc/pascal_triangle.cc @@ -1,5 +1,6 @@ #include "pascal_triangle.hh" -#include <cstdio> + +#include <iostream> using namespace ogu; @@ -41,10 +42,10 @@ PascalRow::prolongFirst(int n) void PascalRow::print() const { - printf("k=%d\n", k); + std::cout << "k=" << k << std::endl; for (unsigned int i = 0; i < size(); i++) - printf("%d ", operator[](i)); - printf("\n"); + std::cout << operator[](i) << ' '; + std::cout << std::endl; } int diff --git a/dynare++/utils/cc/pascal_triangle.hh b/dynare++/utils/cc/pascal_triangle.hh index 6b316532d..4cd2adb50 100644 --- a/dynare++/utils/cc/pascal_triangle.hh +++ b/dynare++/utils/cc/pascal_triangle.hh @@ -16,8 +16,7 @@ namespace ogu { int k{1}; public: - PascalRow() - : vector<int>() + PascalRow() : vector<int>{} { push_back(2); } @@ -35,12 +34,8 @@ namespace ogu { tr.emplace_back(); } - PascalTriangle(const PascalTriangle &triang) - - = default; - PascalTriangle & - operator=(const PascalTriangle &triang) - = default; + PascalTriangle(const PascalTriangle &triang) = default; + PascalTriangle &operator=(const PascalTriangle &triang) = default; int noverk(int n, int k); void print() const; protected: -- GitLab