remove template from cell::value setter, closes #131

This commit is contained in:
Thomas Fussell 2017-03-20 19:20:12 -04:00
parent 7fa5099a49
commit e0e38a931c
2 changed files with 115 additions and 82 deletions

View File

@ -31,6 +31,7 @@
#include <xlnt/xlnt_config.hpp> #include <xlnt/xlnt_config.hpp>
#include <xlnt/cell/cell_type.hpp> #include <xlnt/cell/cell_type.hpp>
#include <xlnt/cell/index_types.hpp> #include <xlnt/cell/index_types.hpp>
#include <xlnt/cell/rich_text.hpp>
namespace xlnt { namespace xlnt {
@ -116,12 +117,89 @@ public:
void clear_value(); void clear_value();
/// <summary> /// <summary>
/// Sets the value of this cell to the given value. /// Sets the type of this cell to null.
/// 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.
/// </summary> /// </summary>
template <typename T> void value(std::nullptr_t);
void value(T value);
/// <summary>
/// Sets the value of this cell to the given boolean value.
/// </summary>
void value(bool boolean_value);
/// <summary>
/// Sets the value of this cell to the given value.
/// </summary>
void value(int int_value);
/// <summary>
/// Sets the value of this cell to the given value.
/// </summary>
void value(unsigned int int_value);
/// <summary>
/// Sets the value of this cell to the given value.
/// </summary>
void value(long long int int_value);
/// <summary>
/// Sets the value of this cell to the given value.
/// </summary>
void value(unsigned long long int int_value);
/// <summary>
/// Sets the value of this cell to the given value.
/// </summary>
void value(float float_value);
/// <summary>
/// Sets the value of this cell to the given value.
/// </summary>
void value(double float_value);
/// <summary>
/// Sets the value of this cell to the given value.
/// </summary>
void value(long double float_value);
/// <summary>
/// Sets the value of this cell to the given value.
/// </summary>
void value(const date &date_value);
/// <summary>
/// Sets the value of this cell to the given value.
/// </summary>
void value(const time &time_value);
/// <summary>
/// Sets the value of this cell to the given value.
/// </summary>
void value(const datetime &datetime_value);
/// <summary>
/// Sets the value of this cell to the given value.
/// </summary>
void value(const timedelta &timedelta_value);
/// <summary>
/// Sets the value of this cell to the given value.
/// </summary>
void value(const std::string &string_value);
/// <summary>
/// Sets the value of this cell to the given value.
/// </summary>
void value(const char *string_value);
/// <summary>
/// Sets the value of this cell to the given value.
/// </summary>
void value(const rich_text &text_value);
/// <summary>
/// Sets the value and formatting of this cell to that of other_cell.
/// </summary>
void value(const cell other_cell);
/// <summary> /// <summary>
/// Analyzes string_value to determine its type, convert it to that type, /// Analyzes string_value to determine its type, convert it to that type,

View File

@ -197,124 +197,85 @@ bool cell::garbage_collectible() const
return !(data_type() != type::null || is_merged() || has_formula() || has_format()); return !(data_type() != type::null || is_merged() || has_formula() || has_format());
} }
template <> void cell::value(std::nullptr_t)
XLNT_API void cell::value(std::nullptr_t)
{ {
d_->type_ = type::null; d_->type_ = type::null;
} }
template <> void cell::value(bool boolean_value)
XLNT_API void cell::value(bool b)
{ {
d_->value_numeric_ = b ? 1 : 0;
d_->type_ = type::boolean; d_->type_ = type::boolean;
d_->value_numeric_ = boolean_value ? 1.0L : 0.0L;
} }
template <> void cell::value(int int_value)
XLNT_API void cell::value(std::int8_t i)
{ {
d_->value_numeric_ = static_cast<long double>(i); d_->value_numeric_ = static_cast<long double>(int_value);
d_->type_ = type::numeric; d_->type_ = type::numeric;
} }
template <> void cell::value(unsigned int int_value)
XLNT_API void cell::value(std::int16_t i)
{ {
d_->value_numeric_ = static_cast<long double>(i); d_->value_numeric_ = static_cast<long double>(int_value);
d_->type_ = type::numeric; d_->type_ = type::numeric;
} }
template <> void cell::value(long long int int_value)
XLNT_API void cell::value(std::int32_t i)
{ {
d_->value_numeric_ = static_cast<long double>(i); d_->value_numeric_ = static_cast<long double>(int_value);
d_->type_ = type::numeric; d_->type_ = type::numeric;
} }
template <> void cell::value(unsigned long long int int_value)
XLNT_API void cell::value(std::int64_t i)
{ {
d_->value_numeric_ = static_cast<long double>(i); d_->value_numeric_ = static_cast<long double>(int_value);
d_->type_ = type::numeric; d_->type_ = type::numeric;
} }
template <> void cell::value(float float_value)
XLNT_API void cell::value(std::uint8_t i)
{ {
d_->value_numeric_ = static_cast<long double>(i); d_->value_numeric_ = static_cast<long double>(float_value);
d_->type_ = type::numeric; d_->type_ = type::numeric;
} }
template <> void cell::value(double float_value)
XLNT_API void cell::value(std::uint16_t i)
{ {
d_->value_numeric_ = static_cast<long double>(i); d_->value_numeric_ = static_cast<long double>(float_value);
d_->type_ = type::numeric; d_->type_ = type::numeric;
} }
template <> void cell::value(long double d)
XLNT_API void cell::value(std::uint32_t i)
{ {
d_->value_numeric_ = static_cast<long double>(i); d_->value_numeric_ = d;
d_->type_ = type::numeric; d_->type_ = type::numeric;
} }
template <> void cell::value(const std::string &s)
XLNT_API void cell::value(std::uint64_t i)
{ {
d_->value_numeric_ = static_cast<long double>(i); auto checked = check_string(s);
d_->type_ = type::numeric;
}
template <> if (checked.size() > 1 && checked.front() == '=')
XLNT_API void cell::value(float f)
{
d_->value_numeric_ = static_cast<long double>(f);
d_->type_ = type::numeric;
}
template <>
XLNT_API void cell::value(double d)
{
d_->value_numeric_ = static_cast<long double>(d);
d_->type_ = type::numeric;
}
template <>
XLNT_API void cell::value(long double d)
{
d_->value_numeric_ = static_cast<long double>(d);
d_->type_ = type::numeric;
}
template <>
XLNT_API void cell::value(std::string s)
{
s = check_string(s);
if (s.size() > 1 && s.front() == '=')
{ {
d_->type_ = type::formula; 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 else
{ {
d_->type_ = type::string; 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_); workbook().add_shared_string(d_->value_text_);
} }
} }
} }
template <> void cell::value(const rich_text &text)
XLNT_API void cell::value(rich_text text)
{ {
if (text.runs().size() == 1 && !text.runs().front().second.is_set()) if (text.runs().size() == 1 && !text.runs().front().second.is_set())
{ {
@ -328,14 +289,12 @@ XLNT_API void cell::value(rich_text text)
} }
} }
template <> void cell::value(const char *c)
XLNT_API void cell::value(char const *c)
{ {
value(std::string(c)); value(std::string(c));
} }
template <> void cell::value(const cell c)
XLNT_API void cell::value(cell c)
{ {
d_->type_ = c.d_->type_; d_->type_ = c.d_->type_;
d_->value_numeric_ = c.d_->value_numeric_; d_->value_numeric_ = c.d_->value_numeric_;
@ -345,32 +304,28 @@ XLNT_API void cell::value(cell c)
d_->format_ = c.d_->format_; d_->format_ = c.d_->format_;
} }
template <> void cell::value(const date &d)
XLNT_API void cell::value(date d)
{ {
d_->type_ = type::numeric; d_->type_ = type::numeric;
d_->value_numeric_ = d.to_number(base_date()); d_->value_numeric_ = d.to_number(base_date());
number_format(number_format::date_yyyymmdd2()); number_format(number_format::date_yyyymmdd2());
} }
template <> void cell::value(const datetime &d)
XLNT_API void cell::value(datetime d)
{ {
d_->type_ = type::numeric; d_->type_ = type::numeric;
d_->value_numeric_ = d.to_number(base_date()); d_->value_numeric_ = d.to_number(base_date());
number_format(number_format::date_datetime()); number_format(number_format::date_datetime());
} }
template <> void cell::value(const time &t)
XLNT_API void cell::value(time t)
{ {
d_->type_ = type::numeric; d_->type_ = type::numeric;
d_->value_numeric_ = t.to_number(); d_->value_numeric_ = t.to_number();
number_format(number_format::date_time6()); number_format(number_format::date_time6());
} }
template <> void cell::value(const timedelta &t)
XLNT_API void cell::value(timedelta t)
{ {
d_->type_ = type::numeric; d_->type_ = type::numeric;
d_->value_numeric_ = t.to_number(); d_->value_numeric_ = t.to_number();