Simplify implementations of types without an empty state

Now that these types have no default empty state, we can make
assumptions about certain data members which allow us to skip doing
certain checks.
This commit is contained in:
Chris Thrasher 2024-05-22 21:18:25 -06:00
parent cc87ef7a3c
commit 51a707628b
4 changed files with 61 additions and 48 deletions

View File

@ -38,6 +38,7 @@
#include <ostream>
#include <utility>
#include <cassert>
#include <cstdint>
@ -197,7 +198,9 @@ std::uint64_t InputSoundFile::getSampleOffset() const
////////////////////////////////////////////////////////////
void InputSoundFile::seek(std::uint64_t sampleOffset)
{
if (m_reader && !m_channelMap.empty())
assert(m_reader);
if (!m_channelMap.empty())
{
// The reader handles an overrun gracefully, but we
// pre-check to keep our known position consistent
@ -217,8 +220,10 @@ void InputSoundFile::seek(Time timeOffset)
////////////////////////////////////////////////////////////
std::uint64_t InputSoundFile::read(std::int16_t* samples, std::uint64_t maxCount)
{
assert(m_reader);
std::uint64_t readSamples = 0;
if (m_reader && samples && maxCount)
if (samples && maxCount)
readSamples = m_reader->read(samples, maxCount);
m_sampleOffset += readSamples;
return readSamples;

View File

@ -29,6 +29,8 @@
#include <SFML/Audio/SoundFileFactory.hpp>
#include <SFML/Audio/SoundFileWriter.hpp>
#include <cassert>
namespace sf
{
@ -57,7 +59,9 @@ std::optional<OutputSoundFile> OutputSoundFile::openFromFile(
////////////////////////////////////////////////////////////
void OutputSoundFile::write(const std::int16_t* samples, std::uint64_t count)
{
if (m_writer && samples && count)
assert(m_writer);
if (samples && count)
m_writer->write(samples, count);
}

View File

@ -301,13 +301,13 @@ const Font::Info& Font::getInfo() const
////////////////////////////////////////////////////////////
const Glyph& Font::getGlyph(std::uint32_t codePoint, unsigned int characterSize, bool bold, float outlineThickness) const
{
assert(m_fontHandles);
// Get the page corresponding to the character size
GlyphTable& glyphs = loadPage(characterSize).glyphs;
// Build the key by combining the glyph index (based on code point), bold flag, and outline thickness
const std::uint64_t key = combine(outlineThickness,
bold,
FT_Get_Char_Index(m_fontHandles ? m_fontHandles->face : nullptr, codePoint));
const std::uint64_t key = combine(outlineThickness, bold, FT_Get_Char_Index(m_fontHandles->face, codePoint));
// Search the glyph into the cache
if (const auto it = glyphs.find(key); it != glyphs.end())
@ -327,18 +327,21 @@ const Glyph& Font::getGlyph(std::uint32_t codePoint, unsigned int characterSize,
////////////////////////////////////////////////////////////
bool Font::hasGlyph(std::uint32_t codePoint) const
{
return FT_Get_Char_Index(m_fontHandles ? m_fontHandles->face : nullptr, codePoint) != 0;
assert(m_fontHandles);
return FT_Get_Char_Index(m_fontHandles->face, codePoint) != 0;
}
////////////////////////////////////////////////////////////
float Font::getKerning(std::uint32_t first, std::uint32_t second, unsigned int characterSize, bool bold) const
{
assert(m_fontHandles);
// Special case where first or second is 0 (null character)
if (first == 0 || second == 0)
return 0.f;
FT_Face face = m_fontHandles ? m_fontHandles->face : nullptr;
FT_Face face = m_fontHandles->face;
if (face && setCurrentSize(characterSize))
{
@ -375,9 +378,11 @@ float Font::getKerning(std::uint32_t first, std::uint32_t second, unsigned int c
////////////////////////////////////////////////////////////
float Font::getLineSpacing(unsigned int characterSize) const
{
FT_Face face = m_fontHandles ? m_fontHandles->face : nullptr;
assert(m_fontHandles);
if (face && setCurrentSize(characterSize))
FT_Face face = m_fontHandles->face;
if (setCurrentSize(characterSize))
{
return static_cast<float>(face->size->metrics.height) / static_cast<float>(1 << 6);
}
@ -391,9 +396,11 @@ float Font::getLineSpacing(unsigned int characterSize) const
////////////////////////////////////////////////////////////
float Font::getUnderlinePosition(unsigned int characterSize) const
{
FT_Face face = m_fontHandles ? m_fontHandles->face : nullptr;
assert(m_fontHandles);
if (face && setCurrentSize(characterSize))
FT_Face face = m_fontHandles->face;
if (setCurrentSize(characterSize))
{
// Return a fixed position if font is a bitmap font
if (!FT_IS_SCALABLE(face))
@ -412,7 +419,9 @@ float Font::getUnderlinePosition(unsigned int characterSize) const
////////////////////////////////////////////////////////////
float Font::getUnderlineThickness(unsigned int characterSize) const
{
FT_Face face = m_fontHandles ? m_fontHandles->face : nullptr;
assert(m_fontHandles);
FT_Face face = m_fontHandles->face;
if (face && setCurrentSize(characterSize))
{
@ -470,10 +479,6 @@ Glyph Font::loadGlyph(std::uint32_t codePoint, unsigned int characterSize, bool
// The glyph to return
Glyph glyph;
// Stop if no font is loaded
if (!m_fontHandles)
return glyph;
// Get our FT_Face
FT_Face face = m_fontHandles->face;
if (!face)

View File

@ -45,6 +45,7 @@
#include <utility>
#include <vector>
#include <cassert>
#include <cstdint>
#ifndef SFML_OPENGL_ES
@ -256,8 +257,8 @@ Shader& Shader::operator=(Shader&& right) noexcept
{
// Destroy effect program
const TransientContextLock lock;
if (m_shaderProgram)
glCheck(GLEXT_glDeleteObject(castToGlHandle(m_shaderProgram)));
assert(m_shaderProgram);
glCheck(GLEXT_glDeleteObject(castToGlHandle(m_shaderProgram)));
}
// Move the contents of right.
@ -579,33 +580,32 @@ void Shader::setUniform(const std::string& name, const Glsl::Mat4& matrix)
////////////////////////////////////////////////////////////
void Shader::setUniform(const std::string& name, const Texture& texture)
{
if (m_shaderProgram)
assert(m_shaderProgram);
const TransientContextLock lock;
// Find the location of the variable in the shader
const int location = getUniformLocation(name);
if (location != -1)
{
const TransientContextLock lock;
// Find the location of the variable in the shader
const int location = getUniformLocation(name);
if (location != -1)
// Store the location -> texture mapping
const auto it = m_textures.find(location);
if (it == m_textures.end())
{
// Store the location -> texture mapping
const auto it = m_textures.find(location);
if (it == m_textures.end())
// New entry, make sure there are enough texture units
if (m_textures.size() + 1 >= getMaxTextureUnits())
{
// New entry, make sure there are enough texture units
if (m_textures.size() + 1 >= getMaxTextureUnits())
{
err() << "Impossible to use texture " << std::quoted(name)
<< " for shader: all available texture units are used" << std::endl;
return;
}
err() << "Impossible to use texture " << std::quoted(name)
<< " for shader: all available texture units are used" << std::endl;
return;
}
m_textures[location] = &texture;
}
else
{
// Location already used, just replace the texture
it->second = &texture;
}
m_textures[location] = &texture;
}
else
{
// Location already used, just replace the texture
it->second = &texture;
}
}
}
@ -614,13 +614,12 @@ void Shader::setUniform(const std::string& name, const Texture& texture)
////////////////////////////////////////////////////////////
void Shader::setUniform(const std::string& name, CurrentTextureType)
{
if (m_shaderProgram)
{
const TransientContextLock lock;
assert(m_shaderProgram);
// Find the location of the variable in the shader
m_currentTexture = getUniformLocation(name);
}
const TransientContextLock lock;
// Find the location of the variable in the shader
m_currentTexture = getUniformLocation(name);
}