optimize cell memory usage. big improvement!

This commit is contained in:
Thomas Fussell 2017-09-08 19:41:34 -04:00
parent 716a81a19f
commit fcd68dba8f
3 changed files with 21 additions and 6 deletions

View File

@ -29,6 +29,7 @@
#include <detail/implementations/cell_impl.hpp> #include <detail/implementations/cell_impl.hpp>
#include <detail/implementations/format_impl.hpp> #include <detail/implementations/format_impl.hpp>
#include <detail/implementations/stylesheet.hpp> #include <detail/implementations/stylesheet.hpp>
#include <detail/implementations/worksheet_impl.hpp>
#include <xlnt/cell/cell.hpp> #include <xlnt/cell/cell.hpp>
#include <xlnt/cell/cell_reference.hpp> #include <xlnt/cell/cell_reference.hpp>
#include <xlnt/cell/comment.hpp> #include <xlnt/cell/comment.hpp>
@ -878,7 +879,11 @@ bool cell::has_comment()
void cell::clear_comment() void cell::clear_comment()
{ {
if (has_comment())
{
d_->parent_->comments_.erase(reference().to_string());
d_->comment_.clear(); d_->comment_.clear();
}
} }
class comment cell::comment() class comment cell::comment()
@ -888,7 +893,7 @@ class comment cell::comment()
throw xlnt::exception("cell has no comment"); throw xlnt::exception("cell has no comment");
} }
return d_->comment_.get(); return *d_->comment_.get();
} }
void cell::comment(const std::string &text, const std::string &author) void cell::comment(const std::string &text, const std::string &author)
@ -904,15 +909,23 @@ void cell::comment(const std::string &text, const class font &comment_font, cons
void cell::comment(const class comment &new_comment) void cell::comment(const class comment &new_comment)
{ {
d_->comment_.set(new_comment); if (has_comment())
{
*d_->comment_.get() = new_comment;
}
else
{
d_->parent_->comments_[reference().to_string()] = new_comment;
d_->comment_.set(&d_->parent_->comments_[reference().to_string()]);
}
// offset comment 5 pixels down and 5 pixels right of the top right corner of the cell // offset comment 5 pixels down and 5 pixels right of the top right corner of the cell
auto cell_position = anchor(); auto cell_position = anchor();
cell_position.first += static_cast<int>(width()) + 5; cell_position.first += static_cast<int>(width()) + 5;
cell_position.second += 5; cell_position.second += 5;
d_->comment_.get().position(cell_position.first, cell_position.second); d_->comment_.get()->position(cell_position.first, cell_position.second);
d_->comment_.get().size(200, 100); d_->comment_.get()->size(200, 100);
worksheet().register_comments_in_manifest(); worksheet().register_comments_in_manifest();
} }

View File

@ -56,7 +56,7 @@ struct cell_impl
optional<std::string> formula_; optional<std::string> formula_;
optional<std::string> hyperlink_; optional<std::string> hyperlink_;
optional<format_impl *> format_; optional<format_impl *> format_;
optional<comment> comment_; optional<comment *> comment_;
}; };
} // namespace detail } // namespace detail

View File

@ -115,6 +115,8 @@ struct worksheet_impl
std::vector<column_t> column_breaks_; std::vector<column_t> column_breaks_;
std::vector<row_t> row_breaks_; std::vector<row_t> row_breaks_;
std::unordered_map<std::string, comment> comments_;
}; };
} // namespace detail } // namespace detail