Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving memory usage and allocation #672

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions source/cell/cell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,8 @@ void cell::error(const std::string &error)
{
throw invalid_data_type();
}

d_->value_text_.plain_text(error, false);
d_->value_text_ = error;
d_->value_text_.get().plain_text(error, false);
d_->type_ = type::error;
}

Expand Down Expand Up @@ -717,7 +717,7 @@ XLNT_API rich_text cell::value() const
return workbook().shared_strings(static_cast<std::size_t>(d_->value_numeric_));
}

return d_->value_text_;
return d_->value_text_.get();
}

bool cell::has_value() const
Expand Down Expand Up @@ -750,7 +750,7 @@ std::string cell::to_string() const

bool cell::has_format() const
{
return d_->format_.is_set();
return (d_->format_ != nullptr);
}

void cell::format(const class format new_format)
Expand Down Expand Up @@ -838,10 +838,10 @@ void cell::value(const std::string &value_string, bool infer_type)

void cell::clear_format()
{
if (d_->format_.is_set())
if (d_->format_ != nullptr)
{
format().d_->references -= format().d_->references > 0 ? 1 : 0;
d_->format_.clear();
d_->format_ = nullptr;
}
}

Expand Down Expand Up @@ -899,22 +899,22 @@ bool cell::has_style() const

format cell::modifiable_format()
{
if (!d_->format_.is_set())
if (d_->format_ == nullptr)
{
throw invalid_attribute();
}

return xlnt::format(d_->format_.get());
return xlnt::format(d_->format_);
}

const format cell::format() const
{
if (!d_->format_.is_set())
if (d_->format_ == nullptr)
{
throw invalid_attribute();
}

return xlnt::format(d_->format_.get());
return xlnt::format(d_->format_);
}

alignment cell::alignment() const
Expand Down Expand Up @@ -956,15 +956,15 @@ bool cell::has_hyperlink() const

bool cell::has_comment()
{
return d_->comment_.is_set();
return (d_->comment_ != nullptr);
}

void cell::clear_comment()
{
if (has_comment())
{
d_->parent_->comments_.erase(reference().to_string());
d_->comment_.clear();
d_->comment_ = nullptr;
}
}

Expand All @@ -975,7 +975,7 @@ class comment cell::comment()
throw xlnt::exception("cell has no comment");
}

return *d_->comment_.get();
return *d_->comment_;
}

void cell::comment(const std::string &text, const std::string &author)
Expand All @@ -992,20 +992,20 @@ void cell::comment(const class comment &new_comment)
{
if (has_comment())
{
*d_->comment_.get() = new_comment;
*d_->comment_ = new_comment;
}
else
{
d_->parent_->comments_[reference().to_string()] = new_comment;
d_->comment_.set(&d_->parent_->comments_[reference().to_string()]);
d_->comment_ = (&d_->parent_->comments_[reference().to_string()]);
}

// offset comment 5 pixels down and 5 pixels right of the top right corner of the cell
auto cell_position = anchor();
cell_position.first += static_cast<int>(width()) + 5;
cell_position.second += 5;

d_->comment_.get()->position(cell_position.first, cell_position.second);
d_->comment_->position(cell_position.first, cell_position.second);

worksheet().register_comments_in_manifest();
}
Expand Down
4 changes: 3 additions & 1 deletion source/detail/implementations/cell_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ cell_impl::cell_impl()
row_(1),
is_merged_(false),
phonetics_visible_(false),
value_numeric_(0)
value_numeric_(0),
format_(),
comment_()
{
}

Expand Down
12 changes: 6 additions & 6 deletions source/detail/implementations/cell_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,17 @@ struct cell_impl
bool is_merged_;
bool phonetics_visible_;

rich_text value_text_;
optional<rich_text> value_text_;
double value_numeric_;

optional<std::string> formula_;
optional<hyperlink_impl> hyperlink_;
optional<format_impl *> format_;
optional<comment *> comment_;
format_impl *format_;
comment *comment_;

bool is_garbage_collectible() const
{
return !(type_ != cell_type::empty || is_merged_ || phonetics_visible_ || formula_.is_set() || format_.is_set() || hyperlink_.is_set());
return !(type_ != cell_type::empty || is_merged_ || phonetics_visible_ || formula_.is_set() || format_ != nullptr || hyperlink_.is_set());
}
};

Expand All @@ -84,8 +84,8 @@ inline bool operator==(const cell_impl &lhs, const cell_impl &rhs)
&& float_equals(lhs.value_numeric_, rhs.value_numeric_)
&& lhs.formula_ == rhs.formula_
&& lhs.hyperlink_ == rhs.hyperlink_
&& (lhs.format_.is_set() == rhs.format_.is_set() && (!lhs.format_.is_set() || *lhs.format_.get() == *rhs.format_.get()))
&& (lhs.comment_.is_set() == rhs.comment_.is_set() && (!lhs.comment_.is_set() || *lhs.comment_.get() == *rhs.comment_.get()));
&& ((lhs.format_ != nullptr) == (rhs.format_ != nullptr) && ((lhs.format_ == nullptr) || *lhs.format_ == *rhs.format_))
&& ((lhs.comment_ != nullptr) == (rhs.comment_ != nullptr) && ((lhs.comment_ == nullptr) || *lhs.comment_ == *rhs.comment_));
}

} // namespace detail
Expand Down
3 changes: 2 additions & 1 deletion source/detail/serialization/xlsx_consumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,8 @@ void xlsx_consumer::read_worksheet_sheetdata()
break;
}
case cell::type::error: {
ws_cell_impl->value_text_.plain_text(cell.value, false);
ws_cell_impl->value_text_ = std::move(cell.value);
ws_cell_impl->value_text_.get().plain_text(cell.value, false);
break;
}
}
Expand Down