Make TextMetrics noncopyable

This is done by declaring unimplemented private copy constructor and
assignment operator.

This breaks compilation in BufferView::textMetrics, which does a copy when
inserting a TextMetrics object in the cache. Some C++11 wizardry I will not
pretend to completely understand saves the day.

See the following page for details:
  https://en.cppreference.com/w/cpp/container/map/emplace

This avoids real world bugs like #11512.
This commit is contained in:
Jean-Marc Lasgouttes 2019-03-12 12:40:32 +01:00
parent 569841f292
commit 63bfaa14be
2 changed files with 6 additions and 2 deletions

View File

@ -2542,8 +2542,9 @@ TextMetrics & BufferView::textMetrics(Text const * t)
LBUFERR(t); LBUFERR(t);
TextMetricsCache::iterator tmc_it = d->text_metrics_.find(t); TextMetricsCache::iterator tmc_it = d->text_metrics_.find(t);
if (tmc_it == d->text_metrics_.end()) { if (tmc_it == d->text_metrics_.end()) {
tmc_it = d->text_metrics_.insert( tmc_it = d->text_metrics_.emplace(std::piecewise_construct,
make_pair(t, TextMetrics(this, const_cast<Text *>(t)))).first; std::forward_as_tuple(t),
std::forward_as_tuple(this, const_cast<Text *>(t))).first;
} }
return tmc_it->second; return tmc_it->second;
} }

View File

@ -33,6 +33,9 @@ class Text;
/// A map from a Text to the map of paragraphs metrics /// A map from a Text to the map of paragraphs metrics
class TextMetrics class TextMetrics
{ {
/// noncopyable
TextMetrics(TextMetrics const &);
void operator=(TextMetrics const &);
public: public:
/// Default constructor (only here for STL containers). /// Default constructor (only here for STL containers).
TextMetrics() : bv_(0), text_(0), max_width_(0) {} TextMetrics() : bv_(0), text_(0), max_width_(0) {}