mirror of
https://github.com/NoelFB/blah.git
synced 2025-06-29 19:25:26 +08:00
various memory cleanup, move/copy simplifications
This commit is contained in:
@ -81,61 +81,46 @@ namespace Blah
|
||||
#else
|
||||
namespace Blah
|
||||
{
|
||||
struct RefDel { virtual ~RefDel() = default; virtual void delete_it(void* it) = 0; };
|
||||
template<class Y> struct RefDelOf : public RefDel { void delete_it(void* it) { delete (Y*)it; } };
|
||||
|
||||
template<typename T>
|
||||
class Ref
|
||||
{
|
||||
template<class Y> friend class Ref;
|
||||
private:
|
||||
T* m_instance;
|
||||
i32* m_counter;
|
||||
Ref(T* instance, i32* counter) : m_instance(instance), m_counter(counter) {}
|
||||
inline static RefDelOf<T> del;
|
||||
|
||||
T* m_obj = nullptr;
|
||||
u32* m_num = nullptr;
|
||||
RefDel* m_del = nullptr;
|
||||
|
||||
Ref(T* o, u32* n, RefDel* d) : m_obj(o), m_num(n), m_del(d) {}
|
||||
|
||||
void copy(const Ref& rhs) { m_obj = rhs.m_obj; m_num = rhs.m_num; m_del = rhs.m_del; }
|
||||
void zero() { m_obj = nullptr; m_num = nullptr; m_del = nullptr; }
|
||||
void steal(Ref& rhs) { copy(rhs); rhs.zero(); }
|
||||
void increment() { if (m_num) (*m_num)++; }
|
||||
|
||||
public:
|
||||
Ref() : m_instance(nullptr), m_counter(nullptr) {}
|
||||
|
||||
Ref() = default;
|
||||
template<class Y>
|
||||
Ref(Y* instance) : Ref(static_cast<T*>(instance), new i32(1)) {}
|
||||
Ref(const Ref& rhs) : Ref(rhs.m_instance, rhs.m_counter) { if (m_counter) (*m_counter)++; }
|
||||
Ref(Ref&& rhs) : Ref(rhs.m_instance, rhs.m_counter) { rhs.m_instance = nullptr; rhs.m_counter = nullptr; }
|
||||
|
||||
Ref& operator=(const Ref& rhs)
|
||||
{
|
||||
if (this != &rhs)
|
||||
{
|
||||
reset();
|
||||
m_instance = rhs.m_instance; m_counter = rhs.m_counter;
|
||||
if (m_counter) (*m_counter)++;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
Ref& operator=(Ref&& rhs)
|
||||
{
|
||||
if (this != &rhs)
|
||||
{
|
||||
reset();
|
||||
m_instance = rhs.m_instance; m_counter = rhs.m_counter;
|
||||
rhs.m_instance = nullptr; rhs.m_counter = nullptr;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
Ref(Y* instance) : Ref(static_cast<T*>(instance), new u32(1), &Ref<Y>::del) {}
|
||||
Ref(const Ref& rhs) { copy(rhs); increment(); }
|
||||
Ref(Ref&& rhs) { steal(rhs); }
|
||||
~Ref() { reset(); }
|
||||
|
||||
void reset()
|
||||
{
|
||||
if (m_counter) (*m_counter)--;
|
||||
if (m_counter && (*m_counter) <= 0) { delete m_instance; delete m_counter; }
|
||||
m_instance = nullptr; m_counter = nullptr;
|
||||
}
|
||||
Ref& operator=(const Ref& rhs) { if (this != &rhs) { reset(); copy(rhs); increment(); } return *this; }
|
||||
Ref& operator=(Ref&& rhs) { if (this != &rhs) { reset(); steal(rhs); } return *this; }
|
||||
|
||||
int use_count() const { return (m_counter ? (*m_counter) : 0); }
|
||||
T* get() const { return m_instance; }
|
||||
T* operator->() const { return m_instance; }
|
||||
operator bool() const { return m_counter && (*m_counter) > 0; }
|
||||
template<class Y> bool operator==(const Ref<Y>& rhs) const { return m_counter == rhs.m_counter; }
|
||||
template<class Y> bool operator!=(const Ref<Y>& rhs) const { return m_counter != rhs.m_counter; }
|
||||
template<class Y> operator Ref<Y>() { if (m_counter) (*m_counter)++; return Ref<Y>(static_cast<Y*>(m_instance), m_counter); }
|
||||
void reset() { if (m_num && --(*m_num) <= 0) { m_del->delete_it(m_obj); delete m_num; } zero(); }
|
||||
int use_count() const { return (m_num ? (*m_num) : 0); }
|
||||
T* get() const { return m_obj; }
|
||||
T* operator->() const { return m_obj; }
|
||||
operator bool() const { return m_num && (*m_num) > 0; }
|
||||
template<class Y> bool operator==(const Ref<Y>& rhs) const { return m_num == rhs.m_num; }
|
||||
template<class Y> bool operator!=(const Ref<Y>& rhs) const { return m_num != rhs.m_num; }
|
||||
template<class Y> operator Ref<Y>() { increment(); return Ref<Y>(static_cast<Y*>(m_obj), m_num, m_del); }
|
||||
};
|
||||
}
|
||||
#endif
|
@ -3,13 +3,12 @@
|
||||
#include <blah/containers/str.h>
|
||||
#include <blah/containers/vector.h>
|
||||
#include <blah/graphics/subtexture.h>
|
||||
#include <blah/images/font.h>
|
||||
#include <blah/math/spatial.h>
|
||||
#include <blah/filesystem.h>
|
||||
|
||||
namespace Blah
|
||||
{
|
||||
class Font;
|
||||
|
||||
// Sprite Font is a bitmap font implementation for font rendering.
|
||||
// It can be constructed from a Font (ttf) and will automatically create
|
||||
// texture atlases for rendering. You can add your own characters
|
||||
@ -59,30 +58,25 @@ namespace Blah
|
||||
String name;
|
||||
|
||||
// Height, in pixels
|
||||
float size;
|
||||
float size = 0;
|
||||
|
||||
// Ascent, in pixels
|
||||
float ascent;
|
||||
float ascent = 0;
|
||||
|
||||
// Descent, in pixels
|
||||
float descent;
|
||||
float descent = 0;
|
||||
|
||||
// Line Gap, in pixels
|
||||
float line_gap;
|
||||
float line_gap = 0;
|
||||
|
||||
SpriteFont();
|
||||
SpriteFont() = default;
|
||||
SpriteFont(const FilePath& file, float size);
|
||||
SpriteFont(const FilePath& file, float size, const CharSet& charset);
|
||||
SpriteFont(const Font& font, float size);
|
||||
SpriteFont(const Font& font, float size, const CharSet& charset);
|
||||
SpriteFont(const SpriteFont&) = delete;
|
||||
SpriteFont& operator=(const SpriteFont&) = delete;
|
||||
SpriteFont(SpriteFont&& src) noexcept;
|
||||
SpriteFont& operator=(SpriteFont&& src) noexcept;
|
||||
~SpriteFont();
|
||||
SpriteFont(const FontRef& font, float size);
|
||||
SpriteFont(const FontRef& font, float size, const CharSet& charset);
|
||||
|
||||
// releases all assets used by the spritefont
|
||||
void dispose();
|
||||
void clear();
|
||||
|
||||
// gets the height of the sprite font
|
||||
float height() const { return ascent - descent; }
|
||||
@ -106,7 +100,7 @@ namespace Blah
|
||||
void rebuild(const FilePath& file, float size, const CharSet& charset);
|
||||
|
||||
// disposes the existing spritefont data and rebuilds from the given font
|
||||
void rebuild(const Font& font, float size, const CharSet& charset);
|
||||
void rebuild(const FontRef& font, float size, const CharSet& charset);
|
||||
|
||||
// gets the kerning between two characters
|
||||
float get_kerning(Codepoint codepoint0, Codepoint codepoint1) const;
|
||||
|
@ -1,14 +1,23 @@
|
||||
#pragma once
|
||||
#include <blah/common.h>
|
||||
#include <blah/streams/stream.h>
|
||||
#include <blah/images/image.h>
|
||||
#include <blah/containers/str.h>
|
||||
#include <blah/containers/vector.h>
|
||||
#include <blah/filesystem.h>
|
||||
|
||||
namespace Blah
|
||||
{
|
||||
// Loads fonts from file and can blit individual characters to images
|
||||
class Font;
|
||||
using FontRef = Ref<Font>;
|
||||
|
||||
// Loads fonts from file and can blit individual characters to images.
|
||||
// Use Font::create() to instantiate a FontRef.
|
||||
class Font
|
||||
{
|
||||
private:
|
||||
Font() = default;
|
||||
|
||||
public:
|
||||
|
||||
// Font uses u32 Codepoints
|
||||
@ -42,18 +51,11 @@ namespace Blah
|
||||
bool has_glyph = false;
|
||||
};
|
||||
|
||||
Font();
|
||||
Font(Stream& stream);
|
||||
Font(const FilePath& path);
|
||||
Font(const Font&) = delete;
|
||||
Font& operator=(const Font&) = delete;
|
||||
Font(Font&& src) noexcept;
|
||||
Font& operator=(Font&& src) noexcept;
|
||||
~Font();
|
||||
// creates a new font from the given file path
|
||||
static FontRef create(const FilePath& path);
|
||||
|
||||
// Releases all Font resources
|
||||
// Note that after doing this various properties may become invalid (ex. font name)
|
||||
void dispose();
|
||||
// creates a new font from the Stream
|
||||
static FontRef create(Stream& stream);
|
||||
|
||||
// returns the font family name
|
||||
const String& family_name() const;
|
||||
@ -97,18 +99,13 @@ namespace Blah
|
||||
// If the character doesn't exist, this will return an empty image.
|
||||
Image get_image(const Character& ch) const;
|
||||
|
||||
// checks if the Font is valid
|
||||
bool is_valid() const;
|
||||
|
||||
private:
|
||||
void load(Stream& stream);
|
||||
void* m_font;
|
||||
unsigned char* m_data;
|
||||
Ref<void> m_font;
|
||||
Vector<u8> m_buffer;
|
||||
String m_family_name;
|
||||
String m_style_name;
|
||||
int m_ascent;
|
||||
int m_descent;
|
||||
int m_line_gap;
|
||||
bool m_valid;
|
||||
int m_ascent = 0;
|
||||
int m_descent = 0;
|
||||
int m_line_gap = 0;
|
||||
};
|
||||
}
|
@ -69,11 +69,6 @@ namespace Blah
|
||||
|
||||
Packer();
|
||||
Packer(int max_size, int spacing, bool power_of_two);
|
||||
Packer(const Packer&) = delete;
|
||||
Packer& operator=(const Packer&) = delete;
|
||||
Packer(Packer&& src) noexcept;
|
||||
Packer& operator=(Packer&& src) noexcept;
|
||||
~Packer();
|
||||
|
||||
// add a new entry
|
||||
void add(u64 id, int width, int height, const Color* pixels);
|
||||
@ -96,9 +91,6 @@ namespace Blah
|
||||
// clear the current packer data
|
||||
void clear();
|
||||
|
||||
// dispose all resources used by the packer
|
||||
void dispose();
|
||||
|
||||
private:
|
||||
struct Node
|
||||
{
|
||||
|
@ -8,34 +8,28 @@ namespace Blah
|
||||
class BufferStream : public Stream
|
||||
{
|
||||
public:
|
||||
BufferStream();
|
||||
BufferStream() = default;
|
||||
BufferStream(int capacity);
|
||||
BufferStream(BufferStream&& bs) noexcept;
|
||||
BufferStream& operator=(BufferStream&& bs) noexcept;
|
||||
~BufferStream();
|
||||
|
||||
size_t length() const override;
|
||||
size_t position() const override;
|
||||
size_t seek(size_t seekTo) override;
|
||||
size_t seek(size_t seek_to) override;
|
||||
bool is_open() const override;
|
||||
bool is_readable() const override;
|
||||
bool is_writable() const override;
|
||||
void close() override;
|
||||
|
||||
void resize(size_t length);
|
||||
void clear();
|
||||
|
||||
char* data();
|
||||
const char* data() const;
|
||||
u8* data();
|
||||
const u8* data() const;
|
||||
|
||||
protected:
|
||||
size_t read_data(void* ptr, size_t length) override;
|
||||
size_t write_data(const void* ptr, size_t length) override;
|
||||
|
||||
private:
|
||||
char* m_buffer;
|
||||
size_t m_capacity;
|
||||
size_t m_length;
|
||||
size_t m_position;
|
||||
Vector<u8> m_buffer;
|
||||
size_t m_position = 0;
|
||||
};
|
||||
}
|
@ -7,7 +7,7 @@ namespace Blah
|
||||
class FileStream : public Stream
|
||||
{
|
||||
public:
|
||||
FileStream();
|
||||
FileStream() = default;
|
||||
FileStream(const FilePath& path, FileMode mode);
|
||||
FileStream(FileStream&& fs) noexcept;
|
||||
FileStream& operator=(FileStream&& fs) noexcept;
|
||||
@ -18,14 +18,13 @@ namespace Blah
|
||||
bool is_open() const override;
|
||||
bool is_readable() const override;
|
||||
bool is_writable() const override;
|
||||
void close() override;
|
||||
|
||||
protected:
|
||||
size_t read_data(void* ptr, size_t length) override;
|
||||
size_t write_data(const void* ptr, size_t length) override;
|
||||
|
||||
private:
|
||||
FileMode m_mode;
|
||||
FileMode m_mode = FileMode::OpenRead;
|
||||
FileRef m_file;
|
||||
};
|
||||
}
|
@ -8,30 +8,28 @@ namespace Blah
|
||||
class MemoryStream : public Stream
|
||||
{
|
||||
public:
|
||||
MemoryStream();
|
||||
MemoryStream(unsigned char* data, size_t length);
|
||||
MemoryStream(MemoryStream&& ms) noexcept;
|
||||
MemoryStream& operator=(MemoryStream&& ms) noexcept;
|
||||
~MemoryStream() override { m_data = nullptr; m_length = m_position = 0; }
|
||||
MemoryStream() = default;
|
||||
MemoryStream(u8* data, size_t length);
|
||||
MemoryStream(const u8* data, size_t length);
|
||||
|
||||
size_t length() const override;
|
||||
size_t position() const override;
|
||||
size_t seek(size_t seekTo) override;
|
||||
size_t seek(size_t seek_to) override;
|
||||
bool is_open() const override;
|
||||
bool is_readable() const override;
|
||||
bool is_writable() const override;
|
||||
void close() override;
|
||||
|
||||
unsigned char* data();
|
||||
const unsigned char* data() const;
|
||||
u8* data();
|
||||
const u8* data() const;
|
||||
|
||||
protected:
|
||||
size_t read_data(void* ptr, size_t length) override;
|
||||
size_t write_data(const void* ptr, size_t length) override;
|
||||
|
||||
private:
|
||||
unsigned char* m_data;
|
||||
size_t m_length;
|
||||
size_t m_position;
|
||||
u8* m_data = nullptr;
|
||||
const u8* m_const_data = nullptr;
|
||||
size_t m_length = 0;
|
||||
size_t m_position = 0;
|
||||
};
|
||||
}
|
@ -2,7 +2,6 @@
|
||||
#include <blah/common.h>
|
||||
#include <blah/containers/str.h>
|
||||
#include <blah/math/calc.h>
|
||||
#include <string.h>
|
||||
|
||||
namespace Blah
|
||||
{
|
||||
@ -33,9 +32,6 @@ namespace Blah
|
||||
// returns true of the stream is writable
|
||||
virtual bool is_writable() const = 0;
|
||||
|
||||
// closes the stream
|
||||
virtual void close() = 0;
|
||||
|
||||
// pipes the contents of this stream to another stream
|
||||
size_t pipe(Stream& to, size_t length);
|
||||
|
||||
|
Reference in New Issue
Block a user