Files
Z3D/include/assets/data_stores/shader_source_store.hpp
2025-03-30 22:38:06 +02:00

143 lines
4.1 KiB
C++

#pragma once
#include "generic/generic_basic_store.hpp"
#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_type = shader_source_store_iterator<char>;
using const_iterator = shader_source_store_iterator<const char>;
using view_type = std::ranges::subrange<iterator_type>;
using const_view_type = std::ranges::subrange<const_iterator>;
using id_type = shader_source_id;
// TODO not storing metadata
inline id_type add(
shader_source_id id,
const shader_source_data& shader_source
);
[[nodiscard]] inline std::pair<iterator_type, bool> find(id_type id);
[[nodiscard]] inline std::pair<const_iterator, bool> find(id_type id) const;
inline void remove(const iterator_type& it);
inline void clear();
[[nodiscard]] inline iterator_type begin();
[[nodiscard]] inline iterator_type 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;
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;
};
}
#define INCLUDE_SHADER_SOURCE_STORE_IMPLEMENTATION
#include "assets/data_stores/shader_source_store.ipp"
#undef INCLUDE_SHADER_SOURCE_STORE_IMPLEMENTATION