From e0e38a931c45e38593a085bf6256178da10b994a Mon Sep 17 00:00:00 2001 From: Thomas Fussell Date: Mon, 20 Mar 2017 19:20:12 -0400 Subject: [PATCH] remove template from cell::value setter, closes #131 --- include/xlnt/cell/cell.hpp | 88 ++++++++++++++++++++++++++++-- source/cell/cell.cpp | 109 +++++++++++-------------------------- 2 files changed, 115 insertions(+), 82 deletions(-) diff --git a/include/xlnt/cell/cell.hpp b/include/xlnt/cell/cell.hpp index bd555336..941217b8 100644 --- a/include/xlnt/cell/cell.hpp +++ b/include/xlnt/cell/cell.hpp @@ -31,6 +31,7 @@ #include #include #include +#include namespace xlnt { @@ -116,12 +117,89 @@ public: void clear_value(); /// - /// Sets the value of this cell to the given value. - /// Overloads exist for most C++ fundamental types like bool, int, etc. as well - /// as for std::string and xlnt datetime types: date, time, datetime, and timedelta. + /// Sets the type of this cell to null. /// - template - void value(T value); + void value(std::nullptr_t); + + /// + /// Sets the value of this cell to the given boolean value. + /// + void value(bool boolean_value); + + /// + /// Sets the value of this cell to the given value. + /// + void value(int int_value); + + /// + /// Sets the value of this cell to the given value. + /// + void value(unsigned int int_value); + + /// + /// Sets the value of this cell to the given value. + /// + void value(long long int int_value); + + /// + /// Sets the value of this cell to the given value. + /// + void value(unsigned long long int int_value); + + /// + /// Sets the value of this cell to the given value. + /// + void value(float float_value); + + /// + /// Sets the value of this cell to the given value. + /// + void value(double float_value); + + /// + /// Sets the value of this cell to the given value. + /// + void value(long double float_value); + + /// + /// Sets the value of this cell to the given value. + /// + void value(const date &date_value); + + /// + /// Sets the value of this cell to the given value. + /// + void value(const time &time_value); + + /// + /// Sets the value of this cell to the given value. + /// + void value(const datetime &datetime_value); + + /// + /// Sets the value of this cell to the given value. + /// + void value(const timedelta &timedelta_value); + + /// + /// Sets the value of this cell to the given value. + /// + void value(const std::string &string_value); + + /// + /// Sets the value of this cell to the given value. + /// + void value(const char *string_value); + + /// + /// Sets the value of this cell to the given value. + /// + void value(const rich_text &text_value); + + /// + /// Sets the value and formatting of this cell to that of other_cell. + /// + void value(const cell other_cell); /// /// Analyzes string_value to determine its type, convert it to that type, diff --git a/source/cell/cell.cpp b/source/cell/cell.cpp index 797c170c..c01cce90 100644 --- a/source/cell/cell.cpp +++ b/source/cell/cell.cpp @@ -197,124 +197,85 @@ bool cell::garbage_collectible() const return !(data_type() != type::null || is_merged() || has_formula() || has_format()); } -template <> -XLNT_API void cell::value(std::nullptr_t) +void cell::value(std::nullptr_t) { d_->type_ = type::null; } -template <> -XLNT_API void cell::value(bool b) +void cell::value(bool boolean_value) { - d_->value_numeric_ = b ? 1 : 0; d_->type_ = type::boolean; + d_->value_numeric_ = boolean_value ? 1.0L : 0.0L; } -template <> -XLNT_API void cell::value(std::int8_t i) +void cell::value(int int_value) { - d_->value_numeric_ = static_cast(i); + d_->value_numeric_ = static_cast(int_value); d_->type_ = type::numeric; } -template <> -XLNT_API void cell::value(std::int16_t i) +void cell::value(unsigned int int_value) { - d_->value_numeric_ = static_cast(i); + d_->value_numeric_ = static_cast(int_value); d_->type_ = type::numeric; } -template <> -XLNT_API void cell::value(std::int32_t i) +void cell::value(long long int int_value) { - d_->value_numeric_ = static_cast(i); + d_->value_numeric_ = static_cast(int_value); d_->type_ = type::numeric; } -template <> -XLNT_API void cell::value(std::int64_t i) +void cell::value(unsigned long long int int_value) { - d_->value_numeric_ = static_cast(i); + d_->value_numeric_ = static_cast(int_value); d_->type_ = type::numeric; } -template <> -XLNT_API void cell::value(std::uint8_t i) +void cell::value(float float_value) { - d_->value_numeric_ = static_cast(i); + d_->value_numeric_ = static_cast(float_value); d_->type_ = type::numeric; } -template <> -XLNT_API void cell::value(std::uint16_t i) +void cell::value(double float_value) { - d_->value_numeric_ = static_cast(i); + d_->value_numeric_ = static_cast(float_value); d_->type_ = type::numeric; } -template <> -XLNT_API void cell::value(std::uint32_t i) +void cell::value(long double d) { - d_->value_numeric_ = static_cast(i); + d_->value_numeric_ = d; d_->type_ = type::numeric; } -template <> -XLNT_API void cell::value(std::uint64_t i) +void cell::value(const std::string &s) { - d_->value_numeric_ = static_cast(i); - d_->type_ = type::numeric; -} + auto checked = check_string(s); -template <> -XLNT_API void cell::value(float f) -{ - d_->value_numeric_ = static_cast(f); - d_->type_ = type::numeric; -} - -template <> -XLNT_API void cell::value(double d) -{ - d_->value_numeric_ = static_cast(d); - d_->type_ = type::numeric; -} - -template <> -XLNT_API void cell::value(long double d) -{ - d_->value_numeric_ = static_cast(d); - d_->type_ = type::numeric; -} - -template <> -XLNT_API void cell::value(std::string s) -{ - s = check_string(s); - - if (s.size() > 1 && s.front() == '=') + if (checked.size() > 1 && checked.front() == '=') { d_->type_ = type::formula; - formula(s); + formula(checked); } - else if (cell::error_codes().find(s) != cell::error_codes().end()) + else if (cell::error_codes().find(checked) != cell::error_codes().end()) { - error(s); + error(checked); } else { d_->type_ = type::string; - d_->value_text_.plain_text(s); + d_->value_text_.plain_text(checked); - if (s.size() > 0) + if (checked.size() > 0) { workbook().add_shared_string(d_->value_text_); } } } -template <> -XLNT_API void cell::value(rich_text text) +void cell::value(const rich_text &text) { if (text.runs().size() == 1 && !text.runs().front().second.is_set()) { @@ -328,14 +289,12 @@ XLNT_API void cell::value(rich_text text) } } -template <> -XLNT_API void cell::value(char const *c) +void cell::value(const char *c) { value(std::string(c)); } -template <> -XLNT_API void cell::value(cell c) +void cell::value(const cell c) { d_->type_ = c.d_->type_; d_->value_numeric_ = c.d_->value_numeric_; @@ -345,32 +304,28 @@ XLNT_API void cell::value(cell c) d_->format_ = c.d_->format_; } -template <> -XLNT_API void cell::value(date d) +void cell::value(const date &d) { d_->type_ = type::numeric; d_->value_numeric_ = d.to_number(base_date()); number_format(number_format::date_yyyymmdd2()); } -template <> -XLNT_API void cell::value(datetime d) +void cell::value(const datetime &d) { d_->type_ = type::numeric; d_->value_numeric_ = d.to_number(base_date()); number_format(number_format::date_datetime()); } -template <> -XLNT_API void cell::value(time t) +void cell::value(const time &t) { d_->type_ = type::numeric; d_->value_numeric_ = t.to_number(); number_format(number_format::date_time6()); } -template <> -XLNT_API void cell::value(timedelta t) +void cell::value(const timedelta &t) { d_->type_ = type::numeric; d_->value_numeric_ = t.to_number();