Files
Z3D/include/assets/data_stores/shader_source_store.hpp
2025-04-01 21:51:56 +02:00

154 lines
4.5 KiB
C++

#pragma once
#include <mutex>
#include <shared_mutex>
#include "assets/data/shader_source_data.hpp"
#include "assets/data_views/shader_source_view.hpp"
namespace assets
{
class shader_source_store;
template<typename Char>
class shader_source_store_iterator
{
public:
using size_type = std::size_t;
using length_type = ztu::u32;
using id_type = shader_source_id;
using metadata_type = shader_source_data::metadata;
using value_type = std::pair<id_type, shader_source_view>;
using id_iterator_type = id_type const*;
using string_iterator_type = Char*;
using length_iterator_type = const length_type*;
using metadata_iterator_type = const metadata_type*;
using offset_type = size_type;
using difference_type = std::ptrdiff_t;
using pointer = value_type*;
using reference = value_type;
using iterator_category = std::random_access_iterator_tag;
private:
friend shader_source_store;
shader_source_store_iterator(
id_iterator_type ids,
string_iterator_type strings,
length_iterator_type lengths,
metadata_iterator_type metadata,
std::size_t index,
const offset_type& offset
);
public:
constexpr shader_source_store_iterator() noexcept = default;
constexpr shader_source_store_iterator(const shader_source_store_iterator&) noexcept = default;
constexpr shader_source_store_iterator(shader_source_store_iterator&&) noexcept = default;
constexpr shader_source_store_iterator& operator=(const shader_source_store_iterator&) noexcept = default;
constexpr shader_source_store_iterator& operator=(shader_source_store_iterator&&) noexcept = default;
reference operator*() const;
shader_source_store_iterator& operator++();
shader_source_store_iterator operator++(int);
shader_source_store_iterator& operator--();
shader_source_store_iterator operator--(int);
shader_source_store_iterator& operator+=(difference_type n);
shader_source_store_iterator& operator-=(difference_type n);
shader_source_store_iterator operator+(difference_type n) const;
shader_source_store_iterator operator-(difference_type n) const;
difference_type operator-(const shader_source_store_iterator& other) const;
reference operator[](difference_type n) const;
bool operator==(const shader_source_store_iterator& other) const;
bool operator!=(const shader_source_store_iterator& other) const;
bool operator<(const shader_source_store_iterator& other) const;
bool operator<=(const shader_source_store_iterator& other) const;
bool operator>(const shader_source_store_iterator& other) const;
bool operator>=(const shader_source_store_iterator& other) const;
protected:
void calc_offset(difference_type n);
reference dereference() const;
private:
id_iterator_type m_ids{};
string_iterator_type m_strings{};
length_iterator_type m_lengths{};
metadata_iterator_type m_metadata{};
size_type m_index{};
offset_type m_offset{};
};
class shader_source_store
{
public:
using size_type = std::size_t;
using count_type = ztu::u32;
using iterator = shader_source_store_iterator<char>;
using const_iterator = shader_source_store_iterator<const char>;
using view_type = std::ranges::subrange<iterator>;
using const_view_type = std::ranges::subrange<const_iterator>;
using id_type = shader_source_id;
using data_type = shader_source_data;
bool insert(id_type id, const data_type& data);
bool insert(iterator it, id_type id, const data_type& data);
[[nodiscard]] std::pair<iterator, bool> find(id_type id);
[[nodiscard]] std::pair<const_iterator, bool> find(id_type id) const;
inline void remove(const iterator& it);
inline void clear();
[[nodiscard]] inline std::shared_lock<std::shared_mutex> acquire_read_lock() const;
[[nodiscard]] inline std::unique_lock<std::shared_mutex> acquire_write_lock();
[[nodiscard]] inline iterator begin();
[[nodiscard]] inline iterator end();
[[nodiscard]] inline const_iterator begin() const;
[[nodiscard]] inline const_iterator end() const;
[[nodiscard]] inline const_iterator cbegin() const;
[[nodiscard]] inline const_iterator cend() const;
protected:
bool unsafe_insert(iterator it, id_type id, const data_type& data);
[[nodiscard]] std::pair<iterator, bool> unsafe_find(id_type id);
[[nodiscard]] std::pair<const_iterator, bool> unsafe_find(id_type id) const;
private:
std::vector<id_type> m_ids;
std::vector<char> m_strings;
std::vector<count_type> m_lengths;
std::vector<shader_source_data::metadata> m_metadata;
mutable std::shared_mutex m_mutex;
};
}
#define INCLUDE_SHADER_SOURCE_STORE_IMPLEMENTATION
#include "assets/data_stores/shader_source_store.ipp"
#undef INCLUDE_SHADER_SOURCE_STORE_IMPLEMENTATION