Skip to content

Commit

Permalink
Use std::span
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisThrasher committed Feb 8, 2025
1 parent 3cebd72 commit 3b8f774
Show file tree
Hide file tree
Showing 77 changed files with 650 additions and 715 deletions.
8 changes: 4 additions & 4 deletions examples/sockets/TCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ void runTcpServer(unsigned short port)

// Send a message to the connected client
static constexpr std::string_view out = "Hi, I'm the server";
if (socket.send(out.data(), out.size()) != sf::Socket::Status::Done)
if (socket.send(std::as_bytes(std::span(out))) != sf::Socket::Status::Done)
return;
std::cout << "Message sent to the client: " << std::quoted(out.data()) << std::endl;

// Receive a message back from the client
std::array<char, 128> in{};
std::size_t received = 0;
if (socket.receive(in.data(), in.size(), received) != sf::Socket::Status::Done)
if (socket.receive(std::as_writable_bytes(std::span(in)), received) != sf::Socket::Status::Done)
return;
std::cout << "Answer received from the client: " << std::quoted(in.data()) << std::endl;
}
Expand Down Expand Up @@ -75,13 +75,13 @@ void runTcpClient(unsigned short port)
// Receive a message from the server
std::array<char, 128> in{};
std::size_t received = 0;
if (socket.receive(in.data(), in.size(), received) != sf::Socket::Status::Done)
if (socket.receive(std::as_writable_bytes(std::span(in)), received) != sf::Socket::Status::Done)
return;
std::cout << "Message received from the server: " << std::quoted(in.data()) << std::endl;

// Send an answer to the server
static constexpr std::string_view out = "Hi, I'm a client";
if (socket.send(out.data(), out.size()) != sf::Socket::Status::Done)
if (socket.send(std::as_bytes(std::span(out))) != sf::Socket::Status::Done)
return;
std::cout << "Message sent to the server: " << std::quoted(out.data()) << std::endl;
}
8 changes: 4 additions & 4 deletions examples/sockets/UDP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ void runUdpServer(unsigned short port)
std::size_t received = 0;
std::optional<sf::IpAddress> sender;
unsigned short senderPort = 0;
if (socket.receive(in.data(), in.size(), received, sender, senderPort) != sf::Socket::Status::Done)
if (socket.receive(std::as_writable_bytes(std::span(in)), received, sender, senderPort) != sf::Socket::Status::Done)
return;
std::cout << "Message received from client " << sender.value() << ": " << std::quoted(in.data()) << std::endl;

// Send an answer to the client
static constexpr std::string_view out = "Hi, I'm the server";
if (socket.send(out.data(), out.size(), sender.value(), senderPort) != sf::Socket::Status::Done)
if (socket.send(std::as_bytes(std::span(out)), sender.value(), senderPort) != sf::Socket::Status::Done)
return;
std::cout << "Message sent to the client: " << std::quoted(out.data()) << std::endl;
}
Expand All @@ -63,7 +63,7 @@ void runUdpClient(unsigned short port)

// Send a message to the server
static constexpr std::string_view out = "Hi, I'm a client";
if (socket.send(out.data(), out.size(), server.value(), port) != sf::Socket::Status::Done)
if (socket.send(std::as_bytes(std::span(out)), server.value(), port) != sf::Socket::Status::Done)
return;
std::cout << "Message sent to the server: " << std::quoted(out.data()) << std::endl;

Expand All @@ -72,7 +72,7 @@ void runUdpClient(unsigned short port)
std::size_t received = 0;
std::optional<sf::IpAddress> sender;
unsigned short senderPort = 0;
if (socket.receive(in.data(), in.size(), received, sender, senderPort) != sf::Socket::Status::Done)
if (socket.receive(std::as_writable_bytes(std::span(in)), received, sender, senderPort) != sf::Socket::Status::Done)
return;
std::cout << "Message received from " << sender.value() << ": " << std::quoted(in.data()) << std::endl;
}
4 changes: 2 additions & 2 deletions examples/sound_effects/SoundEffects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ class Tone : public sf::SoundStream, public Effect
m_currentAmplitude.setPosition({windowWidth / 2.f - 150.f, windowHeight / 2.f - 50.f});
m_currentFrequency.setPosition({windowWidth / 2.f - 150.f, windowHeight / 2.f});

sf::SoundStream::initialize(1, sampleRate, {sf::SoundChannel::Mono});
sf::SoundStream::initialize(1, sampleRate, std::array{sf::SoundChannel::Mono});
}

