Antkeeper  0.0.1
physfs-serialize-context.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef ANTKEEPER_RESOURCES_PHYSFS_SERIALIZE_CONTEXT_HPP
21 #define ANTKEEPER_RESOURCES_PHYSFS_SERIALIZE_CONTEXT_HPP
22 
24 #include <physfs.h>
25 #include <filesystem>
26 
31 {
32 public:
40  explicit physfs_serialize_context(const std::filesystem::path& path) noexcept(false);
41 
45  physfs_serialize_context() noexcept = default;
46 
50  virtual ~physfs_serialize_context();
51 
59  void open(const std::filesystem::path& path) noexcept(false);
60 
64  void close() noexcept;
65 
69  [[nodiscard]] bool is_open() const noexcept;
70 
71  [[nodiscard]] const std::filesystem::path& path() const noexcept override;
72  [[nodiscard]] bool error() const noexcept override;
73  std::size_t write8(const std::byte* data, std::size_t count) noexcept(false) override;
74  std::size_t write16_le(const std::byte* data, std::size_t count) noexcept(false) override;
75  std::size_t write16_be(const std::byte* data, std::size_t count) noexcept(false) override;
76  std::size_t write32_le(const std::byte* data, std::size_t count) noexcept(false) override;
77  std::size_t write32_be(const std::byte* data, std::size_t count) noexcept(false) override;
78  std::size_t write64_le(const std::byte* data, std::size_t count) noexcept(false) override;
79  std::size_t write64_be(const std::byte* data, std::size_t count) noexcept(false) override;
80 
81 private:
82  PHYSFS_File* file{nullptr};
83  std::filesystem::path m_path;
84  bool m_error{false};
85 };
86 
87 #endif // ANTKEEPER_RESOURCES_PHYSFS_SERIALIZE_CONTEXT_HPP
Provides access to a serialization state.
physfs_serialize_context() noexcept=default
Constructs a PhysicsFS serialize context.
std::size_t write8(const std::byte *data, std::size_t count) noexcept(false) override
Writes 8-bit (byte) data.
std::size_t write16_be(const std::byte *data, std::size_t count) noexcept(false) override
Writes 16-bit (word) big-endian data.
std::size_t write32_le(const std::byte *data, std::size_t count) noexcept(false) override
Writes 32-bit (double word) little-endian data.
void close() noexcept
Closes the associated file using PhysicsFS.
std::size_t write64_be(const std::byte *data, std::size_t count) noexcept(false) override
Writes 64-bit (quad word) big-endian data.
std::size_t write64_le(const std::byte *data, std::size_t count) noexcept(false) override
Writes 64-bit (quad word) little-endian data.
bool error() const noexcept override
Returns true if an error occured during a write operation or initialization, false otherwise.
std::size_t write16_le(const std::byte *data, std::size_t count) noexcept(false) override
Writes 16-bit (word) little-endian data.
void open(const std::filesystem::path &path) noexcept(false)
Opens a file using PhysicsFS and associates it with the serialize context.
bool is_open() const noexcept
Returns true if the PhysicsFS file associated with this serialize context is open,...
const std::filesystem::path & path() const noexcept override
Returns the path associated with this serialize context.
std::size_t write32_be(const std::byte *data, std::size_t count) noexcept(false) override
Writes 32-bit (double word) big-endian data.
constexpr int count(T x) noexcept
Returns the number of set bits in a value, known as a population count or Hamming weight.
Definition: bit-math.hpp:211
Provides access to a serialization state.