From 370dde90255b7347eef13a32a7aebe9bbe1b35f8 Mon Sep 17 00:00:00 2001 From: Jan Kuhlmann <33833587+M2-TE@users.noreply.github.com> Date: Wed, 13 Nov 2024 16:58:53 +0000 Subject: [PATCH] Fix ODR violations via function inlining (#5) --- graphs.hpp | 40 ++++++++++++++++++++-------------------- tables.hpp | 16 +++++++--------- 2 files changed, 27 insertions(+), 29 deletions(-) diff --git a/graphs.hpp b/graphs.hpp index 07cb94f..92873fe 100644 --- a/graphs.hpp +++ b/graphs.hpp @@ -168,9 +168,6 @@ namespace graphs bool check = true; }; - const options defaultoptions; - options histogram_defaultoptions; - template constexpr size_t size(const T &array) { @@ -179,7 +176,7 @@ namespace graphs // Number of columns needed to represent the string // Adapted from: https://stackoverflow.com/a/31124065 - int strcol(const char *const str) + inline int strcol(const char *const str) { size_t length = strlen(str); for (size_t i = 0; i < length; ++i) @@ -224,7 +221,7 @@ namespace graphs // Word wrap // Source: https://gist.github.com/tdulcet/819821ca69501822ad3f84a060c640a0 // Adapted from: https://stackoverflow.com/a/42016346 and https://stackoverflow.com/a/13094734 - string wrap(const char *const str, const size_t line_length) + inline string wrap(const char *const str, const size_t line_length) { string words = str; string wrapped; @@ -272,7 +269,7 @@ namespace graphs // Auto-scale number to unit // Adapted from: https://github.com/coreutils/coreutils/blob/master/src/numfmt.c - void outputunit(long double number, const units_type scale, ostringstream &strm) + inline void outputunit(long double number, const units_type scale, ostringstream &strm) { unsigned x = 0; long double val = number; @@ -353,7 +350,7 @@ namespace graphs } // Convert fractions and constants to Unicode characters - void outputfraction(const long double number, ostringstream &strm) + inline void outputfraction(const long double number, ostringstream &strm) { bool output = false; @@ -403,7 +400,7 @@ namespace graphs strm << number; } - size_t outputlabel(const long double label, const units_type units, ostringstream &strm) + inline size_t outputlabel(const long double label, const units_type units, ostringstream &strm) { strm.imbue(locale("")); @@ -451,7 +448,7 @@ namespace graphs } // Output graph - int graph(const size_t height, const size_t width, const long double xmin, const long double xmax, const long double ymin, const long double ymax, const vector> &array, const options &aoptions) + inline int graph(const size_t height, const size_t width, const long double xmin, const long double xmax, const long double ymin, const long double ymax, const vector> &array, const options &aoptions) { if (!graphs::size(array)) return 1; @@ -777,7 +774,7 @@ namespace graphs } template - int histogram(size_t height, size_t width, long double xmin, long double xmax, long double ymin, long double ymax, const T &aarray, /* const */ options &aoptions = histogram_defaultoptions) + int histogram(size_t height, size_t width, long double xmin, long double xmax, long double ymin, long double ymax, const T &aarray, const options &aoptions = {}) { if (!graphs::size(aarray)) return 1; @@ -869,14 +866,17 @@ namespace graphs for (size_t y = ay >= ymax ? 0 : yaxis - (ay / ystep); y < yaxis and y < height; ++y) aaarray[x][y] = acolor; } - - aoptions.type = type_histogram; - + + if (aoptions.type != type_histogram) { + options hist_options = aoptions; + hist_options.type = type_histogram; + return graph(height, width, xmin, xmax, ymin, ymax, aaarray, hist_options); + } return graph(height, width, xmin, xmax, ymin, ymax, aaarray, aoptions); } template - int histogram(size_t height, size_t width, long double xmin, long double xmax, long double ymin, long double ymax, const size_t rows, T *aarray, /* const */ options &aoptions = histogram_defaultoptions) + int histogram(size_t height, size_t width, long double xmin, long double xmax, long double ymin, long double ymax, const size_t rows, T *aarray, const options &aoptions = {}) { if (rows == 0) return 1; @@ -889,7 +889,7 @@ namespace graphs // Convert one or more arrays to graph and output template - int plots(size_t height, size_t width, long double xmin, long double xmax, long double ymin, long double ymax, const T &arrays, const options &aoptions = defaultoptions) + int plots(size_t height, size_t width, long double xmin, long double xmax, long double ymin, long double ymax, const T &arrays, const options &aoptions = {}) { if (!graphs::size(arrays)) return 1; @@ -1012,7 +1012,7 @@ namespace graphs // Convert single array to graph and output template - int plot(size_t height, size_t width, long double xmin, long double xmax, long double ymin, long double ymax, const T &aarray, const options &aoptions = defaultoptions) + int plot(size_t height, size_t width, long double xmin, long double xmax, long double ymin, long double ymax, const T &aarray, const options &aoptions = {}) { const std::array aaarray = {aarray}; @@ -1021,7 +1021,7 @@ namespace graphs // Convert single array to graph and output template - int plot(size_t height, size_t width, long double xmin, long double xmax, long double ymin, long double ymax, const size_t rows, T **aarray, const options &aoptions = defaultoptions) + int plot(size_t height, size_t width, long double xmin, long double xmax, long double ymin, long double ymax, const size_t rows, T **aarray, const options &aoptions = {}) { if (rows == 0) return 1; @@ -1036,7 +1036,7 @@ namespace graphs // Convert one or more functions to graph and output template - int functions(size_t height, size_t width, const long double xmin, const long double xmax, const long double ymin, const long double ymax, const size_t numfunctions, function functions[], const options &aoptions = defaultoptions) + int functions(size_t height, size_t width, const long double xmin, const long double xmax, const long double ymin, const long double ymax, const size_t numfunctions, function functions[], const options &aoptions = {}) { const color_type color = aoptions.color; @@ -1125,7 +1125,7 @@ namespace graphs // Convert single function to function array and output template - int function(size_t height, size_t width, const long double xmin, const long double xmax, const long double ymin, const long double ymax, const function &afunction, const options &aoptions = defaultoptions) + int function(size_t height, size_t width, const long double xmin, const long double xmax, const long double ymin, const long double ymax, const function &afunction, const options &aoptions = {}) { std::function afunctions[] = {afunction}; @@ -1134,7 +1134,7 @@ namespace graphs // Convert single function to function array and output template - int function(size_t height, size_t width, const long double xmin, const long double xmax, const long double ymin, const long double ymax, T afunction(T), const options &aoptions = defaultoptions) + int function(size_t height, size_t width, const long double xmin, const long double xmax, const long double ymin, const long double ymax, T afunction(T), const options &aoptions = {}) { std::function afunctions[] = {afunction}; diff --git a/tables.hpp b/tables.hpp index 268b888..00fd608 100644 --- a/tables.hpp +++ b/tables.hpp @@ -65,8 +65,6 @@ namespace tables bool check = true; }; - const options defaultoptions; - template constexpr size_t size(const T &array) { @@ -75,7 +73,7 @@ namespace tables // Number of columns needed to represent the string // Adapted from: https://stackoverflow.com/a/31124065 - int strcol(const char *str) + inline int strcol(const char *str) { const string astr = regex_replace(str, ansi, ""); str = astr.c_str(); @@ -123,7 +121,7 @@ namespace tables // Word wrap // Source: https://gist.github.com/tdulcet/819821ca69501822ad3f84a060c640a0 // Adapted from: https://stackoverflow.com/a/42016346 and https://stackoverflow.com/a/13094734 - string wrap(const char *const str, const size_t line_length) + inline string wrap(const char *const str, const size_t line_length) { string words = str; string wrapped; @@ -352,7 +350,7 @@ namespace tables // Convert array to char array and output as table template - int array(const T1 &aarray, T2 headerrow[] = nullptr, T2 headercolumn[] = nullptr, const options &aoptions = defaultoptions) + int array(const T1 &aarray, T2 headerrow[] = nullptr, T2 headercolumn[] = nullptr, const options &aoptions = {}) { if (!tables::size(aarray)) return 1; @@ -423,7 +421,7 @@ namespace tables } template - int array(const size_t rows, const size_t columns, T **aarray, const char *const headerrow[] = nullptr, const char *const headercolumn[] = nullptr, const options &aoptions = defaultoptions) + int array(const size_t rows, const size_t columns, T **aarray, const char *const headerrow[] = nullptr, const char *const headercolumn[] = nullptr, const options &aoptions = {}) { vector> aaarray(rows, vector(columns)); for (size_t i = 0; i < rows; ++i) @@ -460,7 +458,7 @@ namespace tables // Convert one or more functions to array and output as table template - int functions(const long double xmin, const long double xmax, const long double xstep, const size_t numfunctions, function functions[], const options &aoptions = defaultoptions) + int functions(const long double xmin, const long double xmax, const long double xstep, const size_t numfunctions, function functions[], const options &aoptions = {}) { if (numfunctions == 0) return 1; @@ -538,7 +536,7 @@ namespace tables // Convert single function to array and output as table template - int function(const long double xmin, const long double xmax, const long double xstep, const function &afunction, const options &aoptions = defaultoptions) + int function(const long double xmin, const long double xmax, const long double xstep, const function &afunction, const options &aoptions = {}) { std::function afunctions[] = {afunction}; @@ -547,7 +545,7 @@ namespace tables // Convert single function to array and output as table template - int function(const long double xmin, const long double xmax, const long double xstep, T afunction(T), const options &aoptions = defaultoptions) + int function(const long double xmin, const long double xmax, const long double xstep, T afunction(T), const options &aoptions = {}) { std::function afunctions[] = {afunction};