mirror of https://github.com/OpenTTD/OpenTTD
Codechange: manage the ParagraphLayouter's buffer with std::unique_ptr
parent
3790f29156
commit
bb2b890c88
|
@ -8,7 +8,6 @@
|
||||||
/** @file gfx_layout.cpp Handling of laying out text. */
|
/** @file gfx_layout.cpp Handling of laying out text. */
|
||||||
|
|
||||||
#include "stdafx.h"
|
#include "stdafx.h"
|
||||||
#include "core/alloc_func.hpp"
|
|
||||||
#include "core/math_func.hpp"
|
#include "core/math_func.hpp"
|
||||||
#include "gfx_layout.h"
|
#include "gfx_layout.h"
|
||||||
#include "string_func.h"
|
#include "string_func.h"
|
||||||
|
@ -64,15 +63,15 @@ Font::Font(FontSize size, TextColour colour) :
|
||||||
template <typename T>
|
template <typename T>
|
||||||
static inline void GetLayouter(Layouter::LineCacheItem &line, std::string_view str, FontState &state)
|
static inline void GetLayouter(Layouter::LineCacheItem &line, std::string_view str, FontState &state)
|
||||||
{
|
{
|
||||||
free(line.buffer);
|
typename T::CharType *buff_begin = new typename T::CharType[str.size() + 1];
|
||||||
|
/* Move ownership of buff_begin into the Buffer/unique_ptr. */
|
||||||
|
line.buffer = Layouter::LineCacheItem::Buffer(buff_begin, [](void *p) { delete[] reinterpret_cast<T::CharType *>(p); });
|
||||||
|
|
||||||
typename T::CharType *buff_begin = MallocT<typename T::CharType>(str.size() + 1);
|
|
||||||
const typename T::CharType *buffer_last = buff_begin + str.size() + 1;
|
const typename T::CharType *buffer_last = buff_begin + str.size() + 1;
|
||||||
typename T::CharType *buff = buff_begin;
|
typename T::CharType *buff = buff_begin;
|
||||||
FontMap &font_mapping = line.runs;
|
FontMap &font_mapping = line.runs;
|
||||||
Font *f = Layouter::GetFont(state.fontsize, state.cur_colour);
|
Font *f = Layouter::GetFont(state.fontsize, state.cur_colour);
|
||||||
|
|
||||||
line.buffer = buff_begin;
|
|
||||||
font_mapping.clear();
|
font_mapping.clear();
|
||||||
|
|
||||||
auto cur = str.begin();
|
auto cur = str.begin();
|
||||||
|
|
|
@ -166,15 +166,14 @@ class Layouter : public std::vector<std::unique_ptr<const ParagraphLayouter::Lin
|
||||||
public:
|
public:
|
||||||
/** Item in the linecache */
|
/** Item in the linecache */
|
||||||
struct LineCacheItem {
|
struct LineCacheItem {
|
||||||
|
/* Due to the type of data in the buffer differing depending on the Layouter, we need to pass our own deleter routine. */
|
||||||
|
using Buffer = std::unique_ptr<void, void(*)(void *)>;
|
||||||
/* Stuff that cannot be freed until the ParagraphLayout is freed */
|
/* Stuff that cannot be freed until the ParagraphLayout is freed */
|
||||||
void *buffer; ///< Accessed by our ParagraphLayout::nextLine.
|
Buffer buffer{nullptr, [](void *){}}; ///< Accessed by our ParagraphLayout::nextLine.
|
||||||
FontMap runs; ///< Accessed by our ParagraphLayout::nextLine.
|
FontMap runs; ///< Accessed by our ParagraphLayout::nextLine.
|
||||||
|
|
||||||
FontState state_after; ///< Font state after the line.
|
FontState state_after; ///< Font state after the line.
|
||||||
std::unique_ptr<ParagraphLayouter> layout = nullptr; ///< Layout of the line.
|
std::unique_ptr<ParagraphLayouter> layout = nullptr; ///< Layout of the line.
|
||||||
|
|
||||||
LineCacheItem() : buffer(nullptr) {}
|
|
||||||
~LineCacheItem() { free(buffer); }
|
|
||||||
};
|
};
|
||||||
private:
|
private:
|
||||||
typedef std::map<LineCacheKey, LineCacheItem, LineCacheCompare> LineCache;
|
typedef std::map<LineCacheKey, LineCacheItem, LineCacheCompare> LineCache;
|
||||||
|
|
Loading…
Reference in New Issue