Generalise BlockIterator and rename to PartIterator.

I'm going to need a FrameIterator class too so let's see if
I can reuse BlockIterator.
This commit is contained in:
King_DuckZ 2020-03-20 15:33:16 +01:00
parent 1bca8f0201
commit 4eaa2fc734
5 changed files with 142 additions and 148 deletions

View file

@ -1,49 +1,12 @@
#pragma once
#include "memcard/part_iterator.hpp"
#include "memcard/block.hpp"
#include <type_traits>
#include <iterator>
//#include "memcard/memorycard.hpp"
namespace mc::psx {
class MemoryCard;
template <bool Const>
class BlockIterator {
friend class BlockIterator<not Const>;
typedef typename std::conditional<Const, const MemoryCard, MemoryCard>::type* MemoryCardPtr;
public:
typedef std::random_access_iterator_tag iterator_category;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef BasicBlock<Const> value_type;
typedef BasicBlock<Const> reference;
typedef BasicBlock<Const> pointer;
BlockIterator (MemoryCardPtr mc, size_type index);
BlockIterator (const BlockIterator&) = default;
BlockIterator (BlockIterator&&) = default;
BlockIterator();
template <bool Const2>
bool operator== (const BlockIterator<Const2>& other) const;
template <bool Const2>
bool operator!= (const BlockIterator<Const2>& other) const;
template <bool Const2>
bool operator< (const BlockIterator<Const2>& other) const;
reference operator*();
BlockIterator& operator++();
BlockIterator operator++(int);
BlockIterator& operator--();
BlockIterator operator--(int);
template <bool Const2>
difference_type operator- (const BlockIterator<Const2>& other);
BlockIterator operator+ (std::ptrdiff_t other) const;
BlockIterator operator- (std::ptrdiff_t other) const;
private:
MemoryCardPtr m_mc;
size_type m_index;
};
using BlockIterator = PartIterator<Const, MemoryCard, BasicBlock>;
} //namespace mc::psx

View file

@ -0,0 +1,134 @@
#pragma once
#include <type_traits>
#include <iterator>
#include <cassert>
namespace mc::psx {
template <bool Const, typename ProviderObj, template <bool> class WrappedType>
class PartIterator {
friend class PartIterator<not Const, ProviderObj, WrappedType>;
typedef typename std::conditional<Const, const ProviderObj, ProviderObj>::type* ProviderObjPtr;
public:
typedef std::random_access_iterator_tag iterator_category;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef WrappedType<Const> value_type;
typedef WrappedType<Const> reference;
typedef WrappedType<Const> pointer;
PartIterator (ProviderObjPtr mc, size_type index);
PartIterator (const PartIterator&) = default;
PartIterator (PartIterator&&) = default;
PartIterator();
template <bool Const2>
bool operator== (const PartIterator<Const2, ProviderObj, WrappedType>& other) const;
template <bool Const2>
bool operator!= (const PartIterator<Const2, ProviderObj, WrappedType>& other) const;
template <bool Const2>
bool operator< (const PartIterator<Const2, ProviderObj, WrappedType>& other) const;
reference operator*();
PartIterator& operator++();
PartIterator operator++(int);
PartIterator& operator--();
PartIterator operator--(int);
template <bool Const2>
difference_type operator- (const PartIterator<Const2, ProviderObj, WrappedType>& other);
PartIterator operator+ (std::ptrdiff_t other) const;
PartIterator operator- (std::ptrdiff_t other) const;
private:
ProviderObjPtr m_provider_obj;
size_type m_index;
};
template <bool Const, typename ProviderObj, template <bool> class WrappedType>
inline PartIterator<Const, ProviderObj, WrappedType>::PartIterator() :
m_provider_obj(nullptr),
m_index(0)
{
}
template <bool Const, typename ProviderObj, template <bool> class WrappedType>
inline PartIterator<Const, ProviderObj, WrappedType>::PartIterator (ProviderObjPtr pobj, size_type index) :
m_provider_obj(pobj),
m_index(index)
{
}
template <bool Const, typename ProviderObj, template <bool> class WrappedType>
template <bool Const2>
inline bool PartIterator<Const, ProviderObj, WrappedType>::operator== (const PartIterator<Const2, ProviderObj, WrappedType>& other) const {
assert(other.m_provider_obj == this->m_provider_obj);
return this->m_index == other.m_index;
}
template <bool Const, typename ProviderObj, template <bool> class WrappedType>
template <bool Const2>
inline bool PartIterator<Const, ProviderObj, WrappedType>::operator!= (const PartIterator<Const2, ProviderObj, WrappedType>& other) const {
assert(other.m_provider_obj == this->m_provider_obj);
return this->m_index != other.m_index;
}
template <bool Const, typename ProviderObj, template <bool> class WrappedType>
template <bool Const2>
inline bool PartIterator<Const, ProviderObj, WrappedType>::operator< (const PartIterator<Const2, ProviderObj, WrappedType>& other) const {
assert(other.m_provider_obj == this->m_provider_obj);
return this->m_index < other.m_index;
}
template <bool Const, typename ProviderObj, template <bool> class WrappedType>
inline auto PartIterator<Const, ProviderObj, WrappedType>::operator*() -> reference {
assert(m_provider_obj);
return (*m_provider_obj)[m_index];
}
template <bool Const, typename ProviderObj, template <bool> class WrappedType>
inline PartIterator<Const, ProviderObj, WrappedType>& PartIterator<Const, ProviderObj, WrappedType>::operator++() {
++m_index;
return *this;
}
template <bool Const, typename ProviderObj, template <bool> class WrappedType>
inline PartIterator<Const, ProviderObj, WrappedType> PartIterator<Const, ProviderObj, WrappedType>::operator++(int) {
auto cpy = *this;
++(*this);
return cpy;
}
template <bool Const, typename ProviderObj, template <bool> class WrappedType>
inline PartIterator<Const, ProviderObj, WrappedType>& PartIterator<Const, ProviderObj, WrappedType>::operator--() {
--m_index;
return *this;
}
template <bool Const, typename ProviderObj, template <bool> class WrappedType>
inline PartIterator<Const, ProviderObj, WrappedType> PartIterator<Const, ProviderObj, WrappedType>::operator--(int) {
auto cpy = *this;
--(*this);
return cpy;
}
template <bool Const, typename ProviderObj, template <bool> class WrappedType>
template <bool Const2>
inline auto PartIterator<Const, ProviderObj, WrappedType>::operator- (const PartIterator<Const2, ProviderObj, WrappedType>& other) -> difference_type {
return this->m_index - other.m_index;
}
template <bool Const, typename ProviderObj, template <bool> class WrappedType>
inline PartIterator<Const, ProviderObj, WrappedType> PartIterator<Const, ProviderObj, WrappedType>::operator+ (std::ptrdiff_t other) const {
auto retval = *this;
retval.m_index += other;
return retval;
}
template <bool Const, typename ProviderObj, template <bool> class WrappedType>
inline PartIterator<Const, ProviderObj, WrappedType> PartIterator<Const, ProviderObj, WrappedType>::operator- (std::ptrdiff_t other) const {
auto retval = *this;
retval.m_index -= other;
return retval;
}
} //namespace mc::psx

View file

@ -13,7 +13,7 @@ memcard = shared_library('memcard',
'src/shiftjis_to_utf8.cpp',
'src/content_info.cpp',
'src/make_memory_card.cpp',
'src/block_iterator.cpp',
'src/part_iterator.cpp',
install: true,
include_directories: [private_incl, library_incl],
)

View file

@ -1,107 +0,0 @@
#include "memcard/block_iterator.hpp"
#include "memcard/memorycard.hpp"
#include <cassert>
namespace mc::psx {
template <bool Const>
BlockIterator<Const>::BlockIterator() :
m_mc(nullptr),
m_index(0)
{
}
template <bool Const>
BlockIterator<Const>::BlockIterator (MemoryCardPtr mc, size_type index) :
m_mc(mc),
m_index(index)
{
}
template <bool Const>
template <bool Const2>
bool BlockIterator<Const>::operator== (const BlockIterator<Const2>& other) const {
assert(other.m_mc == this->m_mc);
return this->m_index == other.m_index;
}
template <bool Const>
template <bool Const2>
bool BlockIterator<Const>::operator!= (const BlockIterator<Const2>& other) const {
assert(other.m_mc == this->m_mc);
return this->m_index != other.m_index;
}
template <bool Const>
template <bool Const2>
bool BlockIterator<Const>::operator< (const BlockIterator<Const2>& other) const {
assert(other.m_mc == this->m_mc);
return this->m_index < other.m_index;
}
template <bool Const>
auto BlockIterator<Const>::operator*() -> reference {
assert(m_mc);
return (*m_mc)[m_index];
}
template <bool Const>
BlockIterator<Const>& BlockIterator<Const>::operator++() {
++m_index;
return *this;
}
template <bool Const>
BlockIterator<Const> BlockIterator<Const>::operator++(int) {
auto cpy = *this;
++(*this);
return cpy;
}
template <bool Const>
BlockIterator<Const>& BlockIterator<Const>::operator--() {
--m_index;
return *this;
}
template <bool Const>
BlockIterator<Const> BlockIterator<Const>::operator--(int) {
auto cpy = *this;
--(*this);
return cpy;
}
template <bool Const>
template <bool Const2>
auto BlockIterator<Const>::operator- (const BlockIterator<Const2>& other) -> difference_type {
return this->m_index - other.m_index;
}
template <bool Const>
BlockIterator<Const> BlockIterator<Const>::operator+ (std::ptrdiff_t other) const {
auto retval = *this;
retval.m_index += other;
return retval;
}
template <bool Const>
BlockIterator<Const> BlockIterator<Const>::operator- (std::ptrdiff_t other) const {
auto retval = *this;
retval.m_index -= other;
return retval;
}
template class BlockIterator<true>;
template class BlockIterator<false>;
template bool BlockIterator<true>::operator==(const BlockIterator<true>&) const;
template bool BlockIterator<true>::operator==(const BlockIterator<false>&) const;
template bool BlockIterator<false>::operator==(const BlockIterator<true>&) const;
template bool BlockIterator<false>::operator==(const BlockIterator<false>&) const;
template bool BlockIterator<true>::operator!=(const BlockIterator<true>&) const;
template bool BlockIterator<true>::operator!=(const BlockIterator<false>&) const;
template bool BlockIterator<false>::operator!=(const BlockIterator<true>&) const;
template bool BlockIterator<false>::operator!=(const BlockIterator<false>&) const;
template bool BlockIterator<true>::operator<(const BlockIterator<true>&) const;
template bool BlockIterator<true>::operator<(const BlockIterator<false>&) const;
template bool BlockIterator<false>::operator<(const BlockIterator<true>&) const;
template bool BlockIterator<false>::operator<(const BlockIterator<false>&) const;
} //namespace mc::psx

View file

@ -0,0 +1,4 @@
#include "memcard/part_iterator.hpp"
namespace mc::psx {
} //namespace mc::psx