void onUpdate(float /*time*/, float x, float y) override
Expand Down Expand Up @@ -543,7 +543,7 @@ class Doppler : public sf::SoundStream, public Effect
// Set attenuation to a nice value
setAttenuation(0.05f);

sf::SoundStream::initialize(1, sampleRate, {sf::SoundChannel::Mono});
sf::SoundStream::initialize(1, sampleRate, std::array{sf::SoundChannel::Mono});
}

void onUpdate(float time, float x, float y) override
Expand Down
2 changes: 1 addition & 1 deletion examples/voip/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class NetworkRecorder : public sf::SoundRecorder
// Pack the audio samples into a network packet
sf::Packet packet;
packet << clientAudioData;
packet.append(samples, sampleCount * sizeof(std::int16_t));
packet.append(std::as_bytes(std::span(samples, sampleCount)));

// Send the audio packet to the server
return m_socket.send(packet) == sf::Socket::Status::Done;
Expand Down
6 changes: 3 additions & 3 deletions examples/voip/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class NetworkAudioStream : public sf::SoundStream
NetworkAudioStream()
{
// Set the sound parameters
initialize(1, 44100, {sf::SoundChannel::Mono});
initialize(1, 44100, std::array{sf::SoundChannel::Mono});
}

////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -130,13 +130,13 @@ class NetworkAudioStream : public sf::SoundStream
if (id == serverAudioData)
{
// Extract audio samples from the packet, and append it to our samples buffer
const std::size_t sampleCount = (packet.getDataSize() - 1) / sizeof(std::int16_t);
const std::size_t sampleCount = (packet.getData().size() - 1) / sizeof(std::int16_t);

// Don't forget that the other thread can access the sample array at any time
// (so we protect any operation on it with the mutex)
{
const std::lock_guard lock(m_mutex);
const auto* begin = static_cast<const char*>(packet.getData()) + 1;
const auto* begin = reinterpret_cast<const char*>(packet.getData().data()) + 1;
const auto* end = begin + sampleCount * sizeof(std::int16_t);
m_samples.insert(m_samples.end(), begin, end);
}
Expand Down
8 changes: 4 additions & 4 deletions examples/vulkan/Vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ class VulkanExample
const auto fileSize = file.getSize().value();
std::vector<std::uint32_t> buffer(fileSize / sizeof(std::uint32_t));

if (file.read(buffer.data(), fileSize) != file.getSize())
if (file.read(std::as_writable_bytes(std::span(buffer))) != file.getSize())
{
vulkanAvailable = false;
return;
Expand Down Expand Up @@ -945,7 +945,7 @@ class VulkanExample
const auto fileSize = file.getSize().value();
std::vector<std::uint32_t> buffer(fileSize / sizeof(std::uint32_t));

if (file.read(buffer.data(), fileSize) != file.getSize())
if (file.read(std::as_writable_bytes(std::span(buffer))) != file.getSize())
{
vulkanAvailable = false;
return;
Expand Down Expand Up @@ -1794,7 +1794,7 @@ class VulkanExample
}

// Create a staging buffer to transfer the data with
const VkDeviceSize imageSize = imageData.getSize().x * imageData.getSize().y * 4;
const VkDeviceSize imageSize = imageData.getPixels().size();

VkBuffer stagingBuffer = {};
VkDeviceMemory stagingBufferMemory = {};
Expand All @@ -1817,7 +1817,7 @@ class VulkanExample
}

// Copy the image data into the buffer
std::memcpy(ptr, imageData.getPixelsPtr(), static_cast<std::size_t>(imageSize));
std::memcpy(ptr, imageData.getPixels().data(), imageData.getPixels().size());

// Unmap the buffer
vkUnmapMemory(device, stagingBufferMemory);
Expand Down
11 changes: 5 additions & 6 deletions include/SFML/Audio/InputSoundFile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

#include <filesystem>
#include <memory>
#include <span>
#include <vector>

#include <cstddef>
Expand Down Expand Up @@ -84,13 +85,12 @@ class SFML_AUDIO_API InputSoundFile
/// The supported audio formats are: WAV (PCM only), OGG/Vorbis, FLAC.
/// The supported sample sizes for FLAC and WAV are 8, 16, 24 and 32 bit.
///
/// \param data Pointer to the file data in memory
/// \param sizeInBytes Size of the data to load, in bytes
/// \param buffer File data in memory
///
/// \throws sf::Exception if opening the file was unsuccessful
///
////////////////////////////////////////////////////////////
InputSoundFile(const void* data, std::size_t sizeInBytes);
InputSoundFile(std::span<const std::byte> buffer);

////////////////////////////////////////////////////////////
/// \brief Construct a sound file from a custom stream for reading
Expand Down Expand Up @@ -129,13 +129,12 @@ class SFML_AUDIO_API InputSoundFile
/// The supported audio formats are: WAV (PCM only), OGG/Vorbis, FLAC.
/// The supported sample sizes for FLAC and WAV are 8, 16, 24 and 32 bit.
///
/// \param data Pointer to the file data in memory
/// \param sizeInBytes Size of the data to load, in bytes
/// \param buffer File data in memory
///
/// \return `true` if the file was successfully opened
///
////////////////////////////////////////////////////////////
[[nodiscard]] bool openFromMemory(const void* data, std::size_t sizeInBytes);
[[nodiscard]] bool openFromMemory(std::span<const std::byte> buffer);

////////////////////////////////////////////////////////////
/// \brief Open a sound file from a custom stream for reading
Expand Down
11 changes: 5 additions & 6 deletions include/SFML/Audio/Music.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <filesystem>
#include <memory>
#include <optional>
#include <span>

#include <cstddef>
#include <cstdint>
Expand Down Expand Up @@ -108,15 +109,14 @@ class SFML_AUDIO_API Music : public SoundStream
/// the `sf::Music` object loads a new music or is destroyed. That is,
/// you can't deallocate the buffer right after calling this function.
///
/// \param data Pointer to the file data in memory
/// \param sizeInBytes Size of the data to load, in bytes
/// \param buffer File data in memory
///
/// \throws sf::Exception if loading was unsuccessful
///
/// \see `openFromFile`, `openFromStream`
///
////////////////////////////////////////////////////////////
Music(const void* data, std::size_t sizeInBytes);
Music(std::span<const std::byte> buffer);

////////////////////////////////////////////////////////////
/// \brief Construct a music from an audio file in a custom stream
Expand Down Expand Up @@ -191,15 +191,14 @@ class SFML_AUDIO_API Music : public SoundStream
/// the `sf::Music` object loads a new music or is destroyed. That is,
/// you can't deallocate the buffer right after calling this function.
///
/// \param data Pointer to the file data in memory
/// \param sizeInBytes Size of the data to load, in bytes
/// \param buffer File data in memory
///
/// \return `true` if loading succeeded, `false` if it failed
///
/// \see `openFromFile`, `openFromStream`
///
////////////////////////////////////////////////////////////
[[nodiscard]] bool openFromMemory(const void* data, std::size_t sizeInBytes);
[[nodiscard]] bool openFromMemory(std::span<const std::byte> buffer);

////////////////////////////////////////////////////////////
/// \brief Open a music from an audio file in a custom stream
Expand Down
18 changes: 9 additions & 9 deletions include/SFML/Audio/OutputSoundFile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

#include <filesystem>
#include <memory>
#include <vector>
#include <span>

#include <cstdint>

Expand Down Expand Up @@ -70,10 +70,10 @@ class SFML_AUDIO_API OutputSoundFile
/// \throws sf::Exception if the file could not be opened successfully
///
////////////////////////////////////////////////////////////
OutputSoundFile(const std::filesystem::path& filename,
unsigned int sampleRate,
unsigned int channelCount,
const std::vector<SoundChannel>& channelMap);
OutputSoundFile(const std::filesystem::path& filename,
unsigned int sampleRate,
unsigned int channelCount,
std::span<const SoundChannel> channelMap);

////////////////////////////////////////////////////////////
/// \brief Open the sound file from the disk for writing
Expand All @@ -88,10 +88,10 @@ class SFML_AUDIO_API OutputSoundFile
/// \return `true` if the file was successfully opened
///
////////////////////////////////////////////////////////////
[[nodiscard]] bool openFromFile(const std::filesystem::path& filename,
unsigned int sampleRate,
unsigned int channelCount,
const std::vector<SoundChannel>& channelMap);
[[nodiscard]] bool openFromFile(const std::filesystem::path& filename,
unsigned int sampleRate,
unsigned int channelCount,
std::span<const SoundChannel> channelMap);

////////////////////////////////////////////////////////////
/// \brief Write audio samples to the file
Expand Down
54 changes: 18 additions & 36 deletions include/SFML/Audio/SoundBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <SFML/System/Time.hpp>

#include <filesystem>
#include <span>
#include <unordered_set>
#include <vector>

Expand Down Expand Up @@ -92,15 +93,14 @@ class SFML_AUDIO_API SoundBuffer
/// See the documentation of `sf::InputSoundFile` for the list
/// of supported formats.
///
/// \param data Pointer to the file data in memory
/// \param sizeInBytes Size of the data to load, in bytes
/// \param buffer File data in memory
///
/// \throws sf::Exception if loading was unsuccessful
///
/// \see `loadFromFile`, `loadFromStream`, `loadFromSamples`
///
////////////////////////////////////////////////////////////
SoundBuffer(const void* data, std::size_t sizeInBytes);
SoundBuffer(std::span<const std::byte> buffer);

////////////////////////////////////////////////////////////
/// \brief Construct the sound buffer from a custom stream
Expand Down Expand Up @@ -133,11 +133,11 @@ class SFML_AUDIO_API SoundBuffer
/// \see `loadFromFile`, `loadFromMemory`, `saveToFile`
///
////////////////////////////////////////////////////////////
SoundBuffer(const std::int16_t* samples,
std::uint64_t sampleCount,
unsigned int channelCount,
unsigned int sampleRate,
const std::vector<SoundChannel>& channelMap);
SoundBuffer(const std::int16_t* samples,
std::uint64_t sampleCount,
unsigned int channelCount,
unsigned int sampleRate,
std::span<const SoundChannel> channelMap);

////////////////////////////////////////////////////////////
/// \brief Destructor
Expand Down Expand Up @@ -166,15 +166,14 @@ class SFML_AUDIO_API SoundBuffer
/// See the documentation of `sf::InputSoundFile` for the list
/// of supported formats.
///
/// \param data Pointer to the file data in memory
/// \param sizeInBytes Size of the data to load, in bytes
/// \param buffer File data in memory
///
/// \return `true` if loading succeeded, `false` if it failed
///
/// \see `loadFromFile`, `loadFromStream`, `loadFromSamples`
///
////////////////////////////////////////////////////////////
[[nodiscard]] bool loadFromMemory(const void* data, std::size_t sizeInBytes);
[[nodiscard]] bool loadFromMemory(std::span<const std::byte> buffer);

////////////////////////////////////////////////////////////
/// \brief Load the sound buffer from a custom stream
Expand Down Expand Up @@ -207,11 +206,11 @@ class SFML_AUDIO_API SoundBuffer
/// \see `loadFromFile`, `loadFromMemory`, `saveToFile`
///
////////////////////////////////////////////////////////////
[[nodiscard]] bool loadFromSamples(const std::int16_t* samples,
std::uint64_t sampleCount,
unsigned int channelCount,
unsigned int sampleRate,
const std::vector<SoundChannel>& channelMap);
[[nodiscard]] bool loadFromSamples(const std::int16_t* samples,
std::uint64_t sampleCount,
unsigned int channelCount,
unsigned int sampleRate,
std::span<const SoundChannel> channelMap);

////////////////////////////////////////////////////////////
/// \brief Save the sound buffer to an audio file
Expand All @@ -230,28 +229,11 @@ class SFML_AUDIO_API SoundBuffer
/// \brief Get the array of audio samples stored in the buffer
///
/// The format of the returned samples is 16 bit signed integer.
/// The total number of samples in this array is given by the
/// `getSampleCount()` function.
///
/// \return Read-only pointer to the array of sound samples
///
/// \see `getSampleCount`
///
////////////////////////////////////////////////////////////
[[nodiscard]] const std::int16_t* getSamples() const;

////////////////////////////////////////////////////////////
/// \brief Get the number of samples stored in the buffer
///
/// The array of samples can be accessed with the `getSamples()`
/// function.
///
/// \return Number of samples
///
/// \see `getSamples`
/// \return Read-only view to the array of sound samples
///
////////////////////////////////////////////////////////////
[[nodiscard]] std::uint64_t getSampleCount() const;
[[nodiscard]] std::span<const std::int16_t> getSamples() const;

////////////////////////////////////////////////////////////
/// \brief Get the sample rate of the sound
Expand Down Expand Up @@ -336,7 +318,7 @@ class SFML_AUDIO_API SoundBuffer
/// \return `true` on success, `false` if any error happened
///
////////////////////////////////////////////////////////////
[[nodiscard]] bool update(unsigned int channelCount, unsigned int sampleRate, const std::vector<SoundChannel>& channelMap);
[[nodiscard]] bool update(unsigned int channelCount, unsigned int sampleRate, std::span<const SoundChannel> channelMap);

////////////////////////////////////////////////////////////
/// \brief Add a sound to the list of sounds that use this buffer
Expand Down
Loading

0 comments on commit 3b8f774

Please sign in to comment